[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/ponder/controller/ -> PonderQuestionEditController.php (source)

   1  <?php
   2  
   3  final class PonderQuestionEditController extends PonderController {
   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        $question = id(new PonderQuestionQuery())
  17          ->setViewer($user)
  18          ->withIDs(array($this->id))
  19          ->requireCapabilities(
  20            array(
  21              PhabricatorPolicyCapability::CAN_VIEW,
  22              PhabricatorPolicyCapability::CAN_EDIT,
  23            ))
  24          ->executeOne();
  25        if (!$question) {
  26          return new Aphront404Response();
  27        }
  28        $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
  29          $question->getPHID(),
  30          PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  31        $v_projects = array_reverse($v_projects);
  32      } else {
  33        $question = id(new PonderQuestion())
  34          ->setStatus(PonderQuestionStatus::STATUS_OPEN)
  35          ->setAuthorPHID($user->getPHID())
  36          ->setVoteCount(0)
  37          ->setAnswerCount(0)
  38          ->setHeat(0.0);
  39        $v_projects = array();
  40      }
  41  
  42      $v_title = $question->getTitle();
  43      $v_content = $question->getContent();
  44  
  45      $errors = array();
  46      $e_title = true;
  47      if ($request->isFormPost()) {
  48        $v_title = $request->getStr('title');
  49        $v_content = $request->getStr('content');
  50        $v_projects = $request->getArr('projects');
  51  
  52        $len = phutil_utf8_strlen($v_title);
  53        if ($len < 1) {
  54          $errors[] = pht('Title must not be empty.');
  55          $e_title = pht('Required');
  56        } else if ($len > 255) {
  57          $errors[] = pht('Title is too long.');
  58          $e_title = pht('Too Long');
  59        }
  60  
  61        if (!$errors) {
  62          $template = id(new PonderQuestionTransaction());
  63          $xactions = array();
  64  
  65          $xactions[] = id(clone $template)
  66            ->setTransactionType(PonderQuestionTransaction::TYPE_TITLE)
  67            ->setNewValue($v_title);
  68  
  69          $xactions[] = id(clone $template)
  70            ->setTransactionType(PonderQuestionTransaction::TYPE_CONTENT)
  71            ->setNewValue($v_content);
  72  
  73          $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
  74          $xactions[] = id(new PonderQuestionTransaction())
  75            ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
  76            ->setMetadataValue('edge:type', $proj_edge_type)
  77            ->setNewValue(array('=' => array_fuse($v_projects)));
  78  
  79          $editor = id(new PonderQuestionEditor())
  80            ->setActor($user)
  81            ->setContentSourceFromRequest($request)
  82            ->setContinueOnNoEffect(true);
  83  
  84          $editor->applyTransactions($question, $xactions);
  85  
  86          return id(new AphrontRedirectResponse())
  87            ->setURI('/Q'.$question->getID());
  88        }
  89      }
  90  
  91      $form = id(new AphrontFormView())
  92        ->setUser($user)
  93        ->appendChild(
  94          id(new AphrontFormTextControl())
  95            ->setLabel(pht('Question'))
  96            ->setName('title')
  97            ->setValue($v_title)
  98            ->setError($e_title))
  99        ->appendChild(
 100          id(new PhabricatorRemarkupControl())
 101            ->setUser($user)
 102            ->setName('content')
 103            ->setID('content')
 104            ->setValue($v_content)
 105            ->setLabel(pht('Description'))
 106            ->setUser($user));
 107  
 108      if ($v_projects) {
 109        $project_handles = $this->loadViewerHandles($v_projects);
 110      } else {
 111        $project_handles = array();
 112      }
 113  
 114      $form->appendChild(
 115        id(new AphrontFormTokenizerControl())
 116          ->setLabel(pht('Projects'))
 117          ->setName('projects')
 118          ->setValue($project_handles)
 119          ->setDatasource(new PhabricatorProjectDatasource()));
 120  
 121      $form ->appendChild(
 122        id(new AphrontFormSubmitControl())
 123          ->addCancelButton($this->getApplicationURI())
 124          ->setValue(pht('Ask Away!')));
 125  
 126      $preview = id(new PHUIRemarkupPreviewPanel())
 127        ->setHeader(pht('Question Preview'))
 128        ->setControlID('content')
 129        ->setPreviewURI($this->getApplicationURI('preview/'));
 130  
 131      $form_box = id(new PHUIObjectBoxView())
 132        ->setHeaderText(pht('Ask New Question'))
 133        ->setFormErrors($errors)
 134        ->setForm($form);
 135  
 136      $crumbs = $this->buildApplicationCrumbs();
 137  
 138      $id = $question->getID();
 139      if ($id) {
 140        $crumbs->addTextCrumb("Q{$id}", "/Q{$id}");
 141        $crumbs->addTextCrumb(pht('Edit'));
 142      } else {
 143        $crumbs->addTextCrumb(pht('Ask Question'));
 144      }
 145  
 146      return $this->buildApplicationPage(
 147        array(
 148          $crumbs,
 149          $form_box,
 150          $preview,
 151        ),
 152        array(
 153          'title'  => pht('Ask New Question'),
 154        ));
 155    }
 156  
 157  }


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