]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/siteadminpanel.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / actions / siteadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Site 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  * 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  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2010 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * Administer site settings
38  *
39  * @category Admin
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Zach Copley <zach@status.net>
43  * @author   Sarven Capadisli <csarven@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47 class SiteadminpanelAction extends AdminPanelAction
48 {
49     /**
50      * Returns the page title
51      *
52      * @return string page title
53      */
54     function title()
55     {
56         // TRANS: Title for site administration panel.
57         return _m('TITLE','Site');
58     }
59
60     /**
61      * Instructions for using this form.
62      *
63      * @return string instructions
64      */
65     function getInstructions()
66     {
67         // TRANS: Instructions for site administration panel.
68         return _('Basic settings for this StatusNet site');
69     }
70
71     /**
72      * Show the site admin panel form
73      *
74      * @return void
75      */
76     function showForm()
77     {
78         $form = new SiteAdminPanelForm($this);
79         $form->show();
80         return;
81     }
82
83     /**
84      * Save settings from the form
85      *
86      * @return void
87      */
88     function saveSettings()
89     {
90         static $settings = array(
91             'site' => array('name', 'broughtby', 'broughtbyurl',
92             'email', 'timezone', 'language',
93             'site', 'textlimit', 'dupelimit'),
94         );
95
96         $values = array();
97
98         foreach ($settings as $section => $parts) {
99             foreach ($parts as $setting) {
100                 $values[$section][$setting] = $this->trimmed($setting);
101             }
102         }
103
104         // This throws an exception on validation errors
105
106         $this->validate($values);
107
108         // assert(all values are valid);
109
110         $config = new Config();
111
112         $config->query('BEGIN');
113
114         foreach ($settings as $section => $parts) {
115             foreach ($parts as $setting) {
116                 Config::save($section, $setting, $values[$section][$setting]);
117             }
118         }
119
120         $config->query('COMMIT');
121
122         return;
123     }
124
125     function validate(&$values)
126     {
127         // Validate site name
128
129         if (empty($values['site']['name'])) {
130             // TRANS: Client error displayed trying to save an empty site name.
131             $this->clientError(_('Site name must have non-zero length.'));
132         }
133
134         // Validate email
135
136         $values['site']['email'] = common_canonical_email($values['site']['email']);
137
138         if (empty($values['site']['email'])) {
139             // TRANS: Client error displayed trying to save site settings without a contact address.
140             $this->clientError(_('You must have a valid contact email address.'));
141         }
142         if (!Validate::email($values['site']['email'], common_config('email', 'check_domain'))) {
143             // TRANS: Client error displayed trying to save site settings without a valid contact address.
144             $this->clientError(_('Not a valid email address.'));
145         }
146
147         // Validate timezone
148
149         if (is_null($values['site']['timezone']) ||
150             !in_array($values['site']['timezone'], DateTimeZone::listIdentifiers())) {
151             // TRANS: Client error displayed trying to save site settings without a timezone.
152             $this->clientError(_('Timezone not selected.'));
153             return;
154         }
155
156         // Validate language
157
158         if (!is_null($values['site']['language']) &&
159             !in_array($values['site']['language'], array_keys(get_nice_language_list()))) {
160             // TRANS: Client error displayed trying to save site settings with an invalid language code.
161             // TRANS: %s is the invalid language code.
162             $this->clientError(sprintf(_('Unknown language "%s".'), $values['site']['language']));
163         }
164
165         // Validate text limit
166
167         if (!Validate::number($values['site']['textlimit'], array('min' => 0))) {
168             // TRANS: Client error displayed trying to save site settings with a text limit below 0.
169             $this->clientError(_('Minimum text limit is 0 (unlimited).'));
170         }
171
172         // Validate dupe limit
173
174         if (!Validate::number($values['site']['dupelimit'], array('min' => 1))) {
175             // TRANS: Client error displayed trying to save site settings with a text limit below 1.
176             $this->clientError(_("Dupe limit must be one or more seconds."));
177         }
178     }
179 }
180
181 // @todo FIXME: Class documentation missing.
182 class SiteAdminPanelForm extends AdminForm
183 {
184     /**
185      * ID of the form
186      *
187      * @return int ID of the form
188      */
189     function id()
190     {
191         return 'form_site_admin_panel';
192     }
193
194     /**
195      * class of the form
196      *
197      * @return string class of the form
198      */
199     function formClass()
200     {
201         return 'form_settings';
202     }
203
204     /**
205      * Action of the form
206      *
207      * @return string URL of the action
208      */
209     function action()
210     {
211         return common_local_url('siteadminpanel');
212     }
213
214     /**
215      * Data elements of the form
216      *
217      * @return void
218      */
219     function formData()
220     {
221         $this->out->elementStart('fieldset', array('id' => 'settings_admin_general'));
222         // TRANS: Fieldset legend on site settings panel.
223         $this->out->element('legend', null, _m('LEGEND','General'));
224         $this->out->elementStart('ul', 'form_data');
225         $this->li();
226         // TRANS: Field label on site settings panel.
227         $this->input('name', _m('LABEL','Site name'),
228                      // TRANS: Field title on site settings panel.
229                      _('The name of your site, like "Yourcompany Microblog".'));
230         $this->unli();
231
232         $this->li();
233         // TRANS: Field label on site settings panel.
234         $this->input('broughtby', _('Brought by'),
235                      // TRANS: Field title on site settings panel.
236                      _('Text used for credits link in footer of each page.'));
237         $this->unli();
238
239         $this->li();
240         // TRANS: Field label on site settings panel.
241         $this->input('broughtbyurl', _('Brought by URL'),
242                      // TRANS: Field title on site settings panel.
243                      _('URL used for credits link in footer of each page.'));
244         $this->unli();
245         $this->li();
246         // TRANS: Field label on site settings panel.
247         $this->input('email', _('Email'),
248                      // TRANS: Field title on site settings panel.
249                      _('Contact email address for your site.'));
250         $this->unli();
251         $this->out->elementEnd('ul');
252         $this->out->elementEnd('fieldset');
253
254         $this->out->elementStart('fieldset', array('id' => 'settings_admin_local'));
255         // TRANS: Fieldset legend on site settings panel.
256         $this->out->element('legend', null, _m('LEGEND','Local'));
257         $this->out->elementStart('ul', 'form_data');
258         $timezones = array();
259
260         foreach (DateTimeZone::listIdentifiers() as $k => $v) {
261             $timezones[$v] = $v;
262         }
263
264         asort($timezones);
265
266         $this->li();
267         // TRANS: Dropdown label on site settings panel.
268         $this->out->dropdown('timezone', _('Default timezone'),
269                              // TRANS: Dropdown title on site settings panel.
270                              $timezones, _('Default timezone for the site; usually UTC.'),
271                              true, $this->value('timezone'));
272         $this->unli();
273
274         $this->li();
275         $this->out->dropdown('language',
276                              // TRANS: Dropdown label on site settings panel.
277                              _('Default language'),
278                              get_nice_language_list(),
279                              // TRANS: Dropdown title on site settings panel.
280                              _('Site language when autodetection from browser settings is not available'),
281                              false, $this->value('language'));
282         $this->unli();
283
284         $this->out->elementEnd('ul');
285         $this->out->elementEnd('fieldset');
286
287         $this->out->elementStart('fieldset', array('id' => 'settings_admin_limits'));
288         // TRANS: Fieldset legend on site settings panel.
289         $this->out->element('legend', null, _m('LEGEND','Limits'));
290         $this->out->elementStart('ul', 'form_data');
291         $this->li();
292         $this->input('textlimit',
293                      // TRANS: Field label on site settings panel.
294                      _('Text limit'),
295                      // TRANS: Field title on site settings panel.
296                      _('Maximum number of characters for notices.'));
297         $this->unli();
298
299         $this->li();
300         $this->input('dupelimit',
301                      // TRANS: Field label on site settings panel.
302                      _('Dupe limit'),
303                      // TRANS: Field title on site settings panel.
304                      _('How long users must wait (in seconds) to post the same thing again.'));
305         $this->unli();
306         $this->out->elementEnd('ul');
307         $this->out->elementEnd('fieldset');
308     }
309
310     /**
311      * Action elements
312      *
313      * @return void
314      */
315     function formActions()
316     {
317         $this->out->submit('submit',
318                            // TRANS: Button text for saving site settings.
319                            _m('BUTTON','Save'),
320                            'submit',
321                            null,
322                            // TRANS: Button title for saving site settings.
323                            _('Save site settings'));
324     }
325 }