useradminpanel.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * User administration panel
  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. *
  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 Settings
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @author Sarven Capadisli <csarven@status.net>
  28. * @copyright 2008-2010 StatusNet, Inc.
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://status.net/
  31. */
  32. if (!defined('STATUSNET')) {
  33. exit(1);
  34. }
  35. /**
  36. * Administer user settings
  37. *
  38. * @category Admin
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Zach Copley <zach@status.net>
  42. * @author Sarven Capadisli <csarven@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. */
  46. class UseradminpanelAction extends AdminPanelAction
  47. {
  48. /**
  49. * Returns the page title
  50. *
  51. * @return string page title
  52. */
  53. function title()
  54. {
  55. // TRANS: User admin panel title.
  56. return _m('TITLE', 'User');
  57. }
  58. /**
  59. * Instructions for using this form.
  60. *
  61. * @return string instructions
  62. */
  63. function getInstructions()
  64. {
  65. // TRANS: Instruction for user admin panel.
  66. return _('User settings for this StatusNet site');
  67. }
  68. /**
  69. * Show the site admin panel form
  70. *
  71. * @return void
  72. */
  73. function showForm()
  74. {
  75. $form = new UserAdminPanelForm($this);
  76. $form->show();
  77. return;
  78. }
  79. /**
  80. * Save settings from the form
  81. *
  82. * @return void
  83. */
  84. function saveSettings()
  85. {
  86. static $settings = array(
  87. 'profile' => array('biolimit'),
  88. 'newuser' => array('welcome', 'default')
  89. );
  90. static $booleans = array(
  91. 'invite' => array('enabled')
  92. );
  93. $values = array();
  94. foreach ($settings as $section => $parts) {
  95. foreach ($parts as $setting) {
  96. $values[$section][$setting] = $this->trimmed("$section-$setting");
  97. }
  98. }
  99. foreach ($booleans as $section => $parts) {
  100. foreach ($parts as $setting) {
  101. $values[$section][$setting] = ($this->boolean("$section-$setting")) ? 1 : 0;
  102. }
  103. }
  104. // This throws an exception on validation errors
  105. $this->validate($values);
  106. // assert(all values are valid);
  107. $config = new Config();
  108. $config->query('BEGIN');
  109. foreach ($settings as $section => $parts) {
  110. foreach ($parts as $setting) {
  111. Config::save($section, $setting, $values[$section][$setting]);
  112. }
  113. }
  114. foreach ($booleans as $section => $parts) {
  115. foreach ($parts as $setting) {
  116. Config::save($section, $setting, $values[$section][$setting]);
  117. }
  118. }
  119. $config->query('COMMIT');
  120. return;
  121. }
  122. function validate(&$values)
  123. {
  124. // Validate biolimit
  125. if (!Validate::number($values['profile']['biolimit'])) {
  126. // TRANS: Form validation error in user admin panel when a non-numeric character limit was set.
  127. $this->clientError(_('Invalid bio limit. Must be numeric.'));
  128. }
  129. // Validate welcome text
  130. if (mb_strlen($values['newuser']['welcome']) > 255) {
  131. // TRANS: Form validation error in user admin panel when welcome text is too long.
  132. $this->clientError(_('Invalid welcome text. Maximum length is 255 characters.'));
  133. }
  134. // Validate default subscription
  135. if (!empty($values['newuser']['default'])) {
  136. $defuser = User::getKV('nickname', trim($values['newuser']['default']));
  137. if (empty($defuser)) {
  138. $this->clientError(
  139. sprintf(
  140. // TRANS: Client error displayed when trying to set a non-existing user as default subscription for new
  141. // TRANS: users in user admin panel. %1$s is the invalid nickname.
  142. _('Invalid default subscripton: "%1$s" is not a user.'),
  143. $values['newuser']['default']
  144. )
  145. );
  146. }
  147. }
  148. }
  149. }
  150. // @todo FIXME: Class documentation missing.
  151. class UserAdminPanelForm extends AdminForm
  152. {
  153. /**
  154. * ID of the form
  155. *
  156. * @return int ID of the form
  157. */
  158. function id()
  159. {
  160. return 'useradminpanel';
  161. }
  162. /**
  163. * class of the form
  164. *
  165. * @return string class of the form
  166. */
  167. function formClass()
  168. {
  169. return 'form_settings';
  170. }
  171. /**
  172. * Action of the form
  173. *
  174. * @return string URL of the action
  175. */
  176. function action()
  177. {
  178. return common_local_url('useradminpanel');
  179. }
  180. /**
  181. * Data elements of the form
  182. *
  183. * @return void
  184. */
  185. function formData()
  186. {
  187. $this->out->elementStart('fieldset', array('id' => 'settings_user-profile'));
  188. // TRANS: Fieldset legend in user administration panel.
  189. $this->out->element('legend', null, _m('LEGEND','Profile'));
  190. $this->out->elementStart('ul', 'form_data');
  191. $this->li();
  192. // TRANS: Field label in user admin panel for setting the character limit for the bio field.
  193. $this->input('biolimit', _('Bio Limit'),
  194. // TRANS: Tooltip in user admin panel for setting the character limit for the bio field.
  195. _('Maximum length of a profile bio in characters.'),
  196. 'profile');
  197. $this->unli();
  198. $this->out->elementEnd('ul');
  199. $this->out->elementEnd('fieldset');
  200. $this->out->elementStart('fieldset', array('id' => 'settings_user-newusers'));
  201. // TRANS: Form legend in user admin panel.
  202. $this->out->element('legend', null, _('New users'));
  203. $this->out->elementStart('ul', 'form_data');
  204. $this->li();
  205. // TRANS: Field label in user admin panel for setting new user welcome text.
  206. $this->input('welcome', _('New user welcome'),
  207. // TRANS: Tooltip in user admin panel for setting new user welcome text.
  208. _('Welcome text for new users (maximum 255 characters).'),
  209. 'newuser');
  210. $this->unli();
  211. $this->li();
  212. // TRANS: Field label in user admin panel for setting default subscription for new users.
  213. $this->input('default', _('Default subscription'),
  214. // TRANS: Tooltip in user admin panel for setting default subscription for new users.
  215. _('Automatically subscribe new users to this user.'),
  216. 'newuser');
  217. $this->unli();
  218. $this->out->elementEnd('ul');
  219. $this->out->elementEnd('fieldset');
  220. $this->out->elementStart('fieldset', array('id' => 'settings_user-invitations'));
  221. // TRANS: Form legend in user admin panel.
  222. $this->out->element('legend', null, _('Invitations'));
  223. $this->out->elementStart('ul', 'form_data');
  224. $this->li();
  225. // TRANS: Field label for checkbox in user admin panel for allowing users to invite friend using site e-mail.
  226. $this->out->checkbox('invite-enabled', _('Invitations enabled'),
  227. (bool) $this->value('enabled', 'invite'),
  228. // TRANS: Tooltip for checkbox in user admin panel for allowing users to invite friend using site e-mail.
  229. _('Whether to allow users to invite new users.'));
  230. $this->unli();
  231. $this->out->elementEnd('ul');
  232. $this->out->elementEnd('fieldset');
  233. }
  234. /**
  235. * Utility to simplify some of the duplicated code around
  236. * params and settings. Overrided from base class to be
  237. * more specific about input ids.
  238. *
  239. * @param string $setting Name of the setting
  240. * @param string $title Title to use for the input
  241. * @param string $instructions Instructions for this field
  242. * @param string $section config section, default = 'site'
  243. *
  244. * @return void
  245. */
  246. function input($setting, $title, $instructions, $section='site')
  247. {
  248. $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
  249. }
  250. /**
  251. * Action elements
  252. *
  253. * @return void
  254. */
  255. function formActions()
  256. {
  257. $this->out->submit('submit',
  258. // TRANS: Button text to save user settings in user admin panel.
  259. _m('BUTTON','Save'),
  260. 'submit',
  261. null,
  262. // TRANS: Button title to save user settings in user admin panel.
  263. _('Save user settings.'));
  264. }
  265. }