<?php
/* Name: contacts_sample.php
Author: Jerry Mattsson
Created oct-06 Jerry Mattsson, www.Timehole.com
Functions for a simple contact list and edit form using dbff
nov-06, Added javascript field check to edit form.
If you do not want this, remove the require row for "dbff_jsChk.php"
and the "Js check functions" code in the form.
*/
require_once "dbff.php";
require_once "dbff_jsChk.php";
$action = 'list';
if (isset($_POST['cid'])) $cont_id = $_POST['cid'];
if (isset($_POST['update'])) $action = 'update';
if (isset($_POST['edit'])) $action = 'edit';
if (isset($_POST['delete'])) $action = 'delete';
if (isset($_POST['add'])) $action = 'add';
$contacts_def = array ( 'TABLE_NAME'=>'CONTACTS', 'FILE_NAME'=>'contacts',
'cid' =>array('size'=>10, 'type'=>'INT', 'pk'=>'sequence'),
'first_name' =>array('size'=>60, 'chkNN'=>true),
'last_name' =>array('size'=>60, 'chkNN'=>true),
'address' =>array('size'=>100,'chkNN'=>true),
'city' =>array('size'=>40, 'chkNN'=>true),
'country' =>array('size'=>40),
'mail' =>array('size'=>50, 'uk'=>true, 'type'=>'email','chkNN'=>true),
'contact_info' =>array('size'=>60),
'notes' =>array('size'=>200),
'birth_date' =>array('size'=>16, 'type'=>'date'),
'category' =>array('size'=>1, 'upper'=>true, 'chkList'=>array ('P','B','O')), // Pers,Biz,Oth
'status' =>array('size'=>1, 'chkNN'=>true, 'upper'=>true, 'chkList'=>array ('A','D','U')), // Active, Deleted, Unknown
'last_contacted' =>array('size'=>30, 'type'=>'DATETIME', 'default'=>'now'),
'reg_date' =>array('size'=>25, 'chkNN'=>true, 'type'=>'DATE','default'=>'now'));
$contacts = new dbff;
$contacts->recdef = $contacts_def; // $contacts->trace=true;
$contacts->read();
reset($contacts->fldnames);
while (list ($pos, $fld_name) = each ($contacts->fldnames)) {
if (isset($_POST[$fld_name])) $editrec[$fld_name] = $_POST[$fld_name];
else $editrec[$fld_name] = NULL;
$dbrec[$pos] = $editrec[$fld_name];
}
if ($action=='add') {
$action = 'list';
if (addContactsRec($dbrec)) $editrec = NULL;
}
if ($action=='delete') {
$action = 'list';
$editrec = NULL;
delContactsRec($cont_id);
}
if ($action=='update') {
if (updContactsRec($dbrec, $cont_id)) {
$action = 'list';
$editrec = NULL;
} else $action = 'edit';
}
if ($action=='edit') {
$contrec = contactGetByCid ($cont_id);
$heading = 'Edit Contacts';
}
if ($action=='list') {
$allcrecs = getContactsRecs();
$heading = 'List of Contacts';
}
print pgStart();
print pc("<br><br>$heading<br><br>", 'font-size:16; font-weight:bold; text-align:center;');
if (!is_null($dbfferr)) print pc($dbfferr."<br><br>",'color:red; text-align:center;');
if ($action=='list') print contlist($allcrecs, $editrec);
else print contedit($contrec, $contacts_def);
print pgEnd();
exit;
///
/// db api functions, keep all data functions separate for simpler replacement to other db later
///
function contactGetByCid ($cid) { // get one record by id
global $contacts;
$r = NULL;
if ($contacts->select('CID',$cid)>0) $r = $contacts->selectGet();
return $r;
}
function getContactsRecs() { // get all contacts
global $contacts;
$r = NULL;
$contacts->select();
while ($cr = $contacts->selectGet()) $r[] = $cr;
return $r;
}
function updContactsRec ($crec, $cid) {
global $contacts;
if ($contacts->update($crec, $cid) && $contacts->commit()) return true;
return false;
}
function addContactsRec ($crec) {
global $contacts;
if ($contacts->insert($crec) && $contacts->commit()) return true;
return false;
}
function delContactsRec ($cid) {
global $contacts;
if ($contacts->delete($cid) && $contacts->commit()) return true;
return false;
}
///
/// List records!
///
function contlist ($crecs, $editrec) {
global $contacts;
$pg = $_SERVER['REQUEST_URI'];
$cid = NULL;
$bstr = "\n".'<td valign="top"><input type="SUBMIT" name="update" id="update" value="Save"></td>
<td valign="top"><input type="SUBMIT" name="edit" id="edit" value="Edit"></td>
<td valign="top"><input type="SUBMIT" name="delete" id="delete" value="Delete"></td>';
$str = "\n".'<table width="100%" bgcolor="lightblue" align="center" border="1"
cellpadding="0" cellspacing="1"><tr valign="top">';
reset($contacts->fldnames);
while ( list ($pos, $fld_name) = each ($contacts->fldnames) ) { /// header line
if (!$contacts->NN($fld_name)) continue; // Skip non required fields in list
$str .= "\n".'<td valign="top">'.mkhdr($fld_name).'</td>';
}
$str .= "\n".'<td> </td><td> </td><td> </td></tr>';
if (count($crecs)>0) {
while ($crec = current($crecs)) { /// Data rows
$cid = $crec['CONTACTS.CID'];
$str .= "\n".'<tr valign="top"><form name="cid_'.$cid.'" action="'.$pg.'" method="post">';
reset($contacts->fldnames);
while ( list ($pos, $fld_name) = each ($contacts->fldnames) ) {
if (!$contacts->NN($fld_name)) continue; // Skip non required fields in list
$idx = 'CONTACTS.'.strtoupper($fld_name);
if (isset($crec[$idx])) $val = $crec[$idx];
else $val = '';
$size = $dsz = $contacts->size($fld_name);
if ($dsz>8) $dsz = round($size / 4);
$str .= "\n".'<td valign="top">';
if ($fld_name=='cid') { $cid = $val; $str .= $cid.'</td>'; }
else $str .= "\n".'<input type="TEXT" name="'.$fld_name.'" id="'.$fld_name.
'" size="'.$dsz.'" value="'.$val.'" maxlength="'.$size.'"></td>';
}
$str .= "\n".'<input type="HIDDEN" name="cid" id="cid" value="'.$cid.'">'.$bstr.'</FORM></tr>';
next($crecs);
}
}
// add line
$str .= "\n".'<tr valign="top"><form name="add_contact" action="'.$pg.'" method="post">';
reset($contacts->fldnames);
while ( list ($pos, $fld_name) = each ($contacts->fldnames) ) {
if (!$contacts->NN($fld_name)) continue; // Skip non required fields in list
$size = $dsz = $contacts->size($fld_name);
if ($dsz>8) $dsz = round($size / 4);
$str .= "\n".'<td valign="top">';
if ($fld_name=='cid') $str .= ' </td>';
else $str .= "\n".'<input type="TEXT" name="'.$fld_name.'" id="'.$fld_name.
'" size="'.$dsz.'" value="'.$editrec[$fld_name].'" maxlength="'.$size.'"></td>';
}
$str .= "\n".'<td valign="top"><input type="HIDDEN" name="cid" id="cid" value="0">';
$str .= "\n".'<input type="SUBMIT" name="add" id="add" value="Add"></td>';
$str .= '<td> </td><td> </td></FORM></tr></table>';
return $str;
}
///
/// Edit form
///
function contedit ($r, $flds) {
global $contacts;
$pg = $_SERVER['REQUEST_URI'];
if (empty($r)) return 'No record to Edit!';
/// Js check functions
$js = new dbff_jsChk;
$str = $js->jsGen("cedit", $flds);
///
$str .= "\n".'<table width="90%" bgcolor="lightblue" align="center" border="1"
cellpadding="0" cellspacing="4"><tr valign="top">
<form name="cedit" action="'.$pg.'" method="post" onSubmit="return cedit_chk(this);">';
reset($contacts->fldnames);
while ( list ($pos, $fld_name) = each ($contacts->fldnames) ) {
$size = $dsz = $contacts->size($fld_name);
if ($dsz>50) $dsz = round($size / 3);
$str .= '<tr valign="top">';
$str .= "\n".'<td valign="top">'.mkhdr($fld_name,'R').'</td>'."\n".'<td valign="top">';
$idx = 'CONTACTS.'.strtoupper($fld_name);
if (isset($r[$idx])) $value = $r[$idx];
else $value = '';
if ($fld_name=='cid') $str .= $value.'</td></tr>';
else $str .= '<input type="TEXT" name="'.$fld_name.'" id="'.$fld_name.
'" size="'.$dsz.'" value="'.$value.'" maxlength="'.$size.'"></td></tr>';
}
$str .= "\n".'<tr><td> </td><td><input type="HIDDEN" name="cid" id="cid" value="'.
$r['CONTACTS.CID'].'">'."\n".'<input type="SUBMIT" name="update" id="update" value="Save">
<input type="RESET" name="cancel" id="cancel" value="Cancel"
onclick="parent.location=\''.$pg.'\'"></td></FORM></tr></table>';
return $str;
// Edit record end
}
///
/// General functions
///
function mkhdr ($str, $align='L') {
$str = ucwords(strtolower(strtr($str,'_',' ')));
if ($align=='R') return pc($str,'font-weight:bold; text-align:right;');
return pc($str,'font-weight:bold; text-align:left;');
};
function pc ($txt, $style=NULL) { // Tag with inline style
$str = "<p";
if (!is_null($style)) $str .= " style=\"$style\"";
$str .= '>' . $txt . "</p>";
return $str;
}
function pgStart() {
return '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>Contacts</title>
<META HTTP-EQUIV="expires" content="0" "content-type" content="text/html; charset=iso-8859-1">
<style><!--
page { margin-left:0cm; margin-right:0cm; margin-top:0cm; margin-bottom:0cm }
body { font-family: frutiger, helvetica, syntax, verdana, univers, sans-serif; color:black;}
p { font-family: frutiger, helvetica, verdana, sans-serif; font-size:0.8em; margin-bottom: 0em; margin-top: 0em;}
input { font-family:frutiger, helvetica, verdana, sans-serif; font-size:0.8em;
margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px;
padding-top:1px; padding-left:0px; padding-right:0px; padding-bottom:0px;
display:inline;}
textarea { text-align:left; font-family:frutiger, helvetica, verdana, sans-serif; font-size:0.8em;}
form { font-size:0.8em; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px;
padding-top:0px; padding-left:0px; padding-right:0px; padding-bottom:0px; display:inline;}
--></style>
</head>
<body topmargin="0" leftmargin="0" bgcolor="white">';
}
function pgEnd() {
return '</body></html>';
}
?>
|