3 * StatusNet, the distributed open-source microblogging tool
5 * Change profile 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 Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @author Sarven Capadisli <csarven@status.net>
27 * @copyright 2008-2009 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('GNUSOCIAL')) { exit(1); }
35 * Change profile settings
39 * @author Evan Prodromou <evan@status.net>
40 * @author Zach Copley <zach@status.net>
41 * @author Sarven Capadisli <csarven@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 ProfilesettingsAction extends SettingsAction
50 * @return string Title of the page
54 // TRANS: Page title for profile settings.
55 return _('Profile settings');
59 * Instructions for use
61 * @return instructions for use
63 function getInstructions()
65 // TRANS: Usage instructions for profile settings.
66 return _('You can update your personal profile info here '.
67 'so people know more about you.');
70 function showScripts()
72 parent::showScripts();
73 $this->autofocus('fullname');
77 * Content area of the page
79 * Shows a form for uploading an avatar.
83 function showContent()
85 $user = $this->scoped->getUser();
87 $this->elementStart('form', array('method' => 'post',
88 'id' => 'form_settings_profile',
89 'class' => 'form_settings',
90 'action' => common_local_url('profilesettings')));
91 $this->elementStart('fieldset');
92 // TRANS: Profile settings form legend.
93 $this->element('legend', null, _('Profile information'));
94 $this->hidden('token', common_session_token());
96 // too much common patterns here... abstractable?
97 $this->elementStart('ul', 'form_data');
98 if (Event::handle('StartProfileFormData', array($this))) {
99 $this->elementStart('li');
100 // TRANS: Field label in form for profile settings.
101 $this->input('nickname', _('Nickname'),
102 $this->trimmed('nickname') ?: $this->scoped->getNickname(),
103 // TRANS: Tooltip for field label in form for profile settings.
104 _('1-64 lowercase letters or numbers, no punctuation or spaces.'),
105 null, false, // "name" (will be set to id), then "required"
106 !common_config('profile', 'changenick')
107 ? array('disabled' => 'disabled', 'placeholder' => null)
108 : array('placeholder' => null));
109 $this->elementEnd('li');
110 $this->elementStart('li');
111 // TRANS: Field label in form for profile settings.
112 $this->input('fullname', _('Full name'),
113 $this->trimmed('fullname') ?: $this->scoped->getFullname());
114 $this->elementEnd('li');
115 $this->elementStart('li');
116 // TRANS: Field label in form for profile settings.
117 $this->input('homepage', _('Homepage'),
118 $this->trimmed('homepage') ?: $this->scoped->getHomepage(),
119 // TRANS: Tooltip for field label in form for profile settings.
120 _('URL of your homepage, blog, or profile on another site.'));
121 $this->elementEnd('li');
122 $this->elementStart('li');
123 $maxBio = Profile::maxBio();
125 // TRANS: Tooltip for field label in form for profile settings. Plural
126 // TRANS: is decided by the number of characters available for the
127 // TRANS: biography (%d).
128 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
129 'Describe yourself and your interests in %d characters.',
133 // TRANS: Tooltip for field label in form for profile settings.
134 $bioInstr = _('Describe yourself and your interests.');
136 // TRANS: Text area label in form for profile settings where users can provide
137 // TRANS: their biography.
138 $this->textarea('bio', _('Bio'),
139 $this->trimmed('bio') ?: $this->scoped->getDescription(),
141 $this->elementEnd('li');
142 $this->elementStart('li');
143 // TRANS: Field label in form for profile settings.
144 $this->input('location', _('Location'),
145 $this->trimmed('location') ?: $this->scoped->location,
146 // TRANS: Tooltip for field label in form for profile settings.
147 _('Where you are, like "City, State (or Region), Country".'));
148 $this->elementEnd('li');
149 if (common_config('location', 'share') == 'user') {
150 $this->elementStart('li');
151 // TRANS: Checkbox label in form for profile settings.
152 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
153 ($this->arg('sharelocation')) ?
154 $this->boolean('sharelocation') : $this->scoped->shareLocation());
155 $this->elementEnd('li');
157 Event::handle('EndProfileFormData', array($this));
158 $this->elementStart('li');
159 // TRANS: Field label in form for profile settings.
160 $this->input('tags', _('Tags'),
161 $this->trimmed('tags') ?: implode(' ', Profile_tag::getSelfTagsArray($this->scoped)),
162 // TRANS: Tooltip for field label in form for profile settings.
163 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
164 $this->elementEnd('li');
165 $this->elementStart('li');
166 $language = common_language();
167 // TRANS: Dropdownlist label in form for profile settings.
168 $this->dropdown('language', _('Language'),
169 // TRANS: Tooltip for dropdown list label in form for profile settings.
170 get_nice_language_list(), _('Preferred language.'),
172 $this->elementEnd('li');
173 $timezone = common_timezone();
174 $timezones = array();
175 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
178 $this->elementStart('li');
179 // TRANS: Dropdownlist label in form for profile settings.
180 $this->dropdown('timezone', _('Timezone'),
181 // TRANS: Tooltip for dropdown list label in form for profile settings.
182 $timezones, _('What timezone are you normally in?'),
184 $this->elementEnd('li');
185 $this->elementStart('li');
186 $this->checkbox('autosubscribe',
187 // TRANS: Checkbox label in form for profile settings.
188 _('Automatically subscribe to whoever '.
189 'subscribes to me (best for non-humans)'),
190 ($this->arg('autosubscribe')) ?
191 $this->boolean('autosubscribe') : $user->autosubscribe);
192 $this->elementEnd('li');
193 $this->elementStart('li');
194 $this->dropdown('subscribe_policy',
195 // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
196 _('Subscription policy'),
197 // TRANS: Dropdown field option for following policy.
198 array(User::SUBSCRIBE_POLICY_OPEN => _('Let anyone follow me'),
199 // TRANS: Dropdown field option for following policy.
200 User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
201 // TRANS: Dropdown field title on group edit form.
202 _('Whether other users need your permission to follow your updates.'),
204 (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
205 $this->elementEnd('li');
207 $this->elementStart('li');
208 $this->checkbox('private_stream',
209 // TRANS: Checkbox label in profile settings.
210 _('Make updates visible only to my followers'),
211 ($this->arg('private_stream')) ?
212 $this->boolean('private_stream') : $user->private_stream);
213 $this->elementEnd('li');
214 $this->elementEnd('ul');
215 // TRANS: Button to save input in profile settings.
216 $this->submit('save', _m('BUTTON','Save'));
218 $this->elementEnd('fieldset');
219 $this->elementEnd('form');
225 * Validate input and save changes. Reload the form with a success
230 protected function doPost()
232 if (Event::handle('StartProfileSaveForm', array($this))) {
234 // $nickname will only be set if this changenick value is true.
235 if (common_config('profile', 'changenick') == true) {
237 $nickname = Nickname::normalize($this->trimmed('nickname'), true);
238 } catch (NicknameTakenException $e) {
239 // Abort only if the nickname is occupied by _another_ local user profile
240 if (!$this->scoped->sameAs($e->profile)) {
243 // Since the variable wasn't set before the exception was thrown, let's run
244 // the normalize sequence again, but without in-use check this time.
245 $nickname = Nickname::normalize($this->trimmed('nickname'));
249 $fullname = $this->trimmed('fullname');
250 $homepage = $this->trimmed('homepage');
251 $bio = $this->trimmed('bio');
252 $location = $this->trimmed('location');
253 $autosubscribe = $this->booleanintstring('autosubscribe');
254 $subscribe_policy = $this->trimmed('subscribe_policy');
255 $private_stream = $this->booleanintstring('private_stream');
256 $language = $this->trimmed('language');
257 $timezone = $this->trimmed('timezone');
258 $tagstring = $this->trimmed('tags');
261 if (!is_null($homepage) && (strlen($homepage) > 0) &&
262 !common_valid_http_url($homepage)) {
263 // TRANS: Validation error in form for profile settings.
264 throw new ClientException(_('Homepage is not a valid URL.'));
265 } else if (!is_null($fullname) && mb_strlen($fullname) > 191) {
266 // TRANS: Validation error in form for profile settings.
267 throw new ClientException(_('Full name is too long (maximum 191 characters).'));
268 } else if (Profile::bioTooLong($bio)) {
269 // TRANS: Validation error in form for profile settings.
270 // TRANS: Plural form is used based on the maximum number of allowed
271 // TRANS: characters for the biography (%d).
272 throw new ClientException(sprintf(_m('Bio is too long (maximum %d character).',
273 'Bio is too long (maximum %d characters).',
276 } else if (!is_null($location) && mb_strlen($location) > 191) {
277 // TRANS: Validation error in form for profile settings.
278 throw new ClientException(_('Location is too long (maximum 191 characters).'));
279 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
280 // TRANS: Validation error in form for profile settings.
281 throw new ClientException(_('Timezone not selected.'));
282 } else if (!is_null($language) && strlen($language) > 50) {
283 // TRANS: Validation error in form for profile settings.
284 throw new ClientException(_('Language is too long (maximum 50 characters).'));
289 if (is_string($tagstring) && strlen($tagstring) > 0) {
291 $tags = preg_split('/[\s,]+/', $tagstring);
293 foreach ($tags as &$tag) {
294 $private = @$tag[0] === '.';
296 $tag = common_canonical_tag($tag);
297 if (!common_valid_profile_tag($tag)) {
298 // TRANS: Validation error in form for profile settings.
299 // TRANS: %s is an invalid tag.
300 throw new ClientException(sprintf(_('Invalid tag: "%s".'), $tag));
303 $tag_priv[$tag] = $private;
307 $user = $this->scoped->getUser();
308 $user->query('BEGIN');
310 // $user->nickname is updated through Profile->update();
313 if (($user->autosubscribe ^ $autosubscribe)
314 || ($user->private_stream ^ $private_stream)
315 || $user->timezone != $timezone
316 || $user->language != $language
317 || $user->subscribe_policy != $subscribe_policy) {
319 $original = clone($user);
321 $user->autosubscribe = $autosubscribe;
322 $user->language = $language;
323 $user->private_stream = $private_stream;
324 $user->subscribe_policy = $subscribe_policy;
325 $user->timezone = $timezone;
327 $result = $user->update($original);
328 if ($result === false) {
329 common_log_db_error($user, 'UPDATE', __FILE__);
330 $user->query('ROLLBACK');
331 // TRANS: Server error thrown when user profile settings could not be updated to
332 // TRANS: automatically subscribe to any subscriber.
333 throw new ServerException(_('Could not update user for autosubscribe or subscribe_policy.'));
336 // Re-initialize language environment if it changed
337 common_init_language();
340 $original = clone($this->scoped);
342 if (common_config('profile', 'changenick') == true && $this->scoped->getNickname() !== $nickname) {
343 assert(Nickname::normalize($nickname)===$nickname);
344 common_debug("Changing user nickname from '{$this->scoped->getNickname()}' to '{$nickname}'.");
345 $this->scoped->nickname = $nickname;
346 $this->scoped->profileurl = common_profile_url($this->scoped->getNickname());
348 $this->scoped->fullname = $fullname;
349 $this->scoped->homepage = $homepage;
350 $this->scoped->bio = $bio;
351 $this->scoped->location = $location;
353 $loc = Location::fromName($location);
356 $this->scoped->lat = null;
357 $this->scoped->lon = null;
358 $this->scoped->location_id = null;
359 $this->scoped->location_ns = null;
361 $this->scoped->lat = $loc->lat;
362 $this->scoped->lon = $loc->lon;
363 $this->scoped->location_id = $loc->location_id;
364 $this->scoped->location_ns = $loc->location_ns;
367 if (common_config('location', 'share') == 'user') {
371 $prefs = User_location_prefs::getKV('user_id', $this->scoped->getID());
374 $prefs = new User_location_prefs();
376 $prefs->user_id = $this->scoped->getID();
377 $prefs->created = common_sql_now();
380 $orig = clone($prefs);
383 $prefs->share_location = $this->booleanintstring('sharelocation');
386 $result = $prefs->update($orig);
388 $result = $prefs->insert();
391 if ($result === false) {
392 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
393 $user->query('ROLLBACK');
394 // TRANS: Server error thrown when user profile location preference settings could not be updated.
395 throw new ServerException(_('Could not save location prefs.'));
399 common_debug('Old profile: ' . common_log_objstring($original), __FILE__);
400 common_debug('New profile: ' . common_log_objstring($this->scoped), __FILE__);
402 $result = $this->scoped->update($original);
404 if ($result === false) {
405 common_log_db_error($this->scoped, 'UPDATE', __FILE__);
406 $user->query('ROLLBACK');
407 // TRANS: Server error thrown when user profile settings could not be saved.
408 throw new ServerException(_('Could not save profile.'));
412 $result = Profile_tag::setSelfTags($this->scoped, $tags, $tag_priv);
414 $user->query('COMMIT');
415 Event::handle('EndProfileSaveForm', array($this));
417 // TRANS: Confirmation shown when user profile settings are saved.
418 return _('Settings saved.');
423 function showAside() {
424 $this->elementStart('div', array('id' => 'aside_primary',
425 'class' => 'aside'));
427 $this->elementStart('div', array('id' => 'account_actions',
428 'class' => 'section'));
429 $this->elementStart('ul');
430 if (Event::handle('StartProfileSettingsActions', array($this))) {
431 if ($this->scoped->hasRight(Right::BACKUPACCOUNT)) {
432 $this->elementStart('li');
434 array('href' => common_local_url('backupaccount')),
435 // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
436 _('Backup account'));
437 $this->elementEnd('li');
439 if ($this->scoped->hasRight(Right::DELETEACCOUNT)) {
440 $this->elementStart('li');
442 array('href' => common_local_url('deleteaccount')),
443 // TRANS: Option in profile settings to delete the account of the currently logged in user.
444 _('Delete account'));
445 $this->elementEnd('li');
447 if ($this->scoped->hasRight(Right::RESTOREACCOUNT)) {
448 $this->elementStart('li');
450 array('href' => common_local_url('restoreaccount')),
451 // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
452 _('Restore account'));
453 $this->elementEnd('li');
455 Event::handle('EndProfileSettingsActions', array($this));
457 $this->elementEnd('ul');
458 $this->elementEnd('div');
459 $this->elementEnd('div');