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/
49 class TwittersettingsAction extends ConnectSettingsAction
54 * @return string Title of the page
59 return _m('Twitter settings');
63 * Instructions for use
65 * @return instructions for use
68 function getInstructions()
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.
83 function showContent()
86 $user = common_current_user();
88 $profile = $user->getProfile();
92 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
95 $fuser = $flink->getForeignUser();
98 $this->elementStart('form', array('method' => 'post',
99 'id' => 'form_settings_twitter',
100 'class' => 'form_settings',
102 common_local_url('twittersettings')));
104 $this->hidden('token', common_session_token());
106 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
109 $this->elementStart('ul', 'form_data');
110 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
111 $this->element('a', array('href' => common_local_url('twitterauthorization')),
112 'Connect my Twitter account');
113 $this->elementEnd('li');
114 $this->elementEnd('ul');
116 $this->elementEnd('fieldset');
118 $this->element('legend', null, _m('Twitter account'));
119 $this->elementStart('p', array('id' => 'form_confirmed'));
120 $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
121 $this->elementEnd('p');
122 $this->element('p', 'form_note',
123 _m('Connected Twitter account'));
124 $this->elementEnd('fieldset');
126 $this->elementStart('fieldset');
128 $this->element('legend', null, _m('Disconnect my account from Twitter'));
130 if (!$user->password) {
132 $this->elementStart('p', array('class' => 'form_guide'));
133 $this->text(_m('Disconnecting your Twitter ' .
134 'could make it impossible to log in! Please '));
136 array('href' => common_local_url('passwordsettings')),
137 _m('set a password'));
139 $this->text(_m(' first.'));
140 $this->elementEnd('p');
143 $note = _m('Keep your %1$s account but disconnect from Twitter. ' .
144 'You can use your %1$s password to log in.');
146 $site = common_config('site', 'name');
148 $this->element('p', 'instructions',
149 sprintf($note, $site));
151 $this->submit('disconnect', _m('Disconnect'));
154 $this->elementEnd('fieldset');
156 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
158 $this->element('legend', null, _m('Preferences'));
159 $this->elementStart('ul', 'form_data');
160 $this->elementStart('li');
161 $this->checkbox('noticesend',
162 _m('Automatically send my notices to Twitter.'),
164 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
166 $this->elementEnd('li');
167 $this->elementStart('li');
168 $this->checkbox('replysync',
169 _m('Send local "@" replies to Twitter.'),
171 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
173 $this->elementEnd('li');
174 $this->elementStart('li');
175 $this->checkbox('friendsync',
176 _m('Subscribe to my Twitter friends here.'),
178 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
180 $this->elementEnd('li');
182 if (common_config('twitterimport','enabled')) {
183 $this->elementStart('li');
184 $this->checkbox('noticerecv',
185 _m('Import my Friends Timeline.'),
187 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
189 $this->elementEnd('li');
191 // preserve setting even if bidrection bridge toggled off
193 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
194 $this->hidden('noticerecv', true, 'noticerecv');
198 $this->elementEnd('ul');
201 $this->submit('save', _m('Save'));
203 $this->submit('add', _m('Add'));
206 $this->elementEnd('fieldset');
209 $this->elementEnd('form');
213 * Handle posts to this form
215 * Based on the button that was pressed, muxes out to other functions
216 * to do the actual task requested.
218 * All sub-functions reload the form with a message -- success or failure.
223 function handlePost()
226 $token = $this->trimmed('token');
227 if (!$token || $token != common_session_token()) {
228 $this->showForm(_m('There was a problem with your session token. '.
229 'Try again, please.'));
233 if ($this->arg('save')) {
234 $this->savePreferences();
235 } else if ($this->arg('disconnect')) {
236 $this->removeTwitterAccount();
238 $this->showForm(_m('Unexpected form submission.'));
243 * Disassociate an existing Twitter account from this account
248 function removeTwitterAccount()
250 $user = common_current_user();
251 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
253 $result = $flink->safeDelete();
255 if (empty($result)) {
256 common_log_db_error($flink, 'DELETE', __FILE__);
257 $this->serverError(_m('Couldn\'t remove Twitter user.'));
261 $this->showForm(_m('Twitter account disconnected.'), true);
265 * 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 $user = common_current_user();
278 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
281 common_log_db_error($flink, 'SELECT', __FILE__);
282 $this->showForm(_m('Couldn\'t save Twitter preferences.'));
286 $original = clone($flink);
287 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
288 $result = $flink->update($original);
290 if ($result === false) {
291 common_log_db_error($flink, 'UPDATE', __FILE__);
292 $this->showForm(_m('Couldn\'t save Twitter preferences.'));
296 $this->showForm(_m('Twitter preferences saved.'), true);