[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/people/controller/ -> PhabricatorPeopleProfilePictureController.php (source)

   1  <?php
   2  
   3  final class PhabricatorPeopleProfilePictureController
   4    extends PhabricatorPeopleController {
   5  
   6    private $id;
   7  
   8    public function shouldRequireAdmin() {
   9      return false;
  10    }
  11  
  12    public function willProcessRequest(array $data) {
  13      $this->id = $data['id'];
  14    }
  15  
  16    public function processRequest() {
  17      $request = $this->getRequest();
  18      $viewer = $request->getUser();
  19  
  20      $user = id(new PhabricatorPeopleQuery())
  21        ->setViewer($viewer)
  22        ->withIDs(array($this->id))
  23        ->requireCapabilities(
  24          array(
  25            PhabricatorPolicyCapability::CAN_VIEW,
  26            PhabricatorPolicyCapability::CAN_EDIT,
  27          ))
  28        ->executeOne();
  29      if (!$user) {
  30        return new Aphront404Response();
  31      }
  32  
  33      $profile_uri = '/p/'.$user->getUsername().'/';
  34  
  35      $supported_formats = PhabricatorFile::getTransformableImageFormats();
  36      $e_file = true;
  37      $errors = array();
  38  
  39      if ($request->isFormPost()) {
  40        $phid = $request->getStr('phid');
  41        $is_default = false;
  42        if ($phid == PhabricatorPHIDConstants::PHID_VOID) {
  43          $phid = null;
  44          $is_default = true;
  45        } else if ($phid) {
  46          $file = id(new PhabricatorFileQuery())
  47            ->setViewer($viewer)
  48            ->withPHIDs(array($phid))
  49            ->executeOne();
  50        } else {
  51          if ($request->getFileExists('picture')) {
  52            $file = PhabricatorFile::newFromPHPUpload(
  53              $_FILES['picture'],
  54              array(
  55                'authorPHID' => $viewer->getPHID(),
  56                'canCDN' => true,
  57              ));
  58          } else {
  59            $e_file = pht('Required');
  60            $errors[] = pht(
  61              'You must choose a file when uploading a new profile picture.');
  62          }
  63        }
  64  
  65        if (!$errors && !$is_default) {
  66          if (!$file->isTransformableImage()) {
  67            $e_file = pht('Not Supported');
  68            $errors[] = pht(
  69              'This server only supports these image formats: %s.',
  70              implode(', ', $supported_formats));
  71          } else {
  72            $xformer = new PhabricatorImageTransformer();
  73            $xformed = $xformer->executeProfileTransform(
  74              $file,
  75              $width = 50,
  76              $min_height = 50,
  77              $max_height = 50);
  78          }
  79        }
  80  
  81        if (!$errors) {
  82          if ($is_default) {
  83            $user->setProfileImagePHID(null);
  84          } else {
  85            $user->setProfileImagePHID($xformed->getPHID());
  86            $xformed->attachToObject($user->getPHID());
  87          }
  88          $user->save();
  89          return id(new AphrontRedirectResponse())->setURI($profile_uri);
  90        }
  91      }
  92  
  93      $title = pht('Edit Profile Picture');
  94      $crumbs = $this->buildApplicationCrumbs();
  95      $crumbs->addTextCrumb($user->getUsername(), $profile_uri);
  96      $crumbs->addTextCrumb($title);
  97  
  98      $form = id(new PHUIFormLayoutView())
  99        ->setUser($viewer);
 100  
 101      $default_image = PhabricatorFile::loadBuiltin($viewer, 'profile.png');
 102  
 103      $images = array();
 104  
 105      $current = $user->getProfileImagePHID();
 106      $has_current = false;
 107      if ($current) {
 108        $files = id(new PhabricatorFileQuery())
 109          ->setViewer($viewer)
 110          ->withPHIDs(array($current))
 111          ->execute();
 112        if ($files) {
 113          $file = head($files);
 114          if ($file->isTransformableImage()) {
 115            $has_current = true;
 116            $images[$current] = array(
 117              'uri' => $file->getBestURI(),
 118              'tip' => pht('Current Picture'),
 119            );
 120          }
 121        }
 122      }
 123  
 124      // Try to add external account images for any associated external accounts.
 125      $accounts = id(new PhabricatorExternalAccountQuery())
 126        ->setViewer($viewer)
 127        ->withUserPHIDs(array($user->getPHID()))
 128        ->needImages(true)
 129        ->requireCapabilities(
 130          array(
 131            PhabricatorPolicyCapability::CAN_VIEW,
 132            PhabricatorPolicyCapability::CAN_EDIT,
 133          ))
 134        ->execute();
 135  
 136      foreach ($accounts as $account) {
 137        $file = $account->getProfileImageFile();
 138        if ($account->getProfileImagePHID() != $file->getPHID()) {
 139          // This is a default image, just skip it.
 140          continue;
 141        }
 142  
 143        $provider = PhabricatorAuthProvider::getEnabledProviderByKey(
 144          $account->getProviderKey());
 145        if ($provider) {
 146          $tip = pht('Picture From %s', $provider->getProviderName());
 147        } else {
 148          $tip = pht('Picture From External Account');
 149        }
 150  
 151        if ($file->isTransformableImage()) {
 152          $images[$file->getPHID()] = array(
 153            'uri' => $file->getBestURI(),
 154            'tip' => $tip,
 155          );
 156        }
 157      }
 158  
 159      $images[PhabricatorPHIDConstants::PHID_VOID] = array(
 160        'uri' => $default_image->getBestURI(),
 161        'tip' => pht('Default Picture'),
 162      );
 163  
 164      require_celerity_resource('people-profile-css');
 165      Javelin::initBehavior('phabricator-tooltips', array());
 166  
 167      $buttons = array();
 168      foreach ($images as $phid => $spec) {
 169        $button = javelin_tag(
 170          'button',
 171          array(
 172            'class' => 'grey profile-image-button',
 173            'sigil' => 'has-tooltip',
 174            'meta' => array(
 175              'tip' => $spec['tip'],
 176              'size' => 300,
 177            ),
 178          ),
 179          phutil_tag(
 180            'img',
 181            array(
 182              'height' => 50,
 183              'width' => 50,
 184              'src' => $spec['uri'],
 185            )));
 186  
 187        $button = array(
 188          phutil_tag(
 189            'input',
 190            array(
 191              'type'  => 'hidden',
 192              'name'  => 'phid',
 193              'value' => $phid,
 194            )),
 195          $button,
 196        );
 197  
 198        $button = phabricator_form(
 199          $viewer,
 200          array(
 201            'class' => 'profile-image-form',
 202            'method' => 'POST',
 203          ),
 204          $button);
 205  
 206        $buttons[] = $button;
 207      }
 208  
 209      if ($has_current) {
 210        $form->appendChild(
 211          id(new AphrontFormMarkupControl())
 212            ->setLabel(pht('Current Picture'))
 213            ->setValue(array_shift($buttons)));
 214      }
 215  
 216      $form->appendChild(
 217        id(new AphrontFormMarkupControl())
 218          ->setLabel(pht('Use Picture'))
 219          ->setValue($buttons));
 220  
 221      $form_box = id(new PHUIObjectBoxView())
 222        ->setHeaderText($title)
 223        ->setFormErrors($errors)
 224        ->setForm($form);
 225  
 226      $upload_form = id(new AphrontFormView())
 227        ->setUser($viewer)
 228        ->setEncType('multipart/form-data')
 229        ->appendChild(
 230          id(new AphrontFormFileControl())
 231            ->setName('picture')
 232            ->setLabel(pht('Upload Picture'))
 233            ->setError($e_file)
 234            ->setCaption(
 235              pht('Supported formats: %s', implode(', ', $supported_formats))))
 236        ->appendChild(
 237          id(new AphrontFormSubmitControl())
 238            ->addCancelButton($profile_uri)
 239            ->setValue(pht('Upload Picture')));
 240  
 241      $upload_box = id(new PHUIObjectBoxView())
 242        ->setHeaderText(pht('Upload New Picture'))
 243        ->setForm($upload_form);
 244  
 245      return $this->buildApplicationPage(
 246        array(
 247          $crumbs,
 248          $form_box,
 249          $upload_box,
 250        ),
 251        array(
 252          'title' => $title,
 253        ));
 254    }
 255  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1