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('nickname');
77 * Content area of the page
79 * Shows a form for uploading an avatar.
83 function showContent()
85 $user = common_current_user();
86 $profile = $user->getProfile();
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')) ? $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 $this->elementEnd('li');
107 $this->elementStart('li');
108 // TRANS: Field label in form for profile settings.
109 $this->input('fullname', _('Full name'),
110 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
111 $this->elementEnd('li');
112 $this->elementStart('li');
113 // TRANS: Field label in form for profile settings.
114 $this->input('homepage', _('Homepage'),
115 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
116 // TRANS: Tooltip for field label in form for profile settings.
117 _('URL of your homepage, blog, or profile on another site.'));
118 $this->elementEnd('li');
119 $this->elementStart('li');
120 $maxBio = Profile::maxBio();
122 // TRANS: Tooltip for field label in form for profile settings. Plural
123 // TRANS: is decided by the number of characters available for the
124 // TRANS: biography (%d).
125 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character.',
126 'Describe yourself and your interests in %d characters.',
130 // TRANS: Tooltip for field label in form for profile settings.
131 $bioInstr = _('Describe yourself and your interests.');
133 // TRANS: Text area label in form for profile settings where users can provide
134 // TRANS: their biography.
135 $this->textarea('bio', _('Bio'),
136 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
138 $this->elementEnd('li');
139 $this->elementStart('li');
140 // TRANS: Field label in form for profile settings.
141 $this->input('location', _('Location'),
142 ($this->arg('location')) ? $this->arg('location') : $profile->location,
143 // TRANS: Tooltip for field label in form for profile settings.
144 _('Where you are, like "City, State (or Region), Country".'));
145 $this->elementEnd('li');
146 if (common_config('location', 'share') == 'user') {
147 $this->elementStart('li');
148 // TRANS: Checkbox label in form for profile settings.
149 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
150 ($this->arg('sharelocation')) ?
151 $this->arg('sharelocation') : $this->scoped->shareLocation());
152 $this->elementEnd('li');
154 Event::handle('EndProfileFormData', array($this));
155 $this->elementStart('li');
156 // TRANS: Field label in form for profile settings.
157 $this->input('tags', _('Tags'),
158 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
159 // TRANS: Tooltip for field label in form for profile settings.
160 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated.'));
161 $this->elementEnd('li');
162 $this->elementStart('li');
163 $language = common_language();
164 // TRANS: Dropdownlist label in form for profile settings.
165 $this->dropdown('language', _('Language'),
166 // TRANS: Tooltip for dropdown list label in form for profile settings.
167 get_nice_language_list(), _('Preferred language.'),
169 $this->elementEnd('li');
170 $timezone = common_timezone();
171 $timezones = array();
172 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
175 $this->elementStart('li');
176 // TRANS: Dropdownlist label in form for profile settings.
177 $this->dropdown('timezone', _('Timezone'),
178 // TRANS: Tooltip for dropdown list label in form for profile settings.
179 $timezones, _('What timezone are you normally in?'),
181 $this->elementEnd('li');
182 $this->elementStart('li');
183 $this->checkbox('autosubscribe',
184 // TRANS: Checkbox label in form for profile settings.
185 _('Automatically subscribe to whoever '.
186 'subscribes to me (best for non-humans)'),
187 ($this->arg('autosubscribe')) ?
188 $this->boolean('autosubscribe') : $user->autosubscribe);
189 $this->elementEnd('li');
190 $this->elementStart('li');
191 $this->dropdown('subscribe_policy',
192 // TRANS: Dropdown field label on profile settings, for what policies to apply when someone else tries to subscribe to your updates.
193 _('Subscription policy'),
194 // TRANS: Dropdown field option for following policy.
195 array(User::SUBSCRIBE_POLICY_OPEN => _('Let anyone follow me'),
196 // TRANS: Dropdown field option for following policy.
197 User::SUBSCRIBE_POLICY_MODERATE => _('Ask me first')),
198 // TRANS: Dropdown field title on group edit form.
199 _('Whether other users need your permission to follow your updates.'),
201 (empty($user->subscribe_policy)) ? User::SUBSCRIBE_POLICY_OPEN : $user->subscribe_policy);
202 $this->elementEnd('li');
204 $this->elementStart('li');
205 $this->checkbox('private_stream',
206 // TRANS: Checkbox label in profile settings.
207 _('Make updates visible only to my followers'),
208 ($this->arg('private_stream')) ?
209 $this->boolean('private_stream') : $user->private_stream);
210 $this->elementEnd('li');
211 $this->elementEnd('ul');
212 // TRANS: Button to save input in profile settings.
213 $this->submit('save', _m('BUTTON','Save'));
215 $this->elementEnd('fieldset');
216 $this->elementEnd('form');
222 * Validate input and save changes. Reload the form with a success
227 function handlePost()
230 $token = $this->trimmed('token');
231 if (!$token || $token != common_session_token()) {
232 // TRANS: Form validation error.
233 $this->showForm(_('There was a problem with your session token. '.
234 'Try again, please.'));
238 if (Event::handle('StartProfileSaveForm', array($this))) {
240 $nickname = $this->trimmed('nickname');
242 $nickname = Nickname::normalize($nickname, true);
243 } catch (NicknameTakenException $e) {
244 // Abort only if the nickname is occupied by another local profile
245 if ($e->profile->id != $this->scoped->id) {
246 $this->showForm($e->getMessage());
249 $nickname = Nickname::normalize($nickname); // without in-use check this time
250 } catch (NicknameException $e) {
251 $this->showForm($e->getMessage());
255 $fullname = $this->trimmed('fullname');
256 $homepage = $this->trimmed('homepage');
257 $bio = $this->trimmed('bio');
258 $location = $this->trimmed('location');
259 $autosubscribe = $this->boolean('autosubscribe');
260 $subscribe_policy = $this->trimmed('subscribe_policy');
261 $private_stream = $this->boolean('private_stream');
262 $language = $this->trimmed('language');
263 $timezone = $this->trimmed('timezone');
264 $tagstring = $this->trimmed('tags');
267 if (!is_null($homepage) && (strlen($homepage) > 0) &&
268 !common_valid_http_url($homepage)) {
269 // TRANS: Validation error in form for profile settings.
270 $this->showForm(_('Homepage is not a valid URL.'));
272 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
273 // TRANS: Validation error in form for profile settings.
274 $this->showForm(_('Full name is too long (maximum 255 characters).'));
276 } else if (Profile::bioTooLong($bio)) {
277 // TRANS: Validation error in form for profile settings.
278 // TRANS: Plural form is used based on the maximum number of allowed
279 // TRANS: characters for the biography (%d).
280 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
281 'Bio is too long (maximum %d characters).',
285 } else if (!is_null($location) && mb_strlen($location) > 255) {
286 // TRANS: Validation error in form for profile settings.
287 $this->showForm(_('Location is too long (maximum 255 characters).'));
289 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
290 // TRANS: Validation error in form for profile settings.
291 $this->showForm(_('Timezone not selected.'));
293 } else if (!is_null($language) && strlen($language) > 50) {
294 // TRANS: Validation error in form for profile settings.
295 $this->showForm(_('Language is too long (maximum 50 characters).'));
301 if (is_string($tagstring) && strlen($tagstring) > 0) {
303 $tags = preg_split('/[\s,]+/', $tagstring);
305 foreach ($tags as &$tag) {
306 $private = @$tag[0] === '.';
308 $tag = common_canonical_tag($tag);
309 if (!common_valid_profile_tag($tag)) {
310 // TRANS: Validation error in form for profile settings.
311 // TRANS: %s is an invalid tag.
312 $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
316 $tag_priv[$tag] = $private;
320 $user = common_current_user();
321 $user->query('BEGIN');
323 // $user->nickname is updated through Profile->update();
326 if (($user->autosubscribe ^ $autosubscribe)
327 || ($user->private_stream ^ $private_stream)
328 || $user->timezone != $timezone
329 || $user->language != $language
330 || $user->subscribe_policy != $subscribe_policy) {
332 $original = clone($user);
334 $user->autosubscribe = $autosubscribe;
335 $user->language = $language;
336 $user->private_stream = $private_stream;
337 $user->subscribe_policy = $subscribe_policy;
338 $user->timezone = $timezone;
340 $result = $user->update($original);
341 if ($result === false) {
342 common_log_db_error($user, 'UPDATE', __FILE__);
343 // TRANS: Server error thrown when user profile settings could not be updated to
344 // TRANS: automatically subscribe to any subscriber.
345 $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
348 // Re-initialize language environment if it changed
349 common_init_language();
352 $profile = $user->getProfile();
354 $orig_profile = clone($profile);
356 $profile->nickname = $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::getKV('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.'));
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.'));
421 $result = $user->setSelfTags($tags, $tag_priv);
424 // TRANS: Server error thrown when user profile settings tags could not be saved.
425 $this->serverError(_('Could not save tags.'));
428 $user->query('COMMIT');
429 Event::handle('EndProfileSaveForm', array($this));
431 // TRANS: Confirmation shown when user profile settings are saved.
432 $this->showForm(_('Settings saved.'), true);
437 function showAside() {
438 $user = common_current_user();
440 $this->elementStart('div', array('id' => 'aside_primary',
441 'class' => 'aside'));
443 $this->elementStart('div', array('id' => 'account_actions',
444 'class' => 'section'));
445 $this->elementStart('ul');
446 if (Event::handle('StartProfileSettingsActions', array($this))) {
447 if ($user->hasRight(Right::BACKUPACCOUNT)) {
448 $this->elementStart('li');
450 array('href' => common_local_url('backupaccount')),
451 // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
452 _('Backup account'));
453 $this->elementEnd('li');
455 if ($user->hasRight(Right::DELETEACCOUNT)) {
456 $this->elementStart('li');
458 array('href' => common_local_url('deleteaccount')),
459 // TRANS: Option in profile settings to delete the account of the currently logged in user.
460 _('Delete account'));
461 $this->elementEnd('li');
463 if ($user->hasRight(Right::RESTOREACCOUNT)) {
464 $this->elementStart('li');
466 array('href' => common_local_url('restoreaccount')),
467 // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
468 _('Restore account'));
469 $this->elementEnd('li');
471 Event::handle('EndProfileSettingsActions', array($this));
473 $this->elementEnd('ul');
474 $this->elementEnd('div');
475 $this->elementEnd('div');