]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/actions/twittersettings.php
Twittersettings now works better as Profilesettings extension
[quix0rs-gnu-social.git] / plugins / TwitterBridge / actions / twittersettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Settings for Twitter integration
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  * @copyright 2008-2009 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('GNUSOCIAL')) { exit(1); }
31
32 require_once dirname(__DIR__) . '/twitter.php';
33
34 /**
35  * Settings for Twitter integration
36  *
37  * @category Settings
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@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  * @see      SettingsAction
44  */
45 class TwittersettingsAction extends ProfileSettingsAction
46 {
47     protected $flink = null;
48     protected $fuser = null;
49
50     protected function doPreparation()
51     {
52         $this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
53         if ($this->flink instanceof Foreign_link) {
54             $this->fuser = $this->flink->getForeignUser();
55         }
56     }
57     /**
58      * Title of the page
59      *
60      * @return string Title of the page
61      */
62
63     function title()
64     {
65         // TRANS: Title for page with Twitter integration settings.
66         return _m('Twitter settings');
67     }
68
69     /**
70      * Instructions for use
71      *
72      * @return instructions for use
73      */
74
75     function getInstructions()
76     {
77         // TRANS: Instructions for page with Twitter integration settings.
78         return _m('Connect your Twitter account to share your updates ' .
79                   'with your Twitter friends and vice-versa.');
80     }
81
82     /**
83      * Content area of the page
84      *
85      * Shows a form for associating a Twitter account with this
86      * StatusNet account. Also lets the user set preferences.
87      *
88      * @return void
89      */
90     function showContent()
91     {
92         $this->elementStart('form', array('method' => 'post',
93                                           'id' => 'form_settings_twitter',
94                                           'class' => 'form_settings',
95                                           'action' =>
96                                           common_local_url('twittersettings')));
97
98         $this->hidden('token', common_session_token());
99
100         $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
101
102         if ($this->fuser instanceof Foreign_user) {
103             // TRANS: Fieldset legend.
104             $this->element('legend', null, _m('Twitter account'));
105             $this->elementStart('p', array('id' => 'form_confirmed'));
106             $this->element('a', array('href' => $this->fuser->uri), $this->fuser->nickname);
107             $this->elementEnd('p');
108             $this->element('p', 'form_note',
109                            // TRANS: Form note when a Twitter account has been connected.
110                            _m('Connected Twitter account'));
111             $this->elementEnd('fieldset');
112
113             $this->elementStart('fieldset');
114
115             // TRANS: Fieldset legend.
116             $this->element('legend', null, _m('Disconnect my account from Twitter'));
117
118             if (!$this->scoped->hasPassword()) {
119                 $this->elementStart('p', array('class' => 'form_guide'));
120                 // TRANS: Form guide. %s is a URL to the password settings.
121                 // TRANS: This message contains a Markdown link in the form [description](link).
122                 $message = sprintf(_m('Disconnecting your Twitter account ' .
123                                       'could make it impossible to log in! Please ' .
124                                       '[set a password](%s) first.'),
125                                    common_local_url('passwordsettings'));
126                 $message = common_markup_to_html($message);
127                 $this->text($message);
128                 $this->elementEnd('p');
129             } else {
130                 // TRANS: Form instructions. %1$s is the StatusNet sitename.
131                 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
132                     'You can use your %1$s password to log in.');
133                 $site = common_config('site', 'name');
134
135                 $this->element('p', 'instructions',
136                     sprintf($note, $site));
137
138                 // TRANS: Button text for disconnecting a Twitter account.
139                 $this->submit('disconnect', _m('BUTTON','Disconnect'));
140             }
141
142             $this->elementEnd('fieldset');
143
144             $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
145
146             // TRANS: Fieldset legend.
147             $this->element('legend', null, _m('Preferences'));
148             $this->elementStart('ul', 'form_data');
149             $this->elementStart('li');
150             $this->checkbox('noticesend',
151                             // TRANS: Checkbox label.
152                             _m('Automatically send my notices to Twitter.'),
153                             $this->flink->noticesync & FOREIGN_NOTICE_SEND);
154             $this->elementEnd('li');
155             $this->elementStart('li');
156             $this->checkbox('replysync',
157                             // TRANS: Checkbox label.
158                             _m('Send local "@" replies to Twitter.'),
159                             $this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY);
160             $this->elementEnd('li');
161             $this->elementStart('li');
162             $this->checkbox('friendsync',
163                             // TRANS: Checkbox label.
164                             _m('Subscribe to my Twitter friends here.'),
165                             $this->flink->friendsync & FOREIGN_FRIEND_RECV);
166             $this->elementEnd('li');
167
168             if (common_config('twitterimport','enabled')) {
169                 $this->elementStart('li');
170                 $this->checkbox('noticerecv',
171                                 // TRANS: Checkbox label.
172                                 _m('Import my friends timeline.'),
173                                 $this->flink->noticesync & FOREIGN_NOTICE_RECV);
174                 $this->elementEnd('li');
175             } else {
176                 // preserve setting even if bidrection bridge toggled off
177
178                 if ($this->flink->noticesync & FOREIGN_NOTICE_RECV) {
179                     $this->hidden('noticerecv', true, 'noticerecv');
180                 }
181             }
182
183             $this->elementEnd('ul');
184
185             if ($this->flink) {
186                 // TRANS: Button text for saving Twitter integration settings.
187                 $this->submit('save', _m('BUTTON','Save'));
188             } else {
189                 // TRANS: Button text for adding Twitter integration.
190                 $this->submit('add', _m('BUTTON','Add'));
191             }
192         } else {
193             $this->elementStart('ul', 'form_data');
194             $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
195             $this->element('a', array('href' => common_local_url('twitterauthorization')),
196                            // TRANS: Link description to connect to a Twitter account.
197                            'Connect my Twitter account');
198             $this->elementEnd('li');
199             $this->elementEnd('ul');
200         }
201
202         $this->elementEnd('fieldset');
203
204         $this->elementEnd('form');
205     }
206
207     /**
208      * Handle posts to this form
209      *
210      * Based on the button that was pressed, muxes out to other functions
211      * to do the actual task requested.
212      *
213      * All sub-functions reload the form with a message -- success or failure.
214      *
215      * @return void
216      */
217     function handlePost()
218     {
219         // CSRF protection
220         $token = $this->trimmed('token');
221         if (!$token || $token != common_session_token()) {
222             // TRANS: Client error displayed when the session token does not match or is not given.
223             $this->showForm(_m('There was a problem with your session token. '.
224                                'Try again, please.'));
225             return;
226         }
227
228         if ($this->arg('save')) {
229             $this->savePreferences();
230         } else if ($this->arg('disconnect')) {
231             $this->removeTwitterAccount();
232         } else {
233             // TRANS: Client error displayed when the submitted form contains unexpected data.
234             $this->showForm(_m('Unexpected form submission.'));
235         }
236     }
237
238     /**
239      * Disassociate an existing Twitter account from this account
240      *
241      * @return void
242      */
243     function removeTwitterAccount()
244     {
245         $user = common_current_user();
246         $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
247
248         if (empty($flink)) {
249             // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
250             $this->clientError(_m('No Twitter connection to remove.'));
251         }
252
253         $result = $flink->safeDelete();
254
255         if (empty($result)) {
256             common_log_db_error($flink, 'DELETE', __FILE__);
257             // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
258             $this->serverError(_m('Could not remove Twitter user.'));
259         }
260
261         // TRANS: Success message displayed after disconnecting a Twitter account.
262         $this->showForm(_m('Twitter account disconnected.'), true);
263     }
264
265     /**
266      * Save user's Twitter-bridging preferences
267      *
268      * @return void
269      */
270     function savePreferences()
271     {
272         $noticesend = $this->boolean('noticesend');
273         $noticerecv = $this->boolean('noticerecv');
274         $friendsync = $this->boolean('friendsync');
275         $replysync  = $this->boolean('replysync');
276
277         if (!$this->flink instanceof Foreign_link) {
278             common_log_db_error($this->flink, 'SELECT', __FILE__);
279             // TRANS: Server error displayed when saving Twitter integration preferences fails.
280             throw new ServerException(_m('Your account is not linked to Twitter.'));
281         }
282
283         $original = clone($this->flink);
284         $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
285         $this->flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
286         $result = $this->flink->update($original);
287
288         if ($result === false) {
289             common_log_db_error($this->flink, 'UPDATE', __FILE__);
290             // TRANS: Server error displayed when saving Twitter integration preferences fails.
291             throw new ServerException(_m('Could not save Twitter preferences.'));
292         }
293
294         if ($wasReceiving xor $noticerecv) {
295             $this->notifyDaemon($this->flink->foreign_id, $noticerecv);
296         }
297
298         // TRANS: Success message after saving Twitter integration preferences.
299         $this->showForm(_m('Twitter preferences saved.'), true);
300     }
301
302     /**
303      * Tell the import daemon that we've updated a user's receive status.
304      */
305     function notifyDaemon($twitterUserId, $receiving)
306     {
307         // @todo... should use control signals rather than queues
308     }
309 }