]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitteradminpanel.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitteradminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Twitter bridge 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  * Administer global Twitter bridge settings
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 class TwitteradminpanelAction extends AdminPanelAction
44 {
45     /**
46      * Returns the page title
47      *
48      * @return string page title
49      */
50     function title()
51     {
52         // TRANS: Page title for Twitter administration panel.
53         return _m('TITLE','Twitter');
54     }
55
56     /**
57      * Instructions for using this form.
58      *
59      * @return string instructions
60      */
61     function getInstructions()
62     {
63         // TRANS: Instructions for Twitter bridge administration page.
64         return _m('Twitter bridge settings');
65     }
66
67     /**
68      * Show the Twitter admin panel form
69      *
70      * @return void
71      */
72     function showForm()
73     {
74         $form = new TwitterAdminPanelForm($this);
75         $form->show();
76         return;
77     }
78
79     /**
80      * Save settings from the form
81      *
82      * @return void
83      */
84     function saveSettings()
85     {
86         static $settings = array(
87             'twitter'     => array('consumer_key', 'consumer_secret'),
88             'integration' => array('source')
89         );
90
91         static $booleans = array(
92             'twitter'       => array('signin')
93         );
94         if (Event::handle('TwitterBridgeAdminImportControl')) {
95             $booleans['twitterimport'] = array('enabled');
96         }
97
98         $values = array();
99
100         foreach ($settings as $section => $parts) {
101             foreach ($parts as $setting) {
102                 $values[$section][$setting]
103                     = $this->trimmed($setting);
104             }
105         }
106
107         foreach ($booleans as $section => $parts) {
108             foreach ($parts as $setting) {
109                 $values[$section][$setting]
110                     = ($this->boolean($setting)) ? 1 : 0;
111             }
112         }
113
114         // This throws an exception on validation errors
115
116         $this->validate($values);
117
118         // assert(all values are valid);
119
120         $config = new Config();
121
122         $config->query('BEGIN');
123
124         foreach ($settings as $section => $parts) {
125             foreach ($parts as $setting) {
126                 Config::save($section, $setting, $values[$section][$setting]);
127             }
128         }
129
130         foreach ($booleans as $section => $parts) {
131             foreach ($parts as $setting) {
132                 Config::save($section, $setting, $values[$section][$setting]);
133             }
134         }
135
136         $config->query('COMMIT');
137
138         // Flush the router cache: we may have enabled/disabled bridging,
139         // which will add or remove some actions.
140         $cache = Cache::instance();
141         $cache->delete(Router::cacheKey());
142
143         return;
144     }
145
146     function validate(&$values)
147     {
148         // Validate consumer key and secret (can't be too long)
149
150         if (mb_strlen($values['twitter']['consumer_key']) > 255) {
151             $this->clientError(
152                 // TRANS: Client error displayed when a consumer key is invalid because it is too long.
153                 _m('Invalid consumer key. Maximum length is 255 characters.')
154             );
155         }
156
157         if (mb_strlen($values['twitter']['consumer_secret']) > 255) {
158             $this->clientError(
159                 // TRANS: Client error displayed when a consumer secret is invalid because it is too long.
160                 _m('Invalid consumer secret. Maximum length is 255 characters.')
161             );
162         }
163     }
164
165     function isImportEnabled()
166     {
167         // Since daemon setup isn't automated yet...
168         // @todo: if merged into main queues, detect presence of daemon config
169         return true;
170     }
171 }
172
173 class TwitterAdminPanelForm extends AdminForm
174 {
175     /**
176      * ID of the form
177      *
178      * @return int ID of the form
179      */
180     function id()
181     {
182         return 'twitteradminpanel';
183     }
184
185     /**
186      * class of the form
187      *
188      * @return string class of the form
189      */
190     function formClass()
191     {
192         return 'form_settings';
193     }
194
195     /**
196      * Action of the form
197      *
198      * @return string URL of the action
199      */
200     function action()
201     {
202         return common_local_url('twitteradminpanel');
203     }
204
205     /**
206      * Data elements of the form
207      *
208      * @return void
209      */
210     function formData()
211     {
212         $this->out->elementStart(
213             'fieldset',
214             array('id' => 'settings_twitter-application')
215         );
216         // TRANS: Fieldset legend for Twitter application settings.
217         $this->out->element('legend', null, _m('Twitter application settings'));
218         $this->out->elementStart('ul', 'form_data');
219
220         $this->li();
221         $this->input(
222             'consumer_key',
223             // TRANS: Field label for Twitter assigned consumer key.
224             _m('Consumer key'),
225             // TRANS: Field title for Twitter assigned consumer key.
226             _m('The consumer key assigned by Twitter.'),
227             'twitter'
228         );
229         $this->unli();
230
231         $this->li();
232         $this->input(
233             'consumer_secret',
234             // TRANS: Field label for Twitter assigned consumer secret.
235             _m('Consumer secret'),
236             // TRANS: Field title for Twitter assigned consumer secret.
237             _m('The consumer secret assigned by Twitter.'),
238             'twitter'
239         );
240         $this->unli();
241
242         $globalConsumerKey = common_config('twitter', 'global_consumer_key');
243         $globalConsumerSec = common_config('twitter', 'global_consumer_secret');
244
245         if (!empty($globalConsumerKey) && !empty($globalConsumerSec)) {
246             $this->li();
247             // TRANS: Form guide displayed when two required fields have already been provided.
248             $this->out->element('p', 'form_guide', _m('Note: A global consumer key and secret are set.'));
249             $this->unli();
250         }
251
252         $this->li();
253         $this->input(
254             'source',
255             // TRANS: Field label for Twitter application name.
256             _m('Integration source'),
257             // TRANS: Field title for Twitter application name.
258             _m('The name of your Twitter application.'),
259             'integration'
260         );
261         $this->unli();
262
263         $this->out->elementEnd('ul');
264         $this->out->elementEnd('fieldset');
265
266         $this->out->elementStart(
267             'fieldset',
268             array('id' => 'settings_twitter-options')
269         );
270         // TRANS: Fieldset legend for Twitter integration options.
271         $this->out->element('legend', null, _m('Options'));
272
273         $this->out->elementStart('ul', 'form_data');
274
275         $this->li();
276
277         $this->out->checkbox(
278             // TRANS: Checkbox label for global setting.
279             'signin', _m('Enable "Sign-in with Twitter"'),
280             (bool) $this->value('signin', 'twitter'),
281             // TRANS: Checkbox title.
282             _m('This allow users to login with their Twitter credentials.')
283         );
284         $this->unli();
285
286         if (Event::handle('TwitterBridgeAdminImportControl')) {
287             $this->li();
288             $this->out->checkbox(
289                 // TRANS: Checkbox label for global setting.
290                 'enabled', _m('Enable Twitter import'),
291                 (bool) $this->value('enabled', 'twitterimport'),
292                 // TRANS: Checkbox title for global setting.
293                 _m('Allow users to import their Twitter friends\' timelines. Requires daemons to be manually configured.')
294             );
295             $this->unli();
296         }
297
298         $this->out->elementEnd('ul');
299
300         $this->out->elementEnd('fieldset');
301     }
302
303     /**
304      * Action elements
305      *
306      * @return void
307      */
308     function formActions()
309     {
310         // TRANS: Button text for saving the administrative Twitter bridge settings.
311         $this->out->submit('submit', _m('BUTTON','Save'), 'submit', null,
312         // TRANS: Button title for saving the administrative Twitter bridge settings.
313         _m('Save the Twitter bridge settings.'));
314     }
315 }