[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/releeph/controller/request/ -> ReleephRequestEditController.php (source)

   1  <?php
   2  
   3  final class ReleephRequestEditController extends ReleephBranchController {
   4  
   5    private $requestID;
   6    private $branchID;
   7  
   8    public function willProcessRequest(array $data) {
   9      $this->requestID = idx($data, 'requestID');
  10      $this->branchID = idx($data, 'branchID');
  11    }
  12  
  13    public function processRequest() {
  14      $request = $this->getRequest();
  15      $viewer = $request->getUser();
  16  
  17      if ($this->requestID) {
  18        $pull = id(new ReleephRequestQuery())
  19          ->setViewer($viewer)
  20          ->withIDs(array($this->requestID))
  21          ->requireCapabilities(
  22            array(
  23              PhabricatorPolicyCapability::CAN_VIEW,
  24              PhabricatorPolicyCapability::CAN_EDIT,
  25            ))
  26          ->executeOne();
  27        if (!$pull) {
  28          return new Aphront404Response();
  29        }
  30  
  31        $branch = $pull->getBranch();
  32  
  33        $is_edit = true;
  34      } else {
  35        $branch = id(new ReleephBranchQuery())
  36          ->setViewer($viewer)
  37          ->withIDs(array($this->branchID))
  38          ->executeOne();
  39        if (!$branch) {
  40          return new Aphront404Response();
  41        }
  42  
  43        $pull = id(new ReleephRequest())
  44          ->setRequestUserPHID($viewer->getPHID())
  45          ->setBranchID($branch->getID())
  46          ->setInBranch(0)
  47          ->attachBranch($branch);
  48  
  49        $is_edit = false;
  50      }
  51      $this->setBranch($branch);
  52  
  53      $product = $branch->getProduct();
  54  
  55      $request_identifier = $request->getStr('requestIdentifierRaw');
  56      $e_request_identifier = true;
  57  
  58      // Load all the ReleephFieldSpecifications
  59      $selector = $branch->getProduct()->getReleephFieldSelector();
  60      $fields = $selector->getFieldSpecifications();
  61      foreach ($fields as $field) {
  62        $field
  63          ->setReleephProject($product)
  64          ->setReleephBranch($branch)
  65          ->setReleephRequest($pull);
  66      }
  67  
  68      $field_list = PhabricatorCustomField::getObjectFields(
  69        $pull,
  70        PhabricatorCustomField::ROLE_EDIT);
  71      foreach ($field_list->getFields() as $field) {
  72        $field
  73          ->setReleephProject($product)
  74          ->setReleephBranch($branch)
  75          ->setReleephRequest($pull);
  76      }
  77      $field_list->readFieldsFromStorage($pull);
  78  
  79  
  80      if ($this->branchID) {
  81        $cancel_uri = $this->getApplicationURI('branch/'.$this->branchID.'/');
  82      } else {
  83        $cancel_uri = '/'.$pull->getMonogram();
  84      }
  85  
  86      // Make edits
  87      $errors = array();
  88      if ($request->isFormPost()) {
  89        $xactions = array();
  90  
  91        // The commit-identifier being requested...
  92        if (!$is_edit) {
  93          if ($request_identifier ===
  94            ReleephRequestTypeaheadControl::PLACEHOLDER) {
  95  
  96            $errors[] = 'No commit ID was provided.';
  97            $e_request_identifier = 'Required';
  98          } else {
  99            $pr_commit = null;
 100            $finder = id(new ReleephCommitFinder())
 101              ->setUser($viewer)
 102              ->setReleephProject($product);
 103            try {
 104              $pr_commit = $finder->fromPartial($request_identifier);
 105            } catch (Exception $e) {
 106              $e_request_identifier = 'Invalid';
 107              $errors[] =
 108                "Request {$request_identifier} is probably not a valid commit";
 109              $errors[] = $e->getMessage();
 110            }
 111  
 112            if (!$errors) {
 113              $object_phid = $finder->getRequestedObjectPHID();
 114              if (!$object_phid) {
 115                $object_phid = $pr_commit->getPHID();
 116              }
 117  
 118              $pull->setRequestedObjectPHID($object_phid);
 119            }
 120          }
 121  
 122          if (!$errors) {
 123            $existing = id(new ReleephRequest())
 124              ->loadOneWhere('requestCommitPHID = %s AND branchID = %d',
 125                  $pr_commit->getPHID(), $branch->getID());
 126            if ($existing) {
 127              return id(new AphrontRedirectResponse())
 128                ->setURI('/releeph/request/edit/'.$existing->getID().
 129                         '?existing=1');
 130            }
 131  
 132            $xactions[] = id(new ReleephRequestTransaction())
 133              ->setTransactionType(ReleephRequestTransaction::TYPE_REQUEST)
 134              ->setNewValue($pr_commit->getPHID());
 135  
 136            $xactions[] = id(new ReleephRequestTransaction())
 137              ->setTransactionType(ReleephRequestTransaction::TYPE_USER_INTENT)
 138              // To help hide these implicit intents...
 139              ->setMetadataValue('isRQCreate', true)
 140              ->setMetadataValue('userPHID', $viewer->getPHID())
 141              ->setMetadataValue(
 142                'isAuthoritative',
 143                $product->isAuthoritative($viewer))
 144              ->setNewValue(ReleephRequest::INTENT_WANT);
 145          }
 146        }
 147  
 148        // TODO: This should happen implicitly while building transactions
 149        // instead.
 150        foreach ($field_list->getFields() as $field) {
 151          $field->readValueFromRequest($request);
 152        }
 153  
 154        if (!$errors) {
 155          foreach ($fields as $field) {
 156            if ($field->isEditable()) {
 157              try {
 158                $data = $request->getRequestData();
 159                $value = idx($data, $field->getRequiredStorageKey());
 160                $field->validate($value);
 161                $xactions[] = id(new ReleephRequestTransaction())
 162                  ->setTransactionType(ReleephRequestTransaction::TYPE_EDIT_FIELD)
 163                  ->setMetadataValue('fieldClass', get_class($field))
 164                  ->setNewValue($value);
 165              } catch (ReleephFieldParseException $ex) {
 166                $errors[] = $ex->getMessage();
 167              }
 168            }
 169          }
 170        }
 171  
 172        if (!$errors) {
 173          $editor = id(new ReleephRequestTransactionalEditor())
 174            ->setActor($viewer)
 175            ->setContinueOnNoEffect(true)
 176            ->setContentSourceFromRequest($request);
 177          $editor->applyTransactions($pull, $xactions);
 178          return id(new AphrontRedirectResponse())->setURI($cancel_uri);
 179        }
 180      }
 181  
 182      $handle_phids = array(
 183        $pull->getRequestUserPHID(),
 184        $pull->getRequestCommitPHID(),
 185      );
 186      $handle_phids = array_filter($handle_phids);
 187      if ($handle_phids) {
 188        $handles = id(new PhabricatorHandleQuery())
 189          ->setViewer($viewer)
 190          ->withPHIDs($handle_phids)
 191          ->execute();
 192      } else {
 193        $handles = array();
 194      }
 195  
 196      $age_string = '';
 197      if ($is_edit) {
 198        $age_string = phutil_format_relative_time(
 199          time() - $pull->getDateCreated()).' ago';
 200      }
 201  
 202      // Warn the user if we've been redirected here because we tried to
 203      // re-request something.
 204      $notice_view = null;
 205      if ($request->getInt('existing')) {
 206        $notice_messages = array(
 207          'You are editing an existing pick request!',
 208          hsprintf(
 209            'Requested %s by %s',
 210            $age_string,
 211            $handles[$pull->getRequestUserPHID()]->renderLink()),
 212        );
 213        $notice_view = id(new AphrontErrorView())
 214          ->setSeverity(AphrontErrorView::SEVERITY_NOTICE)
 215          ->setErrors($notice_messages);
 216      }
 217  
 218      $form = id(new AphrontFormView())
 219        ->setUser($viewer);
 220  
 221      if ($is_edit) {
 222        $form
 223          ->appendChild(
 224            id(new AphrontFormMarkupControl())
 225              ->setLabel('Original Commit')
 226              ->setValue(
 227                $handles[$pull->getRequestCommitPHID()]->renderLink()))
 228          ->appendChild(
 229            id(new AphrontFormMarkupControl())
 230              ->setLabel('Requestor')
 231              ->setValue(hsprintf(
 232                '%s %s',
 233                $handles[$pull->getRequestUserPHID()]->renderLink(),
 234                $age_string)));
 235      } else {
 236        $origin = null;
 237        $diff_rev_id = $request->getStr('D');
 238        if ($diff_rev_id) {
 239          $diff_rev = id(new DifferentialRevisionQuery())
 240            ->setViewer($viewer)
 241            ->withIDs(array($diff_rev_id))
 242            ->executeOne();
 243          $origin = '/D'.$diff_rev->getID();
 244          $title = sprintf(
 245            'D%d: %s',
 246            $diff_rev_id,
 247            $diff_rev->getTitle());
 248          $form
 249            ->addHiddenInput('requestIdentifierRaw', 'D'.$diff_rev_id)
 250            ->appendChild(
 251              id(new AphrontFormStaticControl())
 252                ->setLabel('Diff')
 253                ->setValue($title));
 254        } else {
 255          $origin = $branch->getURI();
 256          $repo = $product->getRepository();
 257          $branch_cut_point = id(new PhabricatorRepositoryCommit())
 258            ->loadOneWhere(
 259                'phid = %s',
 260                $branch->getCutPointCommitPHID());
 261          $form->appendChild(
 262            id(new ReleephRequestTypeaheadControl())
 263              ->setName('requestIdentifierRaw')
 264              ->setLabel('Commit ID')
 265              ->setRepo($repo)
 266              ->setValue($request_identifier)
 267              ->setError($e_request_identifier)
 268              ->setStartTime($branch_cut_point->getEpoch())
 269              ->setCaption(
 270                'Start typing to autocomplete on commit title, '.
 271                'or give a Phabricator commit identifier like rFOO1234'));
 272        }
 273      }
 274  
 275      $field_list->appendFieldsToForm($form);
 276  
 277      $crumbs = $this->buildApplicationCrumbs();
 278  
 279      if ($is_edit) {
 280        $title = pht('Edit Pull Request');
 281        $submit_name = pht('Save');
 282  
 283        $crumbs->addTextCrumb($pull->getMonogram(), '/'.$pull->getMonogram());
 284        $crumbs->addTextCrumb(pht('Edit'));
 285      } else {
 286        $title = pht('Create Pull Request');
 287        $submit_name = pht('Create Pull Request');
 288  
 289        $crumbs->addTextCrumb(pht('New Pull Request'));
 290      }
 291  
 292      $form->appendChild(
 293        id(new AphrontFormSubmitControl())
 294          ->addCancelButton($cancel_uri, 'Cancel')
 295          ->setValue($submit_name));
 296  
 297      $box = id(new PHUIObjectBoxView())
 298        ->setHeaderText($title)
 299        ->setFormErrors($errors)
 300        ->appendChild($form);
 301  
 302      return $this->buildApplicationPage(
 303        array(
 304          $crumbs,
 305          $notice_view,
 306          $box,
 307        ),
 308        array(
 309          'title' => $title,
 310        ));
 311    }
 312  }


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