]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/snapshotadminpanel.php
be0a793e51bb0f339b68b4ec76aa8ecd85fd4ae8
[quix0rs-gnu-social.git] / actions / snapshotadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Snapshots 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    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Manage snapshots
36  *
37  * @category Admin
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class SnapshotadminpanelAction extends AdminPanelAction
45 {
46     /**
47      * Returns the page title
48      *
49      * @return string page title
50      */
51
52     function title()
53     {
54         return _('Snapshots');
55     }
56
57     /**
58      * Instructions for using this form.
59      *
60      * @return string instructions
61      */
62
63     function getInstructions()
64     {
65         return _('Manage snapshot configuration');
66     }
67
68     /**
69      * Show the snapshots admin panel form
70      *
71      * @return void
72      */
73
74     function showForm()
75     {
76         $form = new SnapshotAdminPanelForm($this);
77         $form->show();
78         return;
79     }
80
81     /**
82      * Save settings from the form
83      *
84      * @return void
85      */
86
87     function saveSettings()
88     {
89         static $settings = array(
90             'snapshot' => array('run', 'reporturl', 'frequency')
91         );
92
93         $values = array();
94
95         foreach ($settings as $section => $parts) {
96             foreach ($parts as $setting) {
97                 $values[$section][$setting] = $this->trimmed($setting);
98             }
99         }
100
101         // This throws an exception on validation errors
102
103         $this->validate($values);
104
105         // assert(all values are valid);
106
107         $config = new Config();
108
109         $config->query('BEGIN');
110
111         foreach ($settings as $section => $parts) {
112             foreach ($parts as $setting) {
113                 Config::save($section, $setting, $values[$section][$setting]);
114             }
115         }
116
117         $config->query('COMMIT');
118
119         return;
120     }
121
122     function validate(&$values)
123     {
124         // Validate snapshot run value
125
126         if (!in_array($values['snapshot']['run'], array('web', 'cron', 'never'))) {
127             $this->clientError(_('Invalid snapshot run value.'));
128         }
129
130         // Validate snapshot frequency value
131
132         if (!Validate::number($values['snapshot']['frequency'])) {
133             $this->clientError(_('Snapshot frequency must be a number.'));
134         }
135
136         // Validate report URL
137
138         if (!is_null($values['snapshot']['reporturl'])
139             && !Validate::uri(
140                 $values['snapshot']['reporturl'],
141                 array('allowed_schemes' => array('http', 'https')
142             )
143         )) {
144             $this->clientError(_('Invalid snapshot report URL.'));
145         }
146     }
147 }
148
149 class SnapshotAdminPanelForm extends AdminForm
150 {
151     /**
152      * ID of the form
153      *
154      * @return int ID of the form
155      */
156
157     function id()
158     {
159         return 'form_snapshot_admin_panel';
160     }
161
162     /**
163      * class of the form
164      *
165      * @return string class of the form
166      */
167
168     function formClass()
169     {
170         return 'form_settings';
171     }
172
173     /**
174      * Action of the form
175      *
176      * @return string URL of the action
177      */
178
179     function action()
180     {
181         return common_local_url('snapshotadminpanel');
182     }
183
184     /**
185      * Data elements of the form
186      *
187      * @return void
188      */
189
190     function formData()
191     {
192         $this->out->elementStart(
193             'fieldset',
194             array('id' => 'settings_admin_snapshots')
195         );
196         $this->out->element('legend', null, _('Snapshots'));
197         $this->out->elementStart('ul', 'form_data');
198         $this->li();
199         $snapshot = array(
200             'web' => _('Randomly during web hit'),
201             'cron'  => _('In a scheduled job'),
202             'never' => _('Never')
203         );
204         $this->out->dropdown(
205             'run',
206             _('Data snapshots'),
207             $snapshot,
208             _('When to send statistical data to status.net servers'),
209             false,
210             $this->value('run', 'snapshot')
211         );
212         $this->unli();
213
214         $this->li();
215         $this->input(
216             'frequency',
217             _('Frequency'),
218             _('Snapshots will be sent once every N web hits'),
219             'snapshot'
220         );
221         $this->unli();
222
223         $this->li();
224         $this->input(
225             'reporturl',
226             _('Report URL'),
227             _('Snapshots will be sent to this URL'),
228             'snapshot'
229         );
230         $this->unli();
231         $this->out->elementEnd('ul');
232         $this->out->elementEnd('fieldset');
233     }
234
235     /**
236      * Action elements
237      *
238      * @return void
239      */
240
241     function formActions()
242     {
243         $this->out->submit(
244             'submit',
245             _('Save'),
246             'submit',
247             null,
248             _('Save snapshot settings')
249         );
250     }
251 }