3 * Laconica, 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@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @copyright 2008-2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
38 * Change profile settings
42 * @author Evan Prodromou <evan@controlyourself.ca>
43 * @author Zach Copley <zach@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://laconi.ca/
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' => 'profilesettings',
88 'action' => common_local_url('profilesettings')));
89 $this->hidden('token', common_session_token());
91 # too much common patterns here... abstractable?
93 $this->input('nickname', _('Nickname'),
94 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
95 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
96 $this->input('fullname', _('Full name'),
97 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
98 $this->input('homepage', _('Homepage'),
99 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
100 _('URL of your homepage, blog, or profile on another site'));
101 $this->textarea('bio', _('Bio'),
102 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
103 _('Describe yourself and your interests in 140 chars'));
104 $this->input('location', _('Location'),
105 ($this->arg('location')) ? $this->arg('location') : $profile->location,
106 _('Where you are, like "City, State (or Region), Country"'));
107 $this->input('tags', _('Tags'),
108 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
109 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
111 $language = common_language();
112 $this->dropdown('language', _('Language'),
113 get_nice_language_list(), _('Preferred language'),
116 $timezone = common_timezone();
117 $timezones = array();
118 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
121 $this->dropdown('timezone', _('Timezone'),
122 $timezones, _('What timezone are you normally in?'),
125 $this->checkbox('autosubscribe',
126 _('Automatically subscribe to whoever '.
127 'subscribes to me (best for non-humans)'),
128 ($this->arg('autosubscribe')) ?
129 $this->boolean('autosubscribe') : $user->autosubscribe);
131 $this->submit('save', _('Save'));
133 $this->elementEnd('form');
140 * Validate input and save changes. Reload the form with a success
146 function handlePost()
150 $token = $this->trimmed('token');
151 if (!$token || $token != common_session_token()) {
152 $this->showForm(_('There was a problem with your session token. '.
153 'Try again, please.'));
157 $nickname = $this->trimmed('nickname');
158 $fullname = $this->trimmed('fullname');
159 $homepage = $this->trimmed('homepage');
160 $bio = $this->trimmed('bio');
161 $location = $this->trimmed('location');
162 $autosubscribe = $this->boolean('autosubscribe');
163 $language = $this->trimmed('language');
164 $timezone = $this->trimmed('timezone');
165 $tagstring = $this->trimmed('tags');
169 if (!Validate::string($nickname, array('min_length' => 1,
171 'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
172 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
174 } else if (!User::allowed_nickname($nickname)) {
175 $this->showForm(_('Not a valid nickname.'));
177 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
178 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
179 $this->showForm(_('Homepage is not a valid URL.'));
181 } else if (!is_null($fullname) && strlen($fullname) > 255) {
182 $this->showForm(_('Full name is too long (max 255 chars).'));
184 } else if (!is_null($bio) && strlen($bio) > 140) {
185 $this->showForm(_('Bio is too long (max 140 chars).'));
187 } else if (!is_null($location) && strlen($location) > 255) {
188 $this->showForm(_('Location is too long (max 255 chars).'));
190 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
191 $this->showForm(_('Timezone not selected.'));
193 } else if ($this->nicknameExists($nickname)) {
194 $this->showForm(_('Nickname already in use. Try another one.'));
196 } else if (!is_null($language) && strlen($language) > 50) {
197 $this->showForm(_('Language is too long (max 50 chars).'));
202 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
207 foreach ($tags as $tag) {
208 if (!common_valid_profile_tag($tag)) {
209 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
214 $user = common_current_user();
216 $user->query('BEGIN');
218 if ($user->nickname != $nickname ||
219 $user->language != $language ||
220 $user->timezone != $timezone) {
222 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
224 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
226 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
229 $original = clone($user);
231 $user->nickname = $nickname;
232 $user->language = $language;
233 $user->timezone = $timezone;
235 $result = $user->updateKeys($original);
237 if ($result === false) {
238 common_log_db_error($user, 'UPDATE', __FILE__);
239 $this->serverError(_('Couldn\'t update user.'));
242 # Re-initialize language environment if it changed
243 common_init_language();
249 if ($user->autosubscribe ^ $autosubscribe) {
251 $original = clone($user);
253 $user->autosubscribe = $autosubscribe;
255 $result = $user->update($original);
257 if ($result === false) {
258 common_log_db_error($user, 'UPDATE', __FILE__);
259 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
264 $profile = $user->getProfile();
266 $orig_profile = clone($profile);
268 $profile->nickname = $user->nickname;
269 $profile->fullname = $fullname;
270 $profile->homepage = $homepage;
271 $profile->bio = $bio;
272 $profile->location = $location;
273 $profile->profileurl = common_profile_url($nickname);
275 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
276 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
278 $result = $profile->update($orig_profile);
281 common_log_db_error($profile, 'UPDATE', __FILE__);
282 $this->serverError(_('Couldn\'t save profile.'));
288 $result = $user->setSelfTags($tags);
291 $this->serverError(_('Couldn\'t save tags.'));
295 $user->query('COMMIT');
297 common_broadcast_profile($profile);
299 $this->showForm(_('Settings saved.'), true);
302 function nicknameExists($nickname)
304 $user = common_current_user();
305 $other = User::staticGet('nickname', $nickname);
309 return $other->id != $user->id;