newpoll.php 6.2 KB

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