[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/paste/controller/ -> PhabricatorPasteEditController.php (source)

   1  <?php
   2  
   3  final class PhabricatorPasteEditController extends PhabricatorPasteController {
   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      $parent = null;
  16      $parent_id = null;
  17      if (!$this->id) {
  18        $is_create = true;
  19  
  20        $paste = PhabricatorPaste::initializeNewPaste($user);
  21  
  22        $parent_id = $request->getStr('parent');
  23        if ($parent_id) {
  24          // NOTE: If the Paste is forked from a paste which the user no longer
  25          // has permission to see, we still let them edit it.
  26          $parent = id(new PhabricatorPasteQuery())
  27            ->setViewer($user)
  28            ->withIDs(array($parent_id))
  29            ->needContent(true)
  30            ->needRawContent(true)
  31            ->execute();
  32          $parent = head($parent);
  33  
  34          if ($parent) {
  35            $paste->setParentPHID($parent->getPHID());
  36            $paste->setViewPolicy($parent->getViewPolicy());
  37          }
  38        }
  39  
  40        $paste->setAuthorPHID($user->getPHID());
  41        $paste->attachRawContent('');
  42      } else {
  43        $is_create = false;
  44  
  45        $paste = id(new PhabricatorPasteQuery())
  46          ->setViewer($user)
  47          ->requireCapabilities(
  48            array(
  49              PhabricatorPolicyCapability::CAN_VIEW,
  50              PhabricatorPolicyCapability::CAN_EDIT,
  51            ))
  52          ->withIDs(array($this->id))
  53          ->needRawContent(true)
  54          ->executeOne();
  55        if (!$paste) {
  56          return new Aphront404Response();
  57        }
  58      }
  59  
  60      $text = null;
  61      $e_text = true;
  62      $errors = array();
  63      if ($is_create && $parent) {
  64        $v_title = pht('Fork of %s', $parent->getFullName());
  65        $v_language = $parent->getLanguage();
  66        $v_text = $parent->getRawContent();
  67      } else {
  68        $v_title = $paste->getTitle();
  69        $v_language = $paste->getLanguage();
  70        $v_text = $paste->getRawContent();
  71      }
  72      $v_policy = $paste->getViewPolicy();
  73  
  74      if ($is_create) {
  75        $v_projects = array();
  76      } else {
  77        $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
  78          $paste->getPHID(),
  79          PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  80        $v_projects = array_reverse($v_projects);
  81      }
  82  
  83      if ($request->isFormPost()) {
  84        $xactions = array();
  85  
  86        $v_text = $request->getStr('text');
  87        if (!strlen($v_text)) {
  88          $e_text = pht('Required');
  89          $errors[] = pht('The paste may not be blank.');
  90        } else {
  91          $e_text = null;
  92        }
  93  
  94        $v_title = $request->getStr('title');
  95        $v_language = $request->getStr('language');
  96        $v_policy = $request->getStr('can_view');
  97        $v_projects = $request->getArr('projects');
  98  
  99        // NOTE: The author is the only editor and can always view the paste,
 100        // so it's impossible for them to choose an invalid policy.
 101  
 102        if (!$errors) {
 103          if ($is_create || ($v_text !== $paste->getRawContent())) {
 104            $file = PhabricatorPasteEditor::initializeFileForPaste(
 105              $user,
 106              $v_title,
 107              $v_text);
 108  
 109            $xactions[] = id(new PhabricatorPasteTransaction())
 110              ->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT)
 111              ->setNewValue($file->getPHID());
 112          }
 113  
 114          $xactions[] = id(new PhabricatorPasteTransaction())
 115            ->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
 116            ->setNewValue($v_title);
 117          $xactions[] = id(new PhabricatorPasteTransaction())
 118            ->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)
 119            ->setNewValue($v_language);
 120          $xactions[] = id(new PhabricatorPasteTransaction())
 121            ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
 122            ->setNewValue($v_policy);
 123  
 124          $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
 125          $xactions[] = id(new PhabricatorPasteTransaction())
 126            ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
 127            ->setMetadataValue('edge:type', $proj_edge_type)
 128            ->setNewValue(array('=' => array_fuse($v_projects)));
 129  
 130          $editor = id(new PhabricatorPasteEditor())
 131            ->setActor($user)
 132            ->setContentSourceFromRequest($request)
 133            ->setContinueOnNoEffect(true);
 134          $xactions = $editor->applyTransactions($paste, $xactions);
 135          return id(new AphrontRedirectResponse())->setURI($paste->getURI());
 136        } else {
 137          // make sure we update policy so its correctly populated to what
 138          // the user chose
 139          $paste->setViewPolicy($v_policy);
 140        }
 141      }
 142  
 143      $form = new AphrontFormView();
 144  
 145      $langs = array(
 146        '' => pht('(Detect From Filename in Title)'),
 147      ) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
 148  
 149      $form
 150        ->setUser($user)
 151        ->addHiddenInput('parent', $parent_id)
 152        ->appendChild(
 153          id(new AphrontFormTextControl())
 154            ->setLabel(pht('Title'))
 155            ->setValue($v_title)
 156            ->setName('title'))
 157        ->appendChild(
 158          id(new AphrontFormSelectControl())
 159            ->setLabel(pht('Language'))
 160            ->setName('language')
 161            ->setValue($v_language)
 162            ->setOptions($langs));
 163  
 164      $policies = id(new PhabricatorPolicyQuery())
 165        ->setViewer($user)
 166        ->setObject($paste)
 167        ->execute();
 168  
 169      $form->appendChild(
 170        id(new AphrontFormPolicyControl())
 171          ->setUser($user)
 172          ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
 173          ->setPolicyObject($paste)
 174          ->setPolicies($policies)
 175          ->setName('can_view'));
 176  
 177  
 178      if ($v_projects) {
 179        $project_handles = $this->loadViewerHandles($v_projects);
 180      } else {
 181        $project_handles = array();
 182      }
 183  
 184      $form->appendChild(
 185        id(new AphrontFormTokenizerControl())
 186          ->setLabel(pht('Projects'))
 187          ->setName('projects')
 188          ->setValue($project_handles)
 189          ->setDatasource(new PhabricatorProjectDatasource()));
 190  
 191      $form
 192        ->appendChild(
 193          id(new AphrontFormTextAreaControl())
 194            ->setLabel(pht('Text'))
 195            ->setError($e_text)
 196            ->setValue($v_text)
 197            ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
 198            ->setCustomClass('PhabricatorMonospaced')
 199            ->setName('text'));
 200  
 201      $submit = new AphrontFormSubmitControl();
 202  
 203      if (!$is_create) {
 204        $submit->addCancelButton($paste->getURI());
 205        $submit->setValue(pht('Save Paste'));
 206        $title = pht('Edit %s', $paste->getFullName());
 207        $short = pht('Edit');
 208      } else {
 209        $submit->setValue(pht('Create Paste'));
 210        $title = pht('Create New Paste');
 211        $short = pht('Create');
 212      }
 213  
 214      $form->appendChild($submit);
 215  
 216      $form_box = id(new PHUIObjectBoxView())
 217        ->setHeaderText($title)
 218        ->setFormErrors($errors)
 219        ->setForm($form);
 220  
 221      $crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
 222      if (!$is_create) {
 223        $crumbs->addTextCrumb('P'.$paste->getID(), '/P'.$paste->getID());
 224      }
 225      $crumbs->addTextCrumb($short);
 226  
 227      return $this->buildApplicationPage(
 228        array(
 229          $crumbs,
 230          $form_box,
 231        ),
 232        array(
 233          'title' => $title,
 234        ));
 235    }
 236  
 237  }


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