Make phone numbers save

This commit is contained in:
Zach Copley 2011-03-10 14:14:21 -08:00
parent 5203fa7151
commit c456e998c7
3 changed files with 155 additions and 26 deletions

View File

@ -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',

View File

@ -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");
}

View File

@ -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)
{
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);
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");
$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->field_value = $value;
$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();
}
/**