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' => '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 $maxBio = Profile::maxBio();
114 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
117 $bioInstr = _('Describe yourself and your interests');
119 $this->textarea('bio', _('Bio'),
120 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
122 $this->elementEnd('li');
123 $this->elementStart('li');
124 $this->input('location', _('Location'),
125 ($this->arg('location')) ? $this->arg('location') : $profile->location,
126 _('Where you are, like "City, State (or Region), Country"'));
127 $this->elementEnd('li');
128 Event::handle('EndProfileFormData', array($this));
129 $this->elementStart('li');
130 $this->input('tags', _('Tags'),
131 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
132 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
133 $this->elementEnd('li');
134 $this->elementStart('li');
135 $language = common_language();
136 $this->dropdown('language', _('Language'),
137 get_nice_language_list(), _('Preferred language'),
139 $this->elementEnd('li');
140 $timezone = common_timezone();
141 $timezones = array();
142 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
145 $this->elementStart('li');
146 $this->dropdown('timezone', _('Timezone'),
147 $timezones, _('What timezone are you normally in?'),
149 $this->elementEnd('li');
150 $this->elementStart('li');
151 $this->checkbox('autosubscribe',
152 _('Automatically subscribe to whoever '.
153 'subscribes to me (best for non-humans)'),
154 ($this->arg('autosubscribe')) ?
155 $this->boolean('autosubscribe') : $user->autosubscribe);
156 $this->elementEnd('li');
158 $this->elementEnd('ul');
159 $this->submit('save', _('Save'));
161 $this->elementEnd('fieldset');
162 $this->elementEnd('form');
168 * Validate input and save changes. Reload the form with a success
174 function handlePost()
177 $token = $this->trimmed('token');
178 if (!$token || $token != common_session_token()) {
179 $this->showForm(_('There was a problem with your session token. '.
180 'Try again, please.'));
184 if (Event::handle('StartProfileSaveForm', array($this))) {
186 $nickname = $this->trimmed('nickname');
187 $fullname = $this->trimmed('fullname');
188 $homepage = $this->trimmed('homepage');
189 $bio = $this->trimmed('bio');
190 $location = $this->trimmed('location');
191 $autosubscribe = $this->boolean('autosubscribe');
192 $language = $this->trimmed('language');
193 $timezone = $this->trimmed('timezone');
194 $tagstring = $this->trimmed('tags');
197 if (!Validate::string($nickname, array('min_length' => 1,
199 'format' => NICKNAME_FMT))) {
200 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
202 } else if (!User::allowed_nickname($nickname)) {
203 $this->showForm(_('Not a valid nickname.'));
205 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
206 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
207 $this->showForm(_('Homepage is not a valid URL.'));
209 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
210 $this->showForm(_('Full name is too long (max 255 chars).'));
212 } else if (Profile::bioTooLong($bio)) {
213 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
216 } else if (!is_null($location) && mb_strlen($location) > 255) {
217 $this->showForm(_('Location is too long (max 255 chars).'));
219 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
220 $this->showForm(_('Timezone not selected.'));
222 } else if ($this->nicknameExists($nickname)) {
223 $this->showForm(_('Nickname already in use. Try another one.'));
225 } else if (!is_null($language) && strlen($language) > 50) {
226 $this->showForm(_('Language is too long (max 50 chars).'));
231 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
236 foreach ($tags as $tag) {
237 if (!common_valid_profile_tag($tag)) {
238 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
243 $user = common_current_user();
245 $user->query('BEGIN');
247 if ($user->nickname != $nickname ||
248 $user->language != $language ||
249 $user->timezone != $timezone) {
251 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
253 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
255 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
258 $original = clone($user);
260 $user->nickname = $nickname;
261 $user->language = $language;
262 $user->timezone = $timezone;
264 $result = $user->updateKeys($original);
266 if ($result === false) {
267 common_log_db_error($user, 'UPDATE', __FILE__);
268 $this->serverError(_('Couldn\'t update user.'));
271 // Re-initialize language environment if it changed
272 common_init_language();
277 if ($user->autosubscribe ^ $autosubscribe) {
279 $original = clone($user);
281 $user->autosubscribe = $autosubscribe;
283 $result = $user->update($original);
285 if ($result === false) {
286 common_log_db_error($user, 'UPDATE', __FILE__);
287 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
292 $profile = $user->getProfile();
294 $orig_profile = clone($profile);
296 $profile->nickname = $user->nickname;
297 $profile->fullname = $fullname;
298 $profile->homepage = $homepage;
299 $profile->bio = $bio;
300 $profile->location = $location;
301 $profile->profileurl = common_profile_url($nickname);
303 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
304 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
306 $result = $profile->update($orig_profile);
309 common_log_db_error($profile, 'UPDATE', __FILE__);
310 $this->serverError(_('Couldn\'t save profile.'));
315 $result = $user->setSelfTags($tags);
318 $this->serverError(_('Couldn\'t save tags.'));
322 $user->query('COMMIT');
323 Event::handle('EndProfileSaveForm', array($this));
324 common_broadcast_profile($profile);
326 $this->showForm(_('Settings saved.'), true);
331 function nicknameExists($nickname)
333 $user = common_current_user();
334 $other = User::staticGet('nickname', $nickname);
338 return $other->id != $user->id;