From c456e998c7de9b70171db4524dc7ac90e908b7ad Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Thu, 10 Mar 2011 14:14:21 -0800 Subject: [PATCH] Make phone numbers save --- plugins/ExtendedProfile/extendedprofile.php | 24 ++-- .../ExtendedProfile/extendedprofilewidget.php | 48 ++++++++ .../profiledetailsettingsaction.php | 107 +++++++++++++++--- 3 files changed, 154 insertions(+), 25 deletions(-) diff --git a/plugins/ExtendedProfile/extendedprofile.php b/plugins/ExtendedProfile/extendedprofile.php index 5ad6db114f..908f0bc721 100644 --- a/plugins/ExtendedProfile/extendedprofile.php +++ b/plugins/ExtendedProfile/extendedprofile.php @@ -39,7 +39,7 @@ class ExtendedProfile $this->user = $profile->getUser(); $this->sections = $this->getSections(); $this->fields = $this->loadFields(); - common_debug(var_export($this->fields, true)); + //common_debug(var_export($this->fields, true)); } /** @@ -96,6 +96,21 @@ class ExtendedProfile } } + + function getPhones() + { + return array( + 'label' => _m('Phone'), + 'type' => 'phone', + 'multi' => true, + 'index' => 8123, + 'rel' => 'home', + 'value' => '510-528-0079', + 'vcard' => 'tel' + + ); + } + /** * Return all the sections of the extended profile * @@ -140,12 +155,7 @@ class ExtendedProfile 'contact' => array( 'label' => _m('Contact'), 'fields' => array( - 'phone' => array( - 'label' => _m('Phone'), - 'type' => 'phone', - 'multi' => true, - 'vcard' => 'tel', - ), + 'phone' => $this->getPhones(), 'im' => array( 'label' => _m('IM'), 'type' => 'im', diff --git a/plugins/ExtendedProfile/extendedprofilewidget.php b/plugins/ExtendedProfile/extendedprofilewidget.php index fbb3ff3c23..f5685e9981 100644 --- a/plugins/ExtendedProfile/extendedprofilewidget.php +++ b/plugins/ExtendedProfile/extendedprofilewidget.php @@ -150,10 +150,55 @@ class ExtendedProfileWidget extends Form case 'tags': $this->out->text($this->ext->getTags()); break; + case 'phone': + common_debug("GOT a PHONE!"); + $this->showPhone($field); + break; default: $this->out->text("TYPE: $type"); } + } + protected function showPhone($field) + { + $this->out->elementStart('div', array('class' => 'phone-display')); + $this->out->text($field['value']); + $this->out->text(' (' . $field['rel'] . ')'); + $this->out->elementEnd('div'); + } + + protected function showEditablePhone($name, $field) + { + $index = $field['index']; + $id = "extprofile-$name-$index"; + $rel = $id . '-rel'; + + $this->out->elementStart('div', array('class' => 'phone-edit')); + $this->out->input($id, null, $field['value']); + $this->out->dropdown( + $id . '-rel', + 'Type', + array( + 'office' => 'Office', + 'mobile' => 'Mobile', + 'home' => 'Home', + 'pager' => 'Pager', + 'other' => 'Other' + ), + null, + false, + $field['rel'] + ); + if ($field['multi']) { + $this->out->element( + 'a', + array( + 'name' => $name, + 'href' => 'javascript://'), + '+' + ); + } + $this->out->elementEnd('div'); } /** @@ -182,6 +227,9 @@ class ExtendedProfileWidget extends Form case 'tags': $out->input($id, null, $this->ext->getTags()); break; + case 'phone': + $this->showEditablePhone($name, $field); + break; default: $out->input($id, null, "TYPE: $type"); } diff --git a/plugins/ExtendedProfile/profiledetailsettingsaction.php b/plugins/ExtendedProfile/profiledetailsettingsaction.php index 0f84dc0582..c18732c058 100644 --- a/plugins/ExtendedProfile/profiledetailsettingsaction.php +++ b/plugins/ExtendedProfile/profiledetailsettingsaction.php @@ -101,51 +101,122 @@ class ProfileDetailSettingsAction extends SettingsAction foreach ($simpleFieldNames as $name) { $value = $this->trimmed('extprofile-' . $name); - $this->saveSimpleField($user, $name, $value); + $this->saveField($user, $name, $value); } + $this->savePhoneNumbers($user); + $this->showForm(_('Details saved.'), true); } - function saveSimpleField($user, $name, $value) + function savePhoneNumbers($user) { + $phones = $this->findPhoneNumbers(); + + foreach ($phones as $phone) { + $this->saveField( + $user, + 'phone', + $phone['value'], + $phone['rel'], + $phone['index'] + ); + } + } + + function findPhoneNumbers() { + $phones = array(); + $phoneParams = $this->findMultiParams('phone'); + ksort($phoneParams); // this sorts them into pairs + $phones = $this->arraySplit($phoneParams, sizeof($phoneParams) / 2); + + $phoneTuples = array(); + + foreach($phones as $phone) { + $firstkey = array_shift(array_keys($phone)); + $index = substr($firstkey, strrpos($firstkey, '-') + 1); + list($number, $rel) = array_values($phone); + + $phoneTuples[] = array( + 'value' => $number, + 'index' => $index, + 'rel' => $rel + ); + + return $phoneTuples; + } + + return $phones; + } + + function arraySplit($array, $pieces) { - $profile = $user->getProfile(); + if ($pieces < 2) { + return array($array); + } + + $newCount = ceil(count($array) / $pieces); + $a = array_slice($array, 0, $newCount); + $b = array_split(array_slice($array, $newCount), $pieces - 1); - $detail = new Profile_detail(); + return array_merge(array($a), $b); + } + + function findMultiParams($type) { + $formVals = array(); + $target = $type; + foreach ($_POST as $key => $val) { + if (strrpos('extprofile-' . $key, $target) !== false) { + $formVals[$key] = $val; + } + } + return $formVals; + } + + /** + * Save an extended profile field as a Profile_detail + * + * @param User $user the current user + * @param string $name field name + * @param string $value field value + * @param string $rel field rel (type) + * @param int $index index (fields can have multiple values) + */ + function saveField($user, $name, $value, $rel = null, $index = null) + { + $profile = $user->getProfile(); + $detail = new Profile_detail(); $detail->profile_id = $profile->id; $detail->field_name = $name; + $detail->value_index = $index; $result = $detail->find(true); - if (empty($result)) { - + $detial->value_index = $index; + $detail->rel = $rel; $detail->field_value = $value; - - $detail->created = common_sql_now(); - - common_debug("XXXXXXXXXXXXXXX not found"); - + $detail->created = common_sql_now(); $result = $detail->insert(); - if (empty($result)) { common_log_db_error($detail, 'INSERT', __FILE__); $this->serverError(_m('Could not save profile details.')); } - } else { - - common_debug('zzzzz FOUND'); $orig = clone($detail); + $detail->field_value = $value; - $result = $detail->update($orig); + $detail->rel = $rel; + $result = $detail->update($orig); + if (empty($result)) { + common_log_db_error($detail, 'UPDATE', __FILE__); + $this->serverError(_m('Could not save profile details.')); + } } $detail->free(); - } /** @@ -189,7 +260,7 @@ class ProfileDetailSettingsAction extends SettingsAction || $location != $profile->location || !empty($newTags) || $bio != $profile->bio) { - + $orig = clone($profile); $profile->nickname = $user->nickname; -- 2.39.5