3 * StatusNet, the distributed open-source microblogging tool
5 * Settings for Twitter integration
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.
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.
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/>.
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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
37 * Settings for Twitter integration
41 * @author Evan Prodromou <evan@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
47 class TwittersettingsAction extends ProfileSettingsAction
52 * @return string Title of the page
57 // TRANS: Title for page with Twitter integration settings.
58 return _m('Twitter settings');
62 * Instructions for use
64 * @return instructions for use
67 function getInstructions()
69 // TRANS: Instructions for page with Twitter integration settings.
70 return _m('Connect your Twitter account to share your updates ' .
71 'with your Twitter friends and vice-versa.');
75 * Content area of the page
77 * Shows a form for associating a Twitter account with this
78 * StatusNet account. Also lets the user set preferences.
82 function showContent()
85 $user = common_current_user();
87 $profile = $user->getProfile();
91 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
94 $fuser = $flink->getForeignUser();
97 $this->elementStart('form', array('method' => 'post',
98 'id' => 'form_settings_twitter',
99 'class' => 'form_settings',
101 common_local_url('twittersettings')));
103 $this->hidden('token', common_session_token());
105 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
108 $this->elementStart('ul', 'form_data');
109 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
110 $this->element('a', array('href' => common_local_url('twitterauthorization')),
111 // TRANS: Link description to connect to a Twitter account.
112 'Connect my Twitter account');
113 $this->elementEnd('li');
114 $this->elementEnd('ul');
116 $this->elementEnd('fieldset');
118 // TRANS: Fieldset legend.
119 $this->element('legend', null, _m('Twitter account'));
120 $this->elementStart('p', array('id' => 'form_confirmed'));
121 $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
122 $this->elementEnd('p');
123 $this->element('p', 'form_note',
124 // TRANS: Form note when a Twitter account has been connected.
125 _m('Connected Twitter account'));
126 $this->elementEnd('fieldset');
128 $this->elementStart('fieldset');
130 // TRANS: Fieldset legend.
131 $this->element('legend', null, _m('Disconnect my account from Twitter'));
133 if (!$user->password) {
134 $this->elementStart('p', array('class' => 'form_guide'));
135 // TRANS: Form guide. %s is a URL to the password settings.
136 // TRANS: This message contains a Markdown link in the form [description](link).
137 $message = sprintf(_m('Disconnecting your Twitter account ' .
138 'could make it impossible to log in! Please ' .
139 '[set a password](%s) first.'),
140 common_local_url('passwordsettings'));
141 $message = common_markup_to_html($message);
142 $this->text($message);
143 $this->elementEnd('p');
145 // TRANS: Form instructions. %s is a URL to the password settings.
146 // TRANS: %1$s is the StatusNet sitename.
147 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
148 'You can use your %1$s password to log in.');
149 $site = common_config('site', 'name');
151 $this->element('p', 'instructions',
152 sprintf($note, $site));
154 // TRANS: Button text for disconnecting a Twitter account.
155 $this->submit('disconnect', _m('BUTTON','Disconnect'));
158 $this->elementEnd('fieldset');
160 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
162 // TRANS: Fieldset legend.
163 $this->element('legend', null, _m('Preferences'));
164 $this->elementStart('ul', 'form_data');
165 $this->elementStart('li');
166 $this->checkbox('noticesend',
167 // TRANS: Checkbox label.
168 _m('Automatically send my notices to Twitter.'),
170 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
172 $this->elementEnd('li');
173 $this->elementStart('li');
174 $this->checkbox('replysync',
175 // TRANS: Checkbox label.
176 _m('Send local "@" replies to Twitter.'),
178 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
180 $this->elementEnd('li');
181 $this->elementStart('li');
182 $this->checkbox('friendsync',
183 // TRANS: Checkbox label.
184 _m('Subscribe to my Twitter friends here.'),
186 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
188 $this->elementEnd('li');
190 if (common_config('twitterimport','enabled')) {
191 $this->elementStart('li');
192 $this->checkbox('noticerecv',
193 // TRANS: Checkbox label.
194 _m('Import my friends timeline.'),
196 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
198 $this->elementEnd('li');
200 // preserve setting even if bidrection bridge toggled off
202 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
203 $this->hidden('noticerecv', true, 'noticerecv');
207 $this->elementEnd('ul');
210 // TRANS: Button text for saving Twitter integration settings.
211 $this->submit('save', _m('BUTTON','Save'));
213 // TRANS: Button text for adding Twitter integration.
214 $this->submit('add', _m('BUTTON','Add'));
217 $this->elementEnd('fieldset');
220 $this->elementEnd('form');
224 * Handle posts to this form
226 * Based on the button that was pressed, muxes out to other functions
227 * to do the actual task requested.
229 * All sub-functions reload the form with a message -- success or failure.
233 function handlePost()
236 $token = $this->trimmed('token');
237 if (!$token || $token != common_session_token()) {
238 // TRANS: Client error displayed when the session token does not match or is not given.
239 $this->showForm(_m('There was a problem with your session token. '.
240 'Try again, please.'));
244 if ($this->arg('save')) {
245 $this->savePreferences();
246 } else if ($this->arg('disconnect')) {
247 $this->removeTwitterAccount();
249 // TRANS: Client error displayed when the submitted form contains unexpected data.
250 $this->showForm(_m('Unexpected form submission.'));
255 * Disassociate an existing Twitter account from this account
259 function removeTwitterAccount()
261 $user = common_current_user();
262 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
265 // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
266 $this->clientError(_m('No Twitter connection to remove.'));
270 $result = $flink->safeDelete();
272 if (empty($result)) {
273 common_log_db_error($flink, 'DELETE', __FILE__);
274 // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
275 $this->serverError(_m('Could not remove Twitter user.'));
279 // TRANS: Success message displayed after disconnecting a Twitter account.
280 $this->showForm(_m('Twitter account disconnected.'), true);
284 * Save user's Twitter-bridging preferences
288 function savePreferences()
290 $noticesend = $this->boolean('noticesend');
291 $noticerecv = $this->boolean('noticerecv');
292 $friendsync = $this->boolean('friendsync');
293 $replysync = $this->boolean('replysync');
295 $user = common_current_user();
296 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
299 common_log_db_error($flink, 'SELECT', __FILE__);
300 // @todo FIXME: Shouldn't this be a serverError()?
301 // TRANS: Server error displayed when saving Twitter integration preferences fails.
302 $this->showForm(_m('Could not save Twitter preferences.'));
306 $original = clone($flink);
307 $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
308 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
309 $result = $flink->update($original);
311 if ($result === false) {
312 common_log_db_error($flink, 'UPDATE', __FILE__);
313 // @todo FIXME: Shouldn't this be a serverError()?
314 // TRANS: Server error displayed when saving Twitter integration preferences fails.
315 $this->showForm(_m('Could not save Twitter preferences.'));
319 if ($wasReceiving xor $noticerecv) {
320 $this->notifyDaemon($flink->foreign_id, $noticerecv);
323 // TRANS: Success message after saving Twitter integration preferences.
324 $this->showForm(_m('Twitter preferences saved.'), true);
328 * Tell the import daemon that we've updated a user's receive status.
330 function notifyDaemon($twitterUserId, $receiving)
332 // @todo... should use control signals rather than queues