Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / phame / controller / blog / PhameBlogProfilePictureController.php
blob4e69034253c2255168f81581d8570f8be3a6496b
1 <?php
3 final class PhameBlogProfilePictureController
4 extends PhameBlogController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
8 $id = $request->getURIData('id');
10 $blog = id(new PhameBlogQuery())
11 ->setViewer($viewer)
12 ->withIDs(array($id))
13 ->needProfileImage(true)
14 ->requireCapabilities(
15 array(
16 PhabricatorPolicyCapability::CAN_VIEW,
17 PhabricatorPolicyCapability::CAN_EDIT,
19 ->executeOne();
20 if (!$blog) {
21 return new Aphront404Response();
24 $blog_uri = '/phame/blog/manage/'.$id;
26 $supported_formats = PhabricatorFile::getTransformableImageFormats();
27 $e_file = true;
28 $errors = array();
30 if ($request->isFormPost()) {
31 $phid = $request->getStr('phid');
32 $is_default = false;
33 if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
34 $phid = null;
35 $is_default = true;
36 } else if ($phid) {
37 $file = id(new PhabricatorFileQuery())
38 ->setViewer($viewer)
39 ->withPHIDs(array($phid))
40 ->executeOne();
41 } else {
42 if ($request->getFileExists('picture')) {
43 $file = PhabricatorFile::newFromPHPUpload(
44 $_FILES['picture'],
45 array(
46 'authorPHID' => $viewer->getPHID(),
47 'canCDN' => true,
48 ));
49 } else {
50 $e_file = pht('Required');
51 $errors[] = pht(
52 'You must choose a file when uploading a new blog picture.');
56 if (!$errors && !$is_default) {
57 if (!$file->isTransformableImage()) {
58 $e_file = pht('Not Supported');
59 $errors[] = pht(
60 'This server only supports these image formats: %s.',
61 implode(', ', $supported_formats));
62 } else {
63 $xform = PhabricatorFileTransform::getTransformByKey(
64 PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE);
65 $xformed = $xform->executeTransform($file);
69 if (!$errors) {
70 if ($is_default) {
71 $new_value = null;
72 } else {
73 $xformed->attachToObject($blog->getPHID());
74 $new_value = $xformed->getPHID();
77 $xactions = array();
78 $xactions[] = id(new PhameBlogTransaction())
79 ->setTransactionType(
80 PhameBlogProfileImageTransaction::TRANSACTIONTYPE)
81 ->setNewValue($new_value);
83 $editor = id(new PhameBlogEditor())
84 ->setActor($viewer)
85 ->setContentSourceFromRequest($request)
86 ->setContinueOnMissingFields(true)
87 ->setContinueOnNoEffect(true);
89 $editor->applyTransactions($blog, $xactions);
91 return id(new AphrontRedirectResponse())->setURI($blog_uri);
95 $title = pht('Edit Blog Picture');
97 $form = id(new PHUIFormLayoutView())
98 ->setUser($viewer);
100 $default_image = PhabricatorFile::loadBuiltin($viewer, 'blog.png');
102 $images = array();
104 $current = $blog->getProfileImagePHID();
105 $has_current = false;
106 if ($current) {
107 $files = id(new PhabricatorFileQuery())
108 ->setViewer($viewer)
109 ->withPHIDs(array($current))
110 ->execute();
111 if ($files) {
112 $file = head($files);
113 if ($file->isTransformableImage()) {
114 $has_current = true;
115 $images[$current] = array(
116 'uri' => $file->getBestURI(),
117 'tip' => pht('Current Picture'),
123 $images[PhabricatorPHIDConstants::PHID_VOID] = array(
124 'uri' => $default_image->getBestURI(),
125 'tip' => pht('Default Picture'),
128 require_celerity_resource('people-profile-css');
129 Javelin::initBehavior('phabricator-tooltips', array());
131 $buttons = array();
132 foreach ($images as $phid => $spec) {
133 $button = javelin_tag(
134 'button',
135 array(
136 'class' => 'button-grey profile-image-button',
137 'sigil' => 'has-tooltip',
138 'meta' => array(
139 'tip' => $spec['tip'],
140 'size' => 300,
143 phutil_tag(
144 'img',
145 array(
146 'height' => 50,
147 'width' => 50,
148 'src' => $spec['uri'],
149 )));
151 $button = array(
152 phutil_tag(
153 'input',
154 array(
155 'type' => 'hidden',
156 'name' => 'phid',
157 'value' => $phid,
159 $button,
162 $button = phabricator_form(
163 $viewer,
164 array(
165 'class' => 'profile-image-form',
166 'method' => 'POST',
168 $button);
170 $buttons[] = $button;
173 if ($has_current) {
174 $form->appendChild(
175 id(new AphrontFormMarkupControl())
176 ->setLabel(pht('Current Picture'))
177 ->setValue(array_shift($buttons)));
180 $form->appendChild(
181 id(new AphrontFormMarkupControl())
182 ->setLabel(pht('Use Picture'))
183 ->setValue($buttons));
185 $form_box = id(new PHUIObjectBoxView())
186 ->setHeaderText($title)
187 ->setFormErrors($errors)
188 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
189 ->setForm($form);
191 $upload_form = id(new AphrontFormView())
192 ->setUser($viewer)
193 ->setEncType('multipart/form-data')
194 ->appendChild(
195 id(new AphrontFormFileControl())
196 ->setName('picture')
197 ->setLabel(pht('Upload Picture'))
198 ->setError($e_file)
199 ->setCaption(
200 pht('Supported formats: %s', implode(', ', $supported_formats))))
201 ->appendChild(
202 id(new AphrontFormSubmitControl())
203 ->addCancelButton($blog_uri)
204 ->setValue(pht('Upload Picture')));
206 $upload_box = id(new PHUIObjectBoxView())
207 ->setHeaderText(pht('Upload New Picture'))
208 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
209 ->setForm($upload_form);
211 $crumbs = $this->buildApplicationCrumbs();
212 $crumbs->addTextCrumb(
213 pht('Blogs'),
214 $this->getApplicationURI('blog/'));
215 $crumbs->addTextCrumb(
216 $blog->getName(),
217 $this->getApplicationURI('blog/view/'.$id));
218 $crumbs->addTextCrumb(pht('Blog Picture'));
219 $crumbs->setBorder(true);
221 $header = id(new PHUIHeaderView())
222 ->setHeader(pht('Edit Blog Picture'))
223 ->setHeaderIcon('fa-camera');
225 $view = id(new PHUITwoColumnView())
226 ->setHeader($header)
227 ->setFooter(array(
228 $form_box,
229 $upload_box,
232 return $this->newPage()
233 ->setTitle($title)
234 ->setCrumbs($crumbs)
235 ->appendChild(
236 array(
237 $view,