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 $profile = $this->scoped;
86 $user = $this->scoped->getUser();
88 $this->elementStart('form', array('method' => 'post',
89 'id' => 'form_settings_profile',
90 'class' => 'form_settings',
91 'action' => common_local_url('profilesettings')));
92 $this->elementStart('fieldset');
93 // TRANS: Profile settings form legend.
94 $this->element('legend', null, _('Profile information'));
95 $this->hidden('token', common_session_token());
97 // too much common patterns here... abstractable?
98 $this->elementStart('ul', 'form_data');
99 if (Event::handle('StartProfileFormData', array($this))) {
100 $this->elementStart('li');
101 // TRANS: Field label in form for profile settings.
102 $this->input('nickname', _('Nickname'),
103 $this->arg('nickname') ?: $profile->nickname,
104 // TRANS: Tooltip for field label in form for profile settings.
105 _('1-64 lowercase letters or numbers, no punctuation or spaces.'),
106 null, false, // "name" (will be set to id), then "required"
107 !common_config('profile', 'changenick')
108 ? array('disabled' => 'disabled', 'placeholder' => null)
109 : array('placeholder' => null));
110 $this->elementEnd('li');
111 $this->elementStart('li');
112 // TRANS: Field label in form for profile settings.
113 $this->input('fullname', _('Full name'),
114 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
115 $this->elementEnd('li');
116 $this->elementStart('li');
117 // TRANS: Field label in form for profile settings.
118 $this->input('homepage', _('Homepage'),
119 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
120 // TRANS: Tooltip for field label in form for profile settings.
121 _('URL of your homepage, blog, or profile on another site.'));
122 $this->elementEnd('li');
123 $this->elementStart('li');
124 $maxBio = Profile::maxBio();
126 // TRANS: Tooltip for field label in form for profile settings. Plural
127 // TRANS: is decided by the number of characters available for the
128 // TRANS: biography (%d).
129 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
130 'Describe yourself and your interests in %d characters.',
134 // TRANS: Tooltip for field label in form for profile settings.
135 $bioInstr = _('Describe yourself and your interests.');
137 // TRANS: Text area label in form for profile settings where users can provide
138 // TRANS: their biography.
139 $this->textarea('bio', _('Bio'),
140 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
142 $this->elementEnd('li');
143 $this->elementStart('li');
144 // TRANS: Field label in form for profile settings.
145 $this->input('location', _('Location'),
146 ($this->arg('location')) ? $this->arg('location') : $profile->location,
147 // TRANS: Tooltip for field label in form for profile settings.
148 _('Where you are, like "City, State (or Region), Country".'));
149 $this->elementEnd('li');
150 if (common_config('location', 'share') == 'user') {
151 $this->elementStart('li');
152 // TRANS: Checkbox label in form for profile settings.
153 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
154 ($this->arg('sharelocation')) ?
155 $this->arg('sharelocation') : $this->scoped->shareLocation());
156 $this->elementEnd('li');
158 Event::handle('EndProfileFormData', array($this));
159 $this->elementStart('li');
160 // TRANS: Field label in form for profile settings.
161 $this->input('tags', _('Tags'),
162 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
163 // TRANS: Tooltip for field label in form for profile settings.
164 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
165 $this->elementEnd('li');
166 $this->elementStart('li');
167 $language = common_language();
168 // TRANS: Dropdownlist label in form for profile settings.
169 $this->dropdown('language', _('Language'),
170 // TRANS: Tooltip for dropdown list label in form for profile settings.
171 get_nice_language_list(), _('Preferred language.'),
173 $this->elementEnd('li');
174 $timezone = common_timezone();
175 $timezones = array();
176 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
179 $this->elementStart('li');
180 // TRANS: Dropdownlist label in form for profile settings.
181 $this->dropdown('timezone', _('Timezone'),
182 // TRANS: Tooltip for dropdown list label in form for profile settings.
183 $timezones, _('What timezone are you normally in?'),
185 $this->elementEnd('li');
186 $this->elementStart('li');
187 $this->checkbox('autosubscribe',
188 // TRANS: Checkbox label in form for profile settings.
189 _('Automatically subscribe to whoever '.
190 'subscribes to me (best for non-humans)'),
191 ($this->arg('autosubscribe')) ?
192 $this->boolean('autosubscribe') : $user->autosubscribe);
193 $this->elementEnd('li');
194 $this->elementStart('li');
195 $this->dropdown('subscribe_policy',
196 // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
197 _('Subscription policy'),
198 // TRANS: Dropdown field option for following policy.
199 array(User::SUBSCRIBE_POLICY_OPEN => _('Let anyone follow me'),
200 // TRANS: Dropdown field option for following policy.
201 User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
202 // TRANS: Dropdown field title on group edit form.
203 _('Whether other users need your permission to follow your updates.'),
205 (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
206 $this->elementEnd('li');
208 $this->elementStart('li');
209 $this->checkbox('private_stream',
210 // TRANS: Checkbox label in profile settings.
211 _('Make updates visible only to my followers'),
212 ($this->arg('private_stream')) ?
213 $this->boolean('private_stream') : $user->private_stream);
214 $this->elementEnd('li');
215 $this->elementEnd('ul');
216 // TRANS: Button to save input in profile settings.
217 $this->submit('save', _m('BUTTON','Save'));
219 $this->elementEnd('fieldset');
220 $this->elementEnd('form');
226 * Validate input and save changes. Reload the form with a success
231 function handlePost()
234 $token = $this->trimmed('token');
235 if (!$token || $token != common_session_token()) {
236 // TRANS: Form validation error.
237 $this->showForm(_('There was a problem with your session token. '.
238 'Try again, please.'));
242 if (Event::handle('StartProfileSaveForm', array($this))) {
244 // $nickname will only be set if this changenick value is true.
245 if (common_config('profile', 'changenick') == true) {
247 $nickname = Nickname::normalize($this->trimmed('nickname'), true);
248 } catch (NicknameTakenException $e) {
249 // Abort only if the nickname is occupied by another local profile
250 if ($e->profile->id != $this->scoped->id) {
251 $this->showForm($e->getMessage());
254 $nickname = Nickname::normalize($this->trimmed('nickname')); // without in-use check this time
255 } catch (NicknameException $e) {
256 $this->showForm($e->getMessage());
261 $fullname = $this->trimmed('fullname');
262 $homepage = $this->trimmed('homepage');
263 $bio = $this->trimmed('bio');
264 $location = $this->trimmed('location');
265 $autosubscribe = $this->booleanintstring('autosubscribe');
266 $subscribe_policy = $this->trimmed('subscribe_policy');
267 $private_stream = $this->booleanintstring('private_stream');
268 $language = $this->trimmed('language');
269 $timezone = $this->trimmed('timezone');
270 $tagstring = $this->trimmed('tags');
273 if (!is_null($homepage) && (strlen($homepage) > 0) &&
274 !common_valid_http_url($homepage)) {
275 // TRANS: Validation error in form for profile settings.
276 $this->showForm(_('Homepage is not a valid URL.'));
278 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
279 // TRANS: Validation error in form for profile settings.
280 $this->showForm(_('Full name is too long (maximum 255 characters).'));
282 } else if (Profile::bioTooLong($bio)) {
283 // TRANS: Validation error in form for profile settings.
284 // TRANS: Plural form is used based on the maximum number of allowed
285 // TRANS: characters for the biography (%d).
286 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
287 'Bio is too long (maximum %d characters).',
291 } else if (!is_null($location) && mb_strlen($location) > 255) {
292 // TRANS: Validation error in form for profile settings.
293 $this->showForm(_('Location is too long (maximum 255 characters).'));
295 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
296 // TRANS: Validation error in form for profile settings.
297 $this->showForm(_('Timezone not selected.'));
299 } else if (!is_null($language) && strlen($language) > 50) {
300 // TRANS: Validation error in form for profile settings.
301 $this->showForm(_('Language is too long (maximum 50 characters).'));
307 if (is_string($tagstring) && strlen($tagstring) > 0) {
309 $tags = preg_split('/[\s,]+/', $tagstring);
311 foreach ($tags as &$tag) {
312 $private = @$tag[0] === '.';
314 $tag = common_canonical_tag($tag);
315 if (!common_valid_profile_tag($tag)) {
316 // TRANS: Validation error in form for profile settings.
317 // TRANS: %s is an invalid tag.
318 $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
322 $tag_priv[$tag] = $private;
326 $user = common_current_user();
327 $user->query('BEGIN');
329 // $user->nickname is updated through Profile->update();
332 if (($user->autosubscribe ^ $autosubscribe)
333 || ($user->private_stream ^ $private_stream)
334 || $user->timezone != $timezone
335 || $user->language != $language
336 || $user->subscribe_policy != $subscribe_policy) {
338 $original = clone($user);
340 $user->autosubscribe = $autosubscribe;
341 $user->language = $language;
342 $user->private_stream = $private_stream;
343 $user->subscribe_policy = $subscribe_policy;
344 $user->timezone = $timezone;
346 $result = $user->update($original);
347 if ($result === false) {
348 common_log_db_error($user, 'UPDATE', __FILE__);
349 // TRANS: Server error thrown when user profile settings could not be updated to
350 // TRANS: automatically subscribe to any subscriber.
351 $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
354 // Re-initialize language environment if it changed
355 common_init_language();
358 $profile = $user->getProfile();
360 $orig_profile = clone($profile);
362 if (common_config('profile', 'changenick') == true && $profile->nickname !== $nickname) {
363 assert(Nickname::normalize($nickname)===$nickname);
364 common_debug("Changing user nickname from '{$profile->nickname}' to '{$nickname}'.");
365 $profile->nickname = $nickname;
366 $profile->profileurl = common_profile_url($profile->nickname);
368 $profile->fullname = $fullname;
369 $profile->homepage = $homepage;
370 $profile->bio = $bio;
371 $profile->location = $location;
373 $loc = Location::fromName($location);
376 $profile->lat = null;
377 $profile->lon = null;
378 $profile->location_id = null;
379 $profile->location_ns = null;
381 $profile->lat = $loc->lat;
382 $profile->lon = $loc->lon;
383 $profile->location_id = $loc->location_id;
384 $profile->location_ns = $loc->location_ns;
387 if (common_config('location', 'share') == 'user') {
391 $prefs = User_location_prefs::getKV('user_id', $user->id);
394 $prefs = new User_location_prefs();
396 $prefs->user_id = $user->id;
397 $prefs->created = common_sql_now();
400 $orig = clone($prefs);
403 $prefs->share_location = $this->booleanintstring('sharelocation');
406 $result = $prefs->update($orig);
408 $result = $prefs->insert();
411 if ($result === false) {
412 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
413 // TRANS: Server error thrown when user profile location preference settings could not be updated.
414 $this->serverError(_('Could not save location prefs.'));
418 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
419 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
421 $result = $profile->update($orig_profile);
423 if ($result === false) {
424 common_log_db_error($profile, 'UPDATE', __FILE__);
425 // TRANS: Server error thrown when user profile settings could not be saved.
426 $this->serverError(_('Could not save profile.'));
430 $result = $user->setSelfTags($tags, $tag_priv);
433 // TRANS: Server error thrown when user profile settings tags could not be saved.
434 $this->serverError(_('Could not save tags.'));
437 $user->query('COMMIT');
438 Event::handle('EndProfileSaveForm', array($this));
440 // TRANS: Confirmation shown when user profile settings are saved.
441 $this->showForm(_('Settings saved.'), true);
446 function showAside() {
447 $user = common_current_user();
449 $this->elementStart('div', array('id' => 'aside_primary',
450 'class' => 'aside'));
452 $this->elementStart('div', array('id' => 'account_actions',
453 'class' => 'section'));
454 $this->elementStart('ul');
455 if (Event::handle('StartProfileSettingsActions', array($this))) {
456 if ($user->hasRight(Right::BACKUPACCOUNT)) {
457 $this->elementStart('li');
459 array('href' => common_local_url('backupaccount')),
460 // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
461 _('Backup account'));
462 $this->elementEnd('li');
464 if ($user->hasRight(Right::DELETEACCOUNT)) {
465 $this->elementStart('li');
467 array('href' => common_local_url('deleteaccount')),
468 // TRANS: Option in profile settings to delete the account of the currently logged in user.
469 _('Delete account'));
470 $this->elementEnd('li');
472 if ($user->hasRight(Right::RESTOREACCOUNT)) {
473 $this->elementStart('li');
475 array('href' => common_local_url('restoreaccount')),
476 // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
477 _('Restore account'));
478 $this->elementEnd('li');
480 Event::handle('EndProfileSettingsActions', array($this));
482 $this->elementEnd('ul');
483 $this->elementEnd('div');
484 $this->elementEnd('div');