]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
move email settings to its own tab
[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 get_instructions() {
27                 return _('You can update your personal profile info here '.
28                                   'so people know more about you.');
29         }
30
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);
35
36                 common_element_start('form', array('method' => 'post',
37                                                                                    'id' => 'profilesettings',
38                                                                                    'action' =>
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_submit('submit', _('Save'));
56                 common_element_end('form');
57                 common_show_footer();
58         }
59
60         function handle_post() {
61
62                 $nickname = $this->trimmed('nickname');
63                 $fullname = $this->trimmed('fullname');
64                 $homepage = $this->trimmed('homepage');
65                 $bio = $this->trimmed('bio');
66                 $location = $this->trimmed('location');
67
68                 # Some validation
69
70                 if (!Validate::string($nickname, array('min_length' => 1,
71                                                                                            'max_length' => 64,
72                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
73                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
74                         return;
75                 } else if (!User::allowed_nickname($nickname)) {
76                         $this->show_form(_('Not a valid nickname.'));
77                         return;
78                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
79                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
80                         $this->show_form(_('Homepage is not a valid URL.'));
81                         return;
82                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
83                         $this->show_form(_('Full name is too long (max 255 chars).'));
84                         return;
85                 } else if (!is_null($bio) && strlen($bio) > 140) {
86                         $this->show_form(_('Bio is too long (max 140 chars).'));
87                         return;
88                 } else if (!is_null($location) && strlen($location) > 255) {
89                         $this->show_form(_('Location is too long (max 255 chars).'));
90                         return;
91                 } else if ($this->nickname_exists($nickname)) {
92                         $this->show_form(_('Nickname already in use. Try another one.'));
93                         return;
94                 }
95
96                 $user = common_current_user();
97
98                 $user->query('BEGIN');
99
100                 if ($user->nickname != $nickname) {
101
102                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
103                                                  __FILE__);
104
105                         $original = clone($user);
106
107                         $user->nickname = $nickname;
108
109                         $result = $user->updateKeys($original);
110
111                         if ($result === FALSE) {
112                                 common_log_db_error($user, 'UPDATE', __FILE__);
113                                 common_server_error(_('Couldn\'t update user.'));
114                                 return;
115                         }
116                 }
117
118                 $profile = $user->getProfile();
119
120                 $orig_profile = clone($profile);
121
122                 $profile->nickname = $user->nickname;
123                 $profile->fullname = $fullname;
124                 $profile->homepage = $homepage;
125                 $profile->bio = $bio;
126                 $profile->location = $location;
127                 $profile->profileurl = common_profile_url($nickname);
128
129                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
130                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
131
132                 $result = $profile->update($orig_profile);
133
134                 if (!$result) {
135                         common_log_db_error($profile, 'UPDATE', __FILE__);
136                         common_server_error(_('Couldn\'t save profile.'));
137                         return;
138                 }
139
140                 $user->query('COMMIT');
141
142                 common_broadcast_profile($profile);
143
144                 $this->show_form(_('Settings saved.'), TRUE);
145         }
146
147         function nickname_exists($nickname) {
148                 $user = common_current_user();
149                 $other = User::staticGet('nickname', $nickname);
150                 if (!$other) {
151                         return false;
152                 } else {
153                         return $other->id != $user->id;
154                 }
155         }
156 }