<?php
$table="t_test_tree_2";
// prevent caching of queries when in DEMO mode
$db->cache_dir = false; // DEFAULT is FALSE
$db->cache_queries = false; // DEFAULT is FALSE
$db->cache_inserts = false; // DEFAULT is FALSE
$db->use_disk_cache = false; // DEFAULT is FALSE
$showdebug=false; // ezSQL: hide/show debug statements for each query
// ====== when an item is selected from list the $get_id parameter is set =====
if($get_id!=""){
$sql_id = "SELECT * FROM $table WHERE id='$get_id' ";
$edit=$db->get_row($sql_id);
// get menu item values for preloading the FORM
$preload_id = $edit->id;
$preload_m_id = $edit->m_id;
$preload_m_parent = $edit->m_parent;
$preload_m_title = $edit->m_title;
// before we leave,, set the submit button value
$button="Update";
}else{
$button="Save";
}
// ============================= FORM ==============================================
// ======================== save a NEW ENTRY ======================================
if($action=="Save"){
$sql="INSERT INTO $table VALUES (NULL, '$m_id', '$m_parent', '$m_title', '', '') ";
$db->query($sql);
$id=$db->insert_id; // ezSQL class: get the auto_incremented 'id' from this INSERT
$showdebug ? $db->debug() : NULL ; // hide/show ezSQL debug
// Get the parent of the row we just inserted so we can update the m_newlineage field
$sql_parent = "SELECT * FROM $table WHERE m_id=$m_parent ";
$parent=$db->get_row($sql_parent);
$showdebug ? $db->debug() : NULL ; // hide/show ezSQL debug
$lineage=$parent->m_newlineage."-".$m_parent;
// finally, update new item with the m_lineage of it's parent cat'ed with m_parent
$newlineage = "UPDATE $table SET m_newlineage='$lineage' WHERE id='".$id."'";
$result=$db->query($newlineage); // works in single inquiry
$showdebug ? $db->debug() : NULL ; // hide/show ezSQL debug
// ========================== update an EDIT of ITEM ===============================
}elseif($action=="Update"){
$newlineage = "UPDATE $table SET m_id=$m_id, m_parent=$m_parent, m_title='$m_title', m_newlineage='$m_newlineage' WHERE id='$id'";
$updated=$db->query($newlineage); // now works in single pass
$showdebug ? $db->debug() : NULL ; // hide/show ezSQL debug
$id="";
}
// build a sidebar list for convenience
$list="SELECT id,m_id,m_parent,m_title,m_newlineage,CONCAT(m_newlineage,'-',m_id) AS sort_Order FROM $table ORDER BY sort_order";
$all_items=$db->get_results($list);
foreach($all_items AS $key=>$item){
$lines.="<tr><td class=list><a href=\"?get_id=$item->id\">$item->m_id</a> </td><td class=list align=right>$item->m_parent   </td><td class=list>$item->m_title </td><td class=list>$item->m_newlineage</td></tr>\n\r";
$all_m_id[] = $item->m_id;
}
if($get_id==""){
rsort($all_m_id);
$preload_m_id = $all_m_id[0] + 1;
}
//$showdebug ? $db->debug() : NULL ; // hide/show ezSQL debug
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>t_test_tree</title>
<style type="text/css">
.footer{
font-size: 11px;
}
.list{
font-size: 9px;
}
th.list{
font-size: 9px;
}
hr.pme-hr { border: 0px solid; padding: 0px; margin: 0px; border-top-width: 1px; height: 1px; }
table.pme-main { border: #004d9c 1px solid; border-collapse: collapse; border-spacing: 0px; width: 100%; }
table.pme-navigation { border: #004d9c 0px solid; border-collapse: collapse; border-spacing: 0px; width: 100%; }
th.pme-header { border: #004d9c 1px solid; padding: 4px; background: #add8e6; }
td.pme-key-0, td.pme-value-0, td.pme-help-0, td.pme-navigation-0, td.pme-cell-0,
td.pme-key-1, td.pme-value-1, td.pme-help-0, td.pme-navigation-1, td.pme-cell-1,
td.pme-sortinfo, td.pme-filter { border: #004d9c 1px solid; padding: 3px; }
td.pme-buttons { text-align: left; }
td.pme-message { text-align: center; }
td.pme-stats { text-align: right; }
</style>
</head>
<body bgcolor="#ffffcc" >
<?php
//echo $next_id."aa<BR>";
//echo $all_m_id[0]." xx<BR>";
//echo print_r($sorted_m_id,true)." yy<br>";
//echo print_r($all_m_id,true)." zz<br>";
//echo print_r($all_items,true)." ww<br>";
?>
<table border="0" bgcolor="#ffffcc" cellpadding="10" cellspacing="0" summary="">
<tr><td>
<table bgcolor="#ffffc1" border=1 cellspacing="0" cellpadding="2">
<tr><th class=list>m_id</th><th class=list>m_parent</th><th class=list>m_title</th><th class=list >m_newlineage</th></tr>
<?php echo $lines?>
</table>
</td>
<td valign=top>
<table width="500" height="100%" align=left class=wrapper border="0" cellpadding="0" cellspacing="0" summary="" >
<tr><td bgcolor="#ffff99" class=header align=center valign=middle><a href="http://www.tinybutstrong.com/"><img border=1 src="tbs_logo1_94x51.gif" vspace=10 alt="TBS home"></a></td><td align=left valign=middle class=header bgcolor="#ffff99">
<div class=logoheader>      
Lineage-based sorting for faster hierarchy menus</div>
</th></tr>
<tr><td colspan=2 align=center><br><br>
<font color="Maroon" size="+1">Menu Item - add/edit to Menu Tree</font> <br>
<font color="Blue" size="-2">(click <big><b>m_id</b></big> link on left to preload form)</font> <P>
<form class="pme-form" method="post" action="add_new_item.php" >
<input type="hidden" name="id" value="<?php echo $edit->id ?>">
<input type="hidden" name="m_newlineage" value="<?php echo $edit->m_newlineage ?>">
<table summary="t_test_tree">
<tr class="pme-row-1">
<td class="pme-key-1" align=right>id</td>
<td class="pme-value-1">
<input disabled="disabled" type="text" name="id" size="4" maxlength="4" value="<?php echo $preload_id ?>" /></td>
</tr>
<tr class="pme-row-1">
<td class="pme-key-1" align=right>m_parent</td>
<td class="pme-value-1">
<input type="text" name="m_parent" size="4" maxlength="4" value="<?php echo $preload_m_parent ?>" /></td>
</tr>
<tr class="pme-row-0">
<td class="pme-key-0" align=right>m_title</td>
<td class="pme-value-0">
<input type="text" name="m_title" size="40" maxlength="40" value="<?php echo $preload_m_title ?>" /></td>
</tr>
<tr class="pme-row-0">
<td class="pme-key-0" align=right>m_id</td>
<td class="pme-value-0">
<input type="text" name="m_id" size="16" maxlength="40" value="<?php echo $preload_m_id ?>" /> (must be unique)</td>
</tr>
<tr >
<td colspan=2 align=center>
<input type="submit" name="action" value="<?php echo $button ?>" />
</td>
</tr>
</table>
</form>
</td></tr>
<tr><td class=footer align=center colspan=2>
<a href="/tbs3/menu_tree/">MenuTree Demo page</a>
<hr color=silver width="100%">
<a href="http://tomhenry.us/tbs3/">Return to TBS apps page</a>
<hr color=silver width="100%">
© 2008 <a href="http://tomhenry.us/" target="_blank">Tom Henry / Strategic Business Sytems</a> - all rights reserved.
</td></tr>
</table>
</td></tr>
</table>
</body>
</html>