<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_tree.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");


$db->ChangeDatabase(DB_WARPIT_MAIN);



	
// here we define projects tree	
	$treeWCprojects = new MakeTree('MysteryVisits');
	$treeName = $treeWCprojects->getName();
	$treeWCprojects->SetTable('_Visits',DB_WARPIT_MYSTERY);
	$treeWCprojects->SetRootVisible('false')
	//  ->setTitle('Help'); // this is not used because we combine in one tab two trees and title is set in combined panel
	  ->setReqUrl('_ajax/ajax.groups.php')
	  ->setSort('id ASC')
	 /* ->setContDelete('Delete')
	  ->setContAdd('Add child')
	  ->setContAddMain('Add sibling')*/
	  //we create data for the language combobox
	  ;

			
if($_SESSION['UserRights'][15] && false)
	  $treeWCprojects 	
	  ->addConfig('addGroupHandler',"function(item){
		Ext.Msg.prompt('Add Folder','Folder name:',function(btn,text){ if(btn == 'ok' ){
		var n;
	    if(item)
			n = item.parentMenu.contextNode;
		else
			{
				n = $treeWCprojects.getSelectionModel().getSelectedNode();
				if(!n)
					n = $treeWCprojects.getRootNode();
			}
		var par;
		//the group gets added after the clicked group (if a q is clicked its parent is used)
		
		if(n.attributes.isProject == 1)
			{par = n.parentNode; } 
		else{ par = n;   }
		$treeName.RMconn.request({params:{
		action:'insertGroup',nodeText:text,
		nodeId:par.id
		,isTargetQ:par.attributes.isQuestion },success:function(resp){
		if(Ext.decode(resp.responseText).success){ $treeName.getLoader().load(par,function(){ par.expand();});  }
		 }  }); }  } );   }")
	->addtBarItem("{iconCls:'icon-add-folder',tooltip:'Add folder',handler: function(){ $treeWCprojects.addGroupHandler(); }}")	 
	->addContItem('Add folder',"function(item){ $treeWCprojects.addGroupHandler(item) }",'false','icon-add-folder')
	->addContSeparator()
	->addContItem('Rename',"function(item){ if(item)
			n = item.parentMenu.contextNode;
		else
			{
				n = $treeWCprojects.getSelectionModel().getSelectedNode();
				if(!n)
					return;
				
			}
			$treeWCprojects.treeEditor.triggerEdit(n);
			}",'false','RMicon_edit_record',2)
	 ->addContItem('Delete',"function(item){
	 	if(item)
			n = item.parentMenu.contextNode;
		else
			{
				n = $treeWCprojects.getSelectionModel().getSelectedNode();
				if(!n || !n.isLeaf())
					return;
				
			}
			var confirmText = RMrandomString();		
		Ext.Msg.prompt('Delete project '+n.text,
			'This will delete the project <b>'+n.text+'</b> and all it`s data!'+
			'<br>Enter following code and confirm<br>'+confirmText,function(btn,text){
				if(btn=='ok')
				{
					if(text == confirmText )
					{
					 $treeName.RMconn.request({ params: {action:'deleteProject',id:n.id,name:n.attributes.text},
					 success:function(resp,opt){ 
							if(Ext.decode(resp.responseText).success)
							{
								var bq = Ext.getCmp('buildQuestionnaire_'+n.text);
								if(bq) mainTabPanel.remove(bq);
								n.remove();
							}
					    } });     
			    }
				else return false;
		 }} );
	 }","node.attributes.isProject!=true",'icon-delete-record',2)
	  ->addContItem('Delete Folder',"function(item){
	 	var n = item.parentMenu.contextNode;
	 	Ext.Msg.show({ title:'Remove folder',buttons: Ext.MessageBox.YESNO,msg:'Are you sure?',
		fn: function(btn){if(btn == 'yes'){
		 $treeName.RMconn.request({ params: {action:'delete',nodeId:n.id,name:n.attributes.text},
		 success:function(resp,opt){ 
				if(Ext.decode(resp.responseText).success)
				{
					n.remove();
				}
		    } });     
		 }} });
	 }","node.attributes.isProject==true || node.hasChildNodes()",'icon-delete-folder')
	 //copy projects!!!
	   ->addContItem('Copy',"function(item){
	 	if(item)
			n = item.parentMenu.contextNode;
		else
			{
				n = $treeWCprojects.getSelectionModel().getSelectedNode();
				if(!n || !n.isLeaf())
					return;
				
			}
	 	Ext.Msg.prompt('Copy project '+n.text,'New project name:',function(btn,text){ if(btn == 'ok' ){
		var par;
		//the group gets added after the clicked group (if a q is clicked its parent is used)
		if(n.attributes.isProject == 1)
			{par = n.parentNode; } 
		else{ par = n;   }
		$treeName.RMconn.request({params:{
		action:'copyProject',nodeText:text,
		nodeId:par.id,id_project:n.id
		,isTargetQ:par.attributes.isQuestion },success:function(resp){
		if(Ext.decode(resp.responseText).success){ $treeName.getLoader().load(par,function(){ par.expand();});  }
		 }  }); }  } ); 
	 }","node.attributes.isProject==false || node.id == 0",'RMicon_copy',2)
	 ->activateDD('wupGrouplink');
	 
	 //grid with old webcati projects (available only if specific config is set)
	 
	  $treeWCprojects->addtBarItem("'->'")
	  ->setSearchField('','top',100)
	 // ->addtBarItem("{text:'Expand All',handler:function(){WCprojects.expandAll();}} ")
	  ->addConfig('region',"'center'")
	  ->baseParams = "attr:'id_project,id_structure'";
	 ;
//	 $treeWCprojects->addListener('click',"function(n){
//	 	if(!n.attributes.leaf)
//		{
//			CallScript('_forms/forms.pregledPoseta.php',{visitFolder:n.attributes.id });
//		}
//	 }");

	echo $treeWCprojects->CreateTree();
	
	
//	echo $treeProjName.".getRootNode().expand();";
		
// we add JS code to define a new panel which has two trees combined in one tab
//this tab we dan add to the western tabpanel with id = 'west-panel'	
?>
	var visitsUniqePanel = new Ext.Panel({
		id:'visitsUniqePanel',
		title:'<?php echo $languagePack['visits']; ?>',
		defaults:{split:true,border:false},
		iconCls:'iconVisits',
		layout:'border',
		border: false,
		items:[<?php echo $treeWCprojects->getName(); ?>  ]
		
				
	});

