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');
104 foreach ($simpleFieldNames as $name) {
105 $value = $this->trimmed('extprofile-' . $name);
106 if (!empty($value)) {
107 $this->saveField($user, $name, $value);
111 $this->savePhoneNumbers($user);
112 $this->saveExperiences($user);
114 } catch (Exception $e) {
115 $this->showForm($e->getMessage(), false);
119 $this->showForm(_('Details saved.'), true);
123 function savePhoneNumbers($user) {
124 $phones = $this->findPhoneNumbers();
125 $this->removeAll($user, 'phone');
127 foreach($phones as $phone) {
128 if (!empty($phone['value'])) {
141 function findPhoneNumbers() {
143 $phoneParams = $this->findMultiParams('phone');
144 ksort($phoneParams); // this sorts them into pairs
145 $phones = $this->arraySplit($phoneParams, sizeof($phoneParams) / 2);
146 $phoneTuples = array();
148 foreach ($phones as $phone) {
149 list($number, $rel) = array_values($phone);
150 $phoneTuples[] = array(
159 function findExperiences() {
161 // Form vals look like this:
162 // 'extprofile-experience-0' => 'Bozotronix',
163 // 'extprofile-experience-0-current' => 'true'
164 // 'extprofile-experience-0-start' => '1/5/10',
165 // 'extprofile-experience-0-end' => '2/3/11',
167 $experiences = array();
168 $expParams = $this->findMultiParams('experience');
170 $experiences = $this->arraySplit($expParams, sizeof($expParams) / 4);
173 foreach ($experiences as $exp) {
175 common_debug('Experience: ' . var_export($exp, true));
177 list($company, $current, $end, $start) = array_values($exp);
179 $startTs = strtotime($start);
181 if ($startTs === false) {
183 sprintf(_m("Invalid start date: %s"), $start)
187 $endTs = strtotime($end);
189 if ($current === 'false' && $endTs === false) {
191 sprintf(_m("Invalid end date: %s"), $start)
196 'company' => $company,
197 'start' => common_sql_date($startTs),
198 'end' => common_sql_date($endTs),
199 'current' => $current,
206 function saveExperiences($user) {
207 common_debug('save experiences');
208 $experiences = $this->findExperiences();
210 $this->removeAll($user, 'company');
211 $this->removeAll($user, 'start');
212 $this->removeAll($user, 'end'); // also stores 'current'
215 foreach($experiences as $experience) {
216 if (!empty($experience['company'])) {
221 $experience['company'],
235 // Save "current" employer indicator in rel
236 if ($experience['current']) {
259 function arraySplit($array, $pieces)
262 return array($array);
265 $newCount = ceil(count($array) / $pieces);
266 $a = array_slice($array, 0, $newCount);
267 $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
269 return array_merge(array($a), $b);
272 function findMultiParams($type) {
275 foreach ($_POST as $key => $val) {
276 if (strrpos('extprofile-' . $key, $target) !== false) {
277 $formVals[$key] = $val;
284 * Save an extended profile field as a Profile_detail
286 * @param User $user the current user
287 * @param string $name field name
288 * @param string $value field value
289 * @param string $rel field rel (type)
290 * @param int $index index (fields can have multiple values)
291 * @param date $date related date
293 function saveField($user, $name, $value, $rel = null, $index = null, $date = null)
295 $profile = $user->getProfile();
296 $detail = new Profile_detail();
298 $detail->profile_id = $profile->id;
299 $detail->field_name = $name;
300 $detail->value_index = $index;
302 $result = $detail->find(true);
304 if (empty($result)) {
305 $detial->value_index = $index;
307 $detail->field_value = $value;
308 $detail->date = $date;
309 $detail->created = common_sql_now();
310 $result = $detail->insert();
311 if (empty($result)) {
312 common_log_db_error($detail, 'INSERT', __FILE__);
313 $this->serverError(_m('Could not save profile details.'));
316 $orig = clone($detail);
318 $detail->field_value = $value;
320 $detail->date = $date;
322 $result = $detail->update($orig);
323 if (empty($result)) {
324 common_log_db_error($detail, 'UPDATE', __FILE__);
325 $this->serverError(_m('Could not save profile details.'));
332 function removeAll($user, $name)
334 $profile = $user->getProfile();
335 $detail = new Profile_detail();
336 $detail->profile_id = $profile->id;
337 $detail->field_name = $name;
343 * Save fields that should be stored in the main profile object
345 * XXX: There's a lot of dupe code here from ProfileSettingsAction.
348 * @param User $user the current user
350 function saveStandardProfileDetails($user)
352 $fullname = $this->trimmed('extprofile-fullname');
353 $location = $this->trimmed('extprofile-location');
354 $tagstring = $this->trimmed('extprofile-tags');
355 $bio = $this->trimmed('extprofile-bio');
359 'common_canonical_tag',
360 preg_split('/[\s,]+/', $tagstring)
366 foreach ($tags as $tag) {
367 if (!common_valid_profile_tag($tag)) {
368 // TRANS: Validation error in form for profile settings.
369 // TRANS: %s is an invalid tag.
370 throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
374 $profile = $user->getProfile();
376 $oldTags = $user->getSelfTags();
377 $newTags = array_diff($tags, $oldTags);
379 if ($fullname != $profile->fullname
380 || $location != $profile->location
382 || $bio != $profile->bio) {
384 $orig = clone($profile);
386 $profile->nickname = $user->nickname;
387 $profile->fullname = $fullname;
388 $profile->bio = $bio;
389 $profile->location = $location;
391 $loc = Location::fromName($location);
394 $profile->lat = null;
395 $profile->lon = null;
396 $profile->location_id = null;
397 $profile->location_ns = null;
399 $profile->lat = $loc->lat;
400 $profile->lon = $loc->lon;
401 $profile->location_id = $loc->location_id;
402 $profile->location_ns = $loc->location_ns;
405 $profile->profileurl = common_profile_url($user->nickname);
407 $result = $profile->update($orig);
409 if ($result === false) {
410 common_log_db_error($profile, 'UPDATE', __FILE__);
411 // TRANS: Server error thrown when user profile settings could not be saved.
412 $this->serverError(_('Could not save profile.'));
417 $result = $user->setSelfTags($tags);
420 // TRANS: Server error thrown when user profile settings tags could not be saved.
421 $this->serverError(_('Could not save tags.'));
425 Event::handle('EndProfileSaveForm', array($this));
426 common_broadcast_profile($profile);