public function processRequest()
 {
     $request = $this->getRequest();
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     $user = $request->getUser();
     $question_id = $request->getInt('question_id');
     $question = PonderQuestionQuery::loadSingle($user, $question_id);
     if (!$question) {
         return new Aphront404Response();
     }
     $answer = $request->getStr('answer');
     // Only want answers with some non whitespace content
     if (!strlen(trim($answer))) {
         $dialog = new AphrontDialogView();
         $dialog->setUser($request->getUser());
         $dialog->setTitle('Empty answer');
         $dialog->appendChild('<p>Your answer must not be empty.</p>');
         $dialog->addCancelButton('/Q' . $question_id);
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_WEB, array('ip' => $request->getRemoteAddr()));
     $res = new PonderAnswer();
     $res->setContent($answer)->setAuthorPHID($user->getPHID())->setVoteCount(0)->setQuestionID($question_id)->setContentSource($content_source);
     id(new PonderAnswerEditor())->setUser($user)->setQuestion($question)->setAnswer($res)->saveAnswer();
     return id(new AphrontRedirectResponse())->setURI(id(new PhutilURI('/Q' . $question->getID())));
 }
Example #2
0
 public function loadPage()
 {
     $answer = new PonderAnswer();
     $conn_r = $answer->establishConnection('r');
     $data = queryfx_all($conn_r, 'SELECT a.* FROM %T a %Q %Q %Q', $answer->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r));
     return $answer->loadAllFromArray($data);
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     $question_id = $request->getInt('question_id');
     $question = id(new PonderQuestionQuery())->setViewer($viewer)->withIDs(array($question_id))->needAnswers(true)->executeOne();
     if (!$question) {
         return new Aphront404Response();
     }
     $content = $request->getStr('answer');
     if (!strlen(trim($content))) {
         $dialog = id(new AphrontDialogView())->setUser($viewer)->setTitle(pht('Empty Answer'))->appendChild(phutil_tag('p', array(), pht('Your answer must not be empty.')))->addCancelButton('/Q' . $question_id);
         return id(new AphrontDialogResponse())->setDialog($dialog);
     }
     $answer = PonderAnswer::initializeNewAnswer($viewer, $question);
     // Question Editor
     $xactions = array();
     $xactions[] = id(new PonderQuestionTransaction())->setTransactionType(PonderQuestionTransaction::TYPE_ANSWERS)->setNewValue(array('+' => array(array('answer' => $answer))));
     $editor = id(new PonderQuestionEditor())->setActor($viewer)->setContentSourceFromRequest($request);
     $editor->applyTransactions($question, $xactions);
     // Answer Editor
     $template = id(new PonderAnswerTransaction());
     $xactions = array();
     $xactions[] = id(clone $template)->setTransactionType(PonderAnswerTransaction::TYPE_QUESTION_ID)->setNewValue($question->getID());
     $xactions[] = id(clone $template)->setTransactionType(PonderAnswerTransaction::TYPE_CONTENT)->setNewValue($content);
     $editor = id(new PonderAnswerEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
     $editor->applyTransactions($answer, $xactions);
     return id(new AphrontRedirectResponse())->setURI(id(new PhutilURI('/Q' . $question->getID())));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $question_id = $request->getInt('question_id');
     $question = PonderQuestionQuery::loadSingle($user, $question_id);
     if (!$question) {
         return new Aphront404Response();
     }
     $author_phid = $user->getPHID();
     $object_phids = array($author_phid);
     $handles = id(new PhabricatorObjectHandleData($object_phids))->loadHandles();
     $answer = new PonderAnswer();
     $answer->setContent($request->getStr('content'));
     $answer->setAuthorPHID($author_phid);
     $view = new PonderCommentBodyView();
     $view->setQuestion($question)->setTarget($answer)->setPreview(true)->setUser($user)->setHandles($handles)->setAction(PonderConstants::ANSWERED_LITERAL);
     return id(new AphrontAjaxResponse())->setContent($view->render());
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     $user = $request->getUser();
     $question_id = $request->getInt('question_id');
     $question = PonderQuestionQuery::loadSingle($user, $question_id);
     if (!$question) {
         return new Aphront404Response();
     }
     $answer = $request->getStr('answer');
     $content_source = PhabricatorContentSource::newForSource(PhabricatorContentSource::SOURCE_WEB, array('ip' => $request->getRemoteAddr()));
     $res = new PonderAnswer();
     $res->setContent($answer)->setAuthorPHID($user->getPHID())->setVoteCount(0)->setQuestionID($question_id)->setContentSource($content_source);
     id(new PonderAnswerEditor())->setQuestion($question)->setAnswer($res)->saveAnswer();
     PhabricatorSearchPonderIndexer::indexQuestion($question);
     return id(new AphrontRedirectResponse())->setURI(id(new PhutilURI('/Q' . $question->getID()))->setFragment('A' . $res->getID()));
 }
