Extended profile - finished basic pattern for adding/removing/saving multiple complex fields

This commit is contained in:
Zach Copley 2011-03-14 01:49:46 -07:00
parent deb40602d2
commit 04c8bf2743
4 changed files with 107 additions and 74 deletions

View File

@ -100,7 +100,7 @@ class ExtendedProfile
function getPhones()
{
$phones = $this->fields['phone'];
$phones = (isset($this->fields['phone'])) ? $this->fields['phone'] : null;
$pArrays = array();
if (empty($phones)) {
@ -109,22 +109,20 @@ class ExtendedProfile
'index' => 0,
'type' => 'phone',
'vcard' => 'tel',
'multi' => true
'rel' => 'office',
'value' => null
);
} else {
for ($i = 0; $i < sizeof($phones); $i++) {
$pa = array(
'label' => _m('Phone'),
'type' => 'phone',
'index' => intva($phones[$i]->value_index),
'index' => intval($phones[$i]->value_index),
'rel' => $phones[$i]->rel,
'value' => $phones[$i]->field_value,
'vcard' => 'tel'
);
// Last phone record should allow adding more
if ($i == sizeof($phones) - 1) {
$pa['multi'] = true;
}
$pArrays[] = $pa;
}
}

View File

@ -156,7 +156,7 @@ class ExtendedProfileWidget extends Form
$this->out->text($this->ext->getTags());
break;
case 'phone':
$this->showPhone($field);
$this->showPhone($name, $field);
break;
default:
$this->out->text("TYPE: $type");
@ -169,7 +169,7 @@ class ExtendedProfileWidget extends Form
}
}
protected function showPhone($field)
protected function showPhone($name, $field)
{
$this->out->elementStart('div', array('class' => 'phone-display'));
$this->out->text($field['value']);
@ -181,7 +181,7 @@ class ExtendedProfileWidget extends Form
protected function showEditablePhone($name, $field)
{
$index = $field['index'];
$index = isset($field['index']) ? $field['index'] : 0;
$id = "extprofile-$name-$index";
$rel = $id . '-rel';
$this->out->elementStart(
@ -190,7 +190,11 @@ class ExtendedProfileWidget extends Form
'class' => 'phone-edit'
)
);
$this->out->input($id, null, $field['value']);
$this->out->input(
$id,
null,
isset($field['value']) ? $field['value'] : null
);
$this->out->dropdown(
$id . '-rel',
'Type',
@ -203,26 +207,29 @@ class ExtendedProfileWidget extends Form
),
null,
false,
$field['rel']
isset($field['rel']) ? $field['rel'] : null
);
if ($field['multi']) {
$this->out->element(
'a',
array(
'class' => 'add_row',
'href' => 'javascript://'),
'+'
);
$this->out->element(
'a',
array(
'class' => 'remove_row',
'href' => 'javascript://',
'style' => 'display: none; '
),
'-'
);
}
$this->out->element(
'a',
array(
'class' => 'add_row',
'href' => 'javascript://',
'style' => 'display: none; '
),
'+'
);
$this->out->element(
'a',
array(
'class' => 'remove_row',
'href' => 'javascript://',
'style' => 'display: none; '
),
'-'
);
$this->out->elementEnd('div');
}

View File

