Files
pmgr/views/contacts/edit.ctp

404 lines
11 KiB
PHP

<?php /* -*- mode:PHP -*- */
/**********************************************************************
* Because we make use of functions here,
* we need to put our top level variables somewhere
* we can access them.
*/
$this->varstore = compact('methodTypes', 'methodPreferences',
'contactPhones', 'phoneTypes',
'contactAddresses',
'contactEmails');
//pr($this->data);
/**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
* Javascript
*/
function contactMethodDiv($obj, $type, $legend, $values = null) {
$div =
// BEGIN type-div
'<DIV>' . "\n" .
// BEGIN type-fieldset
'<FIELDSET CLASS="'.$type.' subset">' . "\n" .
'<LEGEND>'.$legend.' #%{id} (%{remove})</LEGEND>' . "\n" .
// BEGIN source-div
'<DIV ID="'.$type.'-%{id}-source-div">' . "\n" .
// BEGIN source-fieldset
'<FIELDSET>' . "\n" .
//'<LEGEND>Source</LEGEND>' . "\n" .
''
;
if (!isset($values)) {
foreach (array('Existing', 'New') AS $sname) {
$stype = strtolower($sname);
$div .=
'<INPUT TYPE="radio" ' . "\n" .
' NAME="data[Contact'.ucfirst($type).'][%{id}][source]"' . "\n" .
' ONCLICK="switchMethodSource(%{id}, '."'{$type}', '{$stype}'".')"' . "\n" .
' CLASS="'.$type.'-method-%{id}-source" ' . "\n" .
' ID="'.$type.'-method-%{id}-source-'.$stype.'"' . "\n" .
' VALUE="'.$stype.'"' . "\n" .
' />' . "\n" .
' <LABEL FOR="'.$type.'-method-%{id}-source-'.$stype.'">'.$sname.'</LABEL>' . "\n" .
' ';
}
}
$div .= "\n";
if (isset($values)) {
$div .= contactMethodTypeDiv($obj, $type, 'show', $values);
}
else {
$div .= contactMethodTypeDiv($obj, $type, 'existing');
$div .= contactMethodTypeDiv($obj, $type, 'new');
}
$div .=
// END source-fieldset
'</FIELDSET>' . "\n" .
// END source-div
'</DIV>' . "\n" .
// BEGIN method-div
'<div id="'.$type.'-%{id}-method-div"' . "\n" .
$obj->element
('form_table',
array('class' => "item contact-{$type} entry",
'field_prefix' => 'Contact'.ucfirst($type).'.%{id}.ContactsMethod',
'fields' => array
(
'preference' => array
('opts' => array
('options' => $obj->varstore['methodPreferences'],
'selected' => (isset($values) ? $values['ContactsMethod']['preference'] : null),
)),
'type' => array
('opts' => array
('options' => $obj->varstore['methodTypes'],
'selected' => (isset($values) ? $values['ContactsMethod']['type'] : null),
)),
'comment' => array
('opts' => array
('value' => (isset($values) ? $values['ContactsMethod']['comment'] : null),
)),
))) . "\n" .
// END method-div
'</div>' . "\n" .
// END type-fieldset
'</FIELDSET>' . "\n" .
// END type-div
'</DIV>'
;
return $div;
}
function contactMethodTypeDiv($obj, $type, $stype, $values = null) {
$element = 'form_table';
if ($type === 'phone') {
if ($stype === 'existing') {
$fields = array
('id' => array('name' => 'Phone/Ext',
'opts' => array('options' => $obj->varstore['contactPhones'])),
);
}
elseif ($stype === 'new') {
$fields = array
('type' => array('opts' => array('options' => $obj->varstore['phoneTypes'])),
'phone' => true,
'ext' => array('name' => "Extension"),
'comment' => true,
);
}
elseif ($stype === 'show') {
$element = 'table';
$column_class = array('field', 'value');
$rows = array
(array('Type', $values['type']),
array('Phone', $values['phone']),
array('Extension', $values['ext']),
array('Comment', $values['comment']),
);
}
else {
die("\n\nInvalid stype ($stype)\n\n");
}
}
elseif ($type === 'address') {
if ($stype === 'existing') {
$fields = array
('id' => array('name' => 'Address',
'opts' => array('options' => $obj->varstore['contactAddresses'])),
);
}
elseif ($stype === 'new') {
$fields = array
('address' => true,
'city' => true,
'state' => true,
'postcode' => array('name' => 'Zip Code'),
'country' => true,
'comment' => true,
);
}
elseif ($stype === 'show') {
$element = 'table';
$column_class = array('field', 'value');
$rows = array
(array('Address', preg_replace("/\n/", "<BR>", $values['address'])),
array('City', $values['city']),
array('State', $values['state']),
array('Zip Code', $values['postcode']),
array('Country', $values['country']),
array('Comment', $values['comment']),
);
}
else {
die("\n\nInvalid stype ($stype)\n\n");
}
}
elseif ($type === 'email') {
if ($stype === 'existing') {
$fields = array
('id' => array('name' => 'Email',
'opts' => array('options' => $obj->varstore['contactEmails'])),
);
}
elseif ($stype === 'new') {
$fields = array
('email' => true,
'comment' => true,
);
}
elseif ($stype === 'show') {
$element = 'table';
$column_class = array('field', 'value');
$rows = array
(array('Email', $values['email']),
array('Comment', $values['comment']),
);
}
}
else {
die("\n\nInvalid type ($type)\n\n");
}
return
// BEGIN sourcetype-div
'<div ' . "\n" .
' class="'.$type.'-%{id}-div"' . "\n" .
' id="'.$type.'-%{id}-'.$stype.'-div"' . "\n" .
(isset($values) ? '' : ' STYLE="display:none;"' . "\n") .
'>' . "\n" .
$obj->element
($element,
array('class' => "item contact-{$type} {$stype}",
'field_prefix' => 'Contact'.ucfirst($type).'.%{id}')
+ compact('rows', 'fields', 'column_class')) .
($stype === 'show'
? '<input type="hidden" name="data[Contact'.ucfirst($type).'][%{id}][id]" value="'.$values['id'].'"/>' . "\n"
: '') .
// END sourcetype-div
'</div>' . "\n" .
'';
}
//pr($this->data);
?>
<script type="text/javascript"><!--
function addPhone(flash) {
addDiv('phone-entry-id', 'phone', 'phones', flash, <?php
echo FormatHelper::phpVarToJavascript(contactMethodDiv($this,
'phone',
'Phone Number'),
null,
' ');
?>
);
}
function addAddress(flash) {
addDiv('address-entry-id', 'address', 'addresses', flash, <?php
echo FormatHelper::phpVarToJavascript(contactMethodDiv($this,
'address',
'Address'),
null,
' ');
?>
);
}
function addEmail(flash) {
addDiv('email-entry-id', 'email', 'emails', flash, <?php
echo FormatHelper::phpVarToJavascript(contactMethodDiv($this,
'email',
'Email Address'),
null,
' ');
?>
);
}
// Reset the form
function resetForm() {
$('#phones').html('');
$('#addresses').html('');
$('#emails').html('');
$('#phone-entry-id').val(1);
$('#address-entry-id').val(1);
$('#email-entry-id').val(1);
<?php foreach ($this->data['ContactPhone'] AS $phone): ?>
addDiv('phone-entry-id', 'phone', 'phones', false, <?php
echo FormatHelper::phpVarToJavascript(contactMethodDiv($this,
'phone',
'Phone Number',
$phone
),
null,
' ');
?>
);
<?php endforeach; ?>
<?php foreach ($this->data['ContactAddress'] AS $address): ?>
addDiv('address-entry-id', 'address', 'addresses', false, <?php
echo FormatHelper::phpVarToJavascript(contactMethodDiv($this,
'address',
'Address',
$address
),
null,
' ');
?>
);
<?php endforeach; ?>
<?php foreach ($this->data['ContactEmail'] AS $email): ?>
addDiv('email-entry-id', 'email', 'emails', false, <?php
echo FormatHelper::phpVarToJavascript(contactMethodDiv($this,
'email',
'Email Address',
$email
),
null,
' ');
?>
);
<?php endforeach; ?>
}
function switchMethodSource(id, type, source) {
$("."+type+"-"+id+"-div")
.slideUp();
$("#"+type+"-"+id+"-"+source+"-div")
.slideDown();
}
$(document).ready(function(){
resetForm();
});
--></script>
<?php
; // alignment
/**********************************************************************
**********************************************************************
**********************************************************************
**********************************************************************
* Contact Edit
*/
echo '<div class="contact edit">' . "\n";
echo $form->create('Contact', array('action' => 'edit')) . "\n";
echo $form->input('id') . "\n";
echo($this->element
('form_table',
array('class' => 'item contact detail',
'caption' => isset($this->data['Contact']) ? 'Edit Contact' : 'New Contact',
'fields' => array
('first_name' => true,
'last_name' => true,
'middle_name' => true,
'display_name' => true,
'company_name' => array('name' => 'Company'),
'id_federal' => array('name' => 'SSN'),
'id_local' => array('name' => 'ID #'),
'id_local_state' => array('name' => 'ID State'),
/* 'id_local_exp' => array('name' => 'ID Expiration', */
/* 'opts' => array('empty' => true)), */
'comment' => true,
))) . "\n");
echo $form->submit('Update') . "\n";
?>
<div CLASS="dynamic-set">
<fieldset CLASS="phone superset">
<legend>Phone Numbers</legend>
<input type="hidden" id="phone-entry-id" value="0">
<div id="phones"></div>
<fieldset> <legend>
<a href="#" onClick="addPhone(true); return false;">Add a Phone Number</a>
</legend> </fieldset>
</fieldset>
</div>
<div CLASS="dynamic-set">
<fieldset CLASS="address superset">
<legend>Mailing Addresses</legend>
<input type="hidden" id="address-entry-id" value="0">
<div id="addresses"></div>
<fieldset> <legend>
<a href="#" onClick="addAddress(true); return false;">Add a Mailing Address</a>
</legend> </fieldset>
</fieldset>
</div>
<div CLASS="dynamic-set">
<fieldset CLASS="email superset">
<legend>Email Addresses</legend>
<input type="hidden" id="email-entry-id" value="0">
<div id="emails"></div>
<fieldset> <legend>
<a href="#" onClick="addEmail(true); return false;">Add an Email Address</a>
</legend> </fieldset>
</fieldset>
</div>
<?php
; // Alignment
echo $form->submit('Update') . "\n";
echo $form->submit('Cancel', array('name' => 'cancel')) . "\n";
echo $form->end() . "\n";
echo '</div>' . "\n";