Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / conpherence / controller / ConpherenceRoomPictureController.php
blobfbf3def9cd6d2ee4c8653ebeccc2a5e410bd834a
1 <?php
3 final class ConpherenceRoomPictureController
4 extends ConpherenceController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
8 $id = $request->getURIData('id');
10 $conpherence = id(new ConpherenceThreadQuery())
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 (!$conpherence) {
21 return new Aphront404Response();
24 $monogram = $conpherence->getMonogram();
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 room 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($conpherence->getPHID());
74 $new_value = $xformed->getPHID();
77 $xactions = array();
78 $xactions[] = id(new ConpherenceTransaction())
79 ->setTransactionType(
80 ConpherenceThreadPictureTransaction::TRANSACTIONTYPE)
81 ->setNewValue($new_value);
83 $editor = id(new ConpherenceEditor())
84 ->setActor($viewer)
85 ->setContentSourceFromRequest($request)
86 ->setContinueOnMissingFields(true)
87 ->setContinueOnNoEffect(true);
89 $editor->applyTransactions($conpherence, $xactions);
91 return id(new AphrontRedirectResponse())->setURI('/'.$monogram);
95 $title = pht('Edit Room Picture');
97 $form = id(new PHUIFormLayoutView())
98 ->setUser($viewer);
100 $default_image = PhabricatorFile::loadBuiltin($viewer, 'conpherence.png');
102 $images = array();
104 $current = $conpherence->getProfileImagePHID();
105 $has_current = false;
106 if ($current) {
107 $file = id(new PhabricatorFileQuery())
108 ->setViewer($viewer)
109 ->withPHIDs(array($current))
110 ->executeOne();
111 if ($file) {
112 if ($file->isTransformableImage()) {
113 $has_current = true;
114 $images[$current] = array(
115 'uri' => $file->getBestURI(),
116 'tip' => pht('Current Picture'),
122 $images[PhabricatorPHIDConstants::PHID_VOID] = array(
123 'uri' => $default_image->getBestURI(),
124 'tip' => pht('Default Picture'),
127 require_celerity_resource('people-profile-css');
128 Javelin::initBehavior('phabricator-tooltips', array());
130 $buttons = array();
131 foreach ($images as $phid => $spec) {
132 $button = javelin_tag(
133 'button',
134 array(
135 'class' => 'button-grey profile-image-button',
136 'sigil' => 'has-tooltip',
137 'meta' => array(
138 'tip' => $spec['tip'],
139 'size' => 300,
142 phutil_tag(
143 'img',
144 array(
145 'height' => 50,
146 'width' => 50,
147 'src' => $spec['uri'],
148 )));
150 $button = array(
151 phutil_tag(
152 'input',
153 array(
154 'type' => 'hidden',
155 'name' => 'phid',
156 'value' => $phid,
158 $button,
161 $button = phabricator_form(
162 $viewer,
163 array(
164 'class' => 'profile-image-form',
165 'method' => 'POST',
167 $button);
169 $buttons[] = $button;
172 if ($has_current) {
173 $form->appendChild(
174 id(new AphrontFormMarkupControl())
175 ->setLabel(pht('Current Picture'))
176 ->setValue(array_shift($buttons)));
179 $form->appendChild(
180 id(new AphrontFormMarkupControl())
181 ->setLabel(pht('Use Picture'))
182 ->setValue($buttons));
184 $form_box = id(new PHUIObjectBoxView())
185 ->setHeaderText($title)
186 ->setFormErrors($errors)
187 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
188 ->setForm($form);
190 $upload_form = id(new AphrontFormView())
191 ->setUser($viewer)
192 ->setEncType('multipart/form-data')
193 ->appendChild(
194 id(new AphrontFormFileControl())
195 ->setName('picture')
196 ->setLabel(pht('Upload Picture'))
197 ->setError($e_file)
198 ->setCaption(
199 pht('Supported formats: %s', implode(', ', $supported_formats))))
200 ->appendChild(
201 id(new AphrontFormSubmitControl())
202 ->addCancelButton('/'.$monogram)
203 ->setValue(pht('Upload Picture')));
205 $upload_box = id(new PHUIObjectBoxView())
206 ->setHeaderText(pht('Upload New Picture'))
207 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
208 ->setForm($upload_form);
210 $crumbs = $this->buildApplicationCrumbs();
211 $crumbs->addTextCrumb($conpherence->getTitle(), '/'.$monogram);
212 $crumbs->addTextCrumb(pht('Room Picture'));
213 $crumbs->setBorder(true);
215 $header = id(new PHUIHeaderView())
216 ->setHeader(pht('Edit Room Picture'))
217 ->setHeaderIcon('fa-camera');
219 $view = id(new PHUITwoColumnView())
220 ->setHeader($header)
221 ->setFooter(array(
222 $form_box,
223 $upload_box,
226 return $this->newPage()
227 ->setTitle($title)
228 ->setCrumbs($crumbs)
229 ->appendChild(
230 array(
231 $view,