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 show_form($msg=NULL, $success=false) {
27 $user = common_current_user();
28 $profile = $user->getProfile();
29 common_show_header(_t('Profile settings'));
30 $this->settings_menu();
31 $this->message($msg, $success);
32 common_element_start('form', array('method' => 'POST',
33 'id' => 'profilesettings',
35 common_local_url('profilesettings')));
36 # too much common patterns here... abstractable?
37 common_input('nickname', _t('Nickname'),
38 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname);
39 common_input('fullname', _t('Full name'),
40 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
41 common_input('email', _t('Email address'),
42 ($this->arg('email')) ? $this->arg('email') : $user->email);
43 common_input('homepage', _t('Homepage'),
44 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage);
45 common_textarea('bio', _t('Bio'),
46 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio);
47 common_input('location', _t('Location'),
48 ($this->arg('location')) ? $this->arg('location') : $profile->location);
49 common_submit('submit', _t('Save'));
50 common_element_end('form');
54 function handle_post() {
56 $nickname = $this->trimmed('nickname');
57 $fullname = $this->trimmed('fullname');
58 $email = $this->trimmed('email');
59 $homepage = $this->trimmed('homepage');
60 $bio = $this->trimmed('bio');
61 $location = $this->trimmed('location');
65 if (!Validate::email($email, true)) {
66 $this->show_form(_t('Not a valid email address.'));
68 } else if (!Validate::string($nickname, array('min_length' => 1,
70 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
71 $this->show_form(_t('Nickname must have only letters and numbers and no spaces.'));
73 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
74 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
75 $this->show_form(_t('Homepage is not a valid URL.'));
77 } else if (!is_null($fullname) && strlen($fullname) > 255) {
78 $this->show_form(_t('Fullname is too long (max 255 chars).'));
80 } else if (!is_null($bio) && strlen($bio) > 140) {
81 $this->show_form(_t('Bio is too long (max 140 chars).'));
83 } else if (!is_null($location) && strlen($location) > 255) {
84 $this->show_form(_t('Location is too long (max 255 chars).'));
86 } else if ($this->nickname_exists($nickname)) {
87 $this->show_form(_t('Nickname already exists.'));
89 } else if ($this->email_exists($email)) {
90 $this->show_form(_t('Email address already exists.'));
94 $user = common_current_user();
95 assert(!is_null($user)); # should already be checked
99 $original = clone($user);
101 $user->nickname = $nickname;
102 $user->email = $email;
104 if (!$user->update($original)) {
105 common_server_error(_t('Couldnt update user.'));
109 $profile = $user->getProfile();
111 $orig_profile = clone($profile);
113 $profile->nickname = $user->nickname;
114 $profile->fullname = $fullname;
115 $profile->homepage = $homepage;
116 $profile->bio = $bio;
117 $profile->location = $location;
118 $profile->profileurl = common_profile_url($nickname);
120 if (FALSE === $profile->update($orig_profile)) {
121 common_server_error(_t('Couldnt save profile.'));
125 $this->show_form(_t('Settings saved.'), TRUE);
128 function nickname_exists($nickname) {
129 $user = common_current_user();
130 $other = User::staticGet('nickname', $nickname);
134 return $other->id != $user->id;
138 function email_exists($email) {
139 $user = common_current_user();
140 $other = User::staticGet('email', $email);
144 return $other->id != $user->id;