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 . '/lib/connectsettingsaction.php';
35 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
38 * Settings for Twitter integration
42 * @author Evan Prodromou <evan@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
48 class TwittersettingsAction extends ConnectSettingsAction
53 * @return string Title of the page
58 return _m('Twitter settings');
62 * Instructions for use
64 * @return instructions for use
67 function getInstructions()
69 return _m('Connect your Twitter account to share your updates ' .
70 'with your Twitter friends and vice-versa.');
74 * Content area of the page
76 * Shows a form for associating a Twitter account with this
77 * StatusNet account. Also lets the user set preferences.
81 function showContent()
84 $user = common_current_user();
86 $profile = $user->getProfile();
90 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
93 $fuser = $flink->getForeignUser();
96 $this->elementStart('form', array('method' => 'post',
97 'id' => 'form_settings_twitter',
98 'class' => 'form_settings',
100 common_local_url('twittersettings')));
102 $this->hidden('token', common_session_token());
104 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
107 $this->elementStart('ul', 'form_data');
108 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
109 $this->element('a', array('href' => common_local_url('twitterauthorization')),
110 'Connect my Twitter account');
111 $this->elementEnd('li');
112 $this->elementEnd('ul');
114 $this->elementEnd('fieldset');
116 $this->element('legend', null, _m('Twitter account'));
117 $this->elementStart('p', array('id' => 'form_confirmed'));
118 $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
119 $this->elementEnd('p');
120 $this->element('p', 'form_note',
121 _m('Connected Twitter account'));
122 $this->elementEnd('fieldset');
124 $this->elementStart('fieldset');
126 $this->element('legend', null, _m('Disconnect my account from Twitter'));
128 if (!$user->password) {
130 $this->elementStart('p', array('class' => 'form_guide'));
131 $this->text(_m('Disconnecting your Twitter ' .
132 'could make it impossible to log in! Please '));
134 array('href' => common_local_url('passwordsettings')),
135 _m('set a password'));
137 $this->text(_m(' first.'));
138 $this->elementEnd('p');
140 // TRANS: %1$s is the current website name.
141 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
142 'You can use your %1$s password to log in.');
144 $site = common_config('site', 'name');
146 $this->element('p', 'instructions',
147 sprintf($note, $site));
149 $this->submit('disconnect', _m('Disconnect'));
152 $this->elementEnd('fieldset');
154 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
156 $this->element('legend', null, _m('Preferences'));
157 $this->elementStart('ul', 'form_data');
158 $this->elementStart('li');
159 $this->checkbox('noticesend',
160 _m('Automatically send my notices to Twitter.'),
162 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
164 $this->elementEnd('li');
165 $this->elementStart('li');
166 $this->checkbox('replysync',
167 _m('Send local "@" replies to Twitter.'),
169 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
171 $this->elementEnd('li');
172 $this->elementStart('li');
173 $this->checkbox('friendsync',
174 _m('Subscribe to my Twitter friends here.'),
176 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
178 $this->elementEnd('li');
180 if (common_config('twitterimport','enabled')) {
181 $this->elementStart('li');
182 $this->checkbox('noticerecv',
183 _m('Import my Friends Timeline.'),
185 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
187 $this->elementEnd('li');
189 // preserve setting even if bidrection bridge toggled off
191 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
192 $this->hidden('noticerecv', true, 'noticerecv');
196 $this->elementEnd('ul');
199 $this->submit('save', _m('Save'));
201 $this->submit('add', _m('Add'));
204 $this->elementEnd('fieldset');
207 $this->elementEnd('form');
211 * Handle posts to this form
213 * Based on the button that was pressed, muxes out to other functions
214 * to do the actual task requested.
216 * All sub-functions reload the form with a message -- success or failure.
220 function handlePost()
223 $token = $this->trimmed('token');
224 if (!$token || $token != common_session_token()) {
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 $this->showForm(_m('Unexpected form submission.'));
240 * Disassociate an existing Twitter account from this account
244 function removeTwitterAccount()
246 $user = common_current_user();
247 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
249 $result = $flink->safeDelete();
251 if (empty($result)) {
252 common_log_db_error($flink, 'DELETE', __FILE__);
253 $this->serverError(_m('Couldn\'t remove Twitter user.'));
257 $this->showForm(_m('Twitter account disconnected.'), true);
261 * Save user's Twitter-bridging preferences
265 function savePreferences()
267 $noticesend = $this->boolean('noticesend');
268 $noticerecv = $this->boolean('noticerecv');
269 $friendsync = $this->boolean('friendsync');
270 $replysync = $this->boolean('replysync');
272 $user = common_current_user();
273 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
276 common_log_db_error($flink, 'SELECT', __FILE__);
277 $this->showForm(_m('Couldn\'t save Twitter preferences.'));
281 $original = clone($flink);
282 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
283 $result = $flink->update($original);
285 if ($result === false) {
286 common_log_db_error($flink, 'UPDATE', __FILE__);
287 $this->showForm(_m('Couldn\'t save Twitter preferences.'));
291 $this->showForm(_m('Twitter preferences saved.'), true);