qnaclosequestion.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Close a question to further answers
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category QnA
  24. * @package StatusNet
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Close a question to new answers
  37. *
  38. * @category QnA
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class QnaclosequestionAction extends Action
  46. {
  47. protected $user = null;
  48. protected $error = null;
  49. protected $complete = null;
  50. protected $question = null;
  51. protected $answer = null;
  52. /**
  53. * Returns the title of the action
  54. *
  55. * @return string Action title
  56. */
  57. function title()
  58. {
  59. // TRANS: Page title for close a question
  60. return _m('Close question');
  61. }
  62. /**
  63. * For initializing members of the class.
  64. *
  65. * @param array $argarray misc. arguments
  66. *
  67. * @return boolean true
  68. */
  69. function prepare($argarray)
  70. {
  71. parent::prepare($argarray);
  72. if ($this->boolean('ajax')) {
  73. GNUsocial::setApi(true);
  74. }
  75. $this->user = common_current_user();
  76. if (empty($this->user)) {
  77. throw new ClientException(
  78. // TRANS: Client exception thrown trying to close a question when not logged in
  79. _m("You must be logged in to close a question."),
  80. 403
  81. );
  82. }
  83. if ($this->isPost()) {
  84. $this->checkSessionToken();
  85. }
  86. $id = substr($this->trimmed('id'), 9);
  87. $this->question = QnA_Question::getKV('id', $id);
  88. if (empty($this->question)) {
  89. // TRANS: Client exception thrown trying to respond to a non-existing question.
  90. throw new ClientException(_m('Invalid or missing question.'), 404);
  91. }
  92. return true;
  93. }
  94. /**
  95. * Handler method
  96. *
  97. * @param array $argarray is ignored since it's now passed in in prepare()
  98. *
  99. * @return void
  100. */
  101. function handle($argarray=null)
  102. {
  103. parent::handle($argarray);
  104. if ($this->isPost()) {
  105. $this->closeQuestion();
  106. } else {
  107. $this->showPage();
  108. }
  109. return;
  110. }
  111. /**
  112. * Close a question
  113. *
  114. * @return void
  115. */
  116. function closeQuestion()
  117. {
  118. $user = common_current_user();
  119. try {
  120. if ($user->id != $this->question->profile_id) {
  121. // TRANS: Exception thrown trying to close another user's question.
  122. throw new Exception(_m('You did not ask this question.'));
  123. }
  124. $orig = clone($this->question);
  125. $this->question->closed = 1;
  126. $this->question->update($orig);
  127. } catch (ClientException $ce) {
  128. $this->error = $ce->getMessage();
  129. $this->showPage();
  130. return;
  131. }
  132. if ($this->boolean('ajax')) {
  133. $this->startHTML('text/xml;charset=utf-8');
  134. $this->elementStart('head');
  135. // TRANS: Page title after sending an answer.
  136. $this->element('title', null, _m('Answers'));
  137. $this->elementEnd('head');
  138. $this->elementStart('body');
  139. $form = new QnashowquestionForm($this, $this->question);
  140. $form->show();
  141. $this->elementEnd('body');
  142. $this->endHTML();
  143. } else {
  144. common_redirect($this->question->getUrl(), 303);
  145. }
  146. }
  147. /**
  148. * Show the close question form
  149. *
  150. * @return void
  151. */
  152. function showContent()
  153. {
  154. if (!empty($this->error)) {
  155. $this->element('p', 'error', $this->error);
  156. }
  157. // blar
  158. }
  159. /**
  160. * Return true if read only.
  161. *
  162. * MAY override
  163. *
  164. * @param array $args other arguments
  165. *
  166. * @return boolean is read only action?
  167. */
  168. function isReadOnly($args)
  169. {
  170. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  171. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  172. return true;
  173. } else {
  174. return false;
  175. }
  176. }
  177. }