<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"><?php
include('../lib/generic.inc.php');
?>
<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<style>
BODY {
   font-family: Arial, Helvetica, Sans-Serif;
   font-size: 10pt;
}
P, TD {
   font-family: Arial, Helvetica, Sans-Serif;
   font-size: 10pt;
}
</style>
<LINK rel="stylesheet" type="text/css" href="../script/clickmenu.css"/>
<script src="../script/jquery.js"></script>
<script src="../script/jquery.clickmenu.pack.js"></script>
<link rel="stylesheet" type="text/css" href="../script/dhtmlx/dhtmlxTree/codebase/dhtmlxtree.css">
<script src="../script/dhtmlx/dhtmlxcommon.js"></script>
<script src="../script/dhtmlx/dhtmlxTree/codebase/dhtmlxtree.js"></script>
<script src="../script/dhtmlx/dhtmlxTree/codebase/ext/dhtmlxtree_start.js"></script>
<script src="../script/dhtmlx/dhtmlxTree/codebase/ext/dhtmlxtree_ed.js"></script>
</head>
<body style="margin:0px">
<ul id=menu>
   <li>File
      <ul>
         <li><a href="javascript:serialize()">Save</a></li>
         <li><hr></li>
         <li><a href="javascript:void(0)">iShop book</a></li>
      </ul>
   </li>
   <li>View
      <ul>
         <li><a href="javascript:myTree.openAllItems(0)">Expand all</a></li>
         <li><a href="javascript:myTree.closeAllItems(0)">Collapse all</a></li>
      </ul>
   </li>
   <li>Edit
      <ul>
         <li><a href="javascript:editItem()">Edit label</a></li>
         <li><a href="#">Edit text</a></li>
         <li><hr></li>
         <!-- NOBR should be inside the HREF, not surrounding -->
         <li><a href="javascript:addSibling()"><nobr>Add sibling node</nobr></a></li>
         <li><a href="javascript:addChild()">Add child node</a></li>
         <li><a href="javascript:removeNode()">Delete node</a></li>
         <li><hr></li>
         <li><a href="javascript:moveItem('up_strict')">Move up</a></li>
         <li><a href="javascript:moveItem('down_strict')">Move down</a></li>
         <li><a href="javascript:moveItem('left')">Move left (demote)</a></li>
      </ul>
   </li>