Example #6
0
 public function execute()
 {
     $answer = new PonderAnswer();
     $conn_r = $answer->establishConnection('r');
     $select = qsprintf($conn_r, 'SELECT r.* FROM %T r', $answer->getTableName());
     $where = $this->buildWhereClause($conn_r);
     $order_by = $this->buildOrderByClause($conn_r);
     $limit = $this->buildLimitClause($conn_r);
     return $answer->loadAllFromArray(queryfx_all($conn_r, '%Q %Q %Q %Q', $select, $where, $order_by, $limit));
 }
 private function buildAnswerActions(PonderAnswer $answer)
 {
     $viewer = $this->getViewer();
     $request = $this->getRequest();
     $id = $answer->getID();
     $can_edit = PhabricatorPolicyFilter::hasCapability($viewer, $answer, PhabricatorPolicyCapability::CAN_EDIT);
     $view = id(new PhabricatorActionListView())->setUser($viewer)->setObject($answer)->setObjectURI($request->getRequestURI());
     $view->addAction(id(new PhabricatorActionView())->setIcon('fa-pencil')->setName(pht('Edit Answer'))->setHref($this->getApplicationURI("/answer/edit/{$id}/"))->setDisabled(!$can_edit)->setWorkflow(!$can_edit));
     $view->addAction(id(new PhabricatorActionView())->setIcon('fa-list')->setName(pht('View History'))->setHref($this->getApplicationURI("/answer/history/{$id}/")));
     return $view;
 }
 private function buildJoinsClause(AphrontDatabaseConnection $conn_r)
 {
     $joins = array();
     if ($this->answererPHIDs) {
         $answer_table = new PonderAnswer();
         $joins[] = qsprintf($conn_r, 'JOIN %T a ON a.questionID = q.id AND a.authorPHID IN (%Ls)', $answer_table->getTableName(), $this->answererPHIDs);
     }
     return implode(' ', $joins);
 }
<?php

$map = array();
echo pht('Merging duplicate answers by authors...') . "\n";
$atable = new PonderAnswer();
$conn_w = $atable->establishConnection('w');
$conn_w->openTransaction();
$answers = new LiskMigrationIterator(new PonderAnswer());
foreach ($answers as $answer) {
    $aid = $answer->getID();
    $qid = $answer->getQuestionID();
    $author_phid = $answer->getAuthorPHID();
    echo pht('Processing answer ID #%d...', $aid) . "\n";
    if (empty($map[$qid][$author_phid])) {
        echo pht('Answer is unique.') . "\n";
        $map[$qid][$author_phid] = $answer;
        continue;
    } else {
        echo pht('Merging answer.') . "\n";
        $target = $map[$qid][$author_phid];
        queryfx($conn_w, 'UPDATE %T SET content = %s WHERE id = %d', $target->getTableName(), $target->getContent() . "\n\n" . "---" . "\n\n" . "> (This content was automatically merged from another answer by the " . "same author.)" . "\n\n" . $answer->getContent(), $target->getID());
        queryfx($conn_w, 'DELETE FROM %T WHERE id = %d', $target->getTableName(), $answer->getID());
        queryfx($conn_w, 'UPDATE %T SET targetPHID = %s WHERE targetPHID = %s', 'ponder_comment', $target->getPHID(), $answer->getPHID());
    }
}
$conn_w->saveTransaction();
echo pht('Done.') . "\n";
<?php

$table = new PonderAnswer();
$conn_w = $table->establishConnection('w');
$iterator = new LiskMigrationIterator($table);
foreach ($iterator as $answer) {
    $id = $answer->getID();
    echo pht('Adding mail key for Answer %d...', $id);
    echo "\n";
    queryfx($conn_w, 'UPDATE %T SET mailKey = %s WHERE id = %d', $table->getTableName(), Filesystem::readRandomCharacters(20), $id);
}
 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn)
 {
     $joins = parent::buildJoinClauseParts($conn);
     if ($this->answererPHIDs) {
         $answer_table = new PonderAnswer();
         $joins[] = qsprintf($conn, 'JOIN %T a ON a.questionID = q.id AND a.authorPHID IN (%Ls)', $answer_table->getTableName(), $this->answererPHIDs);
     }
     return $joins;
 }