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');
}
/**
case 'tags':
$out->input($id, null, $this->ext->getTags());
break;
+ case 'phone':
+ $this->showEditablePhone($name, $field);
+ break;
default:
$out->input($id, null, "TYPE: $type");
}
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();
-
}
/**
|| $location != $profile->location
|| !empty($newTags)
|| $bio != $profile->bio) {
-
+
$orig = clone($profile);
$profile->nickname = $user->nickname;