qnaclosequestion.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * Close a question to further answers
  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 late
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Close a question to new answers
  28. *
  29. * @copyright 2010 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or late
  31. */
  32. class QnaclosequestionAction extends Action
  33. {
  34. protected $user = null;
  35. protected $error = null;
  36. protected $complete = null;
  37. protected $question = null;
  38. protected $answer = 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 close a question
  47. return _m('Close question');
  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 close a question when not logged in
  67. _m("You must be logged in to close a question."),
  68. 403
  69. );
  70. }
  71. if ($this->isPost()) {
  72. $this->checkSessionToken();
  73. }
  74. $id = substr($this->trimmed('id'), 9);
  75. $this->question = QnA_Question::getKV('id', $id);
  76. if (empty($this->question)) {
  77. // TRANS: Client exception thrown trying to respond to a non-existing question.
  78. throw new ClientException(_m('Invalid or missing question.'), 404);
  79. }
  80. return true;
  81. }
  82. /**
  83. * Handler method
  84. *
  85. * @return void
  86. */
  87. public function handle()
  88. {
  89. parent::handle();
  90. if ($this->isPost()) {
  91. $this->closeQuestion();
  92. } else {
  93. $this->showPage();
  94. }
  95. return;
  96. }
  97. /**
  98. * Close a question
  99. *
  100. * @return void
  101. */
  102. public function closeQuestion()
  103. {
  104. $user = common_current_user();
  105. try {
  106. if ($user->id != $this->question->profile_id) {
  107. // TRANS: Exception thrown trying to close another user's question.
  108. throw new Exception(_m('You did not ask this question.'));
  109. }
  110. $orig = clone($this->question);
  111. $this->question->closed = true;
  112. $this->question->update($orig);
  113. } catch (ClientException $ce) {
  114. $this->error = $ce->getMessage();
  115. $this->showPage();
  116. return;
  117. }
  118. if ($this->boolean('ajax')) {
  119. $this->startHTML('text/xml;charset=utf-8');
  120. $this->elementStart('head');
  121. // TRANS: Page title after sending an answer.
  122. $this->element('title', null, _m('Answers'));
  123. $this->elementEnd('head');
  124. $this->elementStart('body');
  125. $form = new QnashowquestionForm($this, $this->question);
  126. $form->show();
  127. $this->elementEnd('body');
  128. $this->endHTML();
  129. } else {
  130. common_redirect($this->question->getUrl(), 303);
  131. }
  132. }
  133. /**
  134. * Show the close question form
  135. *
  136. * @return void
  137. */
  138. public function showContent()
  139. {
  140. if (!empty($this->error)) {
  141. $this->element('p', 'error', $this->error);
  142. }
  143. // blar
  144. }
  145. /**
  146. * Return true if read only.
  147. *
  148. * MAY override
  149. *
  150. * @param array $args other arguments
  151. *
  152. * @return boolean is read only action?
  153. */
  154. public function isReadOnly($args)
  155. {
  156. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  157. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. }