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')) {
36 * Miscellaneous settings actions
38 * Currently this just manages URL shortening.
42 * @author Robin Millette <millette@status.net>
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
47 class UrlsettingsAction extends SettingsAction
52 * @return string Title of the page
56 // TRANS: Title of URL settings tab in profile settings.
57 return _('URL settings');
61 * Instructions for use
63 * @return instructions for use
65 function getInstructions()
67 // TRANS: Instructions for tab "Other" in user profile settings.
68 return _('Manage various other options.');
71 function showScripts()
73 parent::showScripts();
74 $this->autofocus('urlshorteningservice');
78 * Content area of the page
80 * Shows a form for uploading an avatar.
84 function showContent()
86 $user = common_current_user();
88 $this->elementStart('form', array('method' => 'post',
89 'id' => 'form_settings_other',
90 'class' => 'form_settings',
92 common_local_url('urlsettings')));
93 $this->elementStart('fieldset');
94 $this->hidden('token', common_session_token());
95 $this->elementStart('ul', 'form_data');
97 $shorteners = array();
99 Event::handle('GetUrlShorteners', array(&$shorteners));
103 foreach ($shorteners as $name => $value)
105 $services[$name] = $name;
106 if ($value['freeService']) {
107 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
108 // TRANS: user's profile settings. This message has one space at the beginning. Use your
109 // TRANS: language's word separator here if it has one (most likely a single space).
110 $services[$name] .= _(' (free service)');
114 // Include default values
116 // TRANS: Default value for URL shortening settings.
117 $services['none'] = _('[none]');
118 // TRANS: Default value for URL shortening settings.
119 $services['internal'] = _('[internal]');
124 $this->elementStart('li');
125 // TRANS: Label for dropdown with URL shortener services.
126 $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
127 // TRANS: Tooltip for for dropdown with URL shortener services.
128 $services, _('Automatic shortening service to use.'),
129 false, $user->urlshorteningservice);
130 $this->elementEnd('li');
132 $this->elementStart('li');
133 $this->input('maxurllength',
134 // TRANS: Field label in URL settings in profile.
135 _('URL longer than'),
136 (!is_null($this->arg('maxurllength'))) ?
137 $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
138 // TRANS: Field title in URL settings in profile.
139 _('URLs longer than this will be shortened, -1 means never shorten because a URL is long.'));
140 $this->elementEnd('li');
141 $this->elementStart('li');
142 $this->input('maxnoticelength',
143 // TRANS: Field label in URL settings in profile.
144 _('Text longer than'),
145 (!is_null($this->arg('maxnoticelength'))) ?
146 $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
147 // TRANS: Field title in URL settings in profile.
148 _('URLs in notices longer than this will always be shortened, -1 means only shorten if the full post exceeds maximum length.'));
149 $this->elementEnd('li');
150 $this->elementEnd('ul');
151 // TRANS: Button text for saving "Other settings" in profile.
152 $this->submit('save', _m('BUTTON','Save'));
153 $this->elementEnd('fieldset');
154 $this->elementEnd('form');
160 * Saves the changes to url-shortening prefs and shows a success or failure
165 function handlePost()
168 $token = $this->trimmed('token');
169 if (!$token || $token != common_session_token()) {
170 // TRANS: Client error displayed when the session token does not match or is not given.
171 $this->showForm(_('There was a problem with your session token. '.
172 'Try again, please.'));
176 $urlshorteningservice = $this->trimmed('urlshorteningservice');
178 if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
179 // TRANS: Form validation error for form "Other settings" in user profile.
180 $this->showForm(_('URL shortening service is too long (maximum 50 characters).'));
184 $maxurllength = $this->trimmed('maxurllength');
186 if (!Validate::number($maxurllength, array('min' => -1))) {
187 // TRANS: Client exception thrown when the maximum URL settings value is invalid in profile URL settings.
188 throw new ClientException(_('Invalid number for maximum URL length.'));
191 $maxnoticelength = $this->trimmed('maxnoticelength');
193 if (!Validate::number($maxnoticelength, array('min' => -1))) {
194 // TRANS: Client exception thrown when the maximum notice length settings value is invalid in profile URL settings.
195 throw new ClientException(_('Invalid number for maximum notice length.'));
198 $user = common_current_user();
200 assert(!is_null($user)); // should already be checked
202 $user->query('BEGIN');
204 $original = clone($user);
206 $user->urlshorteningservice = $urlshorteningservice;
208 $result = $user->update($original);
210 if ($result === false) {
211 common_log_db_error($user, 'UPDATE', __FILE__);
212 // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
213 $this->serverError(_('Could not update user.'));
216 $prefs = User_urlshortener_prefs::getPrefs($user);
220 $prefs = new User_urlshortener_prefs();
222 $prefs->user_id = $user->id;
223 $prefs->created = common_sql_now();
225 $orig = clone($prefs);
228 $prefs->urlshorteningservice = $urlshorteningservice;
229 $prefs->maxurllength = $maxurllength;
230 $prefs->maxnoticelength = $maxnoticelength;
233 $result = $prefs->update($orig);
235 $result = $prefs->insert();
239 // TRANS: Server exception thrown in profile URL settings when preferences could not be saved.
240 throw new ServerException(_('Error saving user URL shortening preferences.'));
243 $user->query('COMMIT');
245 // TRANS: Confirmation message after saving preferences.
246 $this->showForm(_('Preferences saved.'), true);