pollsettings.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Form to set your personal poll settings
  4. *
  5. * StatusNet, the distributed open-source microblogging tool
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugins
  23. * @package StatusNet
  24. * @author Brion Vibber <brion@status.net>
  25. * @copyright 2012 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. class PollSettingsAction extends SettingsAction
  33. {
  34. /**
  35. * Title of the page
  36. *
  37. * @return string Page title
  38. */
  39. function title()
  40. {
  41. // TRANS: Page title.
  42. return _m('Poll settings');
  43. }
  44. /**
  45. * Instructions for use
  46. *
  47. * @return string Instructions for use
  48. */
  49. function getInstructions()
  50. {
  51. // TRANS: Page instructions.
  52. return _m('Set your poll preferences');
  53. }
  54. /**
  55. * Show the form for Poll
  56. *
  57. * @return void
  58. */
  59. function showContent()
  60. {
  61. $user = common_current_user();
  62. $prefs = User_poll_prefs::getKV('user_id', $user->id);
  63. $form = new PollPrefsForm($this, $prefs);
  64. $form->show();
  65. }
  66. /**
  67. * Handler method
  68. *
  69. * @param array $argarray is ignored since it's now passed in in prepare()
  70. *
  71. * @return void
  72. */
  73. function handlePost()
  74. {
  75. $user = common_current_user();
  76. $upp = User_poll_prefs::getKV('user_id', $user->id);
  77. $orig = null;
  78. if (!empty($upp)) {
  79. $orig = clone($upp);
  80. } else {
  81. $upp = new User_poll_prefs();
  82. $upp->user_id = $user->id;
  83. $upp->created = common_sql_now();
  84. }
  85. $upp->hide_responses = $this->boolean('hide_responses');
  86. $upp->modified = common_sql_now();
  87. if (!empty($orig)) {
  88. $upp->update($orig);
  89. } else {
  90. $upp->insert();
  91. }
  92. // TRANS: Confirmation shown when user profile settings are saved.
  93. $this->showForm(_('Settings saved.'), true);
  94. return;
  95. }
  96. }
  97. class PollPrefsForm extends Form
  98. {
  99. var $prefs;
  100. function __construct($out, $prefs)
  101. {
  102. parent::__construct($out);
  103. $this->prefs = $prefs;
  104. }
  105. /**
  106. * Visible or invisible data elements
  107. *
  108. * Display the form fields that make up the data of the form.
  109. * Sub-classes should overload this to show their data.
  110. *
  111. * @return void
  112. */
  113. function formData()
  114. {
  115. $this->elementStart('fieldset');
  116. $this->elementStart('ul', 'form_data');
  117. $this->elementStart('li');
  118. $this->checkbox('hide_responses',
  119. _('Do not deliver poll responses to my home timeline'),
  120. (!empty($this->prefs) && $this->prefs->hide_responses));
  121. $this->elementEnd('li');
  122. $this->elementEnd('ul');
  123. $this->elementEnd('fieldset');
  124. }
  125. /**
  126. * Buttons for form actions
  127. *
  128. * Submit and cancel buttons (or whatever)
  129. * Sub-classes should overload this to show their own buttons.
  130. *
  131. * @return void
  132. */
  133. function formActions()
  134. {
  135. $this->submit('submit', _('Save'));
  136. }
  137. /**
  138. * ID of the form
  139. *
  140. * Should be unique on the page. Sub-classes should overload this
  141. * to show their own IDs.
  142. *
  143. * @return int ID of the form
  144. */
  145. function id()
  146. {
  147. return 'form_poll_prefs';
  148. }
  149. /**
  150. * Action of the form.
  151. *
  152. * URL to post to. Should be overloaded by subclasses to give
  153. * somewhere to post to.
  154. *
  155. * @return string URL to post to
  156. */
  157. function action()
  158. {
  159. return common_local_url('pollsettings');
  160. }
  161. /**
  162. * Class of the form. May include space-separated list of multiple classes.
  163. *
  164. * @return string the form's class
  165. */
  166. function formClass()
  167. {
  168. return 'form_settings';
  169. }
  170. }