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()
52 $this->flink = Foreign_link::getByUserID($this->scoped->getID(), TWITTER_SERVICE);
53 if ($this->flink instanceof Foreign_link) {
54 $this->fuser = $this->flink->getForeignUser();
60 * @return string Title of the page
65 // TRANS: Title for page with Twitter integration settings.
66 return _m('Twitter settings');
70 * Instructions for use
72 * @return instructions for use
75 function getInstructions()
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.');
83 * Content area of the page
85 * Shows a form for associating a Twitter account with this
86 * StatusNet account. Also lets the user set preferences.
90 function showContent()
92 $this->elementStart('form', array('method' => 'post',
93 'id' => 'form_settings_twitter',
94 'class' => 'form_settings',
96 common_local_url('twittersettings')));
98 $this->hidden('token', common_session_token());
100 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
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');
113 $this->elementStart('fieldset');
115 // TRANS: Fieldset legend.
116 $this->element('legend', null, _m('Disconnect my account from Twitter'));
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');
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');
135 $this->element('p', 'instructions',
136 sprintf($note, $site));
138 // TRANS: Button text for disconnecting a Twitter account.
139 $this->submit('disconnect', _m('BUTTON','Disconnect'));
142 $this->elementEnd('fieldset');
144 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
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');
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');
176 // preserve setting even if bidrection bridge toggled off
178 if ($this->flink->noticesync & FOREIGN_NOTICE_RECV) {
179 $this->hidden('noticerecv', true, 'noticerecv');
183 $this->elementEnd('ul');
186 // TRANS: Button text for saving Twitter integration settings.
187 $this->submit('save', _m('BUTTON','Save'));
189 // TRANS: Button text for adding Twitter integration.
190 $this->submit('add', _m('BUTTON','Add'));
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');
202 $this->elementEnd('fieldset');
204 $this->elementEnd('form');
208 * Handle posts to this form
210 * Based on the button that was pressed, muxes out to other functions
211 * to do the actual task requested.
213 * All sub-functions reload the form with a message -- success or failure.
217 function handlePost()
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.'));
228 if ($this->arg('save')) {
229 $this->savePreferences();
230 } else if ($this->arg('disconnect')) {
231 $this->removeTwitterAccount();
233 // TRANS: Client error displayed when the submitted form contains unexpected data.
234 $this->showForm(_m('Unexpected form submission.'));
239 * Disassociate an existing Twitter account from this account
243 function removeTwitterAccount()
245 $user = common_current_user();
246 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
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.'));
253 $result = $flink->safeDelete();
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.'));
261 // TRANS: Success message displayed after disconnecting a Twitter account.
262 $this->showForm(_m('Twitter account disconnected.'), true);
266 * Save user's Twitter-bridging preferences
270 function savePreferences()
272 $noticesend = $this->boolean('noticesend');
273 $noticerecv = $this->boolean('noticerecv');
274 $friendsync = $this->boolean('friendsync');
275 $replysync = $this->boolean('replysync');
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.'));
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);
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.'));
294 if ($wasReceiving xor $noticerecv) {
295 $this->notifyDaemon($this->flink->foreign_id, $noticerecv);
298 // TRANS: Success message after saving Twitter integration preferences.
299 $this->showForm(_m('Twitter preferences saved.'), true);
303 * Tell the import daemon that we've updated a user's receive status.
305 function notifyDaemon($twitterUserId, $receiving)
307 // @todo... should use control signals rather than queues