]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FacebookBridge/actions/facebooksettings.php
Facebooksettings adapted to FormAction
[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     private $flink;
45
46     protected function doPreparation()
47     {
48         $this->facebook = new Facebook(
49             array(
50                 'appId'  => common_config('facebook', 'appid'),
51                 'secret' => common_config('facebook', 'secret'),
52                 'cookie' => true,
53             )
54         );
55
56         $this->flink = Foreign_link::getByUserID(
57             $this->scoped->getID(),
58             FACEBOOK_SERVICE
59         );
60
61         return true;
62     }
63
64     protected function doPost()
65     {
66         if ($this->arg('save')) {
67             return $this->saveSettings();
68         } else if ($this->arg('disconnect')) {
69             return $this->disconnect();
70         }
71
72         throw new ClientException(_('No action to take on POST.'));
73     }
74
75     /**
76      * Returns the page title
77      *
78      * @return string page title
79      */
80     function title() {
81         // TRANS: Page title for Facebook settings.
82         return _m('TITLE','Facebook settings');
83     }
84
85     /**
86      * Instructions for use
87      *
88      * @return instructions for use
89      */
90     function getInstructions() {
91         // TRANS: Instructions for Facebook settings.
92         return _m('Facebook settings');
93     }
94
95     /*
96      * Show the settings form if he/she has a link to Facebook
97      *
98      * @return void
99      */
100     function showContent() {
101         if (!empty($this->flink)) {
102
103             $this->elementStart(
104                 'form',
105                 array(
106                     'method' => 'post',
107                     'id' => 'form_settings_facebook',
108                     'class' => 'form_settings',
109                     'action' => common_local_url('facebooksettings')
110                 )
111             );
112
113             $this->hidden('token', common_session_token());
114
115             // TRANS: Form note. User is connected to facebook.
116             $this->element('p', 'form_note', _m('Connected Facebook user'));
117
118             $this->elementStart('p', array('class' => 'facebook-user-display'));
119
120             $this->element(
121                 'fb:profile-pic',
122                 array(
123                     'uid' => $this->flink->foreign_id,
124                     'size' => 'small',
125                     'linked' => 'true',
126                     'facebook-logo' => 'true'
127                 )
128             );
129
130             $this->element(
131                 'fb:name',
132                 array('uid' => $this->flink->foreign_id, 'useyou' => 'false')
133             );
134
135             $this->elementEnd('p');
136
137             $this->elementStart('ul', 'form_data');
138
139             $this->elementStart('li');
140
141             $this->checkbox(
142                 'noticesync',
143                 // TRANS: Checkbox label in Facebook settings.
144                 _m('Publish my notices to Facebook.'),
145                 $this->flink->noticesync & FOREIGN_NOTICE_SEND
146             );
147
148             $this->elementEnd('li');
149
150             $this->elementStart('li');
151
152             $this->checkbox(
153                     'replysync',
154                     // TRANS: Checkbox label in Facebook settings.
155                     _m('Send "@" replies to Facebook.'),
156                     $this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY
157             );
158
159             $this->elementEnd('li');
160
161             $this->elementStart('li');
162
163             // TRANS: Submit button to save synchronisation settings.
164             $this->submit('save', _m('BUTTON', 'Save'));
165
166             $this->elementEnd('li');
167
168             $this->elementEnd('ul');
169
170             $this->elementStart('fieldset');
171
172             // TRANS: Fieldset legend for form to disconnect from Facebook.
173             $this->element('legend', null, _m('Disconnect my account from Facebook'));
174
175             if (!$this->scoped->hasPassword()) {
176                 $this->elementStart('p', array('class' => 'form_guide'));
177
178                 $msg = sprintf(
179                     // TRANS: Notice in disconnect from Facebook form if user has no local StatusNet password.
180                     _m('Disconnecting your Faceboook would make it impossible to '.
181                        'log in! Please [set a password](%s) first.'),
182                     common_local_url('passwordsettings')
183                 );
184
185                 $this->raw(common_markup_to_html($msg));
186                 $this->elementEnd('p');
187             } else {
188                 // @todo FIXME: i18n: This message is not being used.
189                 // TRANS: Message displayed when initiating disconnect of a StatusNet user
190                 // TRANS: from a Facebook account. %1$s is the StatusNet site name.
191                 $msg = sprintf(_m('Keep your %1$s account but disconnect from Facebook. ' .
192                                   'You\'ll use your %1$s password to log in.'),
193                                common_config('site', 'name')
194                 );
195
196                 // TRANS: Submit button.
197                 $this->submit('disconnect', _m('BUTTON', 'Disconnect'));
198             }
199
200             $this->elementEnd('fieldset');
201
202             $this->elementEnd('form');
203          }
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 }