]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/siteadminpanel.php
Merge branch '0.9.x' into adminpanel
[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                                  'closed', 'inviteonly', 'private');
96
97         $values = array();
98
99         foreach ($settings as $setting) {
100             $values[$setting] = $this->trimmed($setting);
101         }
102
103         // This throws an exception on validation errors
104
105         $this->validate($values);
106
107         // assert(all values are valid);
108
109         $config = new Config();
110
111         $config->query('BEGIN');
112
113         foreach ($settings as $setting) {
114             Config::save('site', $setting, $values[$setting]);
115         }
116
117         $config->query('COMMIT');
118
119         return;
120     }
121
122     function validate(&$values)
123     {
124         // Validate site name
125
126         if (empty($values['name'])) {
127             $this->clientError(_("Site name must have non-zero length."));
128         }
129
130         // Validate email
131
132         $values['email'] = common_canonical_email($values['email']);
133
134         if (empty($values['email'])) {
135             $this->clientError(_('You must have a valid contact email address'));
136         }
137         if (!Validate::email($values['email'], common_config('email', 'check_domain'))) {
138             $this->clientError(_('Not a valid email address'));
139         }
140
141         // Validate timezone
142
143         if (is_null($values['timezone']) ||
144             !in_array($values['timezone'], DateTimeZone::listIdentifiers())) {
145             $this->clientError(_('Timezone not selected.'));
146             return;
147         }
148
149         // Validate language
150
151         if (!is_null($language) && !in_array($language, array_keys(get_nice_language_list()))) {
152             $this->clientError(sprintf(_('Unknown language "%s"'), $language));
153         }
154     }
155 }
156
157 class SiteAdminPanelForm extends Form
158 {
159     /**
160      * ID of the form
161      *
162      * @return int ID of the form
163      */
164
165     function id()
166     {
167         return 'siteadminpanel';
168     }
169
170     /**
171      * class of the form
172      *
173      * @return string class of the form
174      */
175
176     function formClass()
177     {
178         return 'form_site_admin_panel';
179     }
180
181     /**
182      * Action of the form
183      *
184      * @return string URL of the action
185      */
186
187     function action()
188     {
189         return common_local_url('siteadminpanel');
190     }
191
192     /**
193      * Data elements of the form
194      *
195      * @return void
196      */
197
198     function formData()
199     {
200         $this->input('name', _('Site name'),
201                      _('The name of your site, like "Yourcompany Microblog"'));
202         $this->input('broughtby', _('Brought by'),
203                      _('Text used for credits link in footer of each page'));
204         $this->input('broughtbyurl', _('Brought by URL'),
205                      _('URL used for credits link in footer of each page'));
206         $this->input('email', _('Email'),
207                      _('contact email address for your site'));
208
209         $timezones = array();
210
211         foreach (DateTimeZone::listIdentifiers() as $k => $v) {
212             $timezones[$v] = $v;
213         }
214
215         asort($timezones);
216
217         $this->out->dropdown('timezone', _('Default timezone'),
218                              $timezones, _('Default timezone for the site; usually UTC.'),
219                              true, $this->value('timezone'));
220
221         $this->out->dropdown('language', _('Language'),
222                              get_nice_language_list(), _('Default site language'),
223                              false, $this->value('language'));
224
225         $this->out->checkbox('closed', _('Closed'),
226                              (bool) $this->value('closed'),
227                              _('Is registration on this site prohibited?'));
228
229         $this->out->checkbox('inviteonly', _('Invite-only'),
230                              (bool) $this->value('inviteonly'),
231                              _('Is registration on this site only open to invited users?'));
232
233         $this->out->checkbox('private', _('Private'),
234                              (bool) $this->value('private'),
235                              _('Prohibit anonymous users (not logged in) from viewing site?'));
236     }
237
238     /**
239      * Utility to simplify some of the duplicated code around
240      * params and settings.
241      *
242      * @param string $setting      Name of the setting
243      * @param string $title        Title to use for the input
244      * @param string $instructions Instructions for this field
245      *
246      * @return void
247      */
248
249     function input($setting, $title, $instructions)
250     {
251         $this->out->input($setting, $title, $this->value($setting), $instructions);
252     }
253
254     /**
255      * Utility to simplify getting the posted-or-stored setting value
256      *
257      * @param string $setting Name of the setting
258      *
259      * @return string param value if posted, or current config value
260      */
261
262     function value($setting)
263     {
264         $value = $this->out->trimmed($setting);
265         if (empty($value)) {
266             $value = common_config('site', $setting);
267         }
268         return $value;
269     }
270
271     /**
272      * Action elements
273      *
274      * @return void
275      */
276
277     function formActions()
278     {
279         $this->out->submit('submit', _('Save'), 'submit', null, _('Save site settings'));
280     }
281 }