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')) {
36 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
39 * Change profile settings
43 * @author Evan Prodromou <evan@status.net>
44 * @author Zach Copley <zach@status.net>
45 * @author Sarven Capadisli <csarven@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 class ProfilesettingsAction extends AccountSettingsAction
54 * @return string Title of the page
58 // TRANS: Page title for profile settings.
59 return _('Profile settings');
63 * Instructions for use
65 * @return instructions for use
67 function getInstructions()
69 // TRANS: Usage instructions for profile settings.
70 return _('You can update your personal profile info here '.
71 'so people know more about you.');
74 function showScripts()
76 parent::showScripts();
77 $this->autofocus('nickname');
81 * Content area of the page
83 * Shows a form for uploading an avatar.
87 function showContent()
89 $user = common_current_user();
90 $profile = $user->getProfile();
92 $this->elementStart('form', array('method' => 'post',
93 'id' => 'form_settings_profile',
94 'class' => 'form_settings',
95 'action' => common_local_url('profilesettings')));
96 $this->elementStart('fieldset');
97 // TRANS: Profile settings form legend.
98 $this->element('legend', null, _('Profile information'));
99 $this->hidden('token', common_session_token());
101 // too much common patterns here... abstractable?
102 $this->elementStart('ul', 'form_data');
103 if (Event::handle('StartProfileFormData', array($this))) {
104 $this->elementStart('li');
105 // TRANS: Field label in form for profile settings.
106 $this->input('nickname', _('Nickname'),
107 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
108 // TRANS: Tooltip for field label in form for profile settings.
109 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
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') : $user->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');
195 $this->elementEnd('ul');
196 // TRANS: Button to save input in profile settings.
197 $this->submit('save', _m('BUTTON','Save'));
199 $this->elementEnd('fieldset');
200 $this->elementEnd('form');
206 * Validate input and save changes. Reload the form with a success
211 function handlePost()
214 $token = $this->trimmed('token');
215 if (!$token || $token != common_session_token()) {
216 // TRANS: Form validation error.
217 $this->showForm(_('There was a problem with your session token. '.
218 'Try again, please.'));
222 if (Event::handle('StartProfileSaveForm', array($this))) {
225 $nickname = Nickname::normalize($this->trimmed('nickname'));
226 } catch (NicknameException $e) {
227 $this->showForm($e->getMessage());
231 $fullname = $this->trimmed('fullname');
232 $homepage = $this->trimmed('homepage');
233 $bio = $this->trimmed('bio');
234 $location = $this->trimmed('location');
235 $autosubscribe = $this->boolean('autosubscribe');
236 $language = $this->trimmed('language');
237 $timezone = $this->trimmed('timezone');
238 $tagstring = $this->trimmed('tags');
241 if (!User::allowed_nickname($nickname)) {
242 // TRANS: Validation error in form for profile settings.
243 $this->showForm(_('Not a valid nickname.'));
245 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
246 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
247 // TRANS: Validation error in form for profile settings.
248 $this->showForm(_('Homepage is not a valid URL.'));
250 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
251 // TRANS: Validation error in form for profile settings.
252 $this->showForm(_('Full name is too long (maximum 255 characters).'));
254 } else if (Profile::bioTooLong($bio)) {
255 // TRANS: Validation error in form for profile settings.
256 // TRANS: Plural form is used based on the maximum number of allowed
257 // TRANS: characters for the biography (%d).
258 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
259 'Bio is too long (maximum %d characters).',
263 } else if (!is_null($location) && mb_strlen($location) > 255) {
264 // TRANS: Validation error in form for profile settings.
265 $this->showForm(_('Location is too long (maximum 255 characters).'));
267 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
268 // TRANS: Validation error in form for profile settings.
269 $this->showForm(_('Timezone not selected.'));
271 } else if ($this->nicknameExists($nickname)) {
272 // TRANS: Validation error in form for profile settings.
273 $this->showForm(_('Nickname already in use. Try another one.'));
275 } else if (!is_null($language) && strlen($language) > 50) {
276 // TRANS: Validation error in form for profile settings.
277 $this->showForm(_('Language is too long (maximum 50 characters).'));
282 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
287 foreach ($tags as $tag) {
288 if (!common_valid_profile_tag($tag)) {
289 // TRANS: Validation error in form for profile settings.
290 // TRANS: %s is an invalid tag.
291 $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
296 $user = common_current_user();
298 $user->query('BEGIN');
300 if ($user->nickname != $nickname ||
301 $user->language != $language ||
302 $user->timezone != $timezone) {
304 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
306 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
308 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
311 $original = clone($user);
313 $user->nickname = $nickname;
314 $user->language = $language;
315 $user->timezone = $timezone;
317 $result = $user->updateKeys($original);
319 if ($result === false) {
320 common_log_db_error($user, 'UPDATE', __FILE__);
321 // TRANS: Server error thrown when user profile settings could not be updated.
322 $this->serverError(_('Could not update user.'));
325 // Re-initialize language environment if it changed
326 common_init_language();
327 // Clear the site owner, in case nickname changed
328 if ($user->hasRole(Profile_role::OWNER)) {
329 User::blow('user:site_owner');
335 if ($user->autosubscribe ^ $autosubscribe) {
337 $original = clone($user);
339 $user->autosubscribe = $autosubscribe;
341 $result = $user->update($original);
343 if ($result === false) {
344 common_log_db_error($user, 'UPDATE', __FILE__);
345 // TRANS: Server error thrown when user profile settings could not be updated to
346 // TRANS: automatically subscribe to any subscriber.
347 $this->serverError(_('Could not update user for autosubscribe.'));
352 $profile = $user->getProfile();
354 $orig_profile = clone($profile);
356 $profile->nickname = $user->nickname;
357 $profile->fullname = $fullname;
358 $profile->homepage = $homepage;
359 $profile->bio = $bio;
360 $profile->location = $location;
362 $loc = Location::fromName($location);
365 $profile->lat = null;
366 $profile->lon = null;
367 $profile->location_id = null;
368 $profile->location_ns = null;
370 $profile->lat = $loc->lat;
371 $profile->lon = $loc->lon;
372 $profile->location_id = $loc->location_id;
373 $profile->location_ns = $loc->location_ns;
376 $profile->profileurl = common_profile_url($nickname);
378 if (common_config('location', 'share') == 'user') {
382 $prefs = User_location_prefs::staticGet('user_id', $user->id);
385 $prefs = new User_location_prefs();
387 $prefs->user_id = $user->id;
388 $prefs->created = common_sql_now();
391 $orig = clone($prefs);
394 $prefs->share_location = $this->boolean('sharelocation');
397 $result = $prefs->update($orig);
399 $result = $prefs->insert();
402 if ($result === false) {
403 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
404 // TRANS: Server error thrown when user profile location preference settings could not be updated.
405 $this->serverError(_('Could not save location prefs.'));
410 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
411 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
413 $result = $profile->update($orig_profile);
415 if ($result === false) {
416 common_log_db_error($profile, 'UPDATE', __FILE__);
417 // TRANS: Server error thrown when user profile settings could not be saved.
418 $this->serverError(_('Could not save profile.'));
423 $result = $user->setSelfTags($tags);
426 // TRANS: Server error thrown when user profile settings tags could not be saved.
427 $this->serverError(_('Could not save tags.'));
431 $user->query('COMMIT');
432 Event::handle('EndProfileSaveForm', array($this));
433 common_broadcast_profile($profile);
435 // TRANS: Confirmation shown when user profile settings are saved.
436 $this->showForm(_('Settings saved.'), true);
441 function nicknameExists($nickname)
443 $user = common_current_user();
444 $other = User::staticGet('nickname', $nickname);
448 return $other->id != $user->id;
452 function showAside() {
453 $user = common_current_user();
455 $this->elementStart('div', array('id' => 'aside_primary',
456 'class' => 'aside'));
458 $this->elementStart('div', array('id' => 'account_actions',
459 'class' => 'section'));
460 $this->elementStart('ul');
461 if (Event::handle('StartProfileSettingsActions', array($this))) {
462 if ($user->hasRight(Right::BACKUPACCOUNT)) {
463 $this->elementStart('li');
465 array('href' => common_local_url('backupaccount')),
466 // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
467 _('Backup account'));
468 $this->elementEnd('li');
470 if ($user->hasRight(Right::DELETEACCOUNT)) {
471 $this->elementStart('li');
473 array('href' => common_local_url('deleteaccount')),
474 // TRANS: Option in profile settings to delete the account of the currently logged in user.
475 _('Delete account'));
476 $this->elementEnd('li');
478 if ($user->hasRight(Right::RESTOREACCOUNT)) {
479 $this->elementStart('li');
481 array('href' => common_local_url('restoreaccount')),
482 // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
483 _('Restore account'));
484 $this->elementEnd('li');
486 Event::handle('EndProfileSettingsActions', array($this));
488 $this->elementEnd('ul');
489 $this->elementEnd('div');
490 $this->elementEnd('div');