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();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$question = id(new PonderQuestion())
|
|
|
|
->setStatus(PonderQuestionStatus::STATUS_OPEN)
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setVoteCount(0)
|
|
|
|
->setAnswerCount(0)
|
|
|
|
->setHeat(0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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');
|
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);
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = id(new AphrontErrorView())
|
2013-05-20 07:55:23 -07:00
|
|
|
->setTitle(pht('Form Errors'))
|
2012-08-10 10:44:04 -07:00
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
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())
|
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'))
|
|
|
|
->setUser($user))
|
2012-08-10 10:44:04 -07:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-07-18 12:40:51 -07:00
|
|
|
->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'))
|
|
|
|
->setFormError($error_view)
|
|
|
|
->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) {
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName("Q{$id}")
|
|
|
|
->setHref("/Q{$id}"));
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName(pht('Edit')));
|
|
|
|
} else {
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName(pht('Ask Question')));
|
|
|
|
}
|
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'),
|
2012-09-30 20:09:18 -07:00
|
|
|
'device' => true,
|
2013-02-19 13:33:10 -08:00
|
|
|
));
|
2012-08-10 10:44:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|