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 dirname(__DIR__) . '/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. %1$s is the StatusNet sitename.
146 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
147 'You can use your %1$s password to log in.');
148 $site = common_config('site', 'name');
150 $this->element('p', 'instructions',
151 sprintf($note, $site));
153 // TRANS: Button text for disconnecting a Twitter account.
154 $this->submit('disconnect', _m('BUTTON','Disconnect'));
157 $this->elementEnd('fieldset');
159 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
161 // TRANS: Fieldset legend.
162 $this->element('legend', null, _m('Preferences'));
163 $this->elementStart('ul', 'form_data');
164 $this->elementStart('li');
165 $this->checkbox('noticesend',
166 // TRANS: Checkbox label.
167 _m('Automatically send my notices to Twitter.'),
169 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
171 $this->elementEnd('li');
172 $this->elementStart('li');
173 $this->checkbox('replysync',
174 // TRANS: Checkbox label.
175 _m('Send local "@" replies to Twitter.'),
177 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
179 $this->elementEnd('li');
180 $this->elementStart('li');
181 $this->checkbox('friendsync',
182 // TRANS: Checkbox label.
183 _m('Subscribe to my Twitter friends here.'),
185 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
187 $this->elementEnd('li');
189 if (common_config('twitterimport','enabled')) {
190 $this->elementStart('li');
191 $this->checkbox('noticerecv',
192 // TRANS: Checkbox label.
193 _m('Import my friends timeline.'),
195 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
197 $this->elementEnd('li');
199 // preserve setting even if bidrection bridge toggled off
201 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
202 $this->hidden('noticerecv', true, 'noticerecv');
206 $this->elementEnd('ul');
209 // TRANS: Button text for saving Twitter integration settings.
210 $this->submit('save', _m('BUTTON','Save'));
212 // TRANS: Button text for adding Twitter integration.
213 $this->submit('add', _m('BUTTON','Add'));
216 $this->elementEnd('fieldset');
219 $this->elementEnd('form');
223 * Handle posts to this form
225 * Based on the button that was pressed, muxes out to other functions
226 * to do the actual task requested.
228 * All sub-functions reload the form with a message -- success or failure.
232 function handlePost()
235 $token = $this->trimmed('token');
236 if (!$token || $token != common_session_token()) {
237 // TRANS: Client error displayed when the session token does not match or is not given.
238 $this->showForm(_m('There was a problem with your session token. '.
239 'Try again, please.'));
243 if ($this->arg('save')) {
244 $this->savePreferences();
245 } else if ($this->arg('disconnect')) {
246 $this->removeTwitterAccount();
248 // TRANS: Client error displayed when the submitted form contains unexpected data.
249 $this->showForm(_m('Unexpected form submission.'));
254 * Disassociate an existing Twitter account from this account
258 function removeTwitterAccount()
260 $user = common_current_user();
261 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
264 // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
265 $this->clientError(_m('No Twitter connection to remove.'));
268 $result = $flink->safeDelete();
270 if (empty($result)) {
271 common_log_db_error($flink, 'DELETE', __FILE__);
272 // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
273 $this->serverError(_m('Could not remove Twitter user.'));
276 // TRANS: Success message displayed after disconnecting a Twitter account.
277 $this->showForm(_m('Twitter account disconnected.'), true);
281 * Save user's Twitter-bridging preferences
285 function savePreferences()
287 $noticesend = $this->boolean('noticesend');
288 $noticerecv = $this->boolean('noticerecv');
289 $friendsync = $this->boolean('friendsync');
290 $replysync = $this->boolean('replysync');
292 $user = common_current_user();
293 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
296 common_log_db_error($flink, 'SELECT', __FILE__);
297 // @todo FIXME: Shouldn't this be a serverError()?
298 // TRANS: Server error displayed when saving Twitter integration preferences fails.
299 $this->showForm(_m('Could not save Twitter preferences.'));
303 $original = clone($flink);
304 $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
305 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
306 $result = $flink->update($original);
308 if ($result === false) {
309 common_log_db_error($flink, 'UPDATE', __FILE__);
310 // @todo FIXME: Shouldn't this be a serverError()?
311 // TRANS: Server error displayed when saving Twitter integration preferences fails.
312 $this->showForm(_m('Could not save Twitter preferences.'));
316 if ($wasReceiving xor $noticerecv) {
317 $this->notifyDaemon($flink->foreign_id, $noticerecv);
320 // TRANS: Success message after saving Twitter integration preferences.
321 $this->showForm(_m('Twitter preferences saved.'), true);
325 * Tell the import daemon that we've updated a user's receive status.
327 function notifyDaemon($twitterUserId, $receiving)
329 // @todo... should use control signals rather than queues