/* This PHP script is designed to output an IC Card editor, and read/write
* to/from an XML Based Database of IC Card. --prr6 04.20.2007
*/
session_start(); /* used in case we need to prompt for group. */
header('Content-Type: text/html; charset=utf-8');
echo ''."\n";
?>
CS2310 IC Card Adder
IC Card Management System
- IC Card Adder
include_once('utils.inc');
if(isSet($_POST['submitCard']))
{
/* an IC Card was posted by the form. Handle it. */
$complete = true;
if($_POST['icNumberCurrent'] > $_POST['icNumberTotal'])
{
echo 'Validation Errors
'."\n".
'Card Number(s) are not valid. '."\n";
$complete = false;
}
/* These are not valid for adding new cards. */
unset($_POST['submitCard']);
unset($_POST['icCardID']);
unset($_POST['icCardGroupID']);
$cardInfo = array();
foreach($_POST as $key => $val)
{
if(empty($val))
{
if($complete){echo 'Validation Errors
'."\n";}
echo "\tMissing Entry: $key \n";
$complete = false;
}
else
{
$cardInfo[$key] = $val;
}
}
if($complete)
{
if($_POST['icNumberTotal'] > 1)
{
$_SESSION['icData'] = $cardInfo;
showGroupForm();
}
else
{
echo '';
}
}
else
{
echo ' '.showCardForm($_POST);
}
}
else
{
if(isSet($_POST['submitGroup']) && isSet($_SESSION['icData']))
{
$cardInfo = $_SESSION['icData'];
echo '';
unset($_SESSION['icData']);
}
else
{
/* user did not post anything, present form. */
echo showCardForm(NULL);
}
}
?>
function generateCardXML($domDoc,$cardDataArray)
{
$cardDoc =& $domDoc->createDocumentFragment();
$domNode =& $domDoc->createElement('icCard');
$cardDoc->appendChild($domNode);
foreach($cardDataArray as $name => $value)
{
$tmpNode =& $domDoc->createElement($name);
$tmpNode->setAttribute('content',$value);
$domNode->appendChild($tmpNode);
}
return $cardDoc;
}
function serializeCard($cardData,$groupNum)
{
include_once('dom4php/XmlParser.php');
include_once('dom4php/Document.php');
include_once('dom4php/DocumentFragment.php');
include_once('dom4php/XmlSerializer.php');
$parser = new XmlParser();
$dbData = @file_get_contents('icdb.xml');
if($dbData == FALSE)
{
// create file from scratch
$dbDoc =& new Document();
$dbNode = $dbDoc->createElement('icCardList');
$dbNode->setAttribute('xmlns:xsi',
'http://www.w3.org/2001/XMLSchema-instance');
$dbNode->setAttribute('xsi:noNamespaceSchemaLocation','icCard.xsd');
$dbDoc->appendChild($dbNode);
$nodeNum = 1;
}
else
{
$dbDoc = $parser->parse($dbData);
$dbNode =& $dbDoc->childNodes[0];
$nodeNum = $dbNode->lastChild->previousSibling->attributes['id'];
$nodeNum++;
}
$tmpNode = $dbDoc->createElement('icCardEntry');
$tmpNode->setAttribute('id',$nodeNum);
$tmpNode->setAttribute('groupId',$groupNum);
$dbNode->appendChild($tmpNode);
$tmpNode->appendChild(generateCardXML($dbDoc,$cardData));
$serializer = new XmlSerializer('XML');
return $serializer->serializeNode($dbDoc);
}
function showGroupForm()
{
include_once('dom4php/XmlParser.php');
include_once('dom4php/Document.php');
include_once('dom4php/DocumentFragment.php');
include_once('dom4php/XmlSerializer.php');
$parser = new XmlParser();
$dbData = @file_get_contents('icdb.xml');
$groupArray = array();
if($dbData == FALSE)
{
$groupNum = 1;
}
else
{
$groupNum=0;
$dbDoc = $parser->parse($dbData);
$entryArray = $dbDoc->getElementsByTagName("icCardEntry");
foreach(array_keys($entryArray) as $key)
{
$tmpVal = $entryArray[$key]->getAttribute('groupId');
if($tmpVal != -1)
{
$card = $entryArray[$key]->getElementsByTagName("icName");
$groupArray[$tmpVal] .= $card[0]->attributes['content'].', ';
if($tmpVal > $groupNum)
{
$groupNum = $tmpVal;
}
}
}
$groupNum++;
}
echo ' ';
}
?>