[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phriction/controller/ -> PhrictionEditController.php (source)

   1  <?php
   2  
   3  final class PhrictionEditController
   4    extends PhrictionController {
   5  
   6    private $id;
   7  
   8    public function willProcessRequest(array $data) {
   9      $this->id = idx($data, 'id');
  10    }
  11  
  12    public function processRequest() {
  13  
  14      $request = $this->getRequest();
  15      $user = $request->getUser();
  16  
  17      $current_version = null;
  18      if ($this->id) {
  19        $document = id(new PhrictionDocumentQuery())
  20          ->setViewer($user)
  21          ->withIDs(array($this->id))
  22          ->needContent(true)
  23          ->requireCapabilities(
  24            array(
  25              PhabricatorPolicyCapability::CAN_VIEW,
  26              PhabricatorPolicyCapability::CAN_EDIT,
  27            ))
  28          ->executeOne();
  29        if (!$document) {
  30          return new Aphront404Response();
  31        }
  32  
  33        $current_version = $document->getContent()->getVersion();
  34  
  35        $revert = $request->getInt('revert');
  36        if ($revert) {
  37          $content = id(new PhrictionContent())->loadOneWhere(
  38            'documentID = %d AND version = %d',
  39            $document->getID(),
  40            $revert);
  41          if (!$content) {
  42            return new Aphront404Response();
  43          }
  44        } else {
  45          $content = $document->getContent();
  46        }
  47  
  48      } else {
  49        $slug = $request->getStr('slug');
  50        $slug = PhabricatorSlug::normalize($slug);
  51        if (!$slug) {
  52          return new Aphront404Response();
  53        }
  54  
  55        $document = id(new PhrictionDocumentQuery())
  56          ->setViewer($user)
  57          ->withSlugs(array($slug))
  58          ->needContent(true)
  59          ->executeOne();
  60  
  61        if ($document) {
  62          $content = $document->getContent();
  63          $current_version = $content->getVersion();
  64        } else {
  65          $document = PhrictionDocument::initializeNewDocument($user, $slug);
  66          $content = $document->getContent();
  67        }
  68      }
  69  
  70      if ($request->getBool('nodraft')) {
  71        $draft = null;
  72        $draft_key = null;
  73      } else {
  74        if ($document->getPHID()) {
  75          $draft_key = $document->getPHID().':'.$content->getVersion();
  76        } else {
  77          $draft_key = 'phriction:'.$content->getSlug();
  78        }
  79        $draft = id(new PhabricatorDraft())->loadOneWhere(
  80          'authorPHID = %s AND draftKey = %s',
  81          $user->getPHID(),
  82          $draft_key);
  83      }
  84  
  85      if ($draft &&
  86        strlen($draft->getDraft()) &&
  87        ($draft->getDraft() != $content->getContent())) {
  88        $content_text = $draft->getDraft();
  89  
  90        $discard = phutil_tag(
  91          'a',
  92          array(
  93            'href' => $request->getRequestURI()->alter('nodraft', true),
  94          ),
  95          pht('discard this draft'));
  96  
  97        $draft_note = new AphrontErrorView();
  98        $draft_note->setSeverity(AphrontErrorView::SEVERITY_NOTICE);
  99        $draft_note->setTitle('Recovered Draft');
 100        $draft_note->appendChild(hsprintf(
 101          '<p>Showing a saved draft of your edits, you can %s.</p>',
 102          $discard));
 103      } else {
 104        $content_text = $content->getContent();
 105        $draft_note = null;
 106      }
 107  
 108      require_celerity_resource('phriction-document-css');
 109  
 110      $e_title = true;
 111      $e_content = true;
 112      $validation_exception = null;
 113      $notes = null;
 114      $title = $content->getTitle();
 115      $overwrite = false;
 116  
 117      if ($request->isFormPost()) {
 118  
 119        $title = $request->getStr('title');
 120        $content_text = $request->getStr('content');
 121        $notes = $request->getStr('description');
 122        $current_version = $request->getInt('contentVersion');
 123        $v_view = $request->getStr('viewPolicy');
 124        $v_edit = $request->getStr('editPolicy');
 125  
 126        $xactions = array();
 127        $xactions[] = id(new PhrictionTransaction())
 128          ->setTransactionType(PhrictionTransaction::TYPE_TITLE)
 129          ->setNewValue($title);
 130        $xactions[] = id(new PhrictionTransaction())
 131          ->setTransactionType(PhrictionTransaction::TYPE_CONTENT)
 132          ->setNewValue($content_text);
 133        $xactions[] = id(new PhrictionTransaction())
 134          ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
 135          ->setNewValue($v_view);
 136        $xactions[] = id(new PhrictionTransaction())
 137          ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
 138          ->setNewValue($v_edit);
 139  
 140        $editor = id(new PhrictionTransactionEditor())
 141          ->setActor($user)
 142          ->setContentSourceFromRequest($request)
 143          ->setContinueOnNoEffect(true)
 144          ->setDescription($notes)
 145          ->setProcessContentVersionError(!$request->getBool('overwrite'))
 146          ->setContentVersion($current_version);
 147  
 148        try {
 149          $editor->applyTransactions($document, $xactions);
 150  
 151          if ($draft) {
 152            $draft->delete();
 153          }
 154  
 155          $uri = PhrictionDocument::getSlugURI($document->getSlug());
 156          return id(new AphrontRedirectResponse())->setURI($uri);
 157        } catch (PhabricatorApplicationTransactionValidationException $ex) {
 158          $validation_exception = $ex;
 159          $e_title = $ex->getShortMessage(
 160            PhrictionTransaction::TYPE_TITLE);
 161          $e_content = $ex->getShortMessage(
 162            PhrictionTransaction::TYPE_CONTENT);
 163  
 164          // if we're not supposed to process the content version error, then
 165          // overwrite that content...!
 166          if (!$editor->getProcessContentVersionError()) {
 167            $overwrite = true;
 168          }
 169  
 170          $document->setViewPolicy($v_view);
 171          $document->setEditPolicy($v_edit);
 172        }
 173      }
 174  
 175      if ($document->getID()) {
 176        $panel_header = pht('Edit Phriction Document');
 177        $page_title = pht('Edit Document');
 178        if ($overwrite) {
 179          $submit_button = pht('Overwrite Changes');
 180        } else {
 181          $submit_button = pht('Save Changes');
 182        }
 183      } else {
 184        $panel_header = pht('Create New Phriction Document');
 185        $submit_button = pht('Create Document');
 186        $page_title = pht('Create Document');
 187      }
 188  
 189      $uri = $document->getSlug();
 190      $uri = PhrictionDocument::getSlugURI($uri);
 191      $uri = PhabricatorEnv::getProductionURI($uri);
 192  
 193      $cancel_uri = PhrictionDocument::getSlugURI($document->getSlug());
 194  
 195      $policies = id(new PhabricatorPolicyQuery())
 196        ->setViewer($user)
 197        ->setObject($document)
 198        ->execute();
 199      $view_capability = PhabricatorPolicyCapability::CAN_VIEW;
 200      $edit_capability = PhabricatorPolicyCapability::CAN_EDIT;
 201  
 202      $form = id(new AphrontFormView())
 203        ->setUser($user)
 204        ->addHiddenInput('slug', $document->getSlug())
 205        ->addHiddenInput('nodraft', $request->getBool('nodraft'))
 206        ->addHiddenInput('contentVersion', $current_version)
 207        ->addHiddenInput('overwrite', $overwrite)
 208        ->appendChild(
 209          id(new AphrontFormTextControl())
 210            ->setLabel(pht('Title'))
 211            ->setValue($title)
 212            ->setError($e_title)
 213            ->setName('title'))
 214        ->appendChild(
 215          id(new AphrontFormStaticControl())
 216            ->setLabel(pht('URI'))
 217            ->setValue($uri))
 218        ->appendChild(
 219          id(new PhabricatorRemarkupControl())
 220            ->setLabel(pht('Content'))
 221            ->setValue($content_text)
 222            ->setError($e_content)
 223            ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
 224            ->setName('content')
 225            ->setID('document-textarea')
 226            ->setUser($user))
 227        ->appendChild(
 228          id(new AphrontFormPolicyControl())
 229            ->setName('viewPolicy')
 230            ->setPolicyObject($document)
 231            ->setCapability($view_capability)
 232            ->setPolicies($policies)
 233            ->setCaption(
 234              $document->describeAutomaticCapability($view_capability)))
 235        ->appendChild(
 236          id(new AphrontFormPolicyControl())
 237            ->setName('editPolicy')
 238            ->setPolicyObject($document)
 239            ->setCapability($edit_capability)
 240            ->setPolicies($policies)
 241            ->setCaption(
 242              $document->describeAutomaticCapability($edit_capability)))
 243        ->appendChild(
 244          id(new AphrontFormTextControl())
 245            ->setLabel(pht('Edit Notes'))
 246            ->setValue($notes)
 247            ->setError(null)
 248            ->setName('description'))
 249        ->appendChild(
 250          id(new AphrontFormSubmitControl())
 251            ->addCancelButton($cancel_uri)
 252            ->setValue($submit_button));
 253  
 254      $form_box = id(new PHUIObjectBoxView())
 255        ->setHeaderText($panel_header)
 256        ->setValidationException($validation_exception)
 257        ->setForm($form);
 258  
 259      $preview = id(new PHUIRemarkupPreviewPanel())
 260        ->setHeader(pht('Document Preview'))
 261        ->setPreviewURI('/phriction/preview/')
 262        ->setControlID('document-textarea')
 263        ->setSkin('document');
 264  
 265      $crumbs = $this->buildApplicationCrumbs();
 266      if ($document->getID()) {
 267        $crumbs->addTextCrumb(
 268          $content->getTitle(),
 269          PhrictionDocument::getSlugURI($document->getSlug()));
 270        $crumbs->addTextCrumb(pht('Edit'));
 271      } else {
 272        $crumbs->addTextCrumb(pht('Create'));
 273      }
 274  
 275      return $this->buildApplicationPage(
 276        array(
 277          $crumbs,
 278          $draft_note,
 279          $form_box,
 280          $preview,
 281        ),
 282        array(
 283          'title'   => $page_title,
 284        ));
 285    }
 286  
 287  }


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