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);
99 } catch (Exception $e) {
100 $this->showForm($e->getMessage(), false);
104 $profile = $user->getProfile();
106 $simpleFieldNames = array('title', 'spouse', 'kids');
108 foreach ($simpleFieldNames as $name) {
109 $value = $this->trimmed('extprofile-' . $name);
110 $this->saveField($user, $name, $value);
113 $this->savePhoneNumbers($user);
115 $this->showForm(_('Details saved.'), true);
119 function savePhoneNumbers($user) {
120 $phones = $this->findPhoneNumbers();
122 foreach ($phones as $phone) {
133 function findPhoneNumbers() {
135 $phoneParams = $this->findMultiParams('phone');
136 ksort($phoneParams); // this sorts them into pairs
137 $phones = $this->arraySplit($phoneParams, sizeof($phoneParams) / 2);
139 $phoneTuples = array();
141 foreach($phones as $phone) {
142 $firstkey = current(array_keys($phone));
143 $index = substr($firstkey, strrpos($firstkey, '-') + 1);
144 list($number, $rel) = array_values($phone);
146 $phoneTuples[] = array(
156 function arraySplit($array, $pieces)
159 return array($array);
162 $newCount = ceil(count($array) / $pieces);
163 $a = array_slice($array, 0, $newCount);
164 $b = $this->arraySplit(array_slice($array, $newCount), $pieces - 1);
166 return array_merge(array($a), $b);
169 function findMultiParams($type) {
172 foreach ($_POST as $key => $val) {
173 if (strrpos('extprofile-' . $key, $target) !== false) {
174 $formVals[$key] = $val;
181 * Save an extended profile field as a Profile_detail
183 * @param User $user the current user
184 * @param string $name field name
185 * @param string $value field value
186 * @param string $rel field rel (type)
187 * @param int $index index (fields can have multiple values)
189 function saveField($user, $name, $value, $rel = null, $index = null)
191 $profile = $user->getProfile();
192 $detail = new Profile_detail();
194 $detail->profile_id = $profile->id;
195 $detail->field_name = $name;
196 $detail->value_index = $index;
198 $result = $detail->find(true);
200 if (empty($result)) {
201 $detial->value_index = $index;
203 $detail->field_value = $value;
204 $detail->created = common_sql_now();
205 $result = $detail->insert();
206 if (empty($result)) {
207 common_log_db_error($detail, 'INSERT', __FILE__);
208 $this->serverError(_m('Could not save profile details.'));
211 $orig = clone($detail);
213 $detail->field_value = $value;
216 $result = $detail->update($orig);
217 if (empty($result)) {
218 common_log_db_error($detail, 'UPDATE', __FILE__);
219 $this->serverError(_m('Could not save profile details.'));
227 * Save fields that should be stored in the main profile object
229 * XXX: There's a lot of dupe code here from ProfileSettingsAction.
232 * @param User $user the current user
234 function saveStandardProfileDetails($user)
236 $fullname = $this->trimmed('extprofile-fullname');
237 $location = $this->trimmed('extprofile-location');
238 $tagstring = $this->trimmed('extprofile-tags');
239 $bio = $this->trimmed('extprofile-bio');
243 'common_canonical_tag',
244 preg_split('/[\s,]+/', $tagstring)
250 foreach ($tags as $tag) {
251 if (!common_valid_profile_tag($tag)) {
252 // TRANS: Validation error in form for profile settings.
253 // TRANS: %s is an invalid tag.
254 throw new Exception(sprintf(_m('Invalid tag: "%s".'), $tag));
258 $profile = $user->getProfile();
260 $oldTags = $user->getSelfTags();
261 $newTags = array_diff($tags, $oldTags);
263 if ($fullname != $profile->fullname
264 || $location != $profile->location
266 || $bio != $profile->bio) {
268 $orig = clone($profile);
270 $profile->nickname = $user->nickname;
271 $profile->fullname = $fullname;
272 $profile->bio = $bio;
273 $profile->location = $location;
275 $loc = Location::fromName($location);
278 $profile->lat = null;
279 $profile->lon = null;
280 $profile->location_id = null;
281 $profile->location_ns = null;
283 $profile->lat = $loc->lat;
284 $profile->lon = $loc->lon;
285 $profile->location_id = $loc->location_id;
286 $profile->location_ns = $loc->location_ns;
289 $profile->profileurl = common_profile_url($user->nickname);
291 $result = $profile->update($orig);
293 if ($result === false) {
294 common_log_db_error($profile, 'UPDATE', __FILE__);
295 // TRANS: Server error thrown when user profile settings could not be saved.
296 $this->serverError(_('Could not save profile.'));
301 $result = $user->setSelfTags($tags);
304 // TRANS: Server error thrown when user profile settings tags could not be saved.
305 $this->serverError(_('Could not save tags.'));
309 Event::handle('EndProfileSaveForm', array($this));
310 common_broadcast_profile($profile);