3 final class DiffusionRepositoryProfilePictureController
4 extends DiffusionController
{
6 public function handleRequest(AphrontRequest
$request) {
7 $viewer = $this->getViewer();
8 $id = $request->getURIData('id');
10 $repository = id(new PhabricatorRepositoryQuery())
13 ->needProfileImage(true)
15 ->requireCapabilities(
17 PhabricatorPolicyCapability
::CAN_VIEW
,
18 PhabricatorPolicyCapability
::CAN_EDIT
,
23 return new Aphront404Response();
26 $supported_formats = PhabricatorFile
::getTransformableImageFormats();
29 $done_uri = $repository->getURI();
31 if ($request->isFormPost()) {
32 $phid = $request->getStr('phid');
34 if ($phid == PhabricatorPHIDConstants
::PHID_VOID
) {
38 $file = id(new PhabricatorFileQuery())
40 ->withPHIDs(array($phid))
43 if ($request->getFileExists('picture')) {
44 $file = PhabricatorFile
::newFromPHPUpload(
47 'authorPHID' => $viewer->getPHID(),
51 $e_file = pht('Required');
53 'You must choose a file when uploading a new profile picture.');
57 if (!$errors && !$is_default) {
58 if (!$file->isTransformableImage()) {
59 $e_file = pht('Not Supported');
61 'This server only supports these image formats: %s.',
62 implode(', ', $supported_formats));
64 $xform = PhabricatorFileTransform
::getTransformByKey(
65 PhabricatorFileThumbnailTransform
::TRANSFORM_PROFILE
);
66 $xformed = $xform->executeTransform($file);
72 $repository->setProfileImagePHID(null);
74 $repository->setProfileImagePHID($xformed->getPHID());
75 $xformed->attachToObject($repository->getPHID());
78 return id(new AphrontRedirectResponse())->setURI($done_uri);
82 $title = pht('Edit Picture');
84 $form = id(new PHUIFormLayoutView())
87 $default_image = PhabricatorFile
::loadBuiltin(
88 $viewer, 'repo/code.png');
92 $current = $repository->getProfileImagePHID();
95 $files = id(new PhabricatorFileQuery())
97 ->withPHIDs(array($current))
100 $file = head($files);
101 if ($file->isTransformableImage()) {
103 $images[$current] = array(
104 'uri' => $file->getBestURI(),
105 'tip' => pht('Current Picture'),
123 'repo/microchip.png',
128 foreach ($builtins as $builtin) {
129 $file = PhabricatorFile
::loadBuiltin($viewer, $builtin);
130 $images[$file->getPHID()] = array(
131 'uri' => $file->getBestURI(),
132 'tip' => pht('Builtin Image'),
136 $images[PhabricatorPHIDConstants
::PHID_VOID
] = array(
137 'uri' => $default_image->getBestURI(),
138 'tip' => pht('Default Picture'),
141 require_celerity_resource('people-profile-css');
142 Javelin
::initBehavior('phabricator-tooltips', array());
145 foreach ($images as $phid => $spec) {
147 if (isset($spec['style'])) {
148 $style = $spec['style'];
150 $button = javelin_tag(
153 'class' => 'button-grey profile-image-button',
154 'sigil' => 'has-tooltip',
156 'tip' => $spec['tip'],
165 'src' => $spec['uri'],
179 $button = phabricator_form(
182 'class' => 'profile-image-form',
187 $buttons[] = $button;
192 id(new AphrontFormMarkupControl())
193 ->setLabel(pht('Current Picture'))
194 ->setValue(array_shift($buttons)));
198 id(new AphrontFormMarkupControl())
199 ->setLabel(pht('Use Picture'))
200 ->setValue($buttons));
202 $form_box = id(new PHUIObjectBoxView())
203 ->setHeaderText($title)
204 ->setFormErrors($errors)
205 ->setBackground(PHUIObjectBoxView
::BLUE_PROPERTY
)
208 $upload_form = id(new AphrontFormView())
210 ->setEncType('multipart/form-data')
212 id(new AphrontFormFileControl())
214 ->setLabel(pht('Upload Picture'))
217 pht('Supported formats: %s', implode(', ', $supported_formats))))
219 id(new AphrontFormSubmitControl())
220 ->addCancelButton($done_uri)
221 ->setValue(pht('Upload Picture')));
223 $header = id(new PHUIHeaderView())
224 ->setHeader(pht('Edit Repository Picture'))
225 ->setHeaderIcon('fa-camera-retro');
227 $crumbs = $this->buildApplicationCrumbs();
228 $crumbs->addTextCrumb($repository->getName(), $repository->getURI());
229 $crumbs->addTextCrumb(pht('Edit Picture'));
230 $crumbs->setBorder(true);
232 $upload_box = id(new PHUIObjectBoxView())
233 ->setHeaderText(pht('Upload New Picture'))
234 ->setBackground(PHUIObjectBoxView
::BLUE_PROPERTY
)
235 ->setForm($upload_form);
237 $view = id(new PHUITwoColumnView())
244 return $this->newPage()
247 ->appendChild($view);