@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PonderAnswerSaveController extends PonderController {
4
5 public function handleRequest(AphrontRequest $request) {
6 $viewer = $request->getViewer();
7
8 if (!$request->isFormPost()) {
9 return new Aphront400Response();
10 }
11
12 $question_id = $request->getInt('question_id');
13 $question = id(new PonderQuestionQuery())
14 ->setViewer($viewer)
15 ->withIDs(array($question_id))
16 ->needAnswers(true)
17 ->executeOne();
18 if (!$question) {
19 return new Aphront404Response();
20 }
21
22 $content = $request->getStr('answer');
23
24 if (!strlen(trim($content))) {
25 $dialog = id(new AphrontDialogView())
26 ->setUser($viewer)
27 ->setTitle(pht('Empty Answer'))
28 ->appendChild(
29 phutil_tag('p', array(), pht('Your answer must not be empty.')))
30 ->addCancelButton('/Q'.$question_id);
31
32 return id(new AphrontDialogResponse())->setDialog($dialog);
33 }
34
35 $answer = PonderAnswer::initializeNewAnswer($viewer, $question);
36
37 // Question Editor
38
39 $xactions = array();
40 $xactions[] = id(new PonderQuestionTransaction())
41 ->setTransactionType(PonderQuestionAnswerTransaction::TRANSACTIONTYPE)
42 ->setNewValue(
43 array(
44 '+' => array(
45 array('answer' => $answer),
46 ),
47 ));
48
49 $editor = id(new PonderQuestionEditor())
50 ->setActor($viewer)
51 ->setContentSourceFromRequest($request);
52
53 $editor->applyTransactions($question, $xactions);
54
55 // Answer Editor
56
57 $template = new PonderAnswerTransaction();
58 $xactions = array();
59
60 $xactions[] = id(clone $template)
61 ->setTransactionType(PonderAnswerQuestionIDTransaction::TRANSACTIONTYPE)
62 ->setNewValue($question->getID());
63
64 $xactions[] = id(clone $template)
65 ->setTransactionType(PonderAnswerContentTransaction::TRANSACTIONTYPE)
66 ->setNewValue($content);
67
68 $editor = id(new PonderAnswerEditor())
69 ->setActor($viewer)
70 ->setContentSourceFromRequest($request)
71 ->setContinueOnNoEffect(true);
72
73 $editor->applyTransactions($answer, $xactions);
74
75
76 return id(new AphrontRedirectResponse())->setURI(
77 id(new PhutilURI('/Q'.$question->getID())));
78 }
79}