3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET')) {
24 class ProfileDetailSettingsAction extends ProfileSettingsAction
29 return _m('Extended profile settings');
33 * Instructions for use
35 * @return instructions for use
37 function getInstructions()
39 // TRANS: Usage instructions for profile settings.
40 return _('You can update your personal profile info here '.
41 'so people know more about you.');
44 function showStylesheets() {
45 parent::showStylesheets();
46 $this->cssLink('plugins/ExtendedProfile/css/profiledetail.css');
50 function showScripts() {
51 parent::showScripts();
52 $this->script('plugins/ExtendedProfile/js/profiledetail.js');
59 $token = $this->trimmed('token');
60 if (!$token || $token != common_session_token()) {
63 'There was a problem with your session token. '
64 . 'Try again, please.'
70 if ($this->arg('save')) {
73 // TRANS: Message given submitting a form with an unknown action
74 $this->showForm(_m('Unexpected form submission.'));
78 function showContent()
80 $cur = common_current_user();
81 $profile = $cur->getProfile();
83 $widget = new ExtendedProfileWidget(
86 ExtendedProfileWidget::EDITABLE
91 function saveDetails()
93 common_debug(var_export($_POST, true));
95 $user = common_current_user();
98 $this->saveStandardProfileDetails($user);
100 $profile = $user->getProfile();
102 $simpleFieldNames = array('title', 'spouse', 'kids', 'manager');
103 $dateFieldNames = array('birthday');
105 foreach ($simpleFieldNames as $name) {
106 $value = $this->trimmed('extprofile-' . $name);
107 if (!empty($value)) {
108 $this->saveField($user, $name, $value);
112 foreach ($dateFieldNames as $name) {
113 $value = $this->trimmed('extprofile-' . $name);
114 $dateVal = $this->parseDate($name, $value);
125 $this->savePhoneNumbers($user);
126 $this->saveIms($user);
127 $this->saveWebsites($user);
128 $this->saveExperiences($user);
129 $this->saveEducations($user);
131 } catch (Exception $e) {
132 $this->showForm($e->getMessage(), false);
136 $this->showForm(_('Details saved.'), true);
140 function parseDate($fieldname, $datestr, $required = false)
142 if (empty($datestr)) {
145 _m('You must supply a date for "%s".'),
148 throw new Exception($msg);
151 $ts = strtotime($datestr);
155 _m('Invalid date entered for "%s": %s'),
161 return common_sql_date($ts);
166 function savePhoneNumbers($user) {
167 $phones = $this->findPhoneNumbers();
168 $this->removeAll($user, 'phone');
170 foreach($phones as $phone) {
171 if (!empty($phone['value'])) {
184 function findPhoneNumbers() {
186 // Form vals look like this:
187 // 'extprofile-phone-1' => '11332',
188 // 'extprofile-phone-1-rel' => 'mobile',
190 $phones = $this->sliceParams('phone', 2);
191 $phoneArray = array();
193 foreach ($phones as $phone) {
194 list($number, $rel) = array_values($phone);
195 $phoneArray[] = array(
206 // Form vals look like this:
207 // 'extprofile-im-0' => 'jed',
208 // 'extprofile-im-0-rel' => 'yahoo',
210 $ims = $this->sliceParams('im', 2);
213 foreach ($ims as $im) {
214 list($id, $rel) = array_values($im);
224 function saveIms($user) {
225 $ims = $this->findIms();
226 $this->removeAll($user, 'im');
228 foreach($ims as $im) {
229 if (!empty($im['value'])) {
242 function findWebsites() {
244 // Form vals look like this:
246 $sites = $this->sliceParams('website', 2);
249 foreach ($sites as $site) {
250 list($id, $rel) = array_values($site);
260 function saveWebsites($user) {
261 $sites = $this->findWebsites();
262 $this->removeAll($user, 'website');
264 foreach($sites as $site) {
265 if (!empty($site['value']) && !Validate::uri(
267 array('allowed_schemes' => array('http', 'https')))
269 throw new Exception(sprintf(_m('Invalid URL: %s'), $site['value']));
272 if (!empty($site['value'])) {
285 function findExperiences() {
287 // Form vals look like this:
288 // 'extprofile-experience-0' => 'Bozotronix',
289 // 'extprofile-experience-0-current' => 'true'
290 // 'extprofile-experience-0-start' => '1/5/10',
291 // 'extprofile-experience-0-end' => '2/3/11',
293 $experiences = $this->sliceParams('experience', 4);
296 foreach ($experiences as $exp) {
297 if (sizeof($experiences) == 4) {
298 list($company, $current, $end, $start) = array_values($exp);
301 list($company, $current, $start) = array_values($exp);
303 if (!empty($company)) {
305 'company' => $company,
306 'start' => $this->parseDate('Start', $start, true),
307 'end' => ($current == 'false') ? $this->parseDate('End', $end, true) : null,
308 'current' => ($current == 'false') ? false : true
316 function saveExperiences($user) {
317 common_debug('save experiences');
318 $experiences = $this->findExperiences();
320 $this->removeAll($user, 'company');
321 $this->removeAll($user, 'start');
322 $this->removeAll($user, 'end'); // also stores 'current'
325 foreach($experiences as $experience) {
326 if (!empty($experience['company'])) {
331 $experience['company'],
345 // Save "current" employer indicator in rel
346 if ($experience['current']) {
369 function findEducations() {
371 // Form vals look like this:
372 // 'extprofile-education-0-school' => 'Pigdog',
373 // 'extprofile-education-0-degree' => 'BA',
374 // 'extprofile-education-0-description' => 'Blar',
375 // 'extprofile-education-0-start' => '05/22/99',
376 // 'extprofile-education-0-end' => '05/22/05',
378 $edus = $this->sliceParams('education', 5);
381 foreach ($edus as $edu) {
382 list($school, $degree, $description, $end, $start) = array_values($edu);
383 if (!empty($school)) {
387 'description' => $description,
388 'start' => $this->parseDate('Start', $start, true),
389 'end' => $this->parseDate('End', $end, true)
398 function saveEducations($user) {
399 common_debug('save education');
400 $edus = $this->findEducations();
401 common_debug(var_export($edus, true));
403 $this->removeAll($user, 'school');
404 $this->removeAll($user, 'degree');
405 $this->removeAll($user, 'degree_descr');
406 $this->removeAll($user, 'school_start');
407 $this->removeAll($user, 'school_end');
410 foreach($edus as $edu) {
411 if (!empty($edu['school'])) {
455 function arraySplit($array, $pieces)
458 return array($array);
461 $newCount = ceil(count($array) / $pieces);
462 $a = array_slice($array, 0, $newCount);
463 $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
465 return array_merge(array($a), $b);
468 function findMultiParams($type) {
471 foreach ($_POST as $key => $val) {
472 if (strrpos('extprofile-' . $key, $target) !== false) {
473 $formVals[$key] = $val;
479 function sliceParams($key, $size) {
481 $params = $this->findMultiParams($key);
483 $slice = $this->arraySplit($params, sizeof($params) / $size);
488 * Save an extended profile field as a Profile_detail
490 * @param User $user the current user
491 * @param string $name field name
492 * @param string $value field value
493 * @param string $rel field rel (type)
494 * @param int $index index (fields can have multiple values)
495 * @param date $date related date
497 function saveField($user, $name, $value, $rel = null, $index = null, $date = null)
499 $profile = $user->getProfile();
500 $detail = new Profile_detail();
502 $detail->profile_id = $profile->id;
503 $detail->field_name = $name;
504 $detail->value_index = $index;
506 $result = $detail->find(true);
508 if (empty($result)) {
509 $detial->value_index = $index;
511 $detail->field_value = $value;
512 $detail->date = $date;
513 $detail->created = common_sql_now();
514 $result = $detail->insert();
515 if (empty($result)) {
516 common_log_db_error($detail, 'INSERT', __FILE__);
517 $this->serverError(_m('Could not save profile details.'));
520 $orig = clone($detail);
522 $detail->field_value = $value;
524 $detail->date = $date;
526 $result = $detail->update($orig);
527 if (empty($result)) {
528 common_log_db_error($detail, 'UPDATE', __FILE__);
529 $this->serverError(_m('Could not save profile details.'));
536 function removeAll($user, $name)
538 $profile = $user->getProfile();
539 $detail = new Profile_detail();
540 $detail->profile_id = $profile->id;
541 $detail->field_name = $name;
547 * Save fields that should be stored in the main profile object
549 * XXX: There's a lot of dupe code here from ProfileSettingsAction.
552 * @param User $user the current user
554 function saveStandardProfileDetails($user)
556 $fullname = $this->trimmed('extprofile-fullname');
557 $location = $this->trimmed('extprofile-location');
558 $tagstring = $this->trimmed('extprofile-tags');
559 $bio = $this->trimmed('extprofile-bio');
563 'common_canonical_tag',
564 preg_split('/[\s,]+/', $tagstring)
570 foreach ($tags as $tag) {
571 if (!common_valid_profile_tag($tag)) {
572 // TRANS: Validation error in form for profile settings.
573 // TRANS: %s is an invalid tag.
574 throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
578 $profile = $user->getProfile();
580 $oldTags = $user->getSelfTags();
581 $newTags = array_diff($tags, $oldTags);
583 if ($fullname != $profile->fullname
584 || $location != $profile->location
586 || $bio != $profile->bio) {
588 $orig = clone($profile);
590 $profile->nickname = $user->nickname;
591 $profile->fullname = $fullname;
592 $profile->bio = $bio;
593 $profile->location = $location;
595 $loc = Location::fromName($location);
598 $profile->lat = null;
599 $profile->lon = null;
600 $profile->location_id = null;
601 $profile->location_ns = null;
603 $profile->lat = $loc->lat;
604 $profile->lon = $loc->lon;
605 $profile->location_id = $loc->location_id;
606 $profile->location_ns = $loc->location_ns;
609 $profile->profileurl = common_profile_url($user->nickname);
611 $result = $profile->update($orig);
613 if ($result === false) {
614 common_log_db_error($profile, 'UPDATE', __FILE__);
615 // TRANS: Server error thrown when user profile settings could not be saved.
616 $this->serverError(_('Could not save profile.'));
621 $result = $user->setSelfTags($tags);
624 // TRANS: Server error thrown when user profile settings tags could not be saved.
625 $this->serverError(_('Could not save tags.'));
629 Event::handle('EndProfileSaveForm', array($this));
630 common_broadcast_profile($profile);