qnanewquestion.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Add a new Question
  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. * Add a new Question
  37. *
  38. * @category Plugin
  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 QnanewquestionAction extends Action
  46. {
  47. protected $user = null;
  48. protected $error = null;
  49. protected $complete = null;
  50. protected $title = null;
  51. protected $description = null;
  52. /**
  53. * Returns the title of the action
  54. *
  55. * @return string Action title
  56. */
  57. function title()
  58. {
  59. // TRANS: Title for Question page.
  60. return _m('New question');
  61. }
  62. /**
  63. * For initializing members of the class.
  64. *
  65. * @param array $args misc. arguments
  66. *
  67. * @return boolean true
  68. * @throws ClientException
  69. */
  70. function prepare(array $args = [])
  71. {
  72. parent::prepare($args);
  73. $this->user = common_current_user();
  74. if (empty($this->user)) {
  75. throw new ClientException(
  76. // TRANS: Client exception thrown trying to create a Question while not logged in.
  77. _m('You must be logged in to post a question.'),
  78. 403
  79. );
  80. }
  81. if ($this->isPost()) {
  82. $this->checkSessionToken();
  83. }
  84. $this->title = $this->trimmed('title');
  85. $this->description = $this->trimmed('description');
  86. return true;
  87. }
  88. /**
  89. * Handler method
  90. *
  91. * @return void
  92. */
  93. function handle()
  94. {
  95. parent::handle();
  96. if ($this->isPost()) {
  97. $this->newQuestion();
  98. } else {
  99. $this->showPage();
  100. }
  101. return;
  102. }
  103. /**
  104. * Add a new Question
  105. *
  106. * @return void
  107. */
  108. function newQuestion()
  109. {
  110. if ($this->boolean('ajax')) {
  111. GNUsocial::setApi(true);
  112. }
  113. try {
  114. if (empty($this->title)) {
  115. // TRANS: Client exception thrown trying to create a question without a title.
  116. throw new ClientException(_m('Question must have a title.'));
  117. }
  118. // Notice options
  119. $options = array();
  120. // Does the heavy-lifting for getting "To:" information
  121. ToSelector::fillOptions($this, $options);
  122. $saved = QnA_Question::saveNew(
  123. $this->user->getProfile(),
  124. $this->title,
  125. $this->description,
  126. $options
  127. );
  128. } catch (ClientException $ce) {
  129. $this->error = $ce->getMessage();
  130. $this->showPage();
  131. return;
  132. }
  133. if ($this->boolean('ajax')) {
  134. $this->startHTML('text/xml;charset=utf-8');
  135. $this->elementStart('head');
  136. // TRANS: Page title after sending a notice.
  137. $this->element('title', null, _m('Question posted'));
  138. $this->elementEnd('head');
  139. $this->elementStart('body');
  140. $this->showNotice($saved);
  141. $this->elementEnd('body');
  142. $this->endHTML();
  143. } else {
  144. common_redirect($saved->getUrl(), 303);
  145. }
  146. }
  147. /**
  148. * Output a notice
  149. *
  150. * Used to generate the notice code for Ajax results.
  151. *
  152. * @param Notice $notice Notice that was saved
  153. *
  154. * @return void
  155. */
  156. function showNotice($notice)
  157. {
  158. $nli = new NoticeQuestionListItem($notice, $this);
  159. $nli->show();
  160. }
  161. /**
  162. * Show the Question form
  163. *
  164. * @return void
  165. */
  166. function showContent()
  167. {
  168. if (!empty($this->error)) {
  169. $this->element('p', 'error', $this->error);
  170. }
  171. $form = new QuestionForm(
  172. $this,
  173. $this->title,
  174. $this->description
  175. );
  176. $form->show();
  177. return;
  178. }
  179. /**
  180. * Return true if read only.
  181. *
  182. * MAY override
  183. *
  184. * @param array $args other arguments
  185. *
  186. * @return boolean is read only action?
  187. */
  188. function isReadOnly($args)
  189. {
  190. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  191. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. }
  198. class NoticeQuestionListItem extends NoticeListItem
  199. {
  200. /**
  201. * constructor
  202. *
  203. * Also initializes the profile attribute.
  204. *
  205. * @param Notice $notice The notice we'll display
  206. */
  207. function __construct($notice, $out = null)
  208. {
  209. parent::__construct($notice, $out);
  210. }
  211. function showEnd()
  212. {
  213. $this->out->element('ul', 'notices threaded-replies xoxo');
  214. parent::showEnd();
  215. }
  216. }