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/
50 class ProfilesettingsAction extends AccountSettingsAction
55 * @return string Title of the page
60 // TRANS: Page title for profile settings.
61 return _('Profile settings');
65 * Instructions for use
67 * @return instructions for use
70 function getInstructions()
72 // TRANS: Usage instructions for profile settings.
73 return _('You can update your personal profile info here '.
74 'so people know more about you.');
77 function showScripts()
79 parent::showScripts();
80 $this->autofocus('nickname');
84 * Content area of the page
86 * Shows a form for uploading an avatar.
91 function showContent()
93 $user = common_current_user();
94 $profile = $user->getProfile();
96 $this->elementStart('form', array('method' => 'post',
97 'id' => 'form_settings_profile',
98 'class' => 'form_settings',
99 'action' => common_local_url('profilesettings')));
100 $this->elementStart('fieldset');
101 // TRANS: Profile settings form legend.
102 $this->element('legend', null, _('Profile information'));
103 $this->hidden('token', common_session_token());
105 // too much common patterns here... abstractable?
106 $this->elementStart('ul', 'form_data');
107 if (Event::handle('StartProfileFormData', array($this))) {
108 $this->elementStart('li');
109 // TRANS: Field label in form for profile settings.
110 $this->input('nickname', _('Nickname'),
111 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
112 // TRANS: Tooltip for field label in form for profile settings.
113 _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
114 $this->elementEnd('li');
115 $this->elementStart('li');
116 // TRANS: Field label in form for profile settings.
117 $this->input('fullname', _('Full name'),
118 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
119 $this->elementEnd('li');
120 $this->elementStart('li');
121 // TRANS: Field label in form for profile settings.
122 $this->input('homepage', _('Homepage'),
123 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
124 // TRANS: Tooltip for field label in form for profile settings.
125 _('URL of your homepage, blog, or profile on another site.'));
126 $this->elementEnd('li');
127 $this->elementStart('li');
128 $maxBio = Profile::maxBio();
130 // TRANS: Tooltip for field label in form for profile settings. Plural
131 // TRANS: is decided by the number of characters available for the
132 // TRANS: biography (%d).
133 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
134 'Describe yourself and your interests in %d characters',
138 // TRANS: Tooltip for field label in form for profile settings.
139 $bioInstr = _('Describe yourself and your interests');
141 // TRANS: Text area label in form for profile settings where users can provide.
142 // TRANS: their biography.
143 $this->textarea('bio', _('Bio'),
144 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
146 $this->elementEnd('li');
147 $this->elementStart('li');
148 // TRANS: Field label in form for profile settings.
149 $this->input('location', _('Location'),
150 ($this->arg('location')) ? $this->arg('location') : $profile->location,
151 // TRANS: Tooltip for field label in form for profile settings.
152 _('Where you are, like "City, State (or Region), Country"'));
153 $this->elementEnd('li');
154 if (common_config('location', 'share') == 'user') {
155 $this->elementStart('li');
156 // TRANS: Checkbox label in form for profile settings.
157 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
158 ($this->arg('sharelocation')) ?
159 $this->arg('sharelocation') : $user->shareLocation());
160 $this->elementEnd('li');
162 Event::handle('EndProfileFormData', array($this));
163 $this->elementStart('li');
164 // TRANS: Field label in form for profile settings.
165 $this->input('tags', _('Tags'),
166 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
167 // TRANS: Tooltip for field label in form for profile settings.
168 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
169 $this->elementEnd('li');
170 $this->elementStart('li');
171 $language = common_language();
172 // TRANS: Dropdownlist label in form for profile settings.
173 $this->dropdown('language', _('Language'),
174 // TRANS: Tooltip for dropdown list label in form for profile settings.
175 get_nice_language_list(), _('Preferred language'),
177 $this->elementEnd('li');
178 $timezone = common_timezone();
179 $timezones = array();
180 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
183 $this->elementStart('li');
184 // TRANS: Dropdownlist label in form for profile settings.
185 $this->dropdown('timezone', _('Timezone'),
186 // TRANS: Tooltip for dropdown list label in form for profile settings.
187 $timezones, _('What timezone are you normally in?'),
189 $this->elementEnd('li');
190 $this->elementStart('li');
191 $this->checkbox('autosubscribe',
192 // TRANS: Checkbox label in form for profile settings.
193 _('Automatically subscribe to whoever '.
194 'subscribes to me (best for non-humans)'),
195 ($this->arg('autosubscribe')) ?
196 $this->boolean('autosubscribe') : $user->autosubscribe);
197 $this->elementEnd('li');
199 $this->elementEnd('ul');
200 // TRANS: Button to save input in profile settings.
201 $this->submit('save', _m('BUTTON','Save'));
203 $this->elementEnd('fieldset');
204 $this->elementEnd('form');
210 * Validate input and save changes. Reload the form with a success
216 function handlePost()
219 $token = $this->trimmed('token');
220 if (!$token || $token != common_session_token()) {
221 $this->showForm(_('There was a problem with your session token. '.
222 'Try again, please.'));
226 if (Event::handle('StartProfileSaveForm', array($this))) {
228 $nickname = $this->trimmed('nickname');
229 $fullname = $this->trimmed('fullname');
230 $homepage = $this->trimmed('homepage');
231 $bio = $this->trimmed('bio');
232 $location = $this->trimmed('location');
233 $autosubscribe = $this->boolean('autosubscribe');
234 $language = $this->trimmed('language');
235 $timezone = $this->trimmed('timezone');
236 $tagstring = $this->trimmed('tags');
239 if (!Validate::string($nickname, array('min_length' => 1,
241 'format' => NICKNAME_FMT))) {
242 // TRANS: Validation error in form for profile settings.
243 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
245 } else if (!User::allowed_nickname($nickname)) {
246 // TRANS: Validation error in form for profile settings.
247 $this->showForm(_('Not a valid nickname.'));
249 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
250 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
251 // TRANS: Validation error in form for profile settings.
252 $this->showForm(_('Homepage is not a valid URL.'));
254 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
255 // TRANS: Validation error in form for profile settings.
256 $this->showForm(_('Full name is too long (maximum 255 characters).'));
258 } else if (Profile::bioTooLong($bio)) {
259 // TRANS: Validation error in form for profile settings.
260 // TRANS: Plural form is used based on the maximum number of allowed
261 // TRANS: characters for the biography (%d).
262 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
263 'Bio is too long (maximum %d characters).',
267 } else if (!is_null($location) && mb_strlen($location) > 255) {
268 // TRANS: Validation error in form for profile settings.
269 $this->showForm(_('Location is too long (maximum 255 characters).'));
271 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
272 // TRANS: Validation error in form for profile settings.
273 $this->showForm(_('Timezone not selected.'));
275 } else if ($this->nicknameExists($nickname)) {
276 // TRANS: Validation error in form for profile settings.
277 $this->showForm(_('Nickname already in use. Try another one.'));
279 } else if (!is_null($language) && strlen($language) > 50) {
280 // TRANS: Validation error in form for profile settings.
281 $this->showForm(_('Language is too long (maximum 50 characters).'));
286 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
291 foreach ($tags as $tag) {
292 if (!common_valid_profile_tag($tag)) {
293 // TRANS: Validation error in form for profile settings.
294 // TRANS: %s is an invalid tag.
295 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
300 $user = common_current_user();
302 $user->query('BEGIN');
304 if ($user->nickname != $nickname ||
305 $user->language != $language ||
306 $user->timezone != $timezone) {
308 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
310 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
312 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
315 $original = clone($user);
317 $user->nickname = $nickname;
318 $user->language = $language;
319 $user->timezone = $timezone;
321 $result = $user->updateKeys($original);
323 if ($result === false) {
324 common_log_db_error($user, 'UPDATE', __FILE__);
325 // TRANS: Server error thrown when user profile settings could not be updated.
326 $this->serverError(_('Couldn\'t update user.'));
329 // Re-initialize language environment if it changed
330 common_init_language();
331 // Clear the site owner, in case nickname changed
332 if ($user->hasRole(Profile_role::OWNER)) {
333 User::blow('user:site_owner');
339 if ($user->autosubscribe ^ $autosubscribe) {
341 $original = clone($user);
343 $user->autosubscribe = $autosubscribe;
345 $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(_('Couldn\'t update user for autosubscribe.'));
356 $profile = $user->getProfile();
358 $orig_profile = clone($profile);
360 $profile->nickname = $user->nickname;
361 $profile->fullname = $fullname;
362 $profile->homepage = $homepage;
363 $profile->bio = $bio;
364 $profile->location = $location;
366 $loc = Location::fromName($location);
369 $profile->lat = null;
370 $profile->lon = null;
371 $profile->location_id = null;
372 $profile->location_ns = null;
374 $profile->lat = $loc->lat;
375 $profile->lon = $loc->lon;
376 $profile->location_id = $loc->location_id;
377 $profile->location_ns = $loc->location_ns;
380 $profile->profileurl = common_profile_url($nickname);
382 if (common_config('location', 'share') == 'user') {
386 $prefs = User_location_prefs::staticGet('user_id', $user->id);
389 $prefs = new User_location_prefs();
391 $prefs->user_id = $user->id;
392 $prefs->created = common_sql_now();
395 $orig = clone($prefs);
398 $prefs->share_location = $this->boolean('sharelocation');
401 $result = $prefs->update($orig);
403 $result = $prefs->insert();
406 if ($result === false) {
407 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
408 // TRANS: Server error thrown when user profile location preference settings could not be updated.
409 $this->serverError(_('Couldn\'t save location prefs.'));
414 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
415 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
417 $result = $profile->update($orig_profile);
419 if ($result === false) {
420 common_log_db_error($profile, 'UPDATE', __FILE__);
421 // TRANS: Server error thrown when user profile settings could not be saved.
422 $this->serverError(_('Couldn\'t save profile.'));
427 $result = $user->setSelfTags($tags);
430 // TRANS: Server error thrown when user profile settings tags could not be saved.
431 $this->serverError(_('Couldn\'t save tags.'));
435 $user->query('COMMIT');
436 Event::handle('EndProfileSaveForm', array($this));
437 common_broadcast_profile($profile);
439 // TRANS: Confirmation shown when user profile settings are saved.
440 $this->showForm(_('Settings saved.'), true);
445 function nicknameExists($nickname)
447 $user = common_current_user();
448 $other = User::staticGet('nickname', $nickname);
452 return $other->id != $user->id;