]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookBridge/actions/facebooksettings.php
67dd20e036dfd09a870e88bc56d0e6749e78712e
[quix0rs-gnu-social.git] / plugins / FacebookBridge / actions / facebooksettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  *  Edit user settings for Facebook
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 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/
28  */
29 if (!defined('GNUSOCIAL')) { exit(1); }
30
31 /**
32  * Edit user settings for Facebook
33  *
34  * @category Settings
35  * @package  StatusNet
36  * @author   Zach Copley <zach@status.net>
37  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
38  * @link     http://status.net/
39  *
40  * @see      SettingsAction
41  */
42 class FacebooksettingsAction extends SettingsAction {
43     private $facebook; // Facebook PHP-SDK client obj
44
45     protected $flink;
46
47     protected function doPreparation()
48     {
49         $this->facebook = new Facebook(
50             array(
51                 'appId'  => common_config('facebook', 'appid'),
52                 'secret' => common_config('facebook', 'secret'),
53                 'cookie' => true,
54             )
55         );
56
57         $this->flink = Foreign_link::getByUserID(
58             $this->scoped->getID(),
59             FACEBOOK_SERVICE
60         );
61     }
62
63     protected function doPost()
64     {
65         if ($this->arg('save')) {
66             return $this->saveSettings();
67         } else if ($this->arg('disconnect')) {
68             return $this->disconnect();
69         }
70
71         throw new ClientException(_('No action to take on POST.'));
72     }
73
74     /**
75      * Returns the page title
76      *
77      * @return string page title
78      */
79     function title() {
80         // TRANS: Page title for Facebook settings.
81         return _m('TITLE','Facebook settings');
82     }
83
84     /**
85      * Instructions for use
86      *
87      * @return instructions for use
88      */
89     function getInstructions() {
90         // TRANS: Instructions for Facebook settings.
91         return _m('Facebook settings');
92     }
93
94     /*
95      * Show the settings form if he/she has a link to Facebook
96      *
97      * @return void
98      */
99     function showContent() {
100         if (!$this->flink instanceof Foreign_link) {
101             throw new ServerException(_m('You have not linked this account to Facebook.'));
102         }
103
104         $this->elementStart(
105             'form',
106             array(
107                 'method' => 'post',
108                 'id' => 'form_settings_facebook',
109                 'class' => 'form_settings',
110                 'action' => common_local_url('facebooksettings')
111             )
112         );
113
114         $this->hidden('token', common_session_token());
115
116         // TRANS: Form note. User is connected to facebook.
117         $this->element('p', 'form_note', _m('Connected Facebook user'));
118
119         $this->elementStart('p', array('class' => 'facebook-user-display'));
120
121         $this->element(
122             'fb:profile-pic',
123             array(
124                 'uid' => $this->flink->foreign_id,
125                 'size' => 'small',
126                 'linked' => 'true',
127                 'facebook-logo' => 'true'
128             )
129         );
130
131         $this->element(
132             'fb:name',
133             array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
134         );
135
136         $this->elementEnd('p');
137
138         $this->elementStart('ul', 'form_data');
139
140         $this->elementStart('li');
141
142         $this->checkbox(
143             'noticesync',
144             // TRANS: Checkbox label in Facebook settings.
145             _m('Publish my notices to Facebook.'),
146             $this->flink->noticesync & FOREIGN_NOTICE_SEND
147         );
148
149         $this->elementEnd('li');
150
151         $this->elementStart('li');
152
153         $this->checkbox(
154                 'replysync',
155                 // TRANS: Checkbox label in Facebook settings.
156                 _m('Send "@" replies to Facebook.'),
157                 $this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY
158         );
159
160         $this->elementEnd('li');
161
162         $this->elementStart('li');
163
164         // TRANS: Submit button to save synchronisation settings.
165         $this->submit('save', _m('BUTTON', 'Save'));
166
167         $this->elementEnd('li');
168
169         $this->elementEnd('ul');
170
171         $this->elementStart('fieldset');
172
173         // TRANS: Fieldset legend for form to disconnect from Facebook.
174         $this->element('legend', null, _m('Disconnect my account from Facebook'));
175
176         if (!$this->scoped->hasPassword()) {
177             $this->elementStart('p', array('class' => 'form_guide'));
178
179             $msg = sprintf(
180                 // TRANS: Notice in disconnect from Facebook form if user has no local StatusNet password.
181                 _m('Disconnecting your Faceboook would make it impossible to '.
182                    'log in! Please [set a password](%s) first.'),
183                 common_local_url('passwordsettings')
184             );
185
186             $this->raw(common_markup_to_html($msg));
187             $this->elementEnd('p');
188         } else {
189             // @todo FIXME: i18n: This message is not being used.
190             // TRANS: Message displayed when initiating disconnect of a StatusNet user
191             // TRANS: from a Facebook account. %1$s is the StatusNet site name.
192             $msg = sprintf(_m('Keep your %1$s account but disconnect from Facebook. ' .
193                               'You\'ll use your %1$s password to log in.'),
194                            common_config('site', 'name')
195             );
196
197             // TRANS: Submit button.
198             $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
199         }
200
201         $this->elementEnd('fieldset');
202
203         $this->elementEnd('form');
204     }
205
206     /*
207      * Save the user's Facebook settings
208      *
209      * @return void
210      */
211     function saveSettings() {
212         $noticesync = $this->boolean('noticesync');
213         $replysync  = $this->boolean('replysync');
214
215         $original = clone($this->flink);
216         $this->flink->set_flags($noticesync, false, $replysync, false);
217         $result = $this->flink->update($original);
218
219         if ($result === false) {
220             // TRANS: Notice in case saving of synchronisation preferences fail.
221             throw new ServerException(_m('There was a problem saving your sync preferences.'));
222         }
223         // TRANS: Confirmation that synchronisation settings have been saved into the system.
224         return _m('Sync preferences saved.');
225     }
226
227     /*
228      * Disconnect the user's Facebook account - deletes the Foreign_link
229      * and shows the user a success message if all goes well.
230      */
231     function disconnect() {
232         $result = $this->flink->delete();
233         $this->flink = null;
234
235         if ($result === false) {
236             common_log_db_error($this->flink, 'DELETE', __FILE__);
237             // TRANS: Server error displayed when deleting the link to a Facebook account fails.
238             throw new ServerException(_m('Could not delete link to Facebook.'));
239         }
240
241         // TRANS: Confirmation message. GNU social account was unlinked from Facebook.
242         return _m('You have disconnected this account from Facebook.');
243     }
244 }