2012-08-10 19:44:04 +02:00
|
|
|
<?php
|
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
final class PonderQuestionEditController extends PonderController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
2012-08-10 19:44:04 +02:00
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2013-07-29 00:02:18 +02: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 20:18:03 +02:00
|
|
|
$v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
|
|
|
|
$question->getPHID(),
|
|
|
|
PhabricatorEdgeConfig::TYPE_OBJECT_HAS_PROJECT);
|
|
|
|
$v_projects = array_reverse($v_projects);
|
2013-07-29 00:02:18 +02:00
|
|
|
} else {
|
|
|
|
$question = id(new PonderQuestion())
|
|
|
|
->setStatus(PonderQuestionStatus::STATUS_OPEN)
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setVoteCount(0)
|
|
|
|
->setAnswerCount(0)
|
|
|
|
->setHeat(0.0);
|
2014-06-10 20:18:03 +02:00
|
|
|
$v_projects = array();
|
2013-07-29 00:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$v_title = $question->getTitle();
|
|
|
|
$v_content = $question->getContent();
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:09:18 +02:00
|
|
|
$errors = array();
|
|
|
|
$e_title = true;
|
|
|
|
if ($request->isFormPost()) {
|
2013-07-29 00:02:18 +02:00
|
|
|
$v_title = $request->getStr('title');
|
|
|
|
$v_content = $request->getStr('content');
|
2014-06-10 20:18:03 +02:00
|
|
|
$v_projects = $request->getArr('projects');
|
2012-10-01 05:09:18 +02:00
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
$len = phutil_utf8_strlen($v_title);
|
2012-10-01 05:09:18 +02: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-29 00:02:18 +02:00
|
|
|
$template = id(new PonderQuestionTransaction());
|
|
|
|
$xactions = array();
|
2012-10-01 05:09:18 +02:00
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PonderQuestionTransaction::TYPE_TITLE)
|
|
|
|
->setNewValue($v_title);
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PonderQuestionTransaction::TYPE_CONTENT)
|
|
|
|
->setNewValue($v_content);
|
|
|
|
|
2014-06-10 20:18:03 +02:00
|
|
|
$proj_edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_PROJECT;
|
|
|
|
$xactions[] = id(new PonderQuestionTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
|
|
|
|
->setMetadataValue('edge:type', $proj_edge_type)
|
|
|
|
->setNewValue(array('=' => array_fuse($v_projects)));
|
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
$editor = id(new PonderQuestionEditor())
|
2012-10-10 19:18:23 +02:00
|
|
|
->setActor($user)
|
2013-07-29 00:02:18 +02:00
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
|
|
|
|
$editor->applyTransactions($question, $xactions);
|
2012-10-01 05:09:18 +02:00
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/Q'.$question->getID());
|
|
|
|
}
|
|
|
|
}
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2012-10-01 05:09:18 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2012-08-10 19:44:04 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2012-10-01 05:09:18 +02:00
|
|
|
->setLabel(pht('Question'))
|
2012-08-10 19:44:04 +02:00
|
|
|
->setName('title')
|
2013-07-29 00:02:18 +02:00
|
|
|
->setValue($v_title)
|
2012-10-01 05:09:18 +02:00
|
|
|
->setError($e_title))
|
2012-08-10 19:44:04 +02:00
|
|
|
->appendChild(
|
2012-09-19 21:27:28 +02:00
|
|
|
id(new PhabricatorRemarkupControl())
|
2012-08-10 19:44:04 +02:00
|
|
|
->setName('content')
|
|
|
|
->setID('content')
|
2013-07-29 00:02:18 +02:00
|
|
|
->setValue($v_content)
|
2012-11-27 23:06:31 +01:00
|
|
|
->setLabel(pht('Description'))
|
2014-06-10 20:18:03 +02: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)
|
|
|
|
->setDatasource('/typeahead/common/projects/'));
|
|
|
|
|
|
|
|
$form ->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->addCancelButton($this->getApplicationURI())
|
|
|
|
->setValue(pht('Ask Away!')));
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2013-08-05 19:47:06 +02:00
|
|
|
$preview = id(new PHUIRemarkupPreviewPanel())
|
|
|
|
->setHeader(pht('Question Preview'))
|
|
|
|
->setControlID('content')
|
|
|
|
->setPreviewURI($this->getApplicationURI('preview/'));
|
2012-08-10 19:44:04 +02:00
|
|
|
|
2013-09-25 20:23:29 +02:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2013-08-26 20:53:11 +02:00
|
|
|
->setHeaderText(pht('Ask New Question'))
|
2014-01-10 18:17:37 +01:00
|
|
|
->setFormErrors($errors)
|
2013-08-26 20:53:11 +02:00
|
|
|
->setForm($form);
|
|
|
|
|
2013-07-18 21:40:51 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-07-29 00:02:18 +02:00
|
|
|
|
|
|
|
$id = $question->getID();
|
|
|
|
if ($id) {
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb("Q{$id}", "/Q{$id}");
|
|
|
|
$crumbs->addTextCrumb(pht('Edit'));
|
2013-07-29 00:02:18 +02:00
|
|
|
} else {
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb(pht('Ask Question'));
|
2013-07-29 00:02:18 +02:00
|
|
|
}
|
2012-10-01 05:09:18 +02:00
|
|
|
|
2013-07-18 21:40:51 +02:00
|
|
|
return $this->buildApplicationPage(
|
2012-10-01 05:09:18 +02:00
|
|
|
array(
|
2013-07-18 21:40:51 +02:00
|
|
|
$crumbs,
|
2013-08-26 20:53:11 +02:00
|
|
|
$form_box,
|
2012-10-01 05:09:18 +02:00
|
|
|
$preview,
|
2013-07-18 21:40:51 +02:00
|
|
|
),
|
2012-10-01 05:09:18 +02:00
|
|
|
array(
|
2013-08-26 20:53:11 +02:00
|
|
|
'title' => pht('Ask New Question'),
|
2012-10-01 05:09:18 +02:00
|
|
|
'device' => true,
|
2013-02-19 22:33:10 +01:00
|
|
|
));
|
2012-08-10 19:44:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|