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('GNUSOCIAL')) { exit(1); }
34 * Miscellaneous settings actions
36 * Currently this just manages URL shortening.
40 * @author Robin Millette <millette@status.net>
41 * @author Zach Copley <zach@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43 * @link http://status.net/
45 class UrlsettingsAction extends SettingsAction
50 * @return string Title of the page
54 // TRANS: Title of URL settings tab in profile settings.
55 return _('URL settings');
59 * Instructions for use
61 * @return instructions for use
63 function getInstructions()
65 // TRANS: Instructions for tab "Other" in user profile settings.
66 return _('Manage various other options.');
69 function showScripts()
71 parent::showScripts();
72 $this->autofocus('urlshorteningservice');
76 * Content area of the page
78 * Shows a form for uploading an avatar.
82 function showContent()
84 $user = $this->scoped->getUser();
86 $this->elementStart('form', array('method' => 'post',
87 'id' => 'form_settings_other',
88 'class' => 'form_settings',
90 common_local_url('urlsettings')));
91 $this->elementStart('fieldset');
92 $this->hidden('token', common_session_token());
93 $this->elementStart('ul', 'form_data');
95 $shorteners = array();
97 Event::handle('GetUrlShorteners', array(&$shorteners));
101 foreach ($shorteners as $name => $value)
103 $services[$name] = $name;
104 if ($value['freeService']) {
105 // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
106 // TRANS: user's profile settings. This message has one space at the beginning. Use your
107 // TRANS: language's word separator here if it has one (most likely a single space).
108 $services[$name] .= _(' (free service)');
112 // Include default values
114 // TRANS: Default value for URL shortening settings.
115 $services['none'] = _('[none]');
116 // TRANS: Default value for URL shortening settings.
117 $services['internal'] = _('[internal]');
122 $this->elementStart('li');
123 // TRANS: Label for dropdown with URL shortener services.
124 $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
125 // TRANS: Tooltip for for dropdown with URL shortener services.
126 $services, _('Automatic shortening service to use.'),
127 false, $user->urlshorteningservice);
128 $this->elementEnd('li');
130 $this->elementStart('li');
131 $this->input('maxurllength',
132 // TRANS: Field label in URL settings in profile.
133 _('URL longer than'),
134 (!is_null($this->arg('maxurllength'))) ?
135 $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
136 // TRANS: Field title in URL settings in profile.
137 _('URLs longer than this will be shortened, -1 means never shorten because a URL is long.'));
138 $this->elementEnd('li');
139 $this->elementStart('li');
140 $this->input('maxnoticelength',
141 // TRANS: Field label in URL settings in profile.
142 _('Text longer than'),
143 (!is_null($this->arg('maxnoticelength'))) ?
144 $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
145 // TRANS: Field title in URL settings in profile.
146 _('URLs in notices longer than this will always be shortened, -1 means only shorten if the full post exceeds maximum length.'));
147 $this->elementEnd('li');
148 $this->elementEnd('ul');
149 // TRANS: Button text for saving "Other settings" in profile.
150 $this->submit('save', _m('BUTTON','Save'));
151 $this->elementEnd('fieldset');
152 $this->elementEnd('form');
155 protected function doPost()
157 $urlshorteningservice = $this->trimmed('urlshorteningservice');
159 if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
160 // TRANS: Form validation error for form "Other settings" in user profile.
161 throw new ClientException(_('URL shortening service is too long (maximum 50 characters).'));
164 $maxurllength = $this->trimmed('maxurllength');
166 if (!Validate::number($maxurllength, array('min' => -1))) {
167 // TRANS: Client exception thrown when the maximum URL settings value is invalid in profile URL settings.
168 throw new ClientException(_('Invalid number for maximum URL length.'));
171 $maxnoticelength = $this->trimmed('maxnoticelength');
173 if (!Validate::number($maxnoticelength, array('min' => -1))) {
174 // TRANS: Client exception thrown when the maximum notice length settings value is invalid in profile URL settings.
175 throw new ClientException(_('Invalid number for maximum notice length.'));
178 $user = $this->scoped->getUser();
180 $user->query('BEGIN');
182 $original = clone($user);
184 $user->urlshorteningservice = $urlshorteningservice;
186 $result = $user->update($original);
188 if ($result === false) {
189 common_log_db_error($user, 'UPDATE', __FILE__);
190 $user->query('ROLLBACK');
191 // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
192 throw new ServerException(_('Could not update user.'));
195 $prefs = User_urlshortener_prefs::getPrefs($user);
198 if (!$prefs instanceof User_urlshortener_prefs) {
199 $prefs = new User_urlshortener_prefs();
201 $prefs->user_id = $user->id;
202 $prefs->created = common_sql_now();
204 $orig = clone($prefs);
207 $prefs->urlshorteningservice = $urlshorteningservice;
208 $prefs->maxurllength = $maxurllength;
209 $prefs->maxnoticelength = $maxnoticelength;
211 if ($orig instanceof User_urlshortener_prefs) {
212 $result = $prefs->update($orig);
214 $result = $prefs->insert();
217 if ($result === null) {
218 $user->query('ROLLBACK');
219 // TRANS: Server exception thrown in profile URL settings when preferences could not be saved.
220 throw new ServerException(_('Error saving user URL shortening preferences.'));
223 $user->query('COMMIT');
225 // TRANS: Confirmation message after saving preferences.
226 return _('Preferences saved.');