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('STATUSNET') && !defined('LACONICA')) {
37 * Change profile settings
41 * @author Evan Prodromou <evan@status.net>
42 * @author Zach Copley <zach@status.net>
43 * @author Sarven Capadisli <csarven@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 ProfilesettingsAction extends SettingsAction
52 * @return string Title of the page
56 // TRANS: Page title for profile settings.
57 return _('Profile settings');
61 * Instructions for use
63 * @return instructions for use
65 function getInstructions()
67 // TRANS: Usage instructions for profile settings.
68 return _('You can update your personal profile info here '.
69 'so people know more about you.');
72 function showScripts()
74 parent::showScripts();
75 $this->autofocus('nickname');
79 * Content area of the page
81 * Shows a form for uploading an avatar.
85 function showContent()
87 $user = common_current_user();
88 $profile = $user->getProfile();
90 $this->elementStart('form', array('method' => 'post',
91 'id' => 'form_settings_profile',
92 'class' => 'form_settings',
93 'action' => common_local_url('profilesettings')));
94 $this->elementStart('fieldset');
95 // TRANS: Profile settings form legend.
96 $this->element('legend', null, _('Profile information'));
97 $this->hidden('token', common_session_token());
99 // too much common patterns here... abstractable?
100 $this->elementStart('ul', 'form_data');
101 if (Event::handle('StartProfileFormData', array($this))) {
102 $this->elementStart('li');
103 // TRANS: Field label in form for profile settings.
104 $this->input('nickname', _('Nickname'),
105 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
106 // TRANS: Tooltip for field label in form for profile settings.
107 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
108 $this->elementEnd('li');
109 $this->elementStart('li');
110 // TRANS: Field label in form for profile settings.
111 $this->input('fullname', _('Full name'),
112 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
113 $this->elementEnd('li');
114 $this->elementStart('li');
115 // TRANS: Field label in form for profile settings.
116 $this->input('homepage', _('Homepage'),
117 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
118 // TRANS: Tooltip for field label in form for profile settings.
119 _('URL of your homepage, blog, or profile on another site.'));
120 $this->elementEnd('li');
121 $this->elementStart('li');
122 $maxBio = Profile::maxBio();
124 // TRANS: Tooltip for field label in form for profile settings. Plural
125 // TRANS: is decided by the number of characters available for the
126 // TRANS: biography (%d).
127 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
128 'Describe yourself and your interests in %d characters.',
132 // TRANS: Tooltip for field label in form for profile settings.
133 $bioInstr = _('Describe yourself and your interests.');
135 // TRANS: Text area label in form for profile settings where users can provide
136 // TRANS: their biography.
137 $this->textarea('bio', _('Bio'),
138 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
140 $this->elementEnd('li');
141 $this->elementStart('li');
142 // TRANS: Field label in form for profile settings.
143 $this->input('location', _('Location'),
144 ($this->arg('location')) ? $this->arg('location') : $profile->location,
145 // TRANS: Tooltip for field label in form for profile settings.
146 _('Where you are, like "City, State (or Region), Country".'));
147 $this->elementEnd('li');
148 if (common_config('location', 'share') == 'user') {
149 $this->elementStart('li');
150 // TRANS: Checkbox label in form for profile settings.
151 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
152 ($this->arg('sharelocation')) ?
153 $this->arg('sharelocation') : $user->shareLocation());
154 $this->elementEnd('li');
156 Event::handle('EndProfileFormData', array($this));
157 $this->elementStart('li');
158 // TRANS: Field label in form for profile settings.
159 $this->input('tags', _('Tags'),
160 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
161 // TRANS: Tooltip for field label in form for profile settings.
162 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
163 $this->elementEnd('li');
164 $this->elementStart('li');
165 $language = common_language();
166 // TRANS: Dropdownlist label in form for profile settings.
167 $this->dropdown('language', _('Language'),
168 // TRANS: Tooltip for dropdown list label in form for profile settings.
169 get_nice_language_list(), _('Preferred language.'),
171 $this->elementEnd('li');
172 $timezone = common_timezone();
173 $timezones = array();
174 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
177 $this->elementStart('li');
178 // TRANS: Dropdownlist label in form for profile settings.
179 $this->dropdown('timezone', _('Timezone'),
180 // TRANS: Tooltip for dropdown list label in form for profile settings.
181 $timezones, _('What timezone are you normally in?'),
183 $this->elementEnd('li');
184 $this->elementStart('li');
185 $this->checkbox('autosubscribe',
186 // TRANS: Checkbox label in form for profile settings.
187 _('Automatically subscribe to whoever '.
188 'subscribes to me (best for non-humans).'),
189 ($this->arg('autosubscribe')) ?
190 $this->boolean('autosubscribe') : $user->autosubscribe);
191 $this->elementEnd('li');
192 $this->elementStart('li');
193 $this->dropdown('subscribe_policy',
194 // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
195 _('Subscription policy'),
196 // TRANS: Dropdown field option for following policy.
197 array(User::SUBSCRIBE_POLICY_OPEN => _('Let anyone follow me'),
198 // TRANS: Dropdown field option for following policy.
199 User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
200 // TRANS: Dropdown field title on group edit form.
201 _('Whether other users need your permission to follow your updates.'),
203 (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
204 $this->elementEnd('li');
206 $this->elementStart('li');
207 $this->checkbox('private_stream',
208 // TRANS: Checkbox label in profile settings.
209 _('Make updates visible only to my followers'),
210 ($this->arg('private_stream')) ?
211 $this->boolean('private_stream') : $user->private_stream);
212 $this->elementEnd('li');
213 $this->elementEnd('ul');
214 // TRANS: Button to save input in profile settings.
215 $this->submit('save', _m('BUTTON','Save'));
217 $this->elementEnd('fieldset');
218 $this->elementEnd('form');
224 * Validate input and save changes. Reload the form with a success
229 function handlePost()
232 $token = $this->trimmed('token');
233 if (!$token || $token != common_session_token()) {
234 // TRANS: Form validation error.
235 $this->showForm(_('There was a problem with your session token. '.
236 'Try again, please.'));
240 if (Event::handle('StartProfileSaveForm', array($this))) {
243 $nickname = Nickname::normalize($this->trimmed('nickname'));
244 } catch (NicknameException $e) {
245 $this->showForm($e->getMessage());
249 $fullname = $this->trimmed('fullname');
250 $homepage = $this->trimmed('homepage');
251 $bio = $this->trimmed('bio');
252 $location = $this->trimmed('location');
253 $autosubscribe = $this->boolean('autosubscribe');
254 $subscribe_policy = $this->trimmed('subscribe_policy');
255 $private_stream = $this->boolean('private_stream');
256 $language = $this->trimmed('language');
257 $timezone = $this->trimmed('timezone');
258 $tagstring = $this->trimmed('tags');
261 if (!User::allowed_nickname($nickname)) {
262 // TRANS: Validation error in form for profile settings.
263 $this->showForm(_('Not a valid nickname.'));
265 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
266 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
267 // TRANS: Validation error in form for profile settings.
268 $this->showForm(_('Homepage is not a valid URL.'));
270 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
271 // TRANS: Validation error in form for profile settings.
272 $this->showForm(_('Full name is too long (maximum 255 characters).'));
274 } else if (Profile::bioTooLong($bio)) {
275 // TRANS: Validation error in form for profile settings.
276 // TRANS: Plural form is used based on the maximum number of allowed
277 // TRANS: characters for the biography (%d).
278 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
279 'Bio is too long (maximum %d characters).',
283 } else if (!is_null($location) && mb_strlen($location) > 255) {
284 // TRANS: Validation error in form for profile settings.
285 $this->showForm(_('Location is too long (maximum 255 characters).'));
287 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
288 // TRANS: Validation error in form for profile settings.
289 $this->showForm(_('Timezone not selected.'));
291 } else if ($this->nicknameExists($nickname)) {
292 // TRANS: Validation error in form for profile settings.
293 $this->showForm(_('Nickname already in use. Try another one.'));
295 } else if (!is_null($language) && strlen($language) > 50) {
296 // TRANS: Validation error in form for profile settings.
297 $this->showForm(_('Language is too long (maximum 50 characters).'));
303 if (is_string($tagstring) && strlen($tagstring) > 0) {
305 $tags = preg_split('/[\s,]+/', $tagstring);
307 foreach ($tags as &$tag) {
308 $private = @$tag[0] === '.';
310 $tag = common_canonical_tag($tag);
311 if (!common_valid_profile_tag($tag)) {
312 // TRANS: Validation error in form for profile settings.
313 // TRANS: %s is an invalid tag.
314 $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
318 $tag_priv[$tag] = $private;
322 $user = common_current_user();
324 $user->query('BEGIN');
326 if ($user->nickname != $nickname ||
327 $user->language != $language ||
328 $user->timezone != $timezone) {
330 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
332 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
334 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
337 $original = clone($user);
339 $user->nickname = $nickname;
340 $user->language = $language;
341 $user->timezone = $timezone;
343 $result = $user->updateKeys($original);
345 if ($result === false) {
346 common_log_db_error($user, 'UPDATE', __FILE__);
347 // TRANS: Server error thrown when user profile settings could not be updated.
348 $this->serverError(_('Could not update user.'));
351 // Re-initialize language environment if it changed
352 common_init_language();
353 // Clear the site owner, in case nickname changed
354 if ($user->hasRole(Profile_role::OWNER)) {
355 User::blow('user:site_owner');
361 if (($user->autosubscribe ^ $autosubscribe) ||
362 ($user->private_stream ^ $private_stream) ||
363 ($user->subscribe_policy != $subscribe_policy)) {
365 $original = clone($user);
367 $user->autosubscribe = $autosubscribe;
368 $user->private_stream = $private_stream;
369 $user->subscribe_policy = $subscribe_policy;
371 $result = $user->update($original);
373 if ($result === false) {
374 common_log_db_error($user, 'UPDATE', __FILE__);
375 // TRANS: Server error thrown when user profile settings could not be updated to
376 // TRANS: automatically subscribe to any subscriber.
377 $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
382 $profile = $user->getProfile();
384 $orig_profile = clone($profile);
386 $profile->nickname = $user->nickname;
387 $profile->fullname = $fullname;
388 $profile->homepage = $homepage;
389 $profile->bio = $bio;
390 $profile->location = $location;
392 $loc = Location::fromName($location);
395 $profile->lat = null;
396 $profile->lon = null;
397 $profile->location_id = null;
398 $profile->location_ns = null;
400 $profile->lat = $loc->lat;
401 $profile->lon = $loc->lon;
402 $profile->location_id = $loc->location_id;
403 $profile->location_ns = $loc->location_ns;
406 $profile->profileurl = common_profile_url($nickname);
408 if (common_config('location', 'share') == 'user') {
412 $prefs = User_location_prefs::staticGet('user_id', $user->id);
415 $prefs = new User_location_prefs();
417 $prefs->user_id = $user->id;
418 $prefs->created = common_sql_now();
421 $orig = clone($prefs);
424 $prefs->share_location = $this->boolean('sharelocation');
427 $result = $prefs->update($orig);
429 $result = $prefs->insert();
432 if ($result === false) {
433 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
434 // TRANS: Server error thrown when user profile location preference settings could not be updated.
435 $this->serverError(_('Could not save location prefs.'));
440 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
441 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
443 $result = $profile->update($orig_profile);
445 if ($result === false) {
446 common_log_db_error($profile, 'UPDATE', __FILE__);
447 // TRANS: Server error thrown when user profile settings could not be saved.
448 $this->serverError(_('Could not save profile.'));
453 $result = $user->setSelfTags($tags, $tag_priv);
456 // TRANS: Server error thrown when user profile settings tags could not be saved.
457 $this->serverError(_('Could not save tags.'));
461 $user->query('COMMIT');
462 Event::handle('EndProfileSaveForm', array($this));
463 common_broadcast_profile($profile);
465 // TRANS: Confirmation shown when user profile settings are saved.
466 $this->showForm(_('Settings saved.'), true);
471 function nicknameExists($nickname)
473 $user = common_current_user();
474 $other = User::staticGet('nickname', $nickname);
478 return $other->id != $user->id;
482 function showAside() {
483 $user = common_current_user();
485 $this->elementStart('div', array('id' => 'aside_primary',
486 'class' => 'aside'));
488 $this->elementStart('div', array('id' => 'account_actions',
489 'class' => 'section'));
490 $this->elementStart('ul');
491 if (Event::handle('StartProfileSettingsActions', array($this))) {
492 if ($user->hasRight(Right::BACKUPACCOUNT)) {
493 $this->elementStart('li');
495 array('href' => common_local_url('backupaccount')),
496 // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
497 _('Backup account'));
498 $this->elementEnd('li');
500 if ($user->hasRight(Right::DELETEACCOUNT)) {
501 $this->elementStart('li');
503 array('href' => common_local_url('deleteaccount')),
504 // TRANS: Option in profile settings to delete the account of the currently logged in user.
505 _('Delete account'));
506 $this->elementEnd('li');
508 if ($user->hasRight(Right::RESTOREACCOUNT)) {
509 $this->elementStart('li');
511 array('href' => common_local_url('restoreaccount')),
512 // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
513 _('Restore account'));
514 $this->elementEnd('li');
516 Event::handle('EndProfileSettingsActions', array($this));
518 $this->elementEnd('ul');
519 $this->elementEnd('div');
520 $this->elementEnd('div');