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 * @author Sarven Capadisli <csarven@status.net>
27 * @copyright 2008-2009 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
36 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
39 * Change profile settings
43 * @author Evan Prodromou <evan@status.net>
44 * @author Zach Copley <zach@status.net>
45 * @author Sarven Capadisli <csarven@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
50 class ProfilesettingsAction extends AccountSettingsAction
55 * @return string Title of the page
60 return _('Profile settings');
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 return _('You can update your personal profile info here '.
72 'so people know more about you.');
75 function showScripts()
77 parent::showScripts();
78 $this->autofocus('nickname');
82 * Content area of the page
84 * Shows a form for uploading an avatar.
89 function showContent()
91 $user = common_current_user();
92 $profile = $user->getProfile();
94 $this->elementStart('form', array('method' => 'post',
95 'id' => 'form_settings_profile',
96 'class' => 'form_settings',
97 'action' => common_local_url('profilesettings')));
98 $this->elementStart('fieldset');
99 $this->element('legend', null, _('Profile information'));
100 $this->hidden('token', common_session_token());
102 // too much common patterns here... abstractable?
103 $this->elementStart('ul', 'form_data');
104 if (Event::handle('StartProfileFormData', array($this))) {
105 $this->elementStart('li');
106 $this->input('nickname', _('Nickname'),
107 ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
108 _('1-64 lowercase letters or numbers, no punctuation or spaces'));
109 $this->elementEnd('li');
110 $this->elementStart('li');
111 $this->input('fullname', _('Full name'),
112 ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
113 $this->elementEnd('li');
114 $this->elementStart('li');
115 $this->input('homepage', _('Homepage'),
116 ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
117 _('URL of your homepage, blog, or profile on another site'));
118 $this->elementEnd('li');
119 $this->elementStart('li');
120 $maxBio = Profile::maxBio();
122 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
125 $bioInstr = _('Describe yourself and your interests');
127 $this->textarea('bio', _('Bio'),
128 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
130 $this->elementEnd('li');
131 $this->elementStart('li');
132 $this->input('location', _('Location'),
133 ($this->arg('location')) ? $this->arg('location') : $profile->location,
134 _('Where you are, like "City, State (or Region), Country"'));
135 $this->elementEnd('li');
136 if (common_config('location', 'share') == 'user') {
137 $this->elementStart('li');
138 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
139 ($this->arg('sharelocation')) ?
140 $this->arg('sharelocation') : $user->shareLocation());
141 $this->elementEnd('li');
143 Event::handle('EndProfileFormData', array($this));
144 $this->elementStart('li');
145 $this->input('tags', _('Tags'),
146 ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
147 _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
148 $this->elementEnd('li');
149 $this->elementStart('li');
150 $language = common_language();
151 $this->dropdown('language', _('Language'),
152 get_nice_language_list(), _('Preferred language'),
154 $this->elementEnd('li');
155 $timezone = common_timezone();
156 $timezones = array();
157 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
160 $this->elementStart('li');
161 $this->dropdown('timezone', _('Timezone'),
162 $timezones, _('What timezone are you normally in?'),
164 $this->elementEnd('li');
165 $this->elementStart('li');
166 $this->checkbox('autosubscribe',
167 _('Automatically subscribe to whoever '.
168 'subscribes to me (best for non-humans)'),
169 ($this->arg('autosubscribe')) ?
170 $this->boolean('autosubscribe') : $user->autosubscribe);
171 $this->elementEnd('li');
173 $this->elementEnd('ul');
174 $this->submit('save', _('Save'));
176 $this->elementEnd('fieldset');
177 $this->elementEnd('form');
183 * Validate input and save changes. Reload the form with a success
189 function handlePost()
192 $token = $this->trimmed('token');
193 if (!$token || $token != common_session_token()) {
194 $this->showForm(_('There was a problem with your session token. '.
195 'Try again, please.'));
199 if (Event::handle('StartProfileSaveForm', array($this))) {
201 $nickname = $this->trimmed('nickname');
202 $fullname = $this->trimmed('fullname');
203 $homepage = $this->trimmed('homepage');
204 $bio = $this->trimmed('bio');
205 $location = $this->trimmed('location');
206 $autosubscribe = $this->boolean('autosubscribe');
207 $language = $this->trimmed('language');
208 $timezone = $this->trimmed('timezone');
209 $tagstring = $this->trimmed('tags');
212 if (!Validate::string($nickname, array('min_length' => 1,
214 'format' => NICKNAME_FMT))) {
215 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
217 } else if (!User::allowed_nickname($nickname)) {
218 $this->showForm(_('Not a valid nickname.'));
220 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
221 !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
222 $this->showForm(_('Homepage is not a valid URL.'));
224 } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
225 $this->showForm(_('Full name is too long (max 255 chars).'));
227 } else if (Profile::bioTooLong($bio)) {
228 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
231 } else if (!is_null($location) && mb_strlen($location) > 255) {
232 $this->showForm(_('Location is too long (max 255 chars).'));
234 } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
235 $this->showForm(_('Timezone not selected.'));
237 } else if ($this->nicknameExists($nickname)) {
238 $this->showForm(_('Nickname already in use. Try another one.'));
240 } else if (!is_null($language) && strlen($language) > 50) {
241 $this->showForm(_('Language is too long (max 50 chars).'));
246 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
251 foreach ($tags as $tag) {
252 if (!common_valid_profile_tag($tag)) {
253 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
258 $user = common_current_user();
260 $user->query('BEGIN');
262 if ($user->nickname != $nickname ||
263 $user->language != $language ||
264 $user->timezone != $timezone) {
266 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
268 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
270 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
273 $original = clone($user);
275 $user->nickname = $nickname;
276 $user->language = $language;
277 $user->timezone = $timezone;
279 $result = $user->updateKeys($original);
281 if ($result === false) {
282 common_log_db_error($user, 'UPDATE', __FILE__);
283 $this->serverError(_('Couldn\'t update user.'));
286 // Re-initialize language environment if it changed
287 common_init_language();
292 if ($user->autosubscribe ^ $autosubscribe) {
294 $original = clone($user);
296 $user->autosubscribe = $autosubscribe;
298 $result = $user->update($original);
300 if ($result === false) {
301 common_log_db_error($user, 'UPDATE', __FILE__);
302 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
307 $profile = $user->getProfile();
309 $orig_profile = clone($profile);
311 $profile->nickname = $user->nickname;
312 $profile->fullname = $fullname;
313 $profile->homepage = $homepage;
314 $profile->bio = $bio;
315 $profile->location = $location;
317 $loc = Location::fromName($location);
320 $profile->lat = null;
321 $profile->lon = null;
322 $profile->location_id = null;
323 $profile->location_ns = null;
325 $profile->lat = $loc->lat;
326 $profile->lon = $loc->lon;
327 $profile->location_id = $loc->location_id;
328 $profile->location_ns = $loc->location_ns;
331 $profile->profileurl = common_profile_url($nickname);
333 if (common_config('location', 'share') == 'user') {
337 $prefs = User_location_prefs::staticGet('user_id', $user->id);
340 $prefs = new User_location_prefs();
342 $prefs->user_id = $user->id;
343 $prefs->created = common_sql_now();
346 $orig = clone($prefs);
349 $prefs->share_location = $this->boolean('sharelocation');
352 $result = $prefs->update($orig);
354 $result = $prefs->insert();
357 if ($result === false) {
358 common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
359 $this->serverError(_('Couldn\'t save location prefs.'));
364 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
365 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
367 $result = $profile->update($orig_profile);
369 if ($result === false) {
370 common_log_db_error($profile, 'UPDATE', __FILE__);
371 $this->serverError(_('Couldn\'t save profile.'));
376 $result = $user->setSelfTags($tags);
379 $this->serverError(_('Couldn\'t save tags.'));
383 $user->query('COMMIT');
384 Event::handle('EndProfileSaveForm', array($this));
385 common_broadcast_profile($profile);
387 $this->showForm(_('Settings saved.'), true);
392 function nicknameExists($nickname)
394 $user = common_current_user();
395 $other = User::staticGet('nickname', $nickname);
399 return $other->id != $user->id;