3 * StatusNet, the distributed open-source microblogging tool
5 * Change profile settings
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2008-2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
38 * Change profile settings
42 * @author Evan Prodromou <evan@status.net>
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class ProfilesettingsAction extends AccountSettingsAction
53 * @return string Title of the page
58 return _('Profile settings');
62 * Instructions for use
64 * @return instructions for use
67 function getInstructions()
69 return _('You can update your personal profile info here '.
70 'so people know more about you.');
74 * Content area of the page
76 * Shows a form for uploading an avatar.
81 function showContent()
83 $user = common_current_user();
84 $profile = $user->getProfile();
86 $this->elementStart('form', array('method' => 'post',
87 'id' => 'form_settings_profile',
88 'class' => 'form_settings',
89 'action' => common_local_url('profilesettings')));
90 $this->elementStart('fieldset');
91 $this->element('legend', null, _('Profile information'));
92 $this->hidden('token', common_session_token());
94 // too much common patterns here... abstractable?
95 $this->elementStart('ul', 'form_data');
96 if (Event::handle('StartProfileFormData', array($this))) {
97 $this->elementStart('li');
98 $this->input('nickname', _('Nickname'),
99 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
100 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
101 $this->elementEnd('li');
102 $this->elementStart('li');
103 $this->input('fullname', _('Full name'),
104 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
105 $this->elementEnd('li');
106 $this->elementStart('li');
107 $this->input('homepage', _('Homepage'),
108 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
109 _('URL of your homepage, blog, or profile on another site'));
110 $this->elementEnd('li');
111 $this->elementStart('li');
112 $this->textarea('bio', _('Bio'),
113 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
114 _('Describe yourself and your interests in 140 chars'));
115 $this->elementEnd('li');
116 $this->elementStart('li');
117 $this->input('location', _('Location'),
118 ($this->arg('location')) ? $this->arg('location') : $profile->location,
119 _('Where you are, like "City, State (or Region), Country"'));
120 $this->elementEnd('li');
121 Event::handle('EndProfileFormData', array($this));
122 $this->elementStart('li');
123 $this->input('tags', _('Tags'),
124 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
125 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
126 $this->elementEnd('li');
127 $this->elementStart('li');
128 $language = common_language();
129 $this->dropdown('language', _('Language'),
130 get_nice_language_list(), _('Preferred language'),
132 $this->elementEnd('li');
133 $timezone = common_timezone();
134 $timezones = array();
135 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
138 $this->elementStart('li');
139 $this->dropdown('timezone', _('Timezone'),
140 $timezones, _('What timezone are you normally in?'),
142 $this->elementEnd('li');
143 $this->elementStart('li');
144 $this->checkbox('autosubscribe',
145 _('Automatically subscribe to whoever '.
146 'subscribes to me (best for non-humans)'),
147 ($this->arg('autosubscribe')) ?
148 $this->boolean('autosubscribe') : $user->autosubscribe);
149 $this->elementEnd('li');
151 $this->elementEnd('ul');
152 $this->submit('save', _('Save'));
154 $this->elementEnd('fieldset');
155 $this->elementEnd('form');
161 * Validate input and save changes. Reload the form with a success
167 function handlePost()
170 $token = $this->trimmed('token');
171 if (!$token || $token != common_session_token()) {
172 $this->showForm(_('There was a problem with your session token. '.
173 'Try again, please.'));
177 if (Event::handle('StartProfileSaveForm', array($this))) {
179 $nickname = $this->trimmed('nickname');
180 $fullname = $this->trimmed('fullname');
181 $homepage = $this->trimmed('homepage');
182 $bio = $this->trimmed('bio');
183 $location = $this->trimmed('location');
184 $autosubscribe = $this->boolean('autosubscribe');
185 $language = $this->trimmed('language');
186 $timezone = $this->trimmed('timezone');
187 $tagstring = $this->trimmed('tags');
190 if (!Validate::string($nickname, array('min_length' => 1,
192 'format' => NICKNAME_FMT))) {
193 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
195 } else if (!User::allowed_nickname($nickname)) {
196 $this->showForm(_('Not a valid nickname.'));
198 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
199 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
200 $this->showForm(_('Homepage is not a valid URL.'));
202 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
203 $this->showForm(_('Full name is too long (max 255 chars).'));
205 } else if (!is_null($bio) && mb_strlen($bio) > 140) {
206 $this->showForm(_('Bio is too long (max 140 chars).'));
208 } else if (!is_null($location) && mb_strlen($location) > 255) {
209 $this->showForm(_('Location is too long (max 255 chars).'));
211 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
212 $this->showForm(_('Timezone not selected.'));
214 } else if ($this->nicknameExists($nickname)) {
215 $this->showForm(_('Nickname already in use. Try another one.'));
217 } else if (!is_null($language) && strlen($language) > 50) {
218 $this->showForm(_('Language is too long (max 50 chars).'));
223 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
228 foreach ($tags as $tag) {
229 if (!common_valid_profile_tag($tag)) {
230 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
235 $user = common_current_user();
237 $user->query('BEGIN');
239 if ($user->nickname != $nickname ||
240 $user->language != $language ||
241 $user->timezone != $timezone) {
243 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
245 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
247 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
250 $original = clone($user);
252 $user->nickname = $nickname;
253 $user->language = $language;
254 $user->timezone = $timezone;
256 $result = $user->updateKeys($original);
258 if ($result === false) {
259 common_log_db_error($user, 'UPDATE', __FILE__);
260 $this->serverError(_('Couldn\'t update user.'));
263 // Re-initialize language environment if it changed
264 common_init_language();
269 if ($user->autosubscribe ^ $autosubscribe) {
271 $original = clone($user);
273 $user->autosubscribe = $autosubscribe;
275 $result = $user->update($original);
277 if ($result === false) {
278 common_log_db_error($user, 'UPDATE', __FILE__);
279 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
284 $profile = $user->getProfile();
286 $orig_profile = clone($profile);
288 $profile->nickname = $user->nickname;
289 $profile->fullname = $fullname;
290 $profile->homepage = $homepage;
291 $profile->bio = $bio;
292 $profile->location = $location;
293 $profile->profileurl = common_profile_url($nickname);
295 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
296 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
298 $result = $profile->update($orig_profile);
301 common_log_db_error($profile, 'UPDATE', __FILE__);
302 $this->serverError(_('Couldn\'t save profile.'));
307 $result = $user->setSelfTags($tags);
310 $this->serverError(_('Couldn\'t save tags.'));
314 $user->query('COMMIT');
315 Event::handle('EndProfileSaveForm', array($this));
316 common_broadcast_profile($profile);
318 $this->showForm(_('Settings saved.'), true);
323 function nicknameExists($nickname)
325 $user = common_current_user();
326 $other = User::staticGet('nickname', $nickname);
330 return $other->id != $user->id;