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())
13 ->needProfileImage(true)
14 ->requireCapabilities(
16 PhabricatorPolicyCapability
::CAN_VIEW
,
17 PhabricatorPolicyCapability
::CAN_EDIT
,
21 return new Aphront404Response();
24 $this->setUser($user);
25 $name = $user->getUserName();
27 $done_uri = '/p/'.$name.'/';
29 $supported_formats = PhabricatorFile
::getTransformableImageFormats();
33 if ($request->isFormPost()) {
34 $phid = $request->getStr('phid');
36 if ($phid == PhabricatorPHIDConstants
::PHID_VOID
) {
40 $file = id(new PhabricatorFileQuery())
42 ->withPHIDs(array($phid))
45 if ($request->getFileExists('picture')) {
46 $file = PhabricatorFile
::newFromPHPUpload(
49 'authorPHID' => $viewer->getPHID(),
53 $e_file = pht('Required');
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');
63 'This server only supports these image formats: %s.',
64 implode(', ', $supported_formats));
66 $xform = PhabricatorFileTransform
::getTransformByKey(
67 PhabricatorFileThumbnailTransform
::TRANSFORM_PROFILE
);
68 $xformed = $xform->executeTransform($file);
74 $user->setProfileImagePHID(null);
76 $user->setProfileImagePHID($xformed->getPHID());
77 $xformed->attachToObject($user->getPHID());
80 return id(new AphrontRedirectResponse())->setURI($done_uri);
84 $title = pht('Edit Profile Picture');
86 $form = id(new PHUIFormLayoutView())
89 $default_image = $user->getDefaultProfileImagePHID();
91 $default_image = id(new PhabricatorFileQuery())
93 ->withPHIDs(array($default_image))
97 if (!$default_image) {
98 $default_image = PhabricatorFile
::loadBuiltin($viewer, 'profile.png');
103 $current = $user->getProfileImagePHID();
104 $has_current = false;
106 $files = id(new PhabricatorFileQuery())
108 ->withPHIDs(array($current))
111 $file = head($files);
112 if ($file->isTransformableImage()) {
114 $images[$current] = array(
115 'uri' => $file->getBestURI(),
116 'tip' => pht('Current Picture'),
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())
144 ->withUserPHIDs(array($user->getPHID()))
146 ->requireCapabilities(
148 PhabricatorPolicyCapability
::CAN_VIEW
,
149 PhabricatorPolicyCapability
::CAN_EDIT
,
153 foreach ($accounts as $account) {
154 $file = $account->getProfileImageFile();
155 if ($account->getProfileImagePHID() != $file->getPHID()) {
156 // This is a default image, just skip it.
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(),
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());
182 foreach ($images as $phid => $spec) {
184 if (isset($spec['style'])) {
185 $style = $spec['style'];
187 $button = javelin_tag(
190 'class' => 'button-grey profile-image-button',
191 'sigil' => 'has-tooltip',
193 'tip' => $spec['tip'],
202 'src' => $spec['uri'],
216 $button = phabricator_form(
219 'class' => 'profile-image-form',
224 $buttons[] = $button;
229 id(new AphrontFormMarkupControl())
230 ->setLabel(pht('Current Picture'))
231 ->setValue(array_shift($buttons)));
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
)
245 $upload_form = id(new AphrontFormView())
247 ->setEncType('multipart/form-data')
249 id(new AphrontFormFileControl())
251 ->setLabel(pht('Upload Picture'))
254 pht('Supported formats: %s', implode(', ', $supported_formats))))
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(
271 PhabricatorPeopleProfileMenuEngine
::ITEM_MANAGE
);
273 $header = $this->buildProfileHeader();
275 $view = id(new PHUITwoColumnView())
277 ->addClass('project-view-home')
278 ->addClass('project-view-people-home')
284 return $this->newPage()
287 ->setNavigation($nav)
288 ->appendChild($view);