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('GNUSOCIAL')) { exit(1); }
32 require_once dirname(__DIR__) . '/twitter.php';
35 * Settings for Twitter integration
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/
45 class TwittersettingsAction extends ProfileSettingsAction
47 protected $flink = null;
48 protected $fuser = null;
50 protected function doPreparation()
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!
62 * @return string Title of the page
67 // TRANS: Title for page with Twitter integration settings.
68 return _m('Twitter settings');
72 * Instructions for use
74 * @return instructions for use
77 function getInstructions()
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.');
85 * Content area of the page
87 * Shows a form for associating a Twitter account with this
88 * StatusNet account. Also lets the user set preferences.
92 function showContent()
94 $this->elementStart('form', array('method' => 'post',
95 'id' => 'form_settings_twitter',
96 'class' => 'form_settings',
98 common_local_url('twittersettings')));
100 $this->hidden('token', common_session_token());
102 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
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');
115 $this->elementStart('fieldset');
117 // TRANS: Fieldset legend.
118 $this->element('legend', null, _m('Disconnect my account from Twitter'));
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');
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');
137 $this->element('p', 'instructions',
138 sprintf($note, $site));
140 // TRANS: Button text for disconnecting a Twitter account.
141 $this->submit('disconnect', _m('BUTTON','Disconnect'));
144 $this->elementEnd('fieldset');
146 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
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');
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');
178 // preserve setting even if bidrection bridge toggled off
180 if ($this->flink->noticesync & FOREIGN_NOTICE_RECV) {
181 $this->hidden('noticerecv', true, 'noticerecv');
185 $this->elementEnd('ul');
188 // TRANS: Button text for saving Twitter integration settings.
189 $this->submit('save', _m('BUTTON','Save'));
191 // TRANS: Button text for adding Twitter integration.
192 $this->submit('add', _m('BUTTON','Add'));
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');
204 $this->elementEnd('fieldset');
206 $this->elementEnd('form');
210 * Handle posts to this form
212 * Based on the button that was pressed, muxes out to other functions
213 * to do the actual task requested.
215 * All sub-functions reload the form with a message -- success or failure.
219 function handlePost()
222 $token = $this->trimmed('token');
223 if (!$token || $token != common_session_token()) {
224 // TRANS: Client error displayed when the session token does not match or is not given.
225 $this->showForm(_m('There was a problem with your session token. '.
226 'Try again, please.'));
230 if ($this->arg('save')) {
231 $this->savePreferences();
232 } else if ($this->arg('disconnect')) {
233 $this->removeTwitterAccount();
235 // TRANS: Client error displayed when the submitted form contains unexpected data.
236 $this->showForm(_m('Unexpected form submission.'));
241 * Disassociate an existing Twitter account from this account
245 function removeTwitterAccount()
247 $user = common_current_user();
248 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
251 // TRANS: Client error displayed when trying to remove a connected Twitter account when there isn't one connected.
252 $this->clientError(_m('No Twitter connection to remove.'));
255 $result = $flink->safeDelete();
257 if (empty($result)) {
258 common_log_db_error($flink, 'DELETE', __FILE__);
259 // TRANS: Server error displayed when trying to remove a connected Twitter account fails.
260 $this->serverError(_m('Could not remove Twitter user.'));
263 // TRANS: Success message displayed after disconnecting a Twitter account.
264 $this->showForm(_m('Twitter account disconnected.'), true);
268 * Save user's Twitter-bridging preferences
272 function savePreferences()
274 $noticesend = $this->boolean('noticesend');
275 $noticerecv = $this->boolean('noticerecv');
276 $friendsync = $this->boolean('friendsync');
277 $replysync = $this->boolean('replysync');
279 if (!$this->flink instanceof Foreign_link) {
280 common_log_db_error($this->flink, 'SELECT', __FILE__);
281 // TRANS: Server error displayed when saving Twitter integration preferences fails.
282 throw new ServerException(_m('Your account is not linked to Twitter.'));
285 $original = clone($this->flink);
286 $wasReceiving = (bool)($original->noticesync & FOREIGN_NOTICE_RECV);
287 $this->flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
288 $result = $this->flink->update($original);
290 if ($result === false) {
291 common_log_db_error($this->flink, 'UPDATE', __FILE__);
292 // TRANS: Server error displayed when saving Twitter integration preferences fails.
293 throw new ServerException(_m('Could not save Twitter preferences.'));
296 if ($wasReceiving xor $noticerecv) {
297 $this->notifyDaemon($this->flink->foreign_id, $noticerecv);
300 // TRANS: Success message after saving Twitter integration preferences.
301 $this->showForm(_m('Twitter preferences saved.'), true);
305 * Tell the import daemon that we've updated a user's receive status.
307 function notifyDaemon($twitterUserId, $receiving)
309 // @todo... should use control signals rather than queues