2012-08-10 10:44:04 -07:00
|
|
|
<?php
|
|
|
|
|
2013-07-28 15:02:18 -07:00
|
|
|
final class PonderQuestionEditController extends PonderController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
2012-08-10 10:44:04 -07:00
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2013-07-28 15:02:18 -07:00
|
|
|
if ($this->id) {
|
|
|
|
$question = id(new PonderQuestionQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
|
|
|
if (!$question) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2014-06-10 11:18:03 -07:00
|
|
|
$v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
|
|
|
|
$question->getPHID(),
|
2014-07-17 15:42:19 -07:00
|
|
|
PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
|
2014-06-10 11:18:03 -07:00
|
|
|
$v_projects = array_reverse($v_projects);
|
2013-07-28 15:02:18 -07:00
|
|
|
} else {
|
|
|
|
$question = id(new PonderQuestion())
|
|
|
|
->setStatus(PonderQuestionStatus::STATUS_OPEN)
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setVoteCount(0)
|
|
|
|
->setAnswerCount(0)
|
|
|
|
->setHeat(0.0);
|
2014-06-10 11:18:03 -07:00
|
|
|
$v_projects = array();
|
2013-07-28 15:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$v_title = $question->getTitle();
|
|
|
|
$v_content = $question->getContent();
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2012-09-30 20:09:18 -07:00
|
|
|
$errors = array();
|
|
|
|
$e_title = true;
|
|
|
|
if ($request->isFormPost()) {
|
2013-07-28 15:02:18 -07:00
|
|
|
$v_title = $request->getStr('title');
|
|
|
|
$v_content = $request->getStr('content');
|
2014-06-10 11:18:03 -07:00
|
|
|
$v_projects = $request->getArr('projects');
|
2012-09-30 20:09:18 -07:00
|
|
|
|
2013-07-28 15:02:18 -07:00
|
|
|
$len = phutil_utf8_strlen($v_title);
|
2012-09-30 20:09:18 -07:00
|
|
|
if ($len < 1) {
|
|
|
|
$errors[] = pht('Title must not be empty.');
|
|
|
|
$e_title = pht('Required');
|
|
|
|
} else if ($len > 255) {
|
|
|
|
$errors[] = pht('Title is too long.');
|
|
|
|
$e_title = pht('Too Long');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
2013-07-28 15:02:18 -07:00
|
|
|
$template = id(new PonderQuestionTransaction());
|
|
|
|
$xactions = array();
|
2012-09-30 20:09:18 -07:00
|
|
|
|
2013-07-28 15:02:18 -07:00
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PonderQuestionTransaction::TYPE_TITLE)
|
|
|
|
->setNewValue($v_title);
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PonderQuestionTransaction::TYPE_CONTENT)
|
|
|
|
->setNewValue($v_content);
|
|
|
|
|
2014-07-17 15:42:19 -07:00
|
|
|
$proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
|
2014-06-10 11:18:03 -07:00
|
|
|
$xactions[] = id(new PonderQuestionTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
|
|
|
|
->setMetadataValue('edge:type', $proj_edge_type)
|
|
|
|
->setNewValue(array('=' => array_fuse($v_projects)));
|
|
|
|
|
2013-07-28 15:02:18 -07:00
|
|
|
$editor = id(new PonderQuestionEditor())
|
2012-10-10 10:18:23 -07:00
|
|
|
->setActor($user)
|
2013-07-28 15:02:18 -07:00
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
|
|
|
|
$editor->applyTransactions($question, $xactions);
|
2012-09-30 20:09:18 -07:00
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/Q'.$question->getID());
|
|
|
|
}
|
|
|
|
}
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2012-09-30 20:09:18 -07:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2012-08-10 10:44:04 -07:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2012-09-30 20:09:18 -07:00
|
|
|
->setLabel(pht('Question'))
|
2012-08-10 10:44:04 -07:00
|
|
|
->setName('title')
|
2013-07-28 15:02:18 -07:00
|
|
|
->setValue($v_title)
|
2012-09-30 20:09:18 -07:00
|
|
|
->setError($e_title))
|
2012-08-10 10:44:04 -07:00
|
|
|
->appendChild(
|
2012-09-19 12:27:28 -07:00
|
|
|
id(new PhabricatorRemarkupControl())
|
2014-11-24 15:25:25 -08:00
|
|
|
->setUser($user)
|
2012-08-10 10:44:04 -07:00
|
|
|
->setName('content')
|
|
|
|
->setID('content')
|
2013-07-28 15:02:18 -07:00
|
|
|
->setValue($v_content)
|
2012-11-27 14:06:31 -08:00
|
|
|
->setLabel(pht('Description'))
|
2014-06-10 11:18:03 -07:00
|
|
|
->setUser($user));
|
|
|
|
|
|
|
|
if ($v_projects) {
|
|
|
|
$project_handles = $this->loadViewerHandles($v_projects);
|
|
|
|
} else {
|
|
|
|
$project_handles = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormTokenizerControl())
|
|
|
|
->setLabel(pht('Projects'))
|
|
|
|
->setName('projects')
|
|
|
|
->setValue($project_handles)
|
2014-07-10 17:28:29 -07:00
|
|
|
->setDatasource(new PhabricatorProjectDatasource()));
|
2014-06-10 11:18:03 -07:00
|
|
|
|
|
|
|
$form ->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->addCancelButton($this->getApplicationURI())
|
|
|
|
->setValue(pht('Ask Away!')));
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2013-08-05 10:47:06 -07:00
|
|
|
$preview = id(new PHUIRemarkupPreviewPanel())
|
|
|
|
->setHeader(pht('Question Preview'))
|
|
|
|
->setControlID('content')
|
|
|
|
->setPreviewURI($this->getApplicationURI('preview/'));
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2013-09-25 11:23:29 -07:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2013-08-26 11:53:11 -07:00
|
|
|
->setHeaderText(pht('Ask New Question'))
|
2014-01-10 09:17:37 -08:00
|
|
|
->setFormErrors($errors)
|
2013-08-26 11:53:11 -07:00
|
|
|
->setForm($form);
|
|
|
|
|
2013-07-18 12:40:51 -07:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-07-28 15:02:18 -07:00
|
|
|
|
|
|
|
$id = $question->getID();
|
|
|
|
if ($id) {
|
2013-12-18 17:47:34 -08:00
|
|
|
$crumbs->addTextCrumb("Q{$id}", "/Q{$id}");
|
|
|
|
$crumbs->addTextCrumb(pht('Edit'));
|
2013-07-28 15:02:18 -07:00
|
|
|
} else {
|
2013-12-18 17:47:34 -08:00
|
|
|
$crumbs->addTextCrumb(pht('Ask Question'));
|
2013-07-28 15:02:18 -07:00
|
|
|
}
|
2012-09-30 20:09:18 -07:00
|
|
|
|
2013-07-18 12:40:51 -07:00
|
|
|
return $this->buildApplicationPage(
|
2012-09-30 20:09:18 -07:00
|
|
|
array(
|
2013-07-18 12:40:51 -07:00
|
|
|
$crumbs,
|
2013-08-26 11:53:11 -07:00
|
|
|
$form_box,
|
2012-09-30 20:09:18 -07:00
|
|
|
$preview,
|
2013-07-18 12:40:51 -07:00
|
|
|
),
|
2012-09-30 20:09:18 -07:00
|
|
|
array(
|
2013-08-26 11:53:11 -07:00
|
|
|
'title' => pht('Ask New Question'),
|
2013-02-19 13:33:10 -08:00
|
|
|
));
|
2012-08-10 10:44:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|