@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 238 lines 6.7 kB view raw
1<?php 2 3final class ConpherenceRoomPictureController 4 extends ConpherenceController { 5 6 public function handleRequest(AphrontRequest $request) { 7 $viewer = $request->getViewer(); 8 $id = $request->getURIData('id'); 9 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, 18 )) 19 ->executeOne(); 20 if (!$conpherence) { 21 return new Aphront404Response(); 22 } 23 24 $monogram = $conpherence->getMonogram(); 25 26 $supported_formats = PhabricatorFile::getTransformableImageFormats(); 27 if ($supported_formats) { 28 $supported_formats_message = pht('Supported image formats: %s.', 29 implode(', ', $supported_formats)); 30 } else { 31 $supported_formats_message = pht('Server supports no image formats.'); 32 } 33 $e_file = true; 34 $errors = array(); 35 36 if ($request->isFormPost()) { 37 $phid = $request->getStr('phid'); 38 $is_default = false; 39 if ($phid == PhabricatorPHIDConstants::PHID_VOID) { 40 $phid = null; 41 $is_default = true; 42 } else if ($phid) { 43 $file = id(new PhabricatorFileQuery()) 44 ->setViewer($viewer) 45 ->withPHIDs(array($phid)) 46 ->executeOne(); 47 } else { 48 if ($request->getFileExists('picture')) { 49 $file = PhabricatorFile::newFromPHPUpload( 50 $_FILES['picture'], 51 array( 52 'authorPHID' => $viewer->getPHID(), 53 'canCDN' => true, 54 )); 55 } else { 56 $e_file = pht('Required'); 57 $errors[] = pht( 58 'You must choose a file when uploading a new room picture.'); 59 } 60 } 61 62 if (!$errors && !$is_default) { 63 if (!$file->isTransformableImage()) { 64 $e_file = pht('Not Supported'); 65 $errors[] = $supported_formats_message; 66 } else { 67 $xform = PhabricatorFileTransform::getTransformByKey( 68 PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE); 69 $xformed = $xform->executeTransformExplicit($file); 70 } 71 } 72 73 if (!$errors) { 74 if ($is_default) { 75 $new_value = null; 76 } else { 77 $xformed->attachToObject($conpherence->getPHID()); 78 $new_value = $xformed->getPHID(); 79 } 80 81 $xactions = array(); 82 $xactions[] = id(new ConpherenceTransaction()) 83 ->setTransactionType( 84 ConpherenceThreadPictureTransaction::TRANSACTIONTYPE) 85 ->setNewValue($new_value); 86 87 $editor = id(new ConpherenceEditor()) 88 ->setActor($viewer) 89 ->setContentSourceFromRequest($request) 90 ->setContinueOnMissingFields(true) 91 ->setContinueOnNoEffect(true); 92 93 $editor->applyTransactions($conpherence, $xactions); 94 95 return id(new AphrontRedirectResponse())->setURI('/'.$monogram); 96 } 97 } 98 99 $title = pht('Edit Room Picture'); 100 101 $form = id(new PHUIFormLayoutView()) 102 ->setUser($viewer); 103 104 $default_image = PhabricatorFile::loadBuiltin($viewer, 'conpherence.png'); 105 106 $images = array(); 107 108 $current = $conpherence->getProfileImagePHID(); 109 $has_current = false; 110 if ($current) { 111 $file = id(new PhabricatorFileQuery()) 112 ->setViewer($viewer) 113 ->withPHIDs(array($current)) 114 ->executeOne(); 115 if ($file) { 116 if ($file->isTransformableImage()) { 117 $has_current = true; 118 $images[$current] = array( 119 'uri' => $file->getBestURI(), 120 'tip' => pht('Current Picture'), 121 ); 122 } 123 } 124 } 125 126 $images[PhabricatorPHIDConstants::PHID_VOID] = array( 127 'uri' => $default_image->getBestURI(), 128 'tip' => pht('Default Picture'), 129 ); 130 131 require_celerity_resource('people-profile-css'); 132 Javelin::initBehavior('phabricator-tooltips', array()); 133 134 $buttons = array(); 135 foreach ($images as $phid => $spec) { 136 $button = javelin_tag( 137 'button', 138 array( 139 'class' => 'button-grey profile-image-button', 140 'sigil' => 'has-tooltip', 141 'meta' => array( 142 'tip' => $spec['tip'], 143 'size' => 300, 144 ), 145 ), 146 phutil_tag( 147 'img', 148 array( 149 'height' => 50, 150 'width' => 50, 151 'src' => $spec['uri'], 152 ))); 153 154 $button = array( 155 phutil_tag( 156 'input', 157 array( 158 'type' => 'hidden', 159 'name' => 'phid', 160 'value' => $phid, 161 )), 162 $button, 163 ); 164 165 $button = phabricator_form( 166 $viewer, 167 array( 168 'class' => 'profile-image-form', 169 'method' => 'POST', 170 ), 171 $button); 172 173 $buttons[] = $button; 174 } 175 176 if ($has_current) { 177 $form->appendChild( 178 id(new AphrontFormMarkupControl()) 179 ->setLabel(pht('Current Picture')) 180 ->setValue(array_shift($buttons))); 181 } 182 183 $form->appendChild( 184 id(new AphrontFormMarkupControl()) 185 ->setLabel(pht('Use Picture')) 186 ->setValue($buttons)); 187 188 $form_box = id(new PHUIObjectBoxView()) 189 ->setHeaderText($title) 190 ->setFormErrors($errors) 191 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 192 ->setForm($form); 193 194 $upload_form = id(new AphrontFormView()) 195 ->setUser($viewer) 196 ->setEncType('multipart/form-data') 197 ->appendChild( 198 id(new AphrontFormFileControl()) 199 ->setName('picture') 200 ->setLabel(pht('Upload Picture')) 201 ->setError($e_file) 202 ->setCaption($supported_formats_message)) 203 ->appendChild( 204 id(new AphrontFormSubmitControl()) 205 ->addCancelButton('/'.$monogram) 206 ->setValue(pht('Upload Picture'))); 207 208 $upload_box = id(new PHUIObjectBoxView()) 209 ->setHeaderText(pht('Upload New Picture')) 210 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 211 ->setForm($upload_form); 212 213 $crumbs = $this->buildApplicationCrumbs(); 214 $crumbs->addTextCrumb($conpherence->getTitle(), '/'.$monogram); 215 $crumbs->addTextCrumb(pht('Room Picture')); 216 $crumbs->setBorder(true); 217 218 $header = id(new PHUIHeaderView()) 219 ->setHeader(pht('Edit Room Picture')) 220 ->setHeaderIcon('fa-camera'); 221 222 $view = id(new PHUITwoColumnView()) 223 ->setHeader($header) 224 ->setFooter(array( 225 $form_box, 226 $upload_box, 227 )); 228 229 return $this->newPage() 230 ->setTitle($title) 231 ->setCrumbs($crumbs) 232 ->appendChild( 233 array( 234 $view, 235 )); 236 237 } 238}