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') ? array('disabled'=>'disabled') : array());
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') : $this->scoped->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))) {
242 // $nickname will only be set if this changenick value is true.
243 if (common_config('profile', 'changenick') == true) {
245 $nickname = Nickname::normalize($this->trimmed('nickname'), true);
246 } catch (NicknameTakenException $e) {
247 // Abort only if the nickname is occupied by another local profile
248 if ($e->profile->id != $this->scoped->id) {
249 $this->showForm($e->getMessage());
252 $nickname = Nickname::normalize($this->trimmed('nickname')); // without in-use check this time
253 } catch (NicknameException $e) {
254 $this->showForm($e->getMessage());
259 $fullname = $this->trimmed('fullname');
260 $homepage = $this->trimmed('homepage');
261 $bio = $this->trimmed('bio');
262 $location = $this->trimmed('location');
263 $autosubscribe = $this->booleanintstring('autosubscribe');
264 $subscribe_policy = $this->trimmed('subscribe_policy');
265 $private_stream = $this->booleanintstring('private_stream');
266 $language = $this->trimmed('language');
267 $timezone = $this->trimmed('timezone');
268 $tagstring = $this->trimmed('tags');
271 if (!is_null($homepage) && (strlen($homepage) > 0) &&
272 !common_valid_http_url($homepage)) {
273 // TRANS: Validation error in form for profile settings.
274 $this->showForm(_('Homepage is not a valid URL.'));
276 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
277 // TRANS: Validation error in form for profile settings.
278 $this->showForm(_('Full name is too long (maximum 255 characters).'));
280 } else if (Profile::bioTooLong($bio)) {
281 // TRANS: Validation error in form for profile settings.
282 // TRANS: Plural form is used based on the maximum number of allowed
283 // TRANS: characters for the biography (%d).
284 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
285 'Bio is too long (maximum %d characters).',
289 } else if (!is_null($location) && mb_strlen($location) > 255) {
290 // TRANS: Validation error in form for profile settings.
291 $this->showForm(_('Location is too long (maximum 255 characters).'));
293 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
294 // TRANS: Validation error in form for profile settings.
295 $this->showForm(_('Timezone not selected.'));
297 } else if (!is_null($language) && strlen($language) > 50) {
298 // TRANS: Validation error in form for profile settings.
299 $this->showForm(_('Language is too long (maximum 50 characters).'));
305 if (is_string($tagstring) && strlen($tagstring) > 0) {
307 $tags = preg_split('/[\s,]+/', $tagstring);
309 foreach ($tags as &$tag) {
310 $private = @$tag[0] === '.';
312 $tag = common_canonical_tag($tag);
313 if (!common_valid_profile_tag($tag)) {
314 // TRANS: Validation error in form for profile settings.
315 // TRANS: %s is an invalid tag.
316 $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
320 $tag_priv[$tag] = $private;
324 $user = common_current_user();
325 $user->query('BEGIN');
327 // $user->nickname is updated through Profile->update();
330 if (($user->autosubscribe ^ $autosubscribe)
331 || ($user->private_stream ^ $private_stream)
332 || $user->timezone != $timezone
333 || $user->language != $language
334 || $user->subscribe_policy != $subscribe_policy) {
336 $original = clone($user);
338 $user->autosubscribe = $autosubscribe;
339 $user->language = $language;
340 $user->private_stream = $private_stream;
341 $user->subscribe_policy = $subscribe_policy;
342 $user->timezone = $timezone;
344 $result = $user->update($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 to
348 // TRANS: automatically subscribe to any subscriber.
349 $this->serverError(_('Could not update user for autosubscribe or subscribe_policy.'));
352 // Re-initialize language environment if it changed
353 common_init_language();
356 $profile = $user->getProfile();
358 $orig_profile = clone($profile);
360 if (common_config('profile', 'changenick') == true && $profile->nickname !== $nickname) {
361 assert(Nickname::normalize($nickname)===$nickname);
362 common_debug("Changing user nickname from '{$profile->nickname}' to '{$nickname}'.");
363 $profile->nickname = $nickname;
364 $profile->profileurl = common_profile_url($profile->nickname);
366 $profile->fullname = $fullname;
367 $profile->homepage = $homepage;
368 $profile->bio = $bio;
369 $profile->location = $location;
371 $loc = Location::fromName($location);
374 $profile->lat = null;
375 $profile->lon = null;
376 $profile->location_id = null;
377 $profile->location_ns = null;
379 $profile->lat = $loc->lat;
380 $profile->lon = $loc->lon;
381 $profile->location_id = $loc->location_id;
382 $profile->location_ns = $loc->location_ns;
385 if (common_config('location', 'share') == 'user') {
389 $prefs = User_location_prefs::getKV('user_id', $user->id);
392 $prefs = new User_location_prefs();
394 $prefs->user_id = $user->id;
395 $prefs->created = common_sql_now();
398 $orig = clone($prefs);
401 $prefs->share_location = $this->booleanintstring('sharelocation');
404 $result = $prefs->update($orig);
406 $result = $prefs->insert();
409 if ($result === false) {
410 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
411 // TRANS: Server error thrown when user profile location preference settings could not be updated.
412 $this->serverError(_('Could not save location prefs.'));
416 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
417 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
419 $result = $profile->update($orig_profile);
421 if ($result === false) {
422 common_log_db_error($profile, 'UPDATE', __FILE__);
423 // TRANS: Server error thrown when user profile settings could not be saved.
424 $this->serverError(_('Could not save profile.'));
428 $result = $user->setSelfTags($tags, $tag_priv);
431 // TRANS: Server error thrown when user profile settings tags could not be saved.
432 $this->serverError(_('Could not save tags.'));
435 $user->query('COMMIT');
436 Event::handle('EndProfileSaveForm', array($this));
438 // TRANS: Confirmation shown when user profile settings are saved.
439 $this->showForm(_('Settings saved.'), true);
444 function showAside() {
445 $user = common_current_user();
447 $this->elementStart('div', array('id' => 'aside_primary',
448 'class' => 'aside'));
450 $this->elementStart('div', array('id' => 'account_actions',
451 'class' => 'section'));
452 $this->elementStart('ul');
453 if (Event::handle('StartProfileSettingsActions', array($this))) {
454 if ($user->hasRight(Right::BACKUPACCOUNT)) {
455 $this->elementStart('li');
457 array('href' => common_local_url('backupaccount')),
458 // TRANS: Option in profile settings to create a backup of the account of the currently logged in user.
459 _('Backup account'));
460 $this->elementEnd('li');
462 if ($user->hasRight(Right::DELETEACCOUNT)) {
463 $this->elementStart('li');
465 array('href' => common_local_url('deleteaccount')),
466 // TRANS: Option in profile settings to delete the account of the currently logged in user.
467 _('Delete account'));
468 $this->elementEnd('li');
470 if ($user->hasRight(Right::RESTOREACCOUNT)) {
471 $this->elementStart('li');
473 array('href' => common_local_url('restoreaccount')),
474 // TRANS: Option in profile settings to restore the account of the currently logged in user from a backup.
475 _('Restore account'));
476 $this->elementEnd('li');
478 Event::handle('EndProfileSettingsActions', array($this));
480 $this->elementEnd('ul');
481 $this->elementEnd('div');
482 $this->elementEnd('div');