3 * StatusNet, the distributed open-source microblogging tool
5 * Miscellaneous settings
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 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/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
38 * Miscellaneous settings actions
40 * Currently this just manages URL shortening.
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/
50 class UrlsettingsAction extends AccountSettingsAction
55 * @return string Title of the page
60 return _('URL settings');
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 // TRANS: Instructions for tab "Other" in user profile settings.
72 return _('Manage various other options.');
75 function showScripts()
77 parent::showScripts();
78 $this->autofocus('urlshorteningservice');
82 * Content area of the page
84 * Shows a form for uploading an avatar.
89 function showContent()
91 $user = common_current_user();
93 $this->elementStart('form', array('method' => 'post',
94 'id' => 'form_settings_other',
95 'class' => 'form_settings',
97 common_local_url('urlsettings')));
98 $this->elementStart('fieldset');
99 $this->hidden('token', common_session_token());
100 $this->elementStart('ul', 'form_data');
102 $shorteners = array(_('[none]') => array('freeService' => false));
104 Event::handle('GetUrlShorteners', array(&$shorteners));
107 foreach($shorteners as $name=>$value)
109 $services[$name]=$name;
110 if($value['freeService']){
111 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
112 // TRANS: user's profile settings. This message has one space at the beginning. Use your
113 // TRANS: language's word separator here if it has one (most likely a single space).
114 $services[$name].=_(' (free service)');
121 $this->elementStart('li');
122 // TRANS: Label for dropdown with URL shortener services.
123 $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
124 // TRANS: Tooltip for for dropdown with URL shortener services.
125 $services, _('Automatic shortening service to use.'),
126 false, $user->urlshorteningservice);
127 $this->elementEnd('li');
129 $this->elementStart('li');
130 $this->input('maxurllength',
131 _('URL longer than'),
132 (!is_null($this->arg('maxurllength'))) ?
133 $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
134 _('URLs longer than this will be shortened, 0 means always shorten.'));
135 $this->elementEnd('li');
136 $this->elementStart('li');
137 $this->input('maxnoticelength',
138 _('Text longer than'),
139 (!is_null($this->arg('maxnoticelength'))) ?
140 $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
141 _('URLs in notices longer than this will be shortened, 0 means always shorten.'));
142 $this->elementEnd('li');
143 $this->elementEnd('ul');
144 // TRANS: Button text for saving "Other settings" in profile.
145 $this->submit('save', _m('BUTTON','Save'));
146 $this->elementEnd('fieldset');
147 $this->elementEnd('form');
153 * Saves the changes to url-shortening prefs and shows a success or failure
159 function handlePost()
162 $token = $this->trimmed('token');
163 if (!$token || $token != common_session_token()) {
164 $this->showForm(_('There was a problem with your session token. '.
165 'Try again, please.'));
169 $urlshorteningservice = $this->trimmed('urlshorteningservice');
171 if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
172 // TRANS: Form validation error for form "Other settings" in user profile.
173 $this->showForm(_('URL shortening service is too long (maximum 50 characters).'));
177 $maxurllength = $this->trimmed('maxurllength');
179 if (!Validate::number($maxurllength, array('min' => 0))) {
180 throw new ClientException(_('Invalid number for max url length.'));
183 $maxnoticelength = $this->trimmed('maxnoticelength');
185 if (!Validate::number($maxnoticelength, array('min' => 0))) {
186 throw new ClientException(_('Invalid number for max notice length.'));
189 $user = common_current_user();
191 assert(!is_null($user)); // should already be checked
193 $user->query('BEGIN');
195 $original = clone($user);
197 $user->urlshorteningservice = $urlshorteningservice;
199 $result = $user->update($original);
201 if ($result === false) {
202 common_log_db_error($user, 'UPDATE', __FILE__);
203 // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
204 $this->serverError(_('Couldn\'t update user.'));
208 $prefs = User_urlshortener_prefs::getPrefs($user);
212 $prefs = new User_urlshortener_prefs();
214 $prefs->user_id = $user->id;
215 $prefs->created = common_sql_now();
217 $orig = clone($prefs);
220 $prefs->urlshorteningservice = $urlshorteningservice;
221 $prefs->maxurllength = $maxurllength;
222 $prefs->maxnoticelength = $maxnoticelength;
225 $result = $prefs->update($orig);
227 $result = $prefs->insert();
231 throw new ServerException(_('Error saving user URL shortening preferences.'));
234 $user->query('COMMIT');
236 $this->showForm(_('Preferences saved.'), true);