[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/pholio/controller/ -> PholioMockEditController.php (source)

   1  <?php
   2  
   3  final class PholioMockEditController extends PholioController {
   4  
   5    private $id;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->id = idx($data, 'id');
   9    }
  10  
  11    public function processRequest() {
  12      $request = $this->getRequest();
  13      $user = $request->getUser();
  14  
  15      if ($this->id) {
  16        $mock = id(new PholioMockQuery())
  17          ->setViewer($user)
  18          ->needImages(true)
  19          ->requireCapabilities(
  20            array(
  21              PhabricatorPolicyCapability::CAN_VIEW,
  22              PhabricatorPolicyCapability::CAN_EDIT,
  23            ))
  24          ->withIDs(array($this->id))
  25          ->executeOne();
  26  
  27        if (!$mock) {
  28          return new Aphront404Response();
  29        }
  30  
  31        $title = pht('Edit Mock');
  32  
  33        $is_new = false;
  34        $mock_images = $mock->getImages();
  35        $files = mpull($mock_images, 'getFile');
  36        $mock_images = mpull($mock_images, null, 'getFilePHID');
  37      } else {
  38        $mock = PholioMock::initializeNewMock($user);
  39  
  40        $title = pht('Create Mock');
  41  
  42        $is_new = true;
  43        $files = array();
  44        $mock_images = array();
  45      }
  46  
  47      if ($is_new) {
  48        $v_projects = array();
  49      } else {
  50        $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
  51          $mock->getPHID(),
  52          PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  53        $v_projects = array_reverse($v_projects);
  54      }
  55  
  56      $e_name = true;
  57      $e_images = count($mock_images) ? null : true;
  58      $errors = array();
  59      $posted_mock_images = array();
  60  
  61      $v_name = $mock->getName();
  62      $v_desc = $mock->getDescription();
  63      $v_status = $mock->getStatus();
  64      $v_view = $mock->getViewPolicy();
  65      $v_edit = $mock->getEditPolicy();
  66      $v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID(
  67        $mock->getPHID());
  68  
  69      if ($request->isFormPost()) {
  70        $xactions = array();
  71  
  72        $type_name = PholioTransactionType::TYPE_NAME;
  73        $type_desc = PholioTransactionType::TYPE_DESCRIPTION;
  74        $type_status = PholioTransactionType::TYPE_STATUS;
  75        $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY;
  76        $type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY;
  77        $type_cc   = PhabricatorTransactions::TYPE_SUBSCRIBERS;
  78  
  79        $v_name = $request->getStr('name');
  80        $v_desc = $request->getStr('description');
  81        $v_status = $request->getStr('status');
  82        $v_view = $request->getStr('can_view');
  83        $v_edit = $request->getStr('can_edit');
  84        $v_cc   = $request->getArr('cc');
  85        $v_projects = $request->getArr('projects');
  86  
  87        $mock_xactions = array();
  88        $mock_xactions[$type_name] = $v_name;
  89        $mock_xactions[$type_desc] = $v_desc;
  90        $mock_xactions[$type_status] = $v_status;
  91        $mock_xactions[$type_view] = $v_view;
  92        $mock_xactions[$type_edit] = $v_edit;
  93        $mock_xactions[$type_cc]   = array('=' => $v_cc);
  94  
  95        if (!strlen($request->getStr('name'))) {
  96          $e_name = 'Required';
  97          $errors[] = pht('You must give the mock a name.');
  98        }
  99  
 100        $file_phids = $request->getArr('file_phids');
 101        if ($file_phids) {
 102          $files = id(new PhabricatorFileQuery())
 103            ->setViewer($user)
 104            ->withPHIDs($file_phids)
 105            ->execute();
 106          $files = mpull($files, null, 'getPHID');
 107          $files = array_select_keys($files, $file_phids);
 108        } else {
 109          $files = array();
 110        }
 111  
 112        if (!$files) {
 113          $e_images = pht('Required');
 114          $errors[] = pht('You must add at least one image to the mock.');
 115        } else {
 116          $mock->setCoverPHID(head($files)->getPHID());
 117        }
 118  
 119        foreach ($mock_xactions as $type => $value) {
 120          $xactions[$type] = id(new PholioTransaction())
 121            ->setTransactionType($type)
 122            ->setNewValue($value);
 123        }
 124  
 125        $order = $request->getStrList('imageOrder');
 126        $sequence_map = array_flip($order);
 127        $replaces = $request->getArr('replaces');
 128        $replaces_map = array_flip($replaces);
 129  
 130        /**
 131         * Foreach file posted, check to see whether we are replacing an image,
 132         * adding an image, or simply updating image metadata. Create
 133         * transactions for these cases as appropos.
 134         */
 135        foreach ($files as $file_phid => $file) {
 136          $replaces_image_phid = null;
 137          if (isset($replaces_map[$file_phid])) {
 138            $old_file_phid = $replaces_map[$file_phid];
 139            if ($old_file_phid != $file_phid) {
 140              $old_image = idx($mock_images, $old_file_phid);
 141              if ($old_image) {
 142                $replaces_image_phid = $old_image->getPHID();
 143              }
 144            }
 145          }
 146  
 147          $existing_image = idx($mock_images, $file_phid);
 148  
 149          $title = (string)$request->getStr('title_'.$file_phid);
 150          $description = (string)$request->getStr('description_'.$file_phid);
 151          $sequence = $sequence_map[$file_phid];
 152  
 153          if ($replaces_image_phid) {
 154            $replace_image = id(new PholioImage())
 155              ->setReplacesImagePHID($replaces_image_phid)
 156              ->setFilePhid($file_phid)
 157              ->attachFile($file)
 158              ->setName(strlen($title) ? $title : $file->getName())
 159              ->setDescription($description)
 160              ->setSequence($sequence);
 161            $xactions[] = id(new PholioTransaction())
 162              ->setTransactionType(
 163                PholioTransactionType::TYPE_IMAGE_REPLACE)
 164              ->setNewValue($replace_image);
 165            $posted_mock_images[] = $replace_image;
 166          } else if (!$existing_image) { // this is an add
 167            $add_image = id(new PholioImage())
 168              ->setFilePhid($file_phid)
 169              ->attachFile($file)
 170              ->setName(strlen($title) ? $title : $file->getName())
 171              ->setDescription($description)
 172              ->setSequence($sequence);
 173            $xactions[] = id(new PholioTransaction())
 174              ->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
 175              ->setNewValue(
 176                array('+' => array($add_image)));
 177            $posted_mock_images[] = $add_image;
 178          } else {
 179            $xactions[] = id(new PholioTransaction())
 180              ->setTransactionType(PholioTransactionType::TYPE_IMAGE_NAME)
 181              ->setNewValue(
 182                array($existing_image->getPHID() => $title));
 183            $xactions[] = id(new PholioTransaction())
 184              ->setTransactionType(
 185                PholioTransactionType::TYPE_IMAGE_DESCRIPTION)
 186                ->setNewValue(
 187                  array($existing_image->getPHID() => $description));
 188            $xactions[] = id(new PholioTransaction())
 189              ->setTransactionType(
 190                PholioTransactionType::TYPE_IMAGE_SEQUENCE)
 191                ->setNewValue(
 192                  array($existing_image->getPHID() => $sequence));
 193  
 194            $posted_mock_images[] = $existing_image;
 195          }
 196        }
 197        foreach ($mock_images as $file_phid => $mock_image) {
 198          if (!isset($files[$file_phid]) && !isset($replaces[$file_phid])) {
 199            // this is an outright delete
 200            $xactions[] = id(new PholioTransaction())
 201              ->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
 202              ->setNewValue(
 203                array('-' => array($mock_image)));
 204          }
 205        }
 206  
 207        if (!$errors) {
 208          $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
 209          $xactions[] = id(new PholioTransaction())
 210            ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
 211            ->setMetadataValue('edge:type', $proj_edge_type)
 212            ->setNewValue(array('=' => array_fuse($v_projects)));
 213  
 214          $mock->openTransaction();
 215          $editor = id(new PholioMockEditor())
 216            ->setContentSourceFromRequest($request)
 217            ->setContinueOnNoEffect(true)
 218            ->setActor($user);
 219  
 220          $xactions = $editor->applyTransactions($mock, $xactions);
 221  
 222          $mock->saveTransaction();
 223  
 224          return id(new AphrontRedirectResponse())
 225            ->setURI('/M'.$mock->getID());
 226        }
 227      }
 228  
 229      if ($this->id) {
 230        $submit = id(new AphrontFormSubmitControl())
 231          ->addCancelButton('/M'.$this->id)
 232          ->setValue(pht('Save'));
 233      } else {
 234        $submit = id(new AphrontFormSubmitControl())
 235          ->addCancelButton($this->getApplicationURI())
 236          ->setValue(pht('Create'));
 237      }
 238  
 239      $policies = id(new PhabricatorPolicyQuery())
 240        ->setViewer($user)
 241        ->setObject($mock)
 242        ->execute();
 243  
 244      // NOTE: Make this show up correctly on the rendered form.
 245      $mock->setViewPolicy($v_view);
 246      $mock->setEditPolicy($v_edit);
 247  
 248      $handles = id(new PhabricatorHandleQuery())
 249        ->setViewer($user)
 250        ->withPHIDs($v_cc)
 251        ->execute();
 252  
 253      $image_elements = array();
 254      if ($posted_mock_images) {
 255        $display_mock_images = $posted_mock_images;
 256      } else {
 257        $display_mock_images = $mock_images;
 258      }
 259      foreach ($display_mock_images as $mock_image) {
 260        $image_elements[] = id(new PholioUploadedImageView())
 261          ->setUser($user)
 262          ->setImage($mock_image)
 263          ->setReplacesPHID($mock_image->getFilePHID());
 264      }
 265  
 266      $list_id = celerity_generate_unique_node_id();
 267      $drop_id = celerity_generate_unique_node_id();
 268      $order_id = celerity_generate_unique_node_id();
 269  
 270      $list_control = phutil_tag(
 271        'div',
 272        array(
 273          'id' => $list_id,
 274          'class' => 'pholio-edit-list',
 275        ),
 276        $image_elements);
 277  
 278      $drop_control = phutil_tag(
 279        'div',
 280        array(
 281          'id' => $drop_id,
 282          'class' => 'pholio-edit-drop',
 283        ),
 284        'Drag and drop images here to add them to the mock.');
 285  
 286      $order_control = phutil_tag(
 287        'input',
 288        array(
 289          'type' => 'hidden',
 290          'name' => 'imageOrder',
 291          'id' => $order_id,
 292        ));
 293  
 294      Javelin::initBehavior(
 295        'pholio-mock-edit',
 296        array(
 297          'listID' => $list_id,
 298          'dropID' => $drop_id,
 299          'orderID' => $order_id,
 300          'uploadURI' => '/file/dropupload/',
 301          'renderURI' => $this->getApplicationURI('image/upload/'),
 302          'pht' => array(
 303            'uploading' => pht('Uploading Image...'),
 304            'uploaded' => pht('Upload Complete...'),
 305            'undo' => pht('Undo'),
 306            'removed' => pht('This image will be removed from the mock.'),
 307          ),
 308        ));
 309  
 310      if ($v_projects) {
 311        $project_handles = $this->loadViewerHandles($v_projects);
 312      } else {
 313        $project_handles = array();
 314      }
 315  
 316      require_celerity_resource('pholio-edit-css');
 317      $form = id(new AphrontFormView())
 318        ->setUser($user)
 319        ->appendChild($order_control)
 320        ->appendChild(
 321          id(new AphrontFormTextControl())
 322          ->setName('name')
 323          ->setValue($v_name)
 324          ->setLabel(pht('Name'))
 325          ->setError($e_name))
 326        ->appendChild(
 327          id(new PhabricatorRemarkupControl())
 328          ->setName('description')
 329          ->setValue($v_desc)
 330          ->setLabel(pht('Description'))
 331          ->setUser($user));
 332  
 333      if ($this->id) {
 334        $form->appendChild(
 335          id(new AphrontFormSelectControl())
 336          ->setLabel(pht('Status'))
 337          ->setName('status')
 338          ->setValue($mock->getStatus())
 339          ->setOptions($mock->getStatuses()));
 340      } else {
 341        $form->addHiddenInput('status', 'open');
 342      }
 343  
 344      $form
 345        ->appendChild(
 346          id(new AphrontFormTokenizerControl())
 347            ->setLabel(pht('Projects'))
 348            ->setName('projects')
 349            ->setValue($project_handles)
 350            ->setDatasource(new PhabricatorProjectDatasource()))
 351        ->appendChild(
 352          id(new AphrontFormTokenizerControl())
 353            ->setLabel(pht('CC'))
 354            ->setName('cc')
 355            ->setValue($handles)
 356            ->setUser($user)
 357            ->setDatasource(new PhabricatorMetaMTAMailableDatasource()))
 358        ->appendChild(
 359          id(new AphrontFormPolicyControl())
 360            ->setUser($user)
 361            ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
 362            ->setPolicyObject($mock)
 363            ->setPolicies($policies)
 364            ->setName('can_view'))
 365        ->appendChild(
 366          id(new AphrontFormPolicyControl())
 367            ->setUser($user)
 368            ->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
 369            ->setPolicyObject($mock)
 370            ->setPolicies($policies)
 371            ->setName('can_edit'))
 372        ->appendChild(
 373          id(new AphrontFormMarkupControl())
 374            ->setValue($list_control))
 375        ->appendChild(
 376          id(new AphrontFormMarkupControl())
 377            ->setValue($drop_control)
 378            ->setError($e_images))
 379        ->appendChild($submit);
 380  
 381      $form_box = id(new PHUIObjectBoxView())
 382        ->setHeaderText($title)
 383        ->setFormErrors($errors)
 384        ->setForm($form);
 385  
 386      $crumbs = $this->buildApplicationCrumbs();
 387      if (!$is_new) {
 388        $crumbs->addTextCrumb($mock->getMonogram(), '/'.$mock->getMonogram());
 389      }
 390      $crumbs->addTextCrumb($title);
 391  
 392      $content = array(
 393        $crumbs,
 394        $form_box,
 395      );
 396  
 397      return $this->buildApplicationPage(
 398        $content,
 399        array(
 400          'title' => $title,
 401        ));
 402    }
 403  
 404  }


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