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