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"'));
56 $language = common_language();
57 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
58 $timezone = common_timezone();
60 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
63 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
65 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
66 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
67 common_submit('submit', _('Save'));
68 common_element_end('form');
72 function handle_post() {
74 $nickname = $this->trimmed('nickname');
75 $fullname = $this->trimmed('fullname');
76 $homepage = $this->trimmed('homepage');
77 $bio = $this->trimmed('bio');
78 $location = $this->trimmed('location');
79 $autosubscribe = $this->boolean('autosubscribe');
80 $language = $this->trimmed('language');
81 $timezone = $this->trimmed('timezone');
85 if (!Validate::string($nickname, array('min_length' => 1,
87 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
88 $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
90 } else if (!User::allowed_nickname($nickname)) {
91 $this->show_form(_('Not a valid nickname.'));
93 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
94 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
95 $this->show_form(_('Homepage is not a valid URL.'));
97 } else if (!is_null($fullname) && strlen($fullname) > 255) {
98 $this->show_form(_('Full name is too long (max 255 chars).'));
100 } else if (!is_null($bio) && strlen($bio) > 140) {
101 $this->show_form(_('Bio is too long (max 140 chars).'));
103 } else if (!is_null($location) && strlen($location) > 255) {
104 $this->show_form(_('Location is too long (max 255 chars).'));
106 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
107 $this->show_form(_('Timezone not selected.'));
109 } else if ($this->nickname_exists($nickname)) {
110 $this->show_form(_('Nickname already in use. Try another one.'));
112 } else if (!is_null($language) && strlen($language) > 50) {
113 $this->show_form(_('Language is too long (max 50 chars).'));
116 $user = common_current_user();
118 $user->query('BEGIN');
120 if ($user->nickname != $nickname ||
121 $user->language != $language ||
122 $user->timezone != $timezone) {
124 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
126 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
128 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
131 $original = clone($user);
133 $user->nickname = $nickname;
134 $user->language = $language;
135 $user->timezone = $timezone;
137 $result = $user->updateKeys($original);
139 if ($result === FALSE) {
140 common_log_db_error($user, 'UPDATE', __FILE__);
141 common_server_error(_('Couldn\'t update user.'));
144 # Re-initialize language environment if it changed
145 common_init_language();
151 if ($user->autosubscribe ^ $autosubscribe) {
153 $original = clone($user);
155 $user->autosubscribe = $autosubscribe;
157 $result = $user->update($original);
159 if ($result === FALSE) {
160 common_log_db_error($user, 'UPDATE', __FILE__);
161 common_server_error(_('Couldn\'t update user for autosubscribe.'));
166 $profile = $user->getProfile();
168 $orig_profile = clone($profile);
170 $profile->nickname = $user->nickname;
171 $profile->fullname = $fullname;
172 $profile->homepage = $homepage;
173 $profile->bio = $bio;
174 $profile->location = $location;
175 $profile->profileurl = common_profile_url($nickname);
177 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
178 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
180 $result = $profile->update($orig_profile);
183 common_log_db_error($profile, 'UPDATE', __FILE__);
184 common_server_error(_('Couldn\'t save profile.'));
188 $user->query('COMMIT');
190 common_broadcast_profile($profile);
192 $this->show_form(_('Settings saved.'), TRUE);
195 function nickname_exists($nickname) {
196 $user = common_current_user();
197 $other = User::staticGet('nickname', $nickname);
201 return $other->id != $user->id;