<?php
include './ui.inc'; // Common View Helper functions
include './add_c.inc'; // Controller
head();
?>
<div id="tItems" class="ta">
<table cellspacing="0" cellpadding="3" width="100%">
<?php foreach($items as $i=>$elem) {
$s = $i%2;
echo <<<EOB
<tr id="{$elem['id']}" class="it$s">
<td>{$elem['id']}</td>
<td>{$elem['sdesc']}</td>
<td>{$elem['cat']}</td>
<td align="right">\${$elem['fprice']}</td>
</tr>
EOB;
}
?>
</table>
</div><br />
<form name="fItem" action="javascript:postForm('add.php','fItem')">
<input type="hidden" name="formName" value="fItem" />
<input type="hidden" name="id" id="f_id" value="" />
<input type="text" class="f" id="f_sdesc" name="sdesc" size="39"
maxlength="128" value="Short Description" dir="LTR" />
<select class="f" name="cat" id="f_cat" size="1">
<option selected>Category</option>
<?php foreach($categories as $cat) echo <<<EOB
<option value="{$cat}">{$cat}</option>
EOB;
?>
</select>
<input type="text" class="f" id="f_price" name="price" size="8"
maxlength="128" value="Price" dir="RTL" /><br />
<textarea style="width: 100%" name="ldesc" class="f"
id="f_ldesc" rows="5" wrap="soft">Details</textarea><br />
<input name="f_submit" type="submit" value="Add Item" />
<input type="reset" value="Clear" />
</form>
<?php
foot();
?>
<?php
include './model/db.inc';
include './model/items.inc';
$db = new items();
if($_SERVER['REQUEST_METHOD']=='POST') {
header("Content-type: application/json");
// Load an item entry from backend and send JSON request to populate form
if(isset($_POST['load_item'])) {
$entry = $db->load($_POST['load_item']);
$entry[0]['submit'] = 'Modify Item';
if($entry) echo json_encode(array('formName'=>$_POST['formName'],
'load_item'=>$entry));
exit;
}
// Validate form fields
foreach($_POST as $k=>$v) {
if(substr($k,0,5)=="desc_") {
if(isset($_POST[substr($k,5)]) && $_POST[substr($k,5)]==$v) {
echo json_encode(array('validate_error'=>'f_'.substr($k,5)));
exit;
}
}
}
// Save changes and display status message
$status = "Failure";
if($_POST['f_submit']=='Modify Item') {
$ret = $db->modify($_POST);
if($ret) $status = "Modified";
} else {
$ret = $db->insert($_POST);
if($ret) $status = "Added";
}
echo json_encode(array('status'=>$status,
'elem'=>'tItems',
'reset'=>$ret,
'formName'=>$_POST['formName']));
exit;
}
// Initialize view data
if(!isset($categories)) load_list('categories');
if(!isset($item)) $item = array('cat'=>'');
$items = $db->load();
?>