Path: blob/master/src/applications/people/controller/PhabricatorPeopleProfilePictureController.php
12262 views
<?php12final class PhabricatorPeopleProfilePictureController3extends PhabricatorPeopleProfileController {45public function handleRequest(AphrontRequest $request) {6$viewer = $this->getViewer();7$id = $request->getURIData('id');89$user = id(new PhabricatorPeopleQuery())10->setViewer($viewer)11->withIDs(array($id))12->needProfileImage(true)13->requireCapabilities(14array(15PhabricatorPolicyCapability::CAN_VIEW,16PhabricatorPolicyCapability::CAN_EDIT,17))18->executeOne();19if (!$user) {20return new Aphront404Response();21}2223$this->setUser($user);24$name = $user->getUserName();2526$done_uri = '/p/'.$name.'/';2728$supported_formats = PhabricatorFile::getTransformableImageFormats();29$e_file = true;30$errors = array();3132if ($request->isFormPost()) {33$phid = $request->getStr('phid');34$is_default = false;35if ($phid == PhabricatorPHIDConstants::PHID_VOID) {36$phid = null;37$is_default = true;38} else if ($phid) {39$file = id(new PhabricatorFileQuery())40->setViewer($viewer)41->withPHIDs(array($phid))42->executeOne();43} else {44if ($request->getFileExists('picture')) {45$file = PhabricatorFile::newFromPHPUpload(46$_FILES['picture'],47array(48'authorPHID' => $viewer->getPHID(),49'canCDN' => true,50));51} else {52$e_file = pht('Required');53$errors[] = pht(54'You must choose a file when uploading a new profile picture.');55}56}5758if (!$errors && !$is_default) {59if (!$file->isTransformableImage()) {60$e_file = pht('Not Supported');61$errors[] = pht(62'This server only supports these image formats: %s.',63implode(', ', $supported_formats));64} else {65$xform = PhabricatorFileTransform::getTransformByKey(66PhabricatorFileThumbnailTransform::TRANSFORM_PROFILE);67$xformed = $xform->executeTransform($file);68}69}7071if (!$errors) {72if ($is_default) {73$user->setProfileImagePHID(null);74} else {75$user->setProfileImagePHID($xformed->getPHID());76$xformed->attachToObject($user->getPHID());77}78$user->save();79return id(new AphrontRedirectResponse())->setURI($done_uri);80}81}8283$title = pht('Edit Profile Picture');8485$form = id(new PHUIFormLayoutView())86->setUser($viewer);8788$default_image = $user->getDefaultProfileImagePHID();89if ($default_image) {90$default_image = id(new PhabricatorFileQuery())91->setViewer($viewer)92->withPHIDs(array($default_image))93->executeOne();94}9596if (!$default_image) {97$default_image = PhabricatorFile::loadBuiltin($viewer, 'profile.png');98}99100$images = array();101102$current = $user->getProfileImagePHID();103$has_current = false;104if ($current) {105$files = id(new PhabricatorFileQuery())106->setViewer($viewer)107->withPHIDs(array($current))108->execute();109if ($files) {110$file = head($files);111if ($file->isTransformableImage()) {112$has_current = true;113$images[$current] = array(114'uri' => $file->getBestURI(),115'tip' => pht('Current Picture'),116);117}118}119}120121$builtins = array(122'user1.png',123'user2.png',124'user3.png',125'user4.png',126'user5.png',127'user6.png',128'user7.png',129'user8.png',130'user9.png',131);132foreach ($builtins as $builtin) {133$file = PhabricatorFile::loadBuiltin($viewer, $builtin);134$images[$file->getPHID()] = array(135'uri' => $file->getBestURI(),136'tip' => pht('Builtin Image'),137);138}139140// Try to add external account images for any associated external accounts.141$accounts = id(new PhabricatorExternalAccountQuery())142->setViewer($viewer)143->withUserPHIDs(array($user->getPHID()))144->needImages(true)145->requireCapabilities(146array(147PhabricatorPolicyCapability::CAN_VIEW,148PhabricatorPolicyCapability::CAN_EDIT,149))150->execute();151152foreach ($accounts as $account) {153$file = $account->getProfileImageFile();154if ($account->getProfileImagePHID() != $file->getPHID()) {155// This is a default image, just skip it.156continue;157}158159$config = $account->getProviderConfig();160$provider = $config->getProvider();161162$tip = pht('Picture From %s', $provider->getProviderName());163164if ($file->isTransformableImage()) {165$images[$file->getPHID()] = array(166'uri' => $file->getBestURI(),167'tip' => $tip,168);169}170}171172$images[PhabricatorPHIDConstants::PHID_VOID] = array(173'uri' => $default_image->getBestURI(),174'tip' => pht('Default Picture'),175);176177require_celerity_resource('people-profile-css');178Javelin::initBehavior('phabricator-tooltips', array());179180$buttons = array();181foreach ($images as $phid => $spec) {182$style = null;183if (isset($spec['style'])) {184$style = $spec['style'];185}186$button = javelin_tag(187'button',188array(189'class' => 'button-grey profile-image-button',190'sigil' => 'has-tooltip',191'meta' => array(192'tip' => $spec['tip'],193'size' => 300,194),195),196phutil_tag(197'img',198array(199'height' => 50,200'width' => 50,201'src' => $spec['uri'],202)));203204$button = array(205phutil_tag(206'input',207array(208'type' => 'hidden',209'name' => 'phid',210'value' => $phid,211)),212$button,213);214215$button = phabricator_form(216$viewer,217array(218'class' => 'profile-image-form',219'method' => 'POST',220),221$button);222223$buttons[] = $button;224}225226if ($has_current) {227$form->appendChild(228id(new AphrontFormMarkupControl())229->setLabel(pht('Current Picture'))230->setValue(array_shift($buttons)));231}232233$form->appendChild(234id(new AphrontFormMarkupControl())235->setLabel(pht('Use Picture'))236->setValue($buttons));237238$form_box = id(new PHUIObjectBoxView())239->setHeaderText($title)240->setFormErrors($errors)241->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)242->setForm($form);243244$upload_form = id(new AphrontFormView())245->setUser($viewer)246->setEncType('multipart/form-data')247->appendChild(248id(new AphrontFormFileControl())249->setName('picture')250->setLabel(pht('Upload Picture'))251->setError($e_file)252->setCaption(253pht('Supported formats: %s', implode(', ', $supported_formats))))254->appendChild(255id(new AphrontFormSubmitControl())256->addCancelButton($done_uri)257->setValue(pht('Upload Picture')));258259$upload_box = id(new PHUIObjectBoxView())260->setHeaderText(pht('Upload New Picture'))261->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)262->setForm($upload_form);263264$crumbs = $this->buildApplicationCrumbs();265$crumbs->addTextCrumb(pht('Edit Profile Picture'));266$crumbs->setBorder(true);267268$nav = $this->newNavigation(269$user,270PhabricatorPeopleProfileMenuEngine::ITEM_MANAGE);271272$header = $this->buildProfileHeader();273274$view = id(new PHUITwoColumnView())275->setHeader($header)276->addClass('project-view-home')277->addClass('project-view-people-home')278->setFooter(array(279$form_box,280$upload_box,281));282283return $this->newPage()284->setTitle($title)285->setCrumbs($crumbs)286->setNavigation($nav)287->appendChild($view);288}289}290291292