]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
more debugging on profile update
[quix0rs-gnu-social.git] / actions / profilesettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 class ProfilesettingsAction extends SettingsAction {
25
26         function show_form($msg=NULL, $success=false) {
27                 $user = common_current_user();
28                 $profile = $user->getProfile();
29                 common_show_header(_t('Profile settings'), NULL, NULL, array($this, 'settings_menu'));
30
31                 if ($msg) {
32                         $this->message($msg, $success);
33                 } else {
34                         common_element('div', 'instructions',
35                                                    _t('You can update your personal profile info here '.
36                                                           'so people know more about you. '));
37                 }
38                 common_element_start('form', array('method' => 'POST',
39                                                                                    'id' => 'profilesettings',
40                                                                                    'action' =>
41                                                                                    common_local_url('profilesettings')));
42                 # too much common patterns here... abstractable?
43                 common_input('nickname', _t('Nickname'),
44                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
45                                          _t('1-64 lowercase letters or numbers, no punctuation or spaces'));
46                 common_input('fullname', _t('Full name'),
47                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
48                 common_input('email', _t('Email address'),
49                                          ($this->arg('email')) ? $this->arg('email') : $user->email,
50                                          _t('Used only for updates, announcements, and password recovery'));
51                 common_input('homepage', _t('Homepage'),
52                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
53                                          _t('URL of your homepage, blog, or profile on another site'));
54                 common_textarea('bio', _t('Bio'),
55                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
56                                                 _t('Describe yourself and your interests in 140 chars'));
57                 common_input('location', _t('Location'),
58                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
59                                          _t('Where you are, like "City, State (or Region), Country"'));
60                 common_submit('submit', _t('Save'));
61                 common_element_end('form');
62                 common_show_footer();
63         }
64
65         function handle_post() {
66                 
67                 $nickname = $this->trimmed('nickname');
68                 $fullname = $this->trimmed('fullname');
69                 $email = $this->trimmed('email');
70                 $homepage = $this->trimmed('homepage');
71                 $bio = $this->trimmed('bio');
72                 $location = $this->trimmed('location');
73
74                 # Some validation
75                 
76                 if (!Validate::email($email, true)) {
77                         $this->show_form(_t('Not a valid email address.'));
78                         return;
79                 } else if (!Validate::string($nickname, array('min_length' => 1,
80                                                                                                           'max_length' => 64,
81                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
82                         $this->show_form(_t('Nickname must have only letters and numbers and no spaces.'));
83                         return;
84                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
85                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
86                         $this->show_form(_t('Homepage is not a valid URL.'));
87                         return;
88                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
89                         $this->show_form(_t('Fullname is too long (max 255 chars).'));
90                         return;
91                 } else if (!is_null($bio) && strlen($bio) > 140) {
92                         $this->show_form(_t('Bio is too long (max 140 chars).'));
93                         return;
94                 } else if (!is_null($location) && strlen($location) > 255) {
95                         $this->show_form(_t('Location is too long (max 255 chars).'));
96                         return;
97                 } else if ($this->nickname_exists($nickname)) {
98                         $this->show_form(_t('Nickname already exists.'));
99                         return;
100                 } else if ($this->email_exists($email)) {
101                         $this->show_form(_t('Email address already exists.'));
102                         return;
103                 }
104                 
105                 $user = common_current_user();
106                 assert(!is_null($user)); # should already be checked
107
108                 # FIXME: transaction!
109
110                 $original = clone($user);
111
112                 $user->nickname = $nickname;
113                 $user->email = $email;
114
115                 common_debug('Updating, nickname ="'.$user->nickname.'" and email ="'.$user->email.'"');
116                 common_debug('Original, nickname ="'.$original->nickname.'" and email ="'.$original->email.'"');
117                 
118                 if (FALSE === $user->update($original)) {
119                         common_server_error(_t('Couldnt update user.'));
120                         return;
121                 }
122
123                 $profile = $user->getProfile();
124
125                 $orig_profile = clone($profile);
126
127                 $profile->nickname = $user->nickname;
128                 $profile->fullname = $fullname;
129                 $profile->homepage = $homepage;
130                 $profile->bio = $bio;
131                 $profile->location = $location;
132                 $profile->profileurl = common_profile_url($nickname);
133
134                 if (FALSE === $profile->update($orig_profile)) {
135                         common_server_error(_t('Couldnt save profile.'));
136                         return;
137                 }
138
139                 common_broadcast_profile($profile);
140                 
141                 $this->show_form(_t('Settings saved.'), TRUE);
142         }
143         
144         function nickname_exists($nickname) {
145                 $user = common_current_user();
146                 $other = User::staticGet('nickname', $nickname);
147                 if (!$other) {
148                         return false;
149                 } else {
150                         return $other->id != $user->id;
151                 }
152         }
153         
154         function email_exists($email) {
155                 $user = common_current_user();
156                 $other = User::staticGet('email', $email);
157                 if (!$other) {
158                         return false;
159                 } else {
160                         return $other->id != $user->id;
161                 }
162         }
163 }