Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / controller / DiffusionRepositoryProfilePictureController.php
blobcd4e3148c7a768f372b0c3fd827ba66f0bfb863f
1 <?php
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())
11 ->setViewer($viewer)
12 ->withIDs(array($id))
13 ->needProfileImage(true)
14 ->needURIs(true)
15 ->requireCapabilities(
16 array(
17 PhabricatorPolicyCapability::CAN_VIEW,
18 PhabricatorPolicyCapability::CAN_EDIT,
20 ->executeOne();
22 if (!$repository) {
23 return new Aphront404Response();
26 $supported_formats = PhabricatorFile::getTransformableImageFormats();
27 $e_file = true;
28 $errors = array();
29 $done_uri = $repository->getURI();
31 if ($request->isFormPost()) {
32 $phid = $request->getStr('phid');
33 $is_default = false;
34 if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
35 $phid = null;
36 $is_default = true;
37 } else if ($phid) {
38 $file = id(new PhabricatorFileQuery())
39 ->setViewer($viewer)
40 ->withPHIDs(array($phid))
41 ->executeOne();
42 } else {
43 if ($request->getFileExists('picture')) {
44 $file = PhabricatorFile::newFromPHPUpload(
45 $_FILES['picture'],
46 array(
47 'authorPHID' => $viewer->getPHID(),
48 'canCDN' => true,
49 ));
50 } else {
51 $e_file = pht('Required');
52 $errors[] = pht(
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');
60 $errors[] = pht(
61 'This server only supports these image formats: %s.',
62 implode(', ', $supported_formats));
63 } else {
64 $xform = PhabricatorFileTransform::getTransformByKey(
65 PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE);
66 $xformed = $xform->executeTransform($file);
70 if (!$errors) {
71 if ($is_default) {
72 $repository->setProfileImagePHID(null);
73 } else {
74 $repository->setProfileImagePHID($xformed->getPHID());
75 $xformed->attachToObject($repository->getPHID());
77 $repository->save();
78 return id(new AphrontRedirectResponse())->setURI($done_uri);
82 $title = pht('Edit Picture');
84 $form = id(new PHUIFormLayoutView())
85 ->setUser($viewer);
87 $default_image = PhabricatorFile::loadBuiltin(
88 $viewer, 'repo/code.png');
90 $images = array();
92 $current = $repository->getProfileImagePHID();
93 $has_current = false;
94 if ($current) {
95 $files = id(new PhabricatorFileQuery())
96 ->setViewer($viewer)
97 ->withPHIDs(array($current))
98 ->execute();
99 if ($files) {
100 $file = head($files);
101 if ($file->isTransformableImage()) {
102 $has_current = true;
103 $images[$current] = array(
104 'uri' => $file->getBestURI(),
105 'tip' => pht('Current Picture'),
111 $builtins = array(
112 'repo/repo-git.png',
113 'repo/repo-svn.png',
114 'repo/repo-hg.png',
115 'repo/building.png',
116 'repo/cloud.png',
117 'repo/commit.png',
118 'repo/database.png',
119 'repo/desktop.png',
120 'repo/gears.png',
121 'repo/globe.png',
122 'repo/locked.png',
123 'repo/microchip.png',
124 'repo/mobile.png',
125 'repo/repo.png',
126 'repo/servers.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());
144 $buttons = array();
145 foreach ($images as $phid => $spec) {
146 $style = null;
147 if (isset($spec['style'])) {
148 $style = $spec['style'];
150 $button = javelin_tag(
151 'button',
152 array(
153 'class' => 'button-grey profile-image-button',
154 'sigil' => 'has-tooltip',
155 'meta' => array(
156 'tip' => $spec['tip'],
157 'size' => 300,
160 phutil_tag(
161 'img',
162 array(
163 'height' => 50,
164 'width' => 50,
165 'src' => $spec['uri'],
166 )));
168 $button = array(
169 phutil_tag(
170 'input',
171 array(
172 'type' => 'hidden',
173 'name' => 'phid',
174 'value' => $phid,
176 $button,
179 $button = phabricator_form(
180 $viewer,
181 array(
182 'class' => 'profile-image-form',
183 'method' => 'POST',
185 $button);
187 $buttons[] = $button;
190 if ($has_current) {
191 $form->appendChild(
192 id(new AphrontFormMarkupControl())
193 ->setLabel(pht('Current Picture'))
194 ->setValue(array_shift($buttons)));
197 $form->appendChild(
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)
206 ->setForm($form);
208 $upload_form = id(new AphrontFormView())
209 ->setUser($viewer)
210 ->setEncType('multipart/form-data')
211 ->appendChild(
212 id(new AphrontFormFileControl())
213 ->setName('picture')
214 ->setLabel(pht('Upload Picture'))
215 ->setError($e_file)
216 ->setCaption(
217 pht('Supported formats: %s', implode(', ', $supported_formats))))
218 ->appendChild(
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())
238 ->setHeader($header)
239 ->setFooter(array(
240 $form_box,
241 $upload_box,
244 return $this->newPage()
245 ->setTitle($title)
246 ->setCrumbs($crumbs)
247 ->appendChild($view);