]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Facebook/facebooksettings.php
Extract out Facebook app stuff into a plugin
[quix0rs-gnu-social.git] / plugins / Facebook / facebooksettings.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.     If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 require_once INSTALLDIR . '/lib/facebookaction.php';
25
26 class FacebooksettingsAction extends FacebookAction
27 {
28
29     function handle($args)
30     {
31         parent::handle($args);
32         $this->showPage();
33     }
34
35     /**
36      * Show the page content
37      *
38      * Either shows the registration form or, if registration was successful,
39      * instructions for using the site.
40      *
41      * @return void
42      */
43
44     function showContent()
45     {
46         if ($this->arg('save')) {
47             $this->saveSettings();
48         } else {
49             $this->showForm();
50         }
51     }
52
53     function saveSettings() {
54
55         $noticesync = $this->arg('noticesync');
56         $replysync = $this->arg('replysync');
57         $prefix = $this->trimmed('prefix');
58
59         $original = clone($this->flink);
60         $this->flink->set_flags($noticesync, $replysync, false, false);
61         $result = $this->flink->update($original);
62
63         if ($prefix == '' || $prefix == '0') {
64                 // Facebook bug: saving empty strings to prefs now fails
65                 // http://bugs.developers.facebook.com/show_bug.cgi?id=7110
66                 $trimmed = $prefix . ' ';
67         } else {
68                 $trimmed = substr($prefix, 0, 128);
69             }
70         $this->facebook->api_client->data_setUserPreference(FACEBOOK_NOTICE_PREFIX,
71             $trimmed);
72
73         if ($result === false) {
74             $this->showForm(_('There was a problem saving your sync preferences!'));
75         } else {
76             $this->showForm(_('Sync preferences saved.'), true);
77         }
78     }
79
80     function showForm($msg = null, $success = false) {
81
82         if ($msg) {
83             if ($success) {
84                 $this->element('fb:success', array('message' => $msg));
85             } else {
86                 $this->element('fb:error', array('message' => $msg));
87             }
88         }
89
90         if ($this->facebook->api_client->users_hasAppPermission('publish_stream')) {
91
92             $this->elementStart('form', array('method' => 'post',
93                                                'id' => 'facebook_settings'));
94
95             $this->elementStart('ul', 'form_data');
96
97             $this->elementStart('li');
98
99             $this->checkbox('noticesync', _('Automatically update my Facebook status with my notices.'),
100                                 ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND) : true);
101
102             $this->elementEnd('li');
103
104             $this->elementStart('li');
105
106             $this->checkbox('replysync', _('Send "@" replies to Facebook.'),
107                              ($this->flink) ? ($this->flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) : true);
108
109             $this->elementEnd('li');
110
111             $this->elementStart('li');
112
113             $prefix = trim($this->facebook->api_client->data_getUserPreference(FACEBOOK_NOTICE_PREFIX));
114
115             $this->input('prefix', _('Prefix'),
116                          ($prefix) ? $prefix : null,
117                          _('A string to prefix notices with.'));
118
119             $this->elementEnd('li');
120
121             $this->elementStart('li');
122
123             $this->submit('save', _('Save'));
124
125             $this->elementEnd('li');
126
127             $this->elementEnd('ul');
128
129             $this->elementEnd('form');
130
131         } else {
132
133             $instructions = sprintf(_('If you would like %s to automatically update ' .
134                 'your Facebook status with your latest notice, you need ' .
135                 'to give it permission.'), $this->app_name);
136
137             $this->elementStart('p');
138             $this->element('span', array('id' => 'permissions_notice'), $instructions);
139             $this->elementEnd('p');
140
141             $this->elementStart('ul', array('id' => 'fb-permissions-list'));
142             $this->elementStart('li', array('id' => 'fb-permissions-item'));
143             $this->elementStart('fb:prompt-permission', array('perms' => 'publish_stream',
144                 'next_fbjs' => 'document.setLocation(\'' . "$this->app_uri/settings.php" . '\')'));
145             $this->element('span', array('class' => 'facebook-button'),
146                 sprintf(_('Allow %s to update my Facebook status'), common_config('site', 'name')));
147             $this->elementEnd('fb:prompt-permission');
148             $this->elementEnd('li');
149             $this->elementEnd('ul');
150         }
151
152     }
153
154     function title()
155     {
156         return _('Sync preferences');
157     }
158
159 }