<?php 
include_once("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.treeData.php");

/*ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL ^ E_NOTICE);
*/
class CustomFillScriptMan extends TreeDataManipulator {
    
    
    public function selectAction()
    {
        
        require_once (PATH_TO_ROOT.'_class/createQuestion/Question.php');
       /* $sql = "SELECT a.id,a.q_name, b.script FROM " . mysql_real_escape_string($_POST['RMtable']) . " as a 
                LEFT JOIN ". mysql_real_escape_string($_POST['RMtable'])."_script as b ON a.id = b.id_question AND id_event = 4 
                WHERE a.q_pos > 0 ORDER BY a.q_pos";*/
          $parent = $_POST['node']?$_POST['node']:0;
          $sql = "SELECT a.id,c.q_name, b.script, a.idLab,a.is_leaf, c.tip_sql  FROM " . mysql_real_escape_string($_POST['RMtable']) . "_tree as a
                LEFT JOIN  " . mysql_real_escape_string($_POST['RMtable'])  . " as c ON a.id_question = c.id
                LEFT JOIN ". mysql_real_escape_string($_POST['RMtable'])."_script as b ON c.id = b.id_question AND id_event = 4 
                WHERE a.id_sup = " . $parent  .  " ORDER BY a.orderBy
                #WHERE c.q_pos > 0 ORDER BY c.q_pos";      
                
            /*    print $sql;
                exit;*/
        $res = $this->db->SQLexecute($sql);
        while ($row = $this->db->fetchAssoc($res)) 
        {
            $path = array();
            if(strlen($row['script']) > 0) $script = "<span style=color:red;>(S)</span>";
            else $script = "";
            $path['isQuestion'] = $row['is_leaf'];
            if(isset($row['tip_sql']))
                $path['qType'] = $row['tip_sql'];
            if(isset($row['node_img']) && $row['node_img']!= '')
            {
                $path['icon'] = $row['node_img'];
            }
            else 
            {           
                if(!$path['isQuestion']){
                    //$path['iconCls']  = 'folder';
                    
                } else{
                    
                
                $q = Question::createQuestion($path['qType']);
                $path['iconCls'] = $q->getIconCls();
                
                }
            }
            $path['id'] = $this->idPrefix . $row['id'];
            $path['origId'] = $row['id'];
            $path['leaf'] = $row['is_leaf']?true:false;
            $path['text'] = $row['idLab'] . $script;
            $path['strText'] = $row['q_name'];
            
            $this->nodes[] = $path;
        }
        return json_encode($this->nodes);
        
    }
    
    public function saveScriptAction()
    {
        $id = $_POST['id_question'];
        $value = $_POST['value'];
        $idEvent = $_POST['idEvent'];
        $sql = "REPLACE $this->table"."_script (id_question,id_event,script) VALUES  ($id,$idEvent,'".addslashes( trim($value) )."') ";
        If (!$res = $this->db->SQLexecute($sql)){return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}'; }
        $rtn['success'] = true;
        return json_encode($rtn);
    }
}
    
    
$db->ChangeDatabase($_POST['database']);

$manipulator = new CustomFillScriptMan($db,$_POST['RMtable']);  
//$manipulator->id_project = $_POST['id_project'];
$comm = $_POST['action'].'Action';
if(method_exists($manipulator,$comm))
    echo $manipulator->$comm();
else 
{
    echo '{success:false,error:"Unknown action!"}'; 
}

