useradminpanel.php 9.3 KB

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