<?php

include("_dirinfo.php");

include (PATH_TO_ROOT."_class/class.Extjs.php");
include (PATH_TO_ROOT."_class/class.Extjs_tree.php");

class MakeTreeHelp extends MakeTree 
{
	protected $reqUrl = 'webhelp/_ajaxManipulators/treeDataManipulator.php';
	
	
	public function activateDD($treeGroup = null)
	{
		if($treeGroup)
		{
			$this->addConfig('ddGroup',"'$treeGroup'");
		}
		$this->listeners[] = "beforenodedrop:function(e){
		if(e.target.ownerTree !== e.dropNode.ownerTree)
		{
			alert('Move only inside this tree, please');
			e.cancel = true;
			return;
		}
		else if( (e.point == 'above')  )
		{
			if((e.target.attributes.id_sup>0) && (e.target.attributes.topic_sub==e.dropNode.attributes.topic_sub))
			{
				console.log(e.target.attributes);
				console.log(e.dropNode.attributes);	
			}else{
				//alert('Above - error');
				console.log(e.target.attributes);
				console.log(e.dropNode.attributes);
				e.cancel = true;
				return;
			}
		}
		else if( (e.point == 'below')  )
		{
			if((e.target.attributes.id_sup>0) && (e.target.attributes.topic_sub==e.dropNode.attributes.topic_sub))
			{
				console.log(e.target.attributes);
				console.log(e.dropNode.attributes);					
			}else{
				//alert('Below - error');
				console.log(e.target.attributes);
				console.log(e.dropNode.attributes);
				e.cancel = true;
				return;
			}
		}
		else if( (e.point == 'append')  )
		{
			if( (e.target.attributes.id_sup>0) && ( ( e.target.attributes.topic==1 && e.target.attributes.topic_sub==0 && e.dropNode.attributes.topic_sub==1) || (e.target.attributes.topic==0 && e.dropNode.attributes.topic_sub==0) ) )
			{
				console.log(e.target.attributes);
				console.log(e.dropNode.attributes);	
			}else{
				//alert('Append - error');
				console.log(e.target.attributes);
				console.log(e.dropNode.attributes);
				e.cancel = true;
				return;
			}
		}
		
		var nodeId = e.dropNode.attributes.id;
		var targetId = e.target.attributes.id;
		this.RMconn.request({params:{action:'moveNode',nodeId: nodeId,targetId:targetId,movePoint:e.point}});}";
		$this->configs['enableDD'] = "true".$this->newLine;
		$this->addConfig('dropConfig','{expandDelay:"0"'. ($treeGroup ? ",ddGroup :'$treeGroup'" : '') .   '}');
		$this->dragDrop = true;
		return $this;
	}
	
	protected $contDelete;
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $text
	 * @return MakeTree
	 */
	public function setContDelete($text = 'Delete')
	{
		
		$context = "function(item) {
			var n = item.parentMenu.contextNode;
			Ext.Msg.show({ title:'Remove record',buttons: Ext.MessageBox.YESNO,msg:'Are you sure?',
				fn: function(btn){
					if(btn == 'yes'){
						$this->compName.RMconn.request({params:{action:'delete',nodeId:n.id} 
						,success:function(resp){
						    if(Ext.decode(resp.responseText).success){
						          if (n.parentNode) {
						              n.remove();
					               }
                            } 
                            else
                            {
						      Ext.Msg.show({ title:'Info', msg:Ext.decode(resp.responseText).STATEMENT , icon: Ext.MessageBox.INFO, buttons: Ext.Msg.OK})
                            }
                        },
						failure:function(){/*alert('failed');*/}	});
					}
				}
			});
			
			}";
			        
		$this->addContItem($text,$context,'false','icon-delete-record');
		$this->contDelete = $text;
		
		return $this;
	}
	
	
	private $contEdit;
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $text
	 * @return MakeTree
	 */
	public function setContEdit($text = 'Edit')
	{
		
		$context = " function(item) {
				
				var n = item.parentMenu.contextNode;	
				//addTab(n.text,'webhelp/_forms/forms.helpEdit.php','id='+n.id+'&tabTitle='+n.text+'&table=".$this->table."');
				CallScript('webhelp/_forms/forms.helpEdit.php','id='+n.id+'&tabTitle='+n.text+'&table=".$this->table."');
				
				
			}";
		
			      
		$this->contEdit = $text;
		$this->addContItem($text,$context,'false','RMicon_edit_record');
		return $this;
	}
	
	private $contAddTopic;
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $text
	 * @return MakeTree
	 */
	public function setContAddTopic($text = 'Add')
	{
		
		$context = "function(item) {
					var defaultText = 'Novi';
					var n = item.parentMenu.contextNode;
                       $this->compName.RMconn.request({params:{action:'insertTopic',nodeId:n.id,nodeText:defaultText} 
					,success:function(resp){
					if(Ext.decode(resp.responseText).success){
					   
						n.leaf = false;
						
						// popravil Tanis
						var newNode = new Ext.tree.TreeNode({
						    id: Ext.decode(resp.responseText).insertId + '_adminTree', 
						    text: defaultText, leaf: true,
						    icon: Ext.decode(resp.responseText).icon
                        });
						n.appendChild(newNode);
						} 
						else
						{
						    /*alert('failed');*/
						     Ext.Msg.show({ title:'Info', msg:Ext.decode(resp.responseText).reason, icon: Ext.MessageBox.INFO, buttons: Ext.Msg.OK})
						}},
						failure:function(){/*alert('failed');*/}	});}";
		
		$this->contAddTopic = $text;
		$this->addContItem($text,$context,'false','icon-add-record');
		return $this;
	}
	
	
	private $contAddCat;
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $text
	 * @return MakeTree
	 */
	public function setContAddCat($text = 'Add')
	{
		
		$context = "function(item) {
					var defaultText = 'Novi';
					var n = item.parentMenu.contextNode;
                       $this->compName.RMconn.request({params:{action:'insertCat',nodeId:n.id,nodeText:defaultText} 
					,success:function(resp){
					if(Ext.decode(resp.responseText).success){
						n.leaf = false;
						var newNode = new Ext.tree.TreeNode({
						    id: Ext.decode(resp.responseText).insertId, 
						    text: defaultText, 
						    leaf: true,
                            icon: 'webhelp/_icons/File_and_Folder.ico'
                        });
						n.appendChild(newNode);
						} else{/*alert('failed');*/}},
						failure:function(){/*alert('failed');*/}	});}";
		
		$this->contAddCat = $text;
		$this->addContItem($text,$context,'false','icon-add-folder');
		return $this;
	}
	
	
	private $contSetFaq;
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $text
	 * @return MakeTree
	 */
	public function setContSetFaq($text = 'FAQ')
	{
		
		$context = "
				function(item) {
					var n = item.parentMenu.contextNode;
					//$this->compName.RMconn.request({params:{action:'setFaq',nodeId:n.id}});
					CallScript('webhelp/_forms/window.helpEdit.php','action=catFaqEdit&id='+n.id+'&tabTitle='+n.text+'&table=".$this->table."');
					
				}";
					
					
		
		$this->contSetFaq = $text;
		$this->addContItem($text,$context,'false','iconHelpFaq');
		return $this;
	}
	
	
}


class MakeTreeHelpAdmin extends MakeTreeHelp 
{
	protected $reqUrl = 'webhelp/_ajaxManipulators/treeDataManipulatorAdmin.php';
	
	
}

?>