2012-10-07 23:35:01 +02:00
|
|
|
<?php
|
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
final class PonderQuestionEditor
|
2013-09-19 00:15:25 +02:00
|
|
|
extends PonderEditor {
|
|
|
|
|
|
|
|
private $answer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is used internally on @{method:applyInitialEffects} if a transaction
|
|
|
|
* of type PonderQuestionTransaction::TYPE_ANSWERS is in the mix. The value
|
|
|
|
* is set to the //last// answer in the transactions. Practically, one
|
|
|
|
* answer is given at a time in the application, though theoretically
|
|
|
|
* this is buggy.
|
|
|
|
*
|
|
|
|
* The answer is used in emails to generate proper links.
|
|
|
|
*/
|
|
|
|
private function setAnswer(PonderAnswer $answer) {
|
|
|
|
$this->answer = $answer;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
private function getAnswer() {
|
|
|
|
return $this->answer;
|
|
|
|
}
|
2012-10-07 23:35:01 +02:00
|
|
|
|
2013-07-29 00:59:53 +02:00
|
|
|
protected function shouldApplyInitialEffects(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case PonderQuestionTransaction::TYPE_ANSWERS:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function applyInitialEffects(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case PonderQuestionTransaction::TYPE_ANSWERS:
|
|
|
|
$new_value = $xaction->getNewValue();
|
|
|
|
$new = idx($new_value, '+', array());
|
|
|
|
foreach ($new as $new_answer) {
|
|
|
|
$answer = idx($new_answer, 'answer');
|
|
|
|
if (!$answer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$answer->save();
|
2013-09-19 00:15:25 +02:00
|
|
|
$this->setAnswer($answer);
|
2013-07-29 00:59:53 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
public function getTransactionTypes() {
|
|
|
|
$types = parent::getTransactionTypes();
|
2012-10-07 23:35:01 +02:00
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
$types[] = PhabricatorTransactions::TYPE_COMMENT;
|
|
|
|
$types[] = PonderQuestionTransaction::TYPE_TITLE;
|
|
|
|
$types[] = PonderQuestionTransaction::TYPE_CONTENT;
|
2013-07-29 00:59:53 +02:00
|
|
|
$types[] = PonderQuestionTransaction::TYPE_ANSWERS;
|
2013-07-29 02:52:35 +02:00
|
|
|
$types[] = PonderQuestionTransaction::TYPE_STATUS;
|
2013-07-29 00:02:18 +02:00
|
|
|
|
|
|
|
return $types;
|
2012-10-07 23:35:01 +02:00
|
|
|
}
|
2013-07-29 00:59:53 +02:00
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
protected function getCustomTransactionOldValue(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
2012-10-07 23:35:01 +02:00
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case PonderQuestionTransaction::TYPE_TITLE:
|
|
|
|
return $object->getTitle();
|
|
|
|
case PonderQuestionTransaction::TYPE_CONTENT:
|
|
|
|
return $object->getContent();
|
2013-07-29 00:59:53 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_ANSWERS:
|
|
|
|
return mpull($object->getAnswers(), 'getPHID');
|
2013-07-29 02:52:35 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_STATUS:
|
|
|
|
return $object->getStatus();
|
2013-07-29 00:02:18 +02:00
|
|
|
}
|
2012-10-08 23:47:21 +02:00
|
|
|
}
|
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
protected function getCustomTransactionNewValue(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case PonderQuestionTransaction::TYPE_TITLE:
|
|
|
|
case PonderQuestionTransaction::TYPE_CONTENT:
|
2013-07-29 02:52:35 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_STATUS:
|
2013-07-29 00:02:18 +02:00
|
|
|
return $xaction->getNewValue();
|
2013-07-29 00:59:53 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_ANSWERS:
|
|
|
|
$raw_new_value = $xaction->getNewValue();
|
|
|
|
$new_value = array();
|
|
|
|
foreach ($raw_new_value as $key => $answers) {
|
|
|
|
$phids = array();
|
|
|
|
foreach ($answers as $answer) {
|
|
|
|
$obj = idx($answer, 'answer');
|
|
|
|
if (!$answer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$phids[] = $obj->getPHID();
|
|
|
|
}
|
|
|
|
$new_value[$key] = $phids;
|
|
|
|
}
|
|
|
|
$xaction->setNewValue($new_value);
|
|
|
|
return $this->getPHIDTransactionNewValue($xaction);
|
2013-07-29 00:02:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function applyCustomInternalTransaction(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case PonderQuestionTransaction::TYPE_TITLE:
|
|
|
|
$object->setTitle($xaction->getNewValue());
|
|
|
|
break;
|
|
|
|
case PonderQuestionTransaction::TYPE_CONTENT:
|
|
|
|
$object->setContent($xaction->getNewValue());
|
|
|
|
break;
|
2013-07-29 02:52:35 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_STATUS:
|
|
|
|
$object->setStatus($xaction->getNewValue());
|
|
|
|
break;
|
2013-07-29 00:59:53 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_ANSWERS:
|
|
|
|
$old = $xaction->getOldValue();
|
|
|
|
$new = $xaction->getNewValue();
|
|
|
|
|
|
|
|
$add = array_diff_key($new, $old);
|
|
|
|
$rem = array_diff_key($old, $new);
|
|
|
|
|
|
|
|
$count = $object->getAnswerCount();
|
|
|
|
$count += count($add);
|
|
|
|
$count -= count($rem);
|
|
|
|
|
|
|
|
$object->setAnswerCount($count);
|
|
|
|
break;
|
2012-10-07 23:35:01 +02:00
|
|
|
}
|
2013-07-29 00:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function applyCustomExternalTransaction(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mergeTransactions(
|
|
|
|
PhabricatorApplicationTransaction $u,
|
|
|
|
PhabricatorApplicationTransaction $v) {
|
2012-10-07 23:35:01 +02:00
|
|
|
|
2013-07-29 00:02:18 +02:00
|
|
|
$type = $u->getTransactionType();
|
|
|
|
switch ($type) {
|
|
|
|
case PonderQuestionTransaction::TYPE_TITLE:
|
|
|
|
case PonderQuestionTransaction::TYPE_CONTENT:
|
2013-07-29 02:52:35 +02:00
|
|
|
case PonderQuestionTransaction::TYPE_STATUS:
|
2013-07-29 00:02:18 +02:00
|
|
|
return $v;
|
2012-10-08 23:47:21 +02:00
|
|
|
}
|
2013-07-29 00:02:18 +02:00
|
|
|
|
|
|
|
return parent::mergeTransactions($u, $v);
|
2012-10-07 23:35:01 +02:00
|
|
|
}
|
2013-07-29 00:02:18 +02:00
|
|
|
|
2013-09-19 00:15:25 +02:00
|
|
|
protected function supportsSearch() {
|
2013-07-29 15:30:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-19 00:15:25 +02:00
|
|
|
protected function getFeedStoryType() {
|
|
|
|
return 'PonderTransactionFeedStory';
|
2013-07-29 17:20:06 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 00:15:25 +02:00
|
|
|
protected function getFeedStoryData(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
$data = parent::getFeedStoryData($object, $xactions);
|
|
|
|
$answer = $this->getAnswer();
|
|
|
|
if ($answer) {
|
|
|
|
$data['answerPHID'] = $answer->getPHID();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2013-07-29 17:20:06 +02:00
|
|
|
protected function shouldImplyCC(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
PhabricatorApplicationTransaction $xaction) {
|
|
|
|
|
|
|
|
switch ($xaction->getTransactionType()) {
|
|
|
|
case PonderQuestionTransaction::TYPE_ANSWERS:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::shouldImplyCC($object, $xaction);
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:08:17 +02:00
|
|
|
protected function shouldSendMail(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
array $xactions) {
|
2013-07-29 17:38:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildReplyHandler(PhabricatorLiskDAO $object) {
|
|
|
|
return id(new PonderQuestionReplyHandler())
|
|
|
|
->setMailReceiver($object);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildMailBody(
|
|
|
|
PhabricatorLiskDAO $object,
|
|
|
|
array $xactions) {
|
|
|
|
|
|
|
|
$body = parent::buildMailBody($object, $xactions);
|
|
|
|
|
2013-09-19 00:15:25 +02:00
|
|
|
$header = pht('QUESTION DETAIL');
|
|
|
|
$uri = '/Q'.$object->getID();
|
2013-07-29 17:38:25 +02:00
|
|
|
foreach ($xactions as $xaction) {
|
|
|
|
$type = $xaction->getTransactionType();
|
|
|
|
$old = $xaction->getOldValue();
|
|
|
|
$new = $xaction->getNewValue();
|
2013-09-19 00:15:25 +02:00
|
|
|
// If the user just asked the question, add the question text.
|
2013-07-29 17:38:25 +02:00
|
|
|
if ($type == PonderQuestionTransaction::TYPE_CONTENT) {
|
|
|
|
if ($old === null) {
|
|
|
|
$body->addRawSection($new);
|
|
|
|
}
|
|
|
|
}
|
2013-09-19 00:15:25 +02:00
|
|
|
// If the user gave an answer, add the answer text. Also update
|
|
|
|
// the header and uri to be more answer-specific.
|
|
|
|
if ($type == PonderQuestionTransaction::TYPE_ANSWERS) {
|
|
|
|
$answer = $this->getAnswer();
|
|
|
|
$body->addRawSection($answer->getContent());
|
|
|
|
$header = pht('ANSWER DETAIL');
|
|
|
|
$uri = $answer->getURI();
|
|
|
|
}
|
2013-07-29 17:38:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$body->addTextSection(
|
2013-09-19 00:15:25 +02:00
|
|
|
$header,
|
|
|
|
PhabricatorEnv::getProductionURI($uri));
|
2013-07-29 17:38:25 +02:00
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
|
|
|
|
2012-10-07 23:35:01 +02:00
|
|
|
}
|