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
28 // TRANS: Title for extended profile settings.
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()) {
62 // TRANS: Client error displayed when the session token does not match or is not given.
63 _m('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 // TRANS: Success message after saving extended profile details.
137 $this->showForm(_m('Details saved.'), true);
141 function parseDate($fieldname, $datestr, $required = false)
143 if (empty($datestr)) {
146 // TRANS: Exception thrown when no date was entered in a required date field.
147 // TRANS: %s is the field name.
148 _m('You must supply a date for "%s".'),
151 throw new Exception($msg);
154 $ts = strtotime($datestr);
158 // TRANS: Exception thrown on incorrect data input.
159 // TRANS: %1$s is a field name, %2$s is the incorrect input.
160 _m('Invalid date entered for "%1$s": %2$s.'),
166 return common_sql_date($ts);
171 function savePhoneNumbers($user) {
172 $phones = $this->findPhoneNumbers();
173 $this->removeAll($user, 'phone');
175 foreach($phones as $phone) {
176 if (!empty($phone['value'])) {
189 function findPhoneNumbers() {
191 // Form vals look like this:
192 // 'extprofile-phone-1' => '11332',
193 // 'extprofile-phone-1-rel' => 'mobile',
195 $phones = $this->sliceParams('phone', 2);
196 $phoneArray = array();
198 foreach ($phones as $phone) {
199 list($number, $rel) = array_values($phone);
200 $phoneArray[] = array(
211 // Form vals look like this:
212 // 'extprofile-im-0' => 'jed',
213 // 'extprofile-im-0-rel' => 'yahoo',
215 $ims = $this->sliceParams('im', 2);
218 foreach ($ims as $im) {
219 list($id, $rel) = array_values($im);
229 function saveIms($user) {
230 $ims = $this->findIms();
231 $this->removeAll($user, 'im');
233 foreach($ims as $im) {
234 if (!empty($im['value'])) {
247 function findWebsites() {
249 // Form vals look like this:
251 $sites = $this->sliceParams('website', 2);
254 foreach ($sites as $site) {
255 list($id, $rel) = array_values($site);
265 function saveWebsites($user) {
266 $sites = $this->findWebsites();
267 $this->removeAll($user, 'website');
269 foreach($sites as $site) {
270 if (!empty($site['value']) && !common_valid_http_url($site['value'])) {
271 // TRANS: Exception thrown when entering an invalid URL.
272 // TRANS: %s is the invalid URL.
273 throw new Exception(sprintf(_m('Invalid URL: %s.'), $site['value']));
276 if (!empty($site['value'])) {
289 function findExperiences() {
291 // Form vals look like this:
292 // 'extprofile-experience-0' => 'Bozotronix',
293 // 'extprofile-experience-0-current' => 'true'
294 // 'extprofile-experience-0-start' => '1/5/10',
295 // 'extprofile-experience-0-end' => '2/3/11',
297 $experiences = $this->sliceParams('experience', 4);
300 foreach ($experiences as $exp) {
301 if (sizeof($experiences) == 4) {
302 list($company, $current, $end, $start) = array_values($exp);
305 list($company, $current, $start) = array_values($exp);
307 if (!empty($company)) {
309 'company' => $company,
310 'start' => $this->parseDate('Start', $start, true),
311 'end' => ($current == 'false') ? $this->parseDate('End', $end, true) : null,
312 'current' => ($current == 'false') ? false : true
320 function saveExperiences($user) {
321 common_debug('save experiences');
322 $experiences = $this->findExperiences();
324 $this->removeAll($user, 'company');
325 $this->removeAll($user, 'start');
326 $this->removeAll($user, 'end'); // also stores 'current'
329 foreach($experiences as $experience) {
330 if (!empty($experience['company'])) {
335 $experience['company'],
349 // Save "current" employer indicator in rel
350 if ($experience['current']) {
373 function findEducations() {
375 // Form vals look like this:
376 // 'extprofile-education-0-school' => 'Pigdog',
377 // 'extprofile-education-0-degree' => 'BA',
378 // 'extprofile-education-0-description' => 'Blar',
379 // 'extprofile-education-0-start' => '05/22/99',
380 // 'extprofile-education-0-end' => '05/22/05',
382 $edus = $this->sliceParams('education', 5);
385 foreach ($edus as $edu) {
386 list($school, $degree, $description, $end, $start) = array_values($edu);
387 if (!empty($school)) {
391 'description' => $description,
392 'start' => $this->parseDate('Start', $start, true),
393 'end' => $this->parseDate('End', $end, true)
402 function saveEducations($user) {
403 common_debug('save education');
404 $edus = $this->findEducations();
405 common_debug(var_export($edus, true));
407 $this->removeAll($user, 'school');
408 $this->removeAll($user, 'degree');
409 $this->removeAll($user, 'degree_descr');
410 $this->removeAll($user, 'school_start');
411 $this->removeAll($user, 'school_end');
414 foreach($edus as $edu) {
415 if (!empty($edu['school'])) {
459 function arraySplit($array, $pieces)
462 return array($array);
465 $newCount = ceil(count($array) / $pieces);
466 $a = array_slice($array, 0, $newCount);
467 $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
469 return array_merge(array($a), $b);
472 function findMultiParams($type) {
475 foreach ($_POST as $key => $val) {
476 if (strrpos('extprofile-' . $key, $target) !== false) {
477 $formVals[$key] = $val;
483 function sliceParams($key, $size) {
485 $params = $this->findMultiParams($key);
487 $slice = $this->arraySplit($params, sizeof($params) / $size);
492 * Save an extended profile field as a Profile_detail
494 * @param User $user the current user
495 * @param string $name field name
496 * @param string $value field value
497 * @param string $rel field rel (type)
498 * @param int $index index (fields can have multiple values)
499 * @param date $date related date
501 function saveField($user, $name, $value, $rel = null, $index = null, $date = null)
503 $profile = $user->getProfile();
504 $detail = new Profile_detail();
506 $detail->profile_id = $profile->id;
507 $detail->field_name = $name;
508 $detail->value_index = $index;
510 $result = $detail->find(true);
512 if (empty($result)) {
513 $detial->value_index = $index;
515 $detail->field_value = $value;
516 $detail->date = $date;
517 $detail->created = common_sql_now();
518 $result = $detail->insert();
519 if (empty($result)) {
520 common_log_db_error($detail, 'INSERT', __FILE__);
521 // TRANS: Server error displayed when a field could not be saved in the database.
522 $this->serverError(_m('Could not save profile details.'));
525 $orig = clone($detail);
527 $detail->field_value = $value;
529 $detail->date = $date;
531 $result = $detail->update($orig);
532 if (empty($result)) {
533 common_log_db_error($detail, 'UPDATE', __FILE__);
534 // TRANS: Server error displayed when a field could not be saved in the database.
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.'));
626 $result = $user->setSelfTags($tags);
629 // TRANS: Server error thrown when user profile settings tags could not be saved.
630 $this->serverError(_m('Could not save tags.'));
633 Event::handle('EndProfileSaveForm', array($this));