</ul>
<script>
var myTree=null;
</script>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST name="goForm">
<input type=hidden name="FUNCTION" value="1">
<input type=hidden name="NODES">
</form>
<?php
$fu=(int)@$_REQUEST['FUNCTION'];
switch ($fu)
{
case 0:
   show_contents();
   break;
case 1:
   upd_contents();
   show_contents();
   break;
}
?>
</body>
</html>
<?php
//--------------------------------------
function str_ends_with($haystack, $needle)
{
   $l1=strlen($haystack);
   $l2=strlen($needle);
   if ($l1<$l2)
      return false;

   return (substr($haystack, $l1-$l2)==$needle);
}
//--------------------------------------
function upd_contents()
{
   save_idx_xml();
   
   $idxfile=IncludeTrailingPathDlm(DOCUMENT_ROOT).'ishop'.FSEP.'help.htm';
   if (($fp=@fopen($idxfile, 'wb'))===false) {
      echo "<h6>Failed to open XML file for writing!</h6>";
      return;
   }

   save_toc_htm($fp);
   save_doc_htm($fp);

   fclose($fp);
}
//--------------------------------------
function save_idx_xml()
{
   $idxfile=IncludeTrailingPathDlm(DOCUMENT_ROOT).'book'.FSEP.'ishop'.FSEP.'help_idx.xml';
   if (($fp=@fopen($idxfile, 'wb'))===false) {
      echo "<h6>Failed to open XML file for writing!</h6>";
      return;
   }
   $s1="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
   fputs($fp,$s1,strlen($s1));
   fputs($fp,$_REQUEST['NODES'],strlen($_REQUEST['NODES']));
   fclose($fp);
}
//--------------------------------------
function save_toc_htm($fp)
{
   $s1='<html>
      <head>
      <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
      <LINK href="help_files/ishophelp.css" type="text/css" rel="stylesheet">
      </head>
      <body>
      <h2>iShopping cart</h2>
      <h4>Περιεχόμενα</h4>';
   fputs($fp,$s1,strlen($s1));

   $s1='';
   $ar1=explode("\n",$_REQUEST['NODES']);
   foreach ($ar1 as $line) {
      $line1=trim($line);
      if (strpos($line1,'<tree')===0) {
         $s1 .= "<ul>\n";
      }
      else if (strpos($line1,'<item')===0) {
         $id=0;
         $text='';
         $l=strlen($line1);
         for ($i=5;$i<$l;$i++) {
            if (substr($line1,$i,4)=='id="') {
               $j=strpos(substr($line1,$i+4),'"');
               if ($j!==false) {
                  $id=(int)substr($line1,$i+4);
                  $i += $j;
               }
            }
            else if (substr($line1,$i,6)=='text="') {
               $j=strpos(substr($line1,$i+6),'"');
               if ($j!==false) {
                  $text=substr($line1,$i+6,$j);
                  $i += $j;
               }
            }
         }
         $s1 .= "<li><a name=\"toc$id\"></a><a href=\"#doc$id\">".myhtml($text)."</a>";
         if (str_ends_with($line1,'/>'))
            $s1 .= "</li>\n";
         else
            $s1 .= "\n<ul>\n";
      }
      else if (strpos($line1,'</item')===0) {
         $s1 .= "</ul></li>\n";
      }
      else if (strpos($line1,'</tree')===0) {
         $s1 .= "</ul>";
      }
   }
   fputs($fp,$s1,strlen($s1));
}
//--------------------------------------
function save_doc_htm($fp)
{
   $ar1=explode("\n",$_REQUEST['NODES']);
   foreach ($ar1 as $line) {
      $line1=trim($line);
      if (strpos($line1,'<item')===0) {
         $id=0;
         $text='';
         $l=strlen($line1);
         for ($i=5;$i<$l;$i++) {
            if (substr($line1,$i,4)=='id="') {
               $j=strpos(substr($line1,$i+4),'"');
               if ($j!==false) {
                  $id=(int)substr($line1,$i+4);
                  $i += $j;
               }
            }
            else if (substr($line1,$i,6)=='text="') {
               $j=strpos(substr($line1,$i+6),'"');
               if ($j!==false) {
                  $text=substr($line1,$i+6,$j);
                  $i += $j;
               }
            }
         }
         $s1 = "<a name=\"doc$id\"></a><h3><a href=\"#toc$id\" class=headline>".myhtml($text)."</a></h3>\n";
         fputs($fp,$s1,strlen($s1));

         $htmlfile=IncludeTrailingPathDlm(DOCUMENT_ROOT).'book'.FSEP.'ishop'.FSEP.'help'.FSEP.$id.'.htm';
         //echo "About to load $htmlfile\n";
         if (($s1=@file_get_contents($htmlfile))!==false) {
            fputs($fp,$s1,strlen($s1));
         }
      }
   }

   $s1="</body></html>";
   fputs($fp,$s1,strlen($s1));
}
//--------------------------------------
function show_contents()
{
?>
<div id="treeboxbox" style="background-color:white;margin-top:5px;">
</div>

<script>
function onMainResize(h1)
{
   if (h1==0)
      h1=$(document).height();

   var odiv=document.getElementById('treeboxbox');
   odiv.style.height = (h1-odiv.offsetTop).toString() + 'px';
}

// Create tree
myTree = new dhtmlXTreeObject("treeboxbox","100%","100%",0);
myTree.setImagePath("../script/dhtmlx/dhtmlxTree/codebase/imgs/csh_yellowbooks/");

//enable Drag&Drop
myTree.enableDragAndDrop(true);

myTree.attachEvent('onMouseIn',myMouseIn);
myTree.attachEvent('onMouseOut',myMouseOut);
myTree.attachEvent('onClick',myClick); // event name is case sensitive

function myClick(nodeId)
{

   var label=myTree.getItemText(nodeId);
   window.parent.F_RIGHT.location='ihelp_doc.php?LABEL='+encodeURIComponent(label)+'&NODEID='+nodeId;
}
function myMouseIn(nodeId)
{
   myTree.setItemStyle(nodeId, 'text-decoration:underline;');
}
function myMouseOut(nodeId)
{
   myTree.setItemStyle(nodeId, 'text-decoration:none;');
}

//set my Drag&Drop handler
//myTree.setDragHandler(myDragHandler);
function myDragHandler(idFrom,idTo){
   //if we return false then drag&drop be aborted
   return true;
}

function editItem()
{
   myTree.enableItemEditor(true);
   var id=myTree.getSelectedItemId();
   if (id!=null && id !='') {
      myTree.editItem(id);
      myTree.enableItemEditor(false);
   }
   else {
      window.alert('No item selected!');
   }
}

function get_max_id()
{
   var s1=myTree.getAllSubItems(0);
   var ar1=s1.split(',');
   var x,maxid=0;
   for (var i in ar1) {
      x=parseInt( ar1[i] );
      if (x>maxid)
         maxid=x;
   }
   return maxid;
}

function addSibling()
{
   var newid=get_max_id()+1;
   var id=myTree.getSelectedItemId();
   if (id!=null && id !='') {
      myTree.insertNewNext(id, newid, 'New item '+newid );
   }
   else {
      id=0;
      myTree.insertNewChild(id, newid, 'New item '+newid );
   }
}

function addChild()
{
   var newid=get_max_id()+1;
   var id=myTree.getSelectedItemId();
   if (id!=null && id !='') {
      myTree.insertNewChild(id, newid, 'New item '+newid );
   }
   else {
      window.alert('No item selected!');
   }
}

function removeNode()
{
   var id=myTree.getSelectedItemId();
   if (id!=null && id !='') {
      myTree.deleteItem(id);
   }
   else {
      window.alert('No item selected!');
   }
}

function moveItem(mode)
{
   var id=myTree.getSelectedItemId();
   if (id!=null && id !='') {
      myTree.moveItem(id,mode);
      myTree.selectItem(id,false,false);
   }
   else {
      window.alert('No item selected!');
   }
}

function serialize()
{
   s1 = '<tree id=\"0\">\r\n';

	var s2=myTree.getSubItems(0);
	if (s2!=0) {
      var ar1=s2.toString().split(',');

      for (var i=0;i<ar1.length;i++)
      {
         s1 += doSerialize(ar1[i]);
      }
   }

   s1 += '</tree>\r\n'
//alert(s1);
   document.goForm.NODES.value=s1;
   document.goForm.submit();
}
function doSerialize(nodeId)
{
	s1 = '<item text=\"' + myTree.getItemText(nodeId) + '\" id=\"' + nodeId + '\" tooltip=\"#' + nodeId + '\"';

	var s2=myTree.getSubItems(nodeId);
	if (s2!=0) {
      s1 += '>\r\n'

      var ar1=s2.toString().split(',');

      for (var i=0;i<ar1.length;i++)
      {
         s1 += doSerialize(ar1[i]);
      }

      s1 += '</item>\r\n'
   }
   else {
      s1 += '/>\r\n'
   }

   return s1;
}
$('#menu').clickMenu();
onMainResize(0);
myTree.loadXML("/book/ishop/help_idx.xml");
</script>
<?php
}
//--------------------------------------
?>