3 * Laconica, 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@controlyourself.ca>
25 * @copyright 2008-2009 Control Yourself, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://laconi.ca/
30 if (!defined('LACONICA')) {
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35 require_once INSTALLDIR.'/lib/twitter.php';
38 * Settings for Twitter integration
42 * @author Evan Prodromou <evan@controlyourself.ca>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://laconi.ca/
49 class TwittersettingsAction extends ConnectSettingsAction
54 * @return string Title of the page
59 return _('Twitter settings');
63 * Instructions for use
65 * @return instructions for use
68 function getInstructions()
70 return _('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 * Laconica account. Also lets the user set preferences.
83 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 'Connect my Twitter account');
112 $this->elementEnd('li');
113 $this->elementEnd('ul');
115 $this->elementEnd('fieldset');
117 $this->element('legend', null, _('Twitter account'));
118 $this->elementStart('p', array('id' => 'form_confirmed'));
119 $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
120 $this->elementEnd('p');
121 $this->element('p', 'form_note',
122 _('Connected Twitter account'));
124 $this->submit('remove', _('Remove'));
126 $this->elementEnd('fieldset');
128 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
130 $this->element('legend', null, _('Preferences'));
131 $this->elementStart('ul', 'form_data');
132 $this->elementStart('li');
133 $this->checkbox('noticesend',
134 _('Automatically send my notices to Twitter.'),
136 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
138 $this->elementEnd('li');
139 $this->elementStart('li');
140 $this->checkbox('replysync',
141 _('Send local "@" replies to Twitter.'),
143 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
145 $this->elementEnd('li');
146 $this->elementStart('li');
147 $this->checkbox('friendsync',
148 _('Subscribe to my Twitter friends here.'),
150 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
152 $this->elementEnd('li');
154 if (common_config('twitterbridge','enabled')) {
155 $this->elementStart('li');
156 $this->checkbox('noticerecv',
157 _('Import my Friends Timeline.'),
159 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
161 $this->elementEnd('li');
163 // preserve setting even if bidrection bridge toggled off
165 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
166 $this->hidden('noticerecv', true, 'noticerecv');
170 $this->elementEnd('ul');
173 $this->submit('save', _('Save'));
175 $this->submit('add', _('Add'));
178 $this->elementEnd('fieldset');
181 $this->elementEnd('form');
185 * Handle posts to this form
187 * Based on the button that was pressed, muxes out to other functions
188 * to do the actual task requested.
190 * All sub-functions reload the form with a message -- success or failure.
195 function handlePost()
198 $token = $this->trimmed('token');
199 if (!$token || $token != common_session_token()) {
200 $this->showForm(_('There was a problem with your session token. '.
201 'Try again, please.'));
205 if ($this->arg('save')) {
206 $this->savePreferences();
207 } else if ($this->arg('remove')) {
208 $this->removeTwitterAccount();
210 $this->showForm(_('Unexpected form submission.'));
215 * Disassociate an existing Twitter account from this account
220 function removeTwitterAccount()
222 $user = common_current_user();
223 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
225 $result = $flink->delete();
227 if (empty($result)) {
228 common_log_db_error($flink, 'DELETE', __FILE__);
229 $this->serverError(_('Couldn\'t remove Twitter user.'));
233 $this->showForm(_('Twitter account removed.'), true);
237 * Save user's Twitter-bridging preferences
242 function savePreferences()
244 $noticesend = $this->boolean('noticesend');
245 $noticerecv = $this->boolean('noticerecv');
246 $friendsync = $this->boolean('friendsync');
247 $replysync = $this->boolean('replysync');
249 $user = common_current_user();
250 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
253 common_log_db_error($flink, 'SELECT', __FILE__);
254 $this->showForm(_('Couldn\'t save Twitter preferences.'));
258 $original = clone($flink);
259 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
260 $result = $flink->update($original);
262 if ($result === false) {
263 common_log_db_error($flink, 'UPDATE', __FILE__);
264 $this->showForm(_('Couldn\'t save Twitter preferences.'));
268 $this->showForm(_('Twitter preferences saved.'), true);