]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/siteadminpanel.php
cd33d5adb1118c32563e1703034313d437b25c3e
[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-2009 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
48 class SiteadminpanelAction extends AdminPanelAction
49 {
50     /**
51      * Returns the page title
52      *
53      * @return string page title
54      */
55
56     function title()
57     {
58         return _('Site');
59     }
60
61     /**
62      * Instructions for using this form.
63      *
64      * @return string instructions
65      */
66
67     function getInstructions()
68     {
69         return _('Basic settings for this StatusNet site.');
70     }
71
72     /**
73      * Show the site admin panel form
74      *
75      * @return void
76      */
77
78     function showForm()
79     {
80         $form = new SiteAdminPanelForm($this);
81         $form->show();
82         return;
83     }
84
85     /**
86      * Save settings from the form
87      *
88      * @return void
89      */
90
91     function saveSettings()
92     {
93         static $settings = array('name', 'broughtby', 'broughtbyurl',
94                                  'email', 'timezone', 'language');
95         static $booleans = array('private');
96
97         $values = array();
98
99         foreach ($settings as $setting) {
100             $values[$setting] = $this->trimmed($setting);
101         }
102
103         foreach ($booleans as $setting) {
104             $values[$setting] = ($this->boolean($setting)) ? 1 : 0;
105         }
106
107         // This throws an exception on validation errors
108
109         $this->validate($values);
110
111         // assert(all values are valid);
112
113         $config = new Config();
114
115         $config->query('BEGIN');
116
117         foreach (array_merge($settings, $booleans) as $setting) {
118             Config::save('site', $setting, $values[$setting]);
119         }
120
121         $config->query('COMMIT');
122
123         return;
124     }
125
126     function validate(&$values)
127     {
128         // Validate site name
129
130         if (empty($values['name'])) {
131             $this->clientError(_("Site name must have non-zero length."));
132         }
133
134         // Validate email
135
136         $values['email'] = common_canonical_email($values['email']);
137
138         if (empty($values['email'])) {
139             $this->clientError(_('You must have a valid contact email address'));
140         }
141         if (!Validate::email($values['email'], common_config('email', 'check_domain'))) {
142             $this->clientError(_('Not a valid email address'));
143         }
144
145         // Validate timezone
146
147         if (is_null($values['timezone']) ||
148             !in_array($values['timezone'], DateTimeZone::listIdentifiers())) {
149             $this->clientError(_('Timezone not selected.'));
150             return;
151         }
152
153         // Validate language
154
155         if (!is_null($language) && !in_array($language, array_keys(get_nice_language_list()))) {
156             $this->clientError(sprintf(_('Unknown language "%s"'), $language));
157         }
158     }
159 }
160
161 class SiteAdminPanelForm extends Form
162 {
163     /**
164      * ID of the form
165      *
166      * @return int ID of the form
167      */
168
169     function id()
170     {
171         return 'siteadminpanel';
172     }
173
174     /**
175      * class of the form
176      *
177      * @return string class of the form
178      */
179
180     function formClass()
181     {
182         return 'form_site_admin_panel';
183     }
184
185     /**
186      * Action of the form
187      *
188      * @return string URL of the action
189      */
190
191     function action()
192     {
193         return common_local_url('siteadminpanel');
194     }
195
196     /**
197      * Data elements of the form
198      *
199      * @return void
200      */
201
202     function formData()
203     {
204         $this->out->elementStart('ul', 'form_data');
205         $this->li();
206         $this->input('name', _('Site name'),
207                      _('The name of your site, like "Yourcompany Microblog"'));
208         $this->unli();
209         $this->li();
210         $this->input('broughtby', _('Brought by'),
211                      _('Text used for credits link in footer of each page'));
212         $this->unli();
213         $this->li();
214         $this->input('broughtbyurl', _('Brought by URL'),
215                      _('URL used for credits link in footer of each page'));
216         $this->unli();
217         $this->li();
218         $this->input('email', _('Email'),
219                      _('contact email address for your site'));
220
221         $this->unli();
222
223         $timezones = array();
224
225         foreach (DateTimeZone::listIdentifiers() as $k => $v) {
226             $timezones[$v] = $v;
227         }
228
229         asort($timezones);
230
231         $this->li();
232
233         $this->out->dropdown('timezone', _('Default timezone'),
234                              $timezones, _('Default timezone for the site; usually UTC.'),
235                              true, $this->value('timezone'));
236
237         $this->unli();
238         $this->li();
239
240         $this->out->dropdown('language', _('Language'),
241                              get_nice_language_list(), _('Default site language'),
242                              false, $this->value('language'));
243
244         $this->unli();
245         $this->li();
246
247         $this->out->checkbox('private', _('Private'),
248                              (bool) $this->value('private'),
249                              _('Prohibit anonymous users (not logged in) from viewing site?'));
250
251         $this->unli();
252
253         $this->out->elementEnd('ul');
254     }
255
256     /**
257      * Utility to simplify some of the duplicated code around
258      * params and settings.
259      *
260      * @param string $setting      Name of the setting
261      * @param string $title        Title to use for the input
262      * @param string $instructions Instructions for this field
263      *
264      * @return void
265      */
266
267     function input($setting, $title, $instructions)
268     {
269         $this->out->input($setting, $title, $this->value($setting), $instructions);
270     }
271
272     /**
273      * Utility to simplify getting the posted-or-stored setting value
274      *
275      * @param string $setting Name of the setting
276      *
277      * @return string param value if posted, or current config value
278      */
279
280     function value($setting)
281     {
282         $value = $this->out->trimmed($setting);
283         if (empty($value)) {
284             $value = common_config('site', $setting);
285         }
286         return $value;
287     }
288
289     function li()
290     {
291         $this->out->elementStart('li');
292     }
293
294     function unli()
295     {
296         $this->out->elementEnd('li');
297     }
298
299     /**
300      * Action elements
301      *
302      * @return void
303      */
304
305     function formActions()
306     {
307         $this->out->submit('submit', _('Save'), 'submit', null, _('Save site settings'));
308     }
309 }