]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/othersettings.php
4dfc5c28402a9068e02abd4394fdc9a9c957642e
[quix0rs-gnu-social.git] / actions / othersettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Miscellaneous settings
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    Robin Millette <millette@status.net>
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36
37 /**
38  * Miscellaneous settings actions
39  *
40  * Currently this just manages URL shortening.
41  *
42  * @category Settings
43  * @package  StatusNet
44  * @author   Robin Millette <millette@status.net>
45  * @author   Zach Copley <zach@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class OthersettingsAction extends AccountSettingsAction
50 {
51     /**
52      * Title of the page
53      *
54      * @return string Title of the page
55      */
56     function title()
57     {
58         // Page title for a tab in user profile settings.
59         return _('Other settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67
68     function getInstructions()
69     {
70         // TRANS: Instructions for tab "Other" in user profile settings.
71         return _('Manage various other options.');
72     }
73
74     function showScripts()
75     {
76         parent::showScripts();
77         $this->autofocus('urlshorteningservice');
78     }
79
80     /**
81      * Content area of the page
82      *
83      * Shows a form for uploading an avatar.
84      *
85      * @return void
86      */
87
88     function showContent()
89     {
90         $user = common_current_user();
91
92         $this->elementStart('form', array('method' => 'post',
93                                           'id' => 'form_settings_other',
94                                           'class' => 'form_settings',
95                                           'action' =>
96                                           common_local_url('othersettings')));
97         $this->elementStart('fieldset');
98         $this->hidden('token', common_session_token());
99         $this->elementStart('ul', 'form_data');
100
101         $shorteners = array(_('[none]') => array('freeService' => false));
102
103         Event::handle('GetUrlShorteners', array(&$shorteners));
104
105         $services = array();
106         foreach($shorteners as $name=>$value)
107         {
108             $services[$name]=$name;
109             if($value['freeService']){
110                 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
111                 // TRANS: user's profile settings. This message has one space at the beginning. Use your
112                 // TRANS: language's word separator here if it has one (most likely a single space).
113                 $services[$name].=_(' (free service)');
114             }
115         }
116         if($services)
117         {
118             asort($services);
119
120             $this->elementStart('li');
121             // TRANS: Label for dropdown with URL shortener services.
122             $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
123                             // TRANS: Tooltip for for dropdown with URL shortener services.
124                             $services, _('Automatic shortening service to use.'),
125                             false, $user->urlshorteningservice);
126             $this->elementEnd('li');
127         }
128         $this->elementStart('li');
129         $this->input('maxurllength',
130                      _('URL longer than'),
131                      (!is_null($this->arg('maxurllength'))) ?
132                      $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
133                      _('URLs longer than this will be shortened.'));
134         $this->elementEnd('li');
135         $this->elementStart('li');
136         $this->input('maxnoticelength',
137                      _('Text longer than'),
138                      (!is_null($this->arg('maxnoticelength'))) ?
139                      $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
140                      _('URLs in notices longer than this will be shortened.'));
141         $this->elementEnd('li');
142         $this->elementStart('li');
143         // TRANS: Label for checkbox.
144         $this->checkbox('viewdesigns', _('View profile designs'),
145                          -                        $user->viewdesigns, _('Show or hide profile designs.'));
146         $this->elementEnd('li');
147         $this->elementEnd('ul');
148         // TRANS: Button text for saving "Other settings" in profile.
149         $this->submit('save', _m('BUTTON','Save'));
150         $this->elementEnd('fieldset');
151         $this->elementEnd('form');
152     }
153
154     /**
155      * Handle a post
156      *
157      * Saves the changes to url-shortening prefs and shows a success or failure
158      * message.
159      *
160      * @return void
161      */
162
163     function handlePost()
164     {
165         // CSRF protection
166         $token = $this->trimmed('token');
167         if (!$token || $token != common_session_token()) {
168             $this->showForm(_('There was a problem with your session token. '.
169                               'Try again, please.'));
170             return;
171         }
172
173         $urlshorteningservice = $this->trimmed('urlshorteningservice');
174
175         if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
176             // TRANS: Form validation error for form "Other settings" in user profile.
177             $this->showForm(_('URL shortening service is too long (maximum 50 characters).'));
178             return;
179         }
180
181         $viewdesigns = $this->boolean('viewdesigns');
182
183         $maxurllength = $this->trimmed('maxurllength');
184
185         if (!Validate::number($maxurllength, array('min' => 0))) {
186             throw new ClientException(_('Invalid number for max url length.'));
187         }
188
189         $maxnoticelength = $this->trimmed('maxnoticelength');
190
191         if (!Validate::number($maxnoticelength, array('min' => 0))) {
192             throw new ClientException(_('Invalid number for max notice length.'));
193         }
194
195         $user = common_current_user();
196
197         assert(!is_null($user)); // should already be checked
198
199         $user->query('BEGIN');
200
201         $original = clone($user);
202
203         $user->urlshorteningservice = $urlshorteningservice;
204         $user->viewdesigns          = $viewdesigns;
205
206         $result = $user->update($original);
207
208         if ($result === false) {
209             common_log_db_error($user, 'UPDATE', __FILE__);
210             // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
211             $this->serverError(_('Couldn\'t update user.'));
212             return;
213         }
214
215         $prefs = User_urlshortener_prefs::getPrefs($user);
216         $orig  = null;
217
218         if (empty($prefs)) {
219             $prefs = new User_urlshortener_prefs();
220
221             $prefs->user_id = $user->id;
222             $prefs->created = common_sql_now();
223         } else {
224             $orig = clone($prefs);
225         }
226
227         $prefs->urlshorteningservice = $urlshorteningservice;
228         $prefs->maxurllength         = $maxurllength;
229         $prefs->maxnoticelength      = $maxnoticelength;
230
231         if (!empty($orig)) {
232             $result = $prefs->update($orig);
233         } else {
234             $result = $prefs->insert();
235         }
236
237         if (!$result) {
238             throw new ServerException(_('Error saving user URL shortening preferences.'));
239         }
240
241         $user->query('COMMIT');
242
243         $this->showForm(_('Preferences saved.'), true);
244     }
245 }