[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/project/controller/ -> PhabricatorProjectEditPictureController.php (source)

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


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