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 _m('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(_m('Details saved.'), true);
140 function parseDate($fieldname, $datestr, $required = false)
142 if (empty($datestr)) {
145 // TRANS: Exception thrown when no date was entered in a required date field.
146 // TRANS: %s is the field name.
147 _m('You must supply a date for "%s".'),
150 throw new Exception($msg);
153 $ts = strtotime($datestr);
157 // TRANS: Exception thrown on incorrect data input.
158 // TRANS: %1$s is a field name, %2$s is the incorrect input.
159 _m('Invalid date entered for "%1$s": %2$s.'),
165 return common_sql_date($ts);
170 function savePhoneNumbers($user) {
171 $phones = $this->findPhoneNumbers();
172 $this->removeAll($user, 'phone');
174 foreach($phones as $phone) {
175 if (!empty($phone['value'])) {
188 function findPhoneNumbers() {
190 // Form vals look like this:
191 // 'extprofile-phone-1' => '11332',
192 // 'extprofile-phone-1-rel' => 'mobile',
194 $phones = $this->sliceParams('phone', 2);
195 $phoneArray = array();
197 foreach ($phones as $phone) {
198 list($number, $rel) = array_values($phone);
199 $phoneArray[] = array(
210 // Form vals look like this:
211 // 'extprofile-im-0' => 'jed',
212 // 'extprofile-im-0-rel' => 'yahoo',
214 $ims = $this->sliceParams('im', 2);
217 foreach ($ims as $im) {
218 list($id, $rel) = array_values($im);
228 function saveIms($user) {
229 $ims = $this->findIms();
230 $this->removeAll($user, 'im');
232 foreach($ims as $im) {
233 if (!empty($im['value'])) {
246 function findWebsites() {
248 // Form vals look like this:
250 $sites = $this->sliceParams('website', 2);
253 foreach ($sites as $site) {
254 list($id, $rel) = array_values($site);
264 function saveWebsites($user) {
265 $sites = $this->findWebsites();
266 $this->removeAll($user, 'website');
268 foreach($sites as $site) {
269 if (!empty($site['value']) && !Validate::uri(
271 array('allowed_schemes' => array('http', 'https')))
273 // TRANS: Exception thrown when entering an invalid URL.
274 // TRANS: %s is the invalid URL.
275 throw new Exception(sprintf(_m('Invalid URL: %s.'), $site['value']));
278 if (!empty($site['value'])) {
291 function findExperiences() {
293 // Form vals look like this:
294 // 'extprofile-experience-0' => 'Bozotronix',
295 // 'extprofile-experience-0-current' => 'true'
296 // 'extprofile-experience-0-start' => '1/5/10',
297 // 'extprofile-experience-0-end' => '2/3/11',
299 $experiences = $this->sliceParams('experience', 4);
302 foreach ($experiences as $exp) {
303 if (sizeof($experiences) == 4) {
304 list($company, $current, $end, $start) = array_values($exp);
307 list($company, $current, $start) = array_values($exp);
309 if (!empty($company)) {
311 'company' => $company,
312 'start' => $this->parseDate('Start', $start, true),
313 'end' => ($current == 'false') ? $this->parseDate('End', $end, true) : null,
314 'current' => ($current == 'false') ? false : true
322 function saveExperiences($user) {
323 common_debug('save experiences');
324 $experiences = $this->findExperiences();
326 $this->removeAll($user, 'company');
327 $this->removeAll($user, 'start');
328 $this->removeAll($user, 'end'); // also stores 'current'
331 foreach($experiences as $experience) {
332 if (!empty($experience['company'])) {
337 $experience['company'],
351 // Save "current" employer indicator in rel
352 if ($experience['current']) {
375 function findEducations() {
377 // Form vals look like this:
378 // 'extprofile-education-0-school' => 'Pigdog',
379 // 'extprofile-education-0-degree' => 'BA',
380 // 'extprofile-education-0-description' => 'Blar',
381 // 'extprofile-education-0-start' => '05/22/99',
382 // 'extprofile-education-0-end' => '05/22/05',
384 $edus = $this->sliceParams('education', 5);
387 foreach ($edus as $edu) {
388 list($school, $degree, $description, $end, $start) = array_values($edu);
389 if (!empty($school)) {
393 'description' => $description,
394 'start' => $this->parseDate('Start', $start, true),
395 'end' => $this->parseDate('End', $end, true)
404 function saveEducations($user) {
405 common_debug('save education');
406 $edus = $this->findEducations();
407 common_debug(var_export($edus, true));
409 $this->removeAll($user, 'school');
410 $this->removeAll($user, 'degree');
411 $this->removeAll($user, 'degree_descr');
412 $this->removeAll($user, 'school_start');
413 $this->removeAll($user, 'school_end');
416 foreach($edus as $edu) {
417 if (!empty($edu['school'])) {
461 function arraySplit($array, $pieces)
464 return array($array);
467 $newCount = ceil(count($array) / $pieces);
468 $a = array_slice($array, 0, $newCount);
469 $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
471 return array_merge(array($a), $b);
474 function findMultiParams($type) {
477 foreach ($_POST as $key => $val) {
478 if (strrpos('extprofile-' . $key, $target) !== false) {
479 $formVals[$key] = $val;
485 function sliceParams($key, $size) {
487 $params = $this->findMultiParams($key);
489 $slice = $this->arraySplit($params, sizeof($params) / $size);
494 * Save an extended profile field as a Profile_detail
496 * @param User $user the current user
497 * @param string $name field name
498 * @param string $value field value
499 * @param string $rel field rel (type)
500 * @param int $index index (fields can have multiple values)
501 * @param date $date related date
503 function saveField($user, $name, $value, $rel = null, $index = null, $date = null)
505 $profile = $user->getProfile();
506 $detail = new Profile_detail();
508 $detail->profile_id = $profile->id;
509 $detail->field_name = $name;
510 $detail->value_index = $index;
512 $result = $detail->find(true);
514 if (empty($result)) {
515 $detial->value_index = $index;
517 $detail->field_value = $value;
518 $detail->date = $date;
519 $detail->created = common_sql_now();
520 $result = $detail->insert();
521 if (empty($result)) {
522 common_log_db_error($detail, 'INSERT', __FILE__);
523 $this->serverError(_m('Could not save profile details.'));
526 $orig = clone($detail);
528 $detail->field_value = $value;
530 $detail->date = $date;
532 $result = $detail->update($orig);
533 if (empty($result)) {
534 common_log_db_error($detail, 'UPDATE', __FILE__);
535 $this->serverError(_m('Could not save profile details.'));
542 function removeAll($user, $name)
544 $profile = $user->getProfile();
545 $detail = new Profile_detail();
546 $detail->profile_id = $profile->id;
547 $detail->field_name = $name;
553 * Save fields that should be stored in the main profile object
555 * XXX: There's a lot of dupe code here from ProfileSettingsAction.
558 * @param User $user the current user
560 function saveStandardProfileDetails($user)
562 $fullname = $this->trimmed('extprofile-fullname');
563 $location = $this->trimmed('extprofile-location');
564 $tagstring = $this->trimmed('extprofile-tags');
565 $bio = $this->trimmed('extprofile-bio');
569 'common_canonical_tag',
570 preg_split('/[\s,]+/', $tagstring)
576 foreach ($tags as $tag) {
577 if (!common_valid_profile_tag($tag)) {
578 // TRANS: Validation error in form for profile settings.
579 // TRANS: %s is an invalid tag.
580 throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
584 $profile = $user->getProfile();
586 $oldTags = $user->getSelfTags();
587 $newTags = array_diff($tags, $oldTags);
589 if ($fullname != $profile->fullname
590 || $location != $profile->location
592 || $bio != $profile->bio) {
594 $orig = clone($profile);
596 $profile->nickname = $user->nickname;
597 $profile->fullname = $fullname;
598 $profile->bio = $bio;
599 $profile->location = $location;
601 $loc = Location::fromName($location);
604 $profile->lat = null;
605 $profile->lon = null;
606 $profile->location_id = null;
607 $profile->location_ns = null;
609 $profile->lat = $loc->lat;
610 $profile->lon = $loc->lon;
611 $profile->location_id = $loc->location_id;
612 $profile->location_ns = $loc->location_ns;
615 $profile->profileurl = common_profile_url($user->nickname);
617 $result = $profile->update($orig);
619 if ($result === false) {
620 common_log_db_error($profile, 'UPDATE', __FILE__);
621 // TRANS: Server error thrown when user profile settings could not be saved.
622 $this->serverError(_m('Could not save profile.'));
627 $result = $user->setSelfTags($tags);
630 // TRANS: Server error thrown when user profile settings tags could not be saved.
631 $this->serverError(_m('Could not save tags.'));
635 Event::handle('EndProfileSaveForm', array($this));
636 common_broadcast_profile($profile);