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 $this->showForm(_('There was a problem with your session token. '.
217 'Try again, please.'));
221 if (Event::handle('StartProfileSaveForm', array($this))) {
224 $nickname = Nickname::normalize($this->trimmed('nickname'));
225 } catch (NicknameException $e) {
226 $this->showForm($e->getMessage());
230 $fullname = $this->trimmed('fullname');
231 $homepage = $this->trimmed('homepage');
232 $bio = $this->trimmed('bio');
233 $location = $this->trimmed('location');
234 $autosubscribe = $this->boolean('autosubscribe');
235 $language = $this->trimmed('language');
236 $timezone = $this->trimmed('timezone');
237 $tagstring = $this->trimmed('tags');
240 if (!User::allowed_nickname($nickname)) {
241 // TRANS: Validation error in form for profile settings.
242 $this->showForm(_('Not a valid nickname.'));
244 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
245 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
246 // TRANS: Validation error in form for profile settings.
247 $this->showForm(_('Homepage is not a valid URL.'));
249 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
250 // TRANS: Validation error in form for profile settings.
251 $this->showForm(_('Full name is too long (maximum 255 characters).'));
253 } else if (Profile::bioTooLong($bio)) {
254 // TRANS: Validation error in form for profile settings.
255 // TRANS: Plural form is used based on the maximum number of allowed
256 // TRANS: characters for the biography (%d).
257 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
258 'Bio is too long (maximum %d characters).',
262 } else if (!is_null($location) && mb_strlen($location) > 255) {
263 // TRANS: Validation error in form for profile settings.
264 $this->showForm(_('Location is too long (maximum 255 characters).'));
266 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
267 // TRANS: Validation error in form for profile settings.
268 $this->showForm(_('Timezone not selected.'));
270 } else if ($this->nicknameExists($nickname)) {
271 // TRANS: Validation error in form for profile settings.
272 $this->showForm(_('Nickname already in use. Try another one.'));
274 } else if (!is_null($language) && strlen($language) > 50) {
275 // TRANS: Validation error in form for profile settings.
276 $this->showForm(_('Language is too long (maximum 50 characters).'));
281 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
286 foreach ($tags as $tag) {
287 if (!common_valid_profile_tag($tag)) {
288 // TRANS: Validation error in form for profile settings.
289 // TRANS: %s is an invalid tag.
290 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
295 $user = common_current_user();
297 $user->query('BEGIN');
299 if ($user->nickname != $nickname ||
300 $user->language != $language ||
301 $user->timezone != $timezone) {
303 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
305 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
307 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
310 $original = clone($user);
312 $user->nickname = $nickname;
313 $user->language = $language;
314 $user->timezone = $timezone;
316 $result = $user->updateKeys($original);
318 if ($result === false) {
319 common_log_db_error($user, 'UPDATE', __FILE__);
320 // TRANS: Server error thrown when user profile settings could not be updated.
321 $this->serverError(_('Could not update user.'));
324 // Re-initialize language environment if it changed
325 common_init_language();
326 // Clear the site owner, in case nickname changed
327 if ($user->hasRole(Profile_role::OWNER)) {
328 User::blow('user:site_owner');
334 if ($user->autosubscribe ^ $autosubscribe) {
336 $original = clone($user);
338 $user->autosubscribe = $autosubscribe;
340 $result = $user->update($original);
342 if ($result === false) {
343 common_log_db_error($user, 'UPDATE', __FILE__);
344 // TRANS: Server error thrown when user profile settings could not be updated to
345 // TRANS: automatically subscribe to any subscriber.
346 $this->serverError(_('Could not update user for autosubscribe.'));
351 $profile = $user->getProfile();
353 $orig_profile = clone($profile);
355 $profile->nickname = $user->nickname;
356 $profile->fullname = $fullname;
357 $profile->homepage = $homepage;
358 $profile->bio = $bio;
359 $profile->location = $location;
361 $loc = Location::fromName($location);
364 $profile->lat = null;
365 $profile->lon = null;
366 $profile->location_id = null;
367 $profile->location_ns = null;
369 $profile->lat = $loc->lat;
370 $profile->lon = $loc->lon;
371 $profile->location_id = $loc->location_id;
372 $profile->location_ns = $loc->location_ns;
375 $profile->profileurl = common_profile_url($nickname);
377 if (common_config('location', 'share') == 'user') {
381 $prefs = User_location_prefs::staticGet('user_id', $user->id);
384 $prefs = new User_location_prefs();
386 $prefs->user_id = $user->id;
387 $prefs->created = common_sql_now();
390 $orig = clone($prefs);
393 $prefs->share_location = $this->boolean('sharelocation');
396 $result = $prefs->update($orig);
398 $result = $prefs->insert();
401 if ($result === false) {
402 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
403 // TRANS: Server error thrown when user profile location preference settings could not be updated.
404 $this->serverError(_('Could not save location prefs.'));
409 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
410 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
412 $result = $profile->update($orig_profile);
414 if ($result === false) {
415 common_log_db_error($profile, 'UPDATE', __FILE__);
416 // TRANS: Server error thrown when user profile settings could not be saved.
417 $this->serverError(_('Could not save profile.'));
422 $result = $user->setSelfTags($tags);
425 // TRANS: Server error thrown when user profile settings tags could not be saved.
426 $this->serverError(_('Could not save tags.'));
430 $user->query('COMMIT');
431 Event::handle('EndProfileSaveForm', array($this));
432 common_broadcast_profile($profile);
434 // TRANS: Confirmation shown when user profile settings are saved.
435 $this->showForm(_('Settings saved.'), true);
440 function nicknameExists($nickname)
442 $user = common_current_user();
443 $other = User::staticGet('nickname', $nickname);
447 return $other->id != $user->id;
451 function showAside() {
452 $user = common_current_user();
454 $this->elementStart('div', array('id' => 'aside_primary',
455 'class' => 'aside'));
457 $this->elementStart('div', array('id' => 'account_actions',
458 'class' => 'section'));
459 $this->elementStart('ul');
460 if (Event::handle('StartProfileSettingsActions', array($this))) {
461 if ($user->hasRight(Right::BACKUPACCOUNT)) {
462 $this->elementStart('li');
464 array('href' => common_local_url('backupaccount')),
465 _('Backup account'));
466 $this->elementEnd('li');
468 if ($user->hasRight(Right::DELETEACCOUNT)) {
469 $this->elementStart('li');
471 array('href' => common_local_url('deleteaccount')),
472 _('Delete account'));
473 $this->elementEnd('li');
475 if ($user->hasRight(Right::RESTOREACCOUNT)) {
476 $this->elementStart('li');
478 array('href' => common_local_url('restoreaccount')),
479 _('Restore account'));
480 $this->elementEnd('li');
482 Event::handle('EndProfileSettingsActions', array($this));
484 $this->elementEnd('ul');
485 $this->elementEnd('div');
486 $this->elementEnd('div');