3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
24 class ProfilesettingsAction extends SettingsAction {
26 function get_instructions() {
27 return _('You can update your personal profile info here '.
28 'so people know more about you.');
31 function show_form($msg=NULL, $success=false) {
32 $user = common_current_user();
33 $profile = $user->getProfile();
34 $this->form_header(_('Profile settings'), $msg, $success);
36 common_element_start('form', array('method' => 'post',
37 'id' => 'profilesettings',
39 common_local_url('profilesettings')));
40 # too much common patterns here... abstractable?
41 common_input('nickname', _('Nickname'),
42 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
43 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
44 common_input('fullname', _('Full name'),
45 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
46 common_input('homepage', _('Homepage'),
47 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
48 _('URL of your homepage, blog, or profile on another site'));
49 common_textarea('bio', _('Bio'),
50 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
51 _('Describe yourself and your interests in 140 chars'));
52 common_input('location', _('Location'),
53 ($this->arg('location')) ? $this->arg('location') : $profile->location,
54 _('Where you are, like "City, State (or Region), Country"'));
55 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
56 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
57 common_submit('submit', _('Save'));
58 common_element_end('form');
62 function handle_post() {
64 $nickname = $this->trimmed('nickname');
65 $fullname = $this->trimmed('fullname');
66 $homepage = $this->trimmed('homepage');
67 $bio = $this->trimmed('bio');
68 $location = $this->trimmed('location');
69 $autosubscribe = $this->boolean('autosubscribe');
73 if (!Validate::string($nickname, array('min_length' => 1,
75 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
76 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
78 } else if (!User::allowed_nickname($nickname)) {
79 $this->show_form(_('Not a valid nickname.'));
81 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
82 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
83 $this->show_form(_('Homepage is not a valid URL.'));
85 } else if (!is_null($fullname) && strlen($fullname) > 255) {
86 $this->show_form(_('Full name is too long (max 255 chars).'));
88 } else if (!is_null($bio) && strlen($bio) > 140) {
89 $this->show_form(_('Bio is too long (max 140 chars).'));
91 } else if (!is_null($location) && strlen($location) > 255) {
92 $this->show_form(_('Location is too long (max 255 chars).'));
94 } else if ($this->nickname_exists($nickname)) {
95 $this->show_form(_('Nickname already in use. Try another one.'));
99 $user = common_current_user();
101 $user->query('BEGIN');
103 if ($user->nickname != $nickname) {
105 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
108 $original = clone($user);
110 $user->nickname = $nickname;
112 $result = $user->updateKeys($original);
114 if ($result === FALSE) {
115 common_log_db_error($user, 'UPDATE', __FILE__);
116 common_server_error(_('Couldn\'t update user.'));
123 if ($user->autosubscribe ^ $autosubscribe) {
125 $original = clone($user);
127 $user->autosubscribe = $autosubscribe;
129 $result = $user->update($original);
131 if ($result === FALSE) {
132 common_log_db_error($user, 'UPDATE', __FILE__);
133 common_server_error(_('Couldn\'t update user for autosubscribe.'));
138 $profile = $user->getProfile();
140 $orig_profile = clone($profile);
142 $profile->nickname = $user->nickname;
143 $profile->fullname = $fullname;
144 $profile->homepage = $homepage;
145 $profile->bio = $bio;
146 $profile->location = $location;
147 $profile->profileurl = common_profile_url($nickname);
149 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
150 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
152 $result = $profile->update($orig_profile);
155 common_log_db_error($profile, 'UPDATE', __FILE__);
156 common_server_error(_('Couldn\'t save profile.'));
160 $user->query('COMMIT');
162 common_broadcast_profile($profile);
164 $this->show_form(_('Settings saved.'), TRUE);
167 function nickname_exists($nickname) {
168 $user = common_current_user();
169 $other = User::staticGet('nickname', $nickname);
173 return $other->id != $user->id;