@ -1,43 +1,35 @@
var removeRow = function() {
var cnt = rowCount(this);
var table = $(this).closest('table');
console.log("row count = " + cnt);
if (cnt > 1) {
var target = $(this).closest('tr');
target.remove();
reorder(table);
}
};
var reorder = function(class) {
console.log("QQQ Enter reorder");
var rowCount = function(row) {
var top = $(row).closest('table');
var trs = $(top).find('tr');
return trs.length - 1; // exclude th section header row
};
var divs = $.find('div[class=' + class + ']');
console.log('divs length = ' + divs.length);
var reorder = function(table) {
var trs = $(table).find('tr').has('td');
$(divs).find('a').hide();
$(trs).find('a').hide();
$(trs).each(function(i, tr) {
$(divs).each(function(i, div) {
console.log("ROW " + i);
$(tr).find('a.remove_row').show();
replaceIndex(rowIndex(tr), i);
$(div).find('a.remove_row').show();
replaceIndex(rowIndex(div), i);
});
$(trs).last().find('a.add_row').show();
$(divs).last().find('a.add_row').show();
if (trs.length == 1) {
$(trs).find('a.remove_row').hide();
if (divs.length == 1) {
$(divs).find('a.remove_row').hide();
}
};
var rowIndex = function(elem) {
var idStr = $(elem).find('div').attr('id');
var id = idStr.match(/\d+/);
var rowIndex = function(div) {
var idstr = $(div).attr('id');
var id = idstr.match(/\d+/);
console.log("id = " + id);
return id;
};
var rowCount = function(class) {
var divs = $.find('div[class=' + class + ']');
return divs.length;
};
var replaceIndex = function(elem, oldIndex, newIndex) {
@ -46,7 +38,7 @@ var replaceIndex = function(elem, oldIndex, newIndex) {
var regexp = /extprofile-.*-\d.*/;
var value = attrib.value;
var match = value.match(regexp);
if (match != null) {
if (match !== null) {
attrib.value = value.replace("-" + oldIndex, "-" + newIndex);
console.log('match: oldIndex = ' + oldIndex + ' newIndex = ' + newIndex + ' name = ' + attrib.name + ' value = ' + attrib.value);
}
@ -60,22 +52,42 @@ var resetRow = function(elem) {
}
var addRow = function() {
var divId = $(this).closest('div').attr('id');
var index = divId.match(/\d+/);
console.log("Current row = " + index);
var div = $(this).closest('div');
var id = $(div).attr('id');
var class = $(div).attr('class');
var index = id.match(/\d+/);
console.log("Current row = " + index + ', class = ' + class);
var tr = $(this).closest('tr');
var newtr = $(tr).clone();
var newIndex = parseInt(index) + 1;
replaceIndex(newtr, index, newIndex);
resetRow(newtr);
$(tr).after(newtr);
console.log("number of rows: " + rowCount(tr));
reorder($(this).closest('table'));
reorder(class);
};
var removeRow = function() {
var div = $(this).closest('div');
var id = $(div).attr('id');
var class = $(div).attr('class');
cnt = rowCount(class);
console.debug("removeRow - cnt = " + cnt);
if (cnt > 1) {
var target = $(this).closest('tr');
target.remove();
reorder(class);
}
};
var init = function() {
reorder('phone-edit');
}
$(document).ready(
function() {
init();
$('.add_row').live('click', addRow);
$('.remove_row').live('click', removeRow);
}

View File

@ -107,7 +107,9 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
foreach ($simpleFieldNames as $name) {
$value = $this->trimmed('extprofile-' . $name);
$this->saveField($user, $name, $value);
if (!empty($value)) {
$this->saveField($user, $name, $value);
}
}
$this->savePhoneNumbers($user);
@ -118,15 +120,19 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
function savePhoneNumbers($user) {
$phones = $this->findPhoneNumbers();
foreach ($phones as $phone) {
$this->saveField(
$user,
'phone',
$phone['value'],
$phone['rel'],
$phone['index']
);
$this->removeAll($user, 'phone');
$i = 0;
foreach($phones as $phone) {
if (!empty($phone['value'])) {
++$i;
$this->saveField(
$user,
'phone',
$phone['value'],
$phone['rel'],
$i
);
}
}
}
@ -223,6 +229,16 @@ class ProfileDetailSettingsAction extends ProfileSettingsAction
$detail->free();
}
function removeAll($user, $name)
{
$profile = $user->getProfile();
$detail = new Profile_detail();
$detail->profile_id = $profile->id;
$detail->field_name = $name;
$detail->delete();
$detail->free();
}
/**
* Save fields that should be stored in the main profile object
*