2012-08-10 10:44:04 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PonderAnswerSaveController extends PonderController {
|
|
|
|
|
2015-07-22 13:23:11 -07:00
|
|
|
public function handleRequest(AphrontRequest $request) {
|
|
|
|
$viewer = $request->getViewer();
|
2013-07-28 15:38:47 -07:00
|
|
|
|
2012-08-10 10:44:04 -07:00
|
|
|
if (!$request->isFormPost()) {
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$question_id = $request->getInt('question_id');
|
2013-07-28 15:38:47 -07:00
|
|
|
$question = id(new PonderQuestionQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withIDs(array($question_id))
|
2013-07-28 15:59:53 -07:00
|
|
|
->needAnswers(true)
|
2013-07-28 15:38:47 -07:00
|
|
|
->executeOne();
|
2012-08-10 10:44:04 -07:00
|
|
|
if (!$question) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2015-08-08 12:20:01 -07:00
|
|
|
$content = $request->getStr('answer');
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2015-08-08 12:20:01 -07:00
|
|
|
if (!strlen(trim($content))) {
|
2013-07-28 15:38:47 -07:00
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
->setUser($viewer)
|
|
|
|
->setTitle(pht('Empty Answer'))
|
|
|
|
->appendChild(
|
2015-05-22 17:27:56 +10:00
|
|
|
phutil_tag('p', array(), pht('Your answer must not be empty.')))
|
2013-07-28 15:38:47 -07:00
|
|
|
->addCancelButton('/Q'.$question_id);
|
2012-09-17 13:48:36 -07:00
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
2015-10-13 09:09:07 -07:00
|
|
|
$answer = PonderAnswer::initializeNewAnswer($viewer, $question);
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2015-08-08 12:20:01 -07:00
|
|
|
// Question Editor
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2013-07-28 15:59:53 -07:00
|
|
|
$xactions = array();
|
|
|
|
$xactions[] = id(new PonderQuestionTransaction())
|
2017-05-01 11:16:38 -07:00
|
|
|
->setTransactionType(PonderQuestionAnswerTransaction::TRANSACTIONTYPE)
|
2013-07-28 15:59:53 -07:00
|
|
|
->setNewValue(
|
|
|
|
array(
|
|
|
|
'+' => array(
|
2015-08-08 12:20:01 -07:00
|
|
|
array('answer' => $answer),
|
2013-07-28 15:59:53 -07:00
|
|
|
),
|
|
|
|
));
|
|
|
|
|
|
|
|
$editor = id(new PonderQuestionEditor())
|
2013-07-28 15:38:47 -07:00
|
|
|
->setActor($viewer)
|
2013-07-28 15:59:53 -07:00
|
|
|
->setContentSourceFromRequest($request);
|
|
|
|
|
|
|
|
$editor->applyTransactions($question, $xactions);
|
2012-08-10 10:44:04 -07:00
|
|
|
|
2015-08-08 12:20:01 -07:00
|
|
|
// Answer Editor
|
|
|
|
|
|
|
|
$template = id(new PonderAnswerTransaction());
|
|
|
|
$xactions = array();
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
2017-05-01 12:34:43 -07:00
|
|
|
->setTransactionType(PonderAnswerQuestionIDTransaction::TRANSACTIONTYPE)
|
2015-08-08 12:20:01 -07:00
|
|
|
->setNewValue($question->getID());
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
2017-05-01 12:34:43 -07:00
|
|
|
->setTransactionType(PonderAnswerContentTransaction::TRANSACTIONTYPE)
|
2015-08-08 12:20:01 -07:00
|
|
|
->setNewValue($content);
|
|
|
|
|
|
|
|
$editor = id(new PonderAnswerEditor())
|
|
|
|
->setActor($viewer)
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true);
|
|
|
|
|
|
|
|
$editor->applyTransactions($answer, $xactions);
|
|
|
|
|
|
|
|
|
2012-09-17 13:48:36 -07:00
|
|
|
return id(new AphrontRedirectResponse())->setURI(
|
2013-07-28 15:59:53 -07:00
|
|
|
id(new PhutilURI('/Q'.$question->getID())));
|
2012-08-10 10:44:04 -07:00
|
|
|
}
|
|
|
|
}
|