Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / people / controller / PhabricatorPeopleProfilePictureController.php
blobd50eccff134a293c9d9a2c95f3eb558d5051416f
1 <?php
3 final class PhabricatorPeopleProfilePictureController
4 extends PhabricatorPeopleProfileController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $this->getViewer();
8 $id = $request->getURIData('id');
10 $user = id(new PhabricatorPeopleQuery())
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 (!$user) {
21 return new Aphront404Response();
24 $this->setUser($user);
25 $name = $user->getUserName();
27 $done_uri = '/p/'.$name.'/';
29 $supported_formats = PhabricatorFile::getTransformableImageFormats();
30 $e_file = true;
31 $errors = array();
33 if ($request->isFormPost()) {
34 $phid = $request->getStr('phid');
35 $is_default = false;
36 if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
37 $phid = null;
38 $is_default = true;
39 } else if ($phid) {
40 $file = id(new PhabricatorFileQuery())
41 ->setViewer($viewer)
42 ->withPHIDs(array($phid))
43 ->executeOne();
44 } else {
45 if ($request->getFileExists('picture')) {
46 $file = PhabricatorFile::newFromPHPUpload(
47 $_FILES['picture'],
48 array(
49 'authorPHID' => $viewer->getPHID(),
50 'canCDN' => true,
51 ));
52 } else {
53 $e_file = pht('Required');
54 $errors[] = pht(
55 'You must choose a file when uploading a new profile picture.');
59 if (!$errors && !$is_default) {
60 if (!$file->isTransformableImage()) {
61 $e_file = pht('Not Supported');
62 $errors[] = pht(
63 'This server only supports these image formats: %s.',
64 implode(', ', $supported_formats));
65 } else {
66 $xform = PhabricatorFileTransform::getTransformByKey(
67 PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE);
68 $xformed = $xform->executeTransform($file);
72 if (!$errors) {
73 if ($is_default) {
74 $user->setProfileImagePHID(null);
75 } else {
76 $user->setProfileImagePHID($xformed->getPHID());
77 $xformed->attachToObject($user->getPHID());
79 $user->save();
80 return id(new AphrontRedirectResponse())->setURI($done_uri);
84 $title = pht('Edit Profile Picture');
86 $form = id(new PHUIFormLayoutView())
87 ->setUser($viewer);
89 $default_image = $user->getDefaultProfileImagePHID();
90 if ($default_image) {
91 $default_image = id(new PhabricatorFileQuery())
92 ->setViewer($viewer)
93 ->withPHIDs(array($default_image))
94 ->executeOne();
97 if (!$default_image) {
98 $default_image = PhabricatorFile::loadBuiltin($viewer, 'profile.png');
101 $images = array();
103 $current = $user->getProfileImagePHID();
104 $has_current = false;
105 if ($current) {
106 $files = id(new PhabricatorFileQuery())
107 ->setViewer($viewer)
108 ->withPHIDs(array($current))
109 ->execute();
110 if ($files) {
111 $file = head($files);
112 if ($file->isTransformableImage()) {
113 $has_current = true;
114 $images[$current] = array(
115 'uri' => $file->getBestURI(),
116 'tip' => pht('Current Picture'),
122 $builtins = array(
123 'user1.png',
124 'user2.png',
125 'user3.png',
126 'user4.png',
127 'user5.png',
128 'user6.png',
129 'user7.png',
130 'user8.png',
131 'user9.png',
133 foreach ($builtins as $builtin) {
134 $file = PhabricatorFile::loadBuiltin($viewer, $builtin);
135 $images[$file->getPHID()] = array(
136 'uri' => $file->getBestURI(),
137 'tip' => pht('Builtin Image'),
141 // Try to add external account images for any associated external accounts.
142 $accounts = id(new PhabricatorExternalAccountQuery())
143 ->setViewer($viewer)
144 ->withUserPHIDs(array($user->getPHID()))
145 ->needImages(true)
146 ->requireCapabilities(
147 array(
148 PhabricatorPolicyCapability::CAN_VIEW,
149 PhabricatorPolicyCapability::CAN_EDIT,
151 ->execute();
153 foreach ($accounts as $account) {
154 $file = $account->getProfileImageFile();
155 if ($account->getProfileImagePHID() != $file->getPHID()) {
156 // This is a default image, just skip it.
157 continue;
160 $config = $account->getProviderConfig();
161 $provider = $config->getProvider();
163 $tip = pht('Picture From %s', $provider->getProviderName());
165 if ($file->isTransformableImage()) {
166 $images[$file->getPHID()] = array(
167 'uri' => $file->getBestURI(),
168 'tip' => $tip,
173 $images[PhabricatorPHIDConstants::PHID_VOID] = array(
174 'uri' => $default_image->getBestURI(),
175 'tip' => pht('Default Picture'),
178 require_celerity_resource('people-profile-css');
179 Javelin::initBehavior('phabricator-tooltips', array());
181 $buttons = array();
182 foreach ($images as $phid => $spec) {
183 $style = null;
184 if (isset($spec['style'])) {
185 $style = $spec['style'];
187 $button = javelin_tag(
188 'button',
189 array(
190 'class' => 'button-grey profile-image-button',
191 'sigil' => 'has-tooltip',
192 'meta' => array(
193 'tip' => $spec['tip'],
194 'size' => 300,
197 phutil_tag(
198 'img',
199 array(
200 'height' => 50,
201 'width' => 50,
202 'src' => $spec['uri'],
203 )));
205 $button = array(
206 phutil_tag(
207 'input',
208 array(
209 'type' => 'hidden',
210 'name' => 'phid',
211 'value' => $phid,
213 $button,
216 $button = phabricator_form(
217 $viewer,
218 array(
219 'class' => 'profile-image-form',
220 'method' => 'POST',
222 $button);
224 $buttons[] = $button;
227 if ($has_current) {
228 $form->appendChild(
229 id(new AphrontFormMarkupControl())
230 ->setLabel(pht('Current Picture'))
231 ->setValue(array_shift($buttons)));
234 $form->appendChild(
235 id(new AphrontFormMarkupControl())
236 ->setLabel(pht('Use Picture'))
237 ->setValue($buttons));
239 $form_box = id(new PHUIObjectBoxView())
240 ->setHeaderText($title)
241 ->setFormErrors($errors)
242 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
243 ->setForm($form);
245 $upload_form = id(new AphrontFormView())
246 ->setUser($viewer)
247 ->setEncType('multipart/form-data')
248 ->appendChild(
249 id(new AphrontFormFileControl())
250 ->setName('picture')
251 ->setLabel(pht('Upload Picture'))
252 ->setError($e_file)
253 ->setCaption(
254 pht('Supported formats: %s', implode(', ', $supported_formats))))
255 ->appendChild(
256 id(new AphrontFormSubmitControl())
257 ->addCancelButton($done_uri)
258 ->setValue(pht('Upload Picture')));
260 $upload_box = id(new PHUIObjectBoxView())
261 ->setHeaderText(pht('Upload New Picture'))
262 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
263 ->setForm($upload_form);
265 $crumbs = $this->buildApplicationCrumbs();
266 $crumbs->addTextCrumb(pht('Edit Profile Picture'));
267 $crumbs->setBorder(true);
269 $nav = $this->newNavigation(
270 $user,
271 PhabricatorPeopleProfileMenuEngine::ITEM_MANAGE);
273 $header = $this->buildProfileHeader();
275 $view = id(new PHUITwoColumnView())
276 ->setHeader($header)
277 ->addClass('project-view-home')
278 ->addClass('project-view-people-home')
279 ->setFooter(array(
280 $form_box,
281 $upload_box,
284 return $this->newPage()
285 ->setTitle($title)
286 ->setCrumbs($crumbs)
287 ->setNavigation($nav)
288 ->appendChild($view);