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.'/lib/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 _('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 * StatusNet account. Also lets the user set preferences.
83 function showContent()
85 if (!common_config('twitter', 'enabled')) {
86 $this->element('div', array('class' => 'error'),
87 _('Twitter is not available.'));
91 $user = common_current_user();
93 $profile = $user->getProfile();
97 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
100 $fuser = $flink->getForeignUser();
103 $this->elementStart('form', array('method' => 'post',
104 'id' => 'form_settings_twitter',
105 'class' => 'form_settings',
107 common_local_url('twittersettings')));
109 $this->hidden('token', common_session_token());
111 $this->elementStart('fieldset', array('id' => 'settings_twitter_account'));
114 $this->elementStart('ul', 'form_data');
115 $this->elementStart('li', array('id' => 'settings_twitter_login_button'));
116 $this->element('a', array('href' => common_local_url('twitterauthorization')),
117 'Connect my Twitter account');
118 $this->elementEnd('li');
119 $this->elementEnd('ul');
121 $this->elementEnd('fieldset');
123 $this->element('legend', null, _('Twitter account'));
124 $this->elementStart('p', array('id' => 'form_confirmed'));
125 $this->element('a', array('href' => $fuser->uri), $fuser->nickname);
126 $this->elementEnd('p');
127 $this->element('p', 'form_note',
128 _('Connected Twitter account'));
130 $this->submit('remove', _('Remove'));
132 $this->elementEnd('fieldset');
134 $this->elementStart('fieldset', array('id' => 'settings_twitter_preferences'));
136 $this->element('legend', null, _('Preferences'));
137 $this->elementStart('ul', 'form_data');
138 $this->elementStart('li');
139 $this->checkbox('noticesend',
140 _('Automatically send my notices to Twitter.'),
142 ($flink->noticesync & FOREIGN_NOTICE_SEND) :
144 $this->elementEnd('li');
145 $this->elementStart('li');
146 $this->checkbox('replysync',
147 _('Send local "@" replies to Twitter.'),
149 ($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) :
151 $this->elementEnd('li');
152 $this->elementStart('li');
153 $this->checkbox('friendsync',
154 _('Subscribe to my Twitter friends here.'),
156 ($flink->friendsync & FOREIGN_FRIEND_RECV) :
158 $this->elementEnd('li');
160 if (common_config('twitterbridge','enabled')) {
161 $this->elementStart('li');
162 $this->checkbox('noticerecv',
163 _('Import my Friends Timeline.'),
165 ($flink->noticesync & FOREIGN_NOTICE_RECV) :
167 $this->elementEnd('li');
169 // preserve setting even if bidrection bridge toggled off
171 if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) {
172 $this->hidden('noticerecv', true, 'noticerecv');
176 $this->elementEnd('ul');
179 $this->submit('save', _('Save'));
181 $this->submit('add', _('Add'));
184 $this->elementEnd('fieldset');
187 $this->elementEnd('form');
191 * Handle posts to this form
193 * Based on the button that was pressed, muxes out to other functions
194 * to do the actual task requested.
196 * All sub-functions reload the form with a message -- success or failure.
201 function handlePost()
204 $token = $this->trimmed('token');
205 if (!$token || $token != common_session_token()) {
206 $this->showForm(_('There was a problem with your session token. '.
207 'Try again, please.'));
211 if ($this->arg('save')) {
212 $this->savePreferences();
213 } else if ($this->arg('remove')) {
214 $this->removeTwitterAccount();
216 $this->showForm(_('Unexpected form submission.'));
221 * Disassociate an existing Twitter account from this account
226 function removeTwitterAccount()
228 $user = common_current_user();
229 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
231 $result = $flink->delete();
233 if (empty($result)) {
234 common_log_db_error($flink, 'DELETE', __FILE__);
235 $this->serverError(_('Couldn\'t remove Twitter user.'));
239 $this->showForm(_('Twitter account removed.'), true);
243 * Save user's Twitter-bridging preferences
248 function savePreferences()
250 $noticesend = $this->boolean('noticesend');
251 $noticerecv = $this->boolean('noticerecv');
252 $friendsync = $this->boolean('friendsync');
253 $replysync = $this->boolean('replysync');
255 $user = common_current_user();
256 $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
259 common_log_db_error($flink, 'SELECT', __FILE__);
260 $this->showForm(_('Couldn\'t save Twitter preferences.'));
264 $original = clone($flink);
265 $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync);
266 $result = $flink->update($original);
268 if ($result === false) {
269 common_log_db_error($flink, 'UPDATE', __FILE__);
270 $this->showForm(_('Couldn\'t save Twitter preferences.'));
274 $this->showForm(_('Twitter preferences saved.'), true);