qnareviseanswer.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Revise an answer
  18. *
  19. * @category QnA
  20. * @package GNUsocial
  21. * @author Zach Copley <zach@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Revise an answer
  28. *
  29. * @copyright 2010 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class QnareviseanswerAction extends Action
  33. {
  34. protected $user = null;
  35. protected $error = null;
  36. protected $question = null;
  37. protected $answer = null;
  38. protected $content = null;
  39. /**
  40. * Returns the title of the action
  41. *
  42. * @return string Action title
  43. */
  44. public function title()
  45. {
  46. // TRANS: Page title for revising a question
  47. return _m('Revise answer');
  48. }
  49. /**
  50. * For initializing members of the class.
  51. *
  52. * @param array $args misc. arguments
  53. *
  54. * @return boolean true
  55. * @throws ClientException
  56. */
  57. public function prepare(array $args = [])
  58. {
  59. parent::prepare($args);
  60. if ($this->boolean('ajax')) {
  61. GNUsocial::setApi(true);
  62. }
  63. $this->user = common_current_user();
  64. if (empty($this->user)) {
  65. throw new ClientException(
  66. // TRANS: Client exception thrown trying to answer a question while not logged in.
  67. _m("You must be logged in to answer to a question."),
  68. 403
  69. );
  70. }
  71. $id = substr($this->trimmed('id'), 7);
  72. $this->answer = QnA_Answer::getKV('id', $id);
  73. $this->question = $this->answer->getQuestion();
  74. if (empty($this->answer) || empty($this->question)) {
  75. throw new ClientException(
  76. // TRANS: Client exception thrown trying to respond to a non-existing question.
  77. _m('Invalid or missing answer.'),
  78. 404
  79. );
  80. }
  81. $this->answerText = $this->trimmed('answer');
  82. return true;
  83. }
  84. /**
  85. * Handler method
  86. *
  87. * @return void
  88. */
  89. public function handle()
  90. {
  91. parent::handle();
  92. if ($this->isPost()) {
  93. $this->checkSessionToken();
  94. if ($this->arg('revise')) {
  95. $this->showContent();
  96. return;
  97. } elseif ($this->arg('best')) {
  98. if ($this->user->id == $this->question->profile_id) {
  99. $this->markBest();
  100. return;
  101. }
  102. } else {
  103. $this->reviseAnswer();
  104. return;
  105. }
  106. }
  107. $this->showPage();
  108. }
  109. /**
  110. * Show the revise answer form
  111. *
  112. * @return void
  113. */
  114. public function showContent()
  115. {
  116. if (!empty($this->error)) {
  117. $this->element('p', 'error', $this->error);
  118. }
  119. if ($this->boolean('ajax')) {
  120. $this->showAjaxReviseForm();
  121. } else {
  122. $form = new QnareviseanswerForm($this->answer, $this);
  123. $form->show();
  124. }
  125. return;
  126. }
  127. public function showAjaxReviseForm()
  128. {
  129. $this->startHTML('text/xml;charset=utf-8');
  130. $this->elementStart('head');
  131. // TRANS: Form title for sending an answer.
  132. $this->element('title', null, _m('TITLE', 'Answer'));
  133. $this->elementEnd('head');
  134. $this->elementStart('body');
  135. $form = new QnareviseanswerForm($this->answer, $this);
  136. $form->show();
  137. $this->elementEnd('body');
  138. $this->endHTML();
  139. }
  140. /**
  141. * Mark the answer as the "best" answer
  142. *
  143. * @return void
  144. */
  145. public function markBest()
  146. {
  147. $question = $this->question;
  148. $answer = $this->answer;
  149. try {
  150. // close the question to further answers
  151. $orig = clone($question);
  152. $question->closed = true;
  153. $result = $question->update($orig);
  154. // mark this answer an the best answer
  155. $orig = clone($answer);
  156. $answer->best = true;
  157. $result = $answer->update($orig);
  158. } catch (ClientException $ce) {
  159. $this->error = $ce->getMessage();
  160. $this->showPage();
  161. return;
  162. }
  163. if ($this->boolean('ajax')) {
  164. common_debug("ajaxy part");
  165. $this->startHTML('text/xml;charset=utf-8');
  166. $this->elementStart('head');
  167. // TRANS: Page title after sending an answer.
  168. $this->element('title', null, _m('Answer'));
  169. $this->elementEnd('head');
  170. $this->elementStart('body');
  171. $form = new QnashowanswerForm($this, $answer);
  172. $form->show();
  173. $this->elementEnd('body');
  174. $this->endHTML();
  175. } else {
  176. common_redirect($this->answer->getUrl(), 303);
  177. }
  178. }
  179. /**
  180. * Revise the answer
  181. *
  182. * @return void
  183. */
  184. public function reviseAnswer()
  185. {
  186. $answer = $this->answer;
  187. try {
  188. $orig = clone($answer);
  189. $answer->content = $this->answerText;
  190. $answer->revisions++;
  191. $result = $answer->update($orig);
  192. } catch (ClientException $ce) {
  193. $this->error = $ce->getMessage();
  194. $this->showPage();
  195. return;
  196. }
  197. if ($this->boolean('ajax')) {
  198. common_debug("ajaxy part");
  199. $this->startHTML('text/xml;charset=utf-8');
  200. $this->elementStart('head');
  201. // TRANS: Page title after sending an answer.
  202. $this->element('title', null, _m('Answer'));
  203. $this->elementEnd('head');
  204. $this->elementStart('body');
  205. $form = new QnashowanswerForm($this, $answer);
  206. $form->show();
  207. $this->elementEnd('body');
  208. $this->endHTML();
  209. } else {
  210. common_redirect($this->answer->getUrl(), 303);
  211. }
  212. }
  213. /**
  214. * Return true if read only.
  215. *
  216. * MAY override
  217. *
  218. * @param array $args other arguments
  219. *
  220. * @return boolean is read only action?
  221. */
  222. public function isReadOnly($args)
  223. {
  224. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  225. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. }
  231. }