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

View File

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

View File

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

View File

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