<?php

include_once("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."config.php");

//ini_set("display_errors", "1");
$table =  "_Plugin";


if($_POST['action'] == "get")
{
    $sql = "SELECT p.id_plugin, p.plugin_name, (j.id_plugin IS NOT NULL ) defined, p.plugin_path
            FROM ". DB_WARPIT_WEBCATI_BASE. "." . $table .  " p 
            LEFT JOIN  " . DB_WARPIT_WEBCATI. ".". $_POST['tableName'] ." j 
            ON p.id_plugin = j.id_plugin AND j.id = " .  $_POST['questionId'];
    $res= $db->SQLexecute($sql);
    
    //print $sql;
    while ($dat = $db->fetchAssoc($res)) {  
        $data[] = $dat;
    }

    $result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    print json_encode($result);
    exit;
}
elseif ($_POST['action'] == "update") {
    
                                
    $sql = "UPDATE `".$_POST['table']."` SET `".$_POST['field']."` = '".$_POST['value']."' WHERE ".$_POST['idField']." = ".$_POST['id'];
    $db->SQLexecute($sql);
    
    echo "{ success : true }";
    exit;
}
elseif($_POST['action'] == "DefinePlugin")
{
    $sql = "UPDATE " . DB_WARPIT_WEBCATI. ".". $_POST['tableName'] . " SET id_plugin = " .  $_POST['id_plugin'] . " WHERE id = " . $_POST['questionId'];
    $db->SQLexecute($sql);
    
    if(isset($_POST['pluginName']))
    {
        $sql = "SELECT * FROM  " . DB_WARPIT_WEBCATI_BASE. "._Plugin_label WHERE id_plugin = " . $_POST['id_plugin'];
        $res= $db->SQLexecute($sql);
    
        $defaultValues = array();
        while ($dat = $db->fetchAssoc($res))
        {
            $defaultValues[] = $dat['name'] . " = '" . $dat['default_value'] . "'";    
        }
        
        $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE. "._plugin_" . $_POST['pluginName'] . " 
                SET id_project = " . $_POST['projectId'] . ", id_question = " . $_POST['questionId'] . ", " . implode(',', $defaultValues) .     
                "ON DUPLICATE KEY UPDATE id_project = " . $_POST['projectId'] . ", id_question = " . $_POST['questionId'] . ", deleted = 0";
         print $sql;
        $db->SQLexecute($sql);
    }
   
    echo "{ success : true }";
    exit;
}
elseif($_POST['action'] == "RemoveSettings")
{
    $sql = "DELETE FROM " . DB_WARPIT_WEBCATI_BASE . "._plugin_". $_POST['pluginName'] . " 
        WHERE id_question = " . $_POST['questionId'] . "
        AND id_project = " . $_POST['projectId'];
        
        //print $sql;
        
        $db->SQLexecute($sql);

        echo "{ success : true }";
        exit;
    
}
elseif($_POST['action'] == "GetProjects")
{
    $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "._Projects";
    if(isset($_POST['filter']))
        $sql .= " WHERE proj_name LIKE '%" . mysql_real_escape_string($_POST['filter']) . "%'";
   // print $sql;
    $res= $db->SQLexecute($sql);
    
    $projects = array();
    while ($dat = $db->fetchAssoc($res)) 
    {
            $project = "{ text: '" . $dat['proj_name'] . "', projectId: '" . $dat['id'] ."', projectName: '" . $dat['proj_name'] . "' }";
           /* $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI . "." . $dat['proj_name'] . " WHERE id_plugin = " . $_POST['pluginId'] . " AND id <> ". $_POST['questionId'];
         /*  // print $sql;
            $res3 =  $db->SQLexecute($sql);
            
            if($db->NumRows($res3) > 0)
            {
                $project .= "{ text: '" . $dat['proj_name'];
                $project .=  "', children: [";
            }
            
            $questions = array();
            while ($dat1 = $db->fetchAssoc($res3))
            {
               // print $dat1['q_name'];
               $questions[] .= "{ text: '" . $dat1['q_name']  . "', projectId: " . $dat['id'] . ", questionId:" . $dat1['id'] . ", leaf:true }";
            }
            $project .= implode(',', $questions);
            $project .= "]}";*/
            $projects[] = $project;
        }  
        print "[" . implode(',', $projects) . "]";
    exit;
}
elseif($_POST['action'] == "GetQuestions")
{
    $projectName = $_POST['projectName'];
    $projectId = $_POST['projectId'];

    $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI . "." . $projectName . " WHERE id_plugin = " . $_POST['pluginId'];
    try{
        
    
        $res= $db->SQLexecute($sql);
        $questions = array();
        while ($dat = $db->fetchAssoc($res))
        {
            $questions[] .= "{ text: '" . $dat['q_name']  . "', leaf:true, questionId: " . $dat['id'] . ", projectId: '" . $projectId ."'}";
        }
        print "[" . implode(',', $questions) . "]";
        //print $sql;
    }catch(Exception $e)
    {
        print $e;
    }
    exit;
}
elseif($_POST['action'] == "GetOptions")
{
    $idProject = $_POST['projectId'];
    $idQuestion = $_POST['questionId'];
    $idPlugin = $_POST['pluginId'];
    
  
    
    $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "._Plugin WHERE id_plugin = " . $idPlugin;
    $res = $db->SQLexecute($sql);
    $zad = $db->fetchAssoc($res);
    
    $tableName = "_plugin_" . $zad['plugin_path'];
    
    $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "." . $tableName . " WHERE id_question = " . $idQuestion . " AND id_project = " . $idProject . " AND deleted = 0";
    $res = $db->SQLexecute($sql);
   
    // print $sql;
    
    $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "._Plugin_label  WHERE id_plugin = " . $idPlugin;
    if(isset($_POST['srcquery']))
    {
        $sql .= " AND label LIKE '%" . mysql_real_escape_string($_POST['srcquery']) ."%'";
    }
    $res2 = $db->SQLexecute($sql);
    
   
    
    while ($dat = $db->fetchAssoc($res2))
    {
        $labels[$dat['name']] = $dat['label'];
    }
   // print $sql;
    //print_r($labels);
    $dat = $db->fetchAssoc($res);
    foreach ($dat as $key => $value)
    {
        
        if(array_key_exists($key, $labels))
        {
            $data2['option'] = $key;
            $data2['optionLabel'] = $labels[$key];
            $data2['value'] = $value;
            $data2['tableName'] = $tableName;
            $data2['copy'] = FALSE;
            $dataResult [] = $data2;
        }
    }
    
    $result['success'] = TRUE;
    $result['results'] = sizeof($dataResult);
    $result['rows'] = $dataResult;
    
    print json_encode($result);
    
    exit;
}
elseif($_POST['action'] == "CopySettings")
{   
    //$data = stripslashes($_POST['data']);
    $data = $_POST['data'];
    $data = json_decode($data, TRUE);
    
    $tableName = $_POST['table'];
    
    
    foreach($data as $val)
        foreach($val as $key => $value)
            $tmpSql[] = $key . " = '" . mysql_real_escape_string(stripcslashes($value)) . "'";
        
    $tmpSql[] = "id_project = '" . $_POST['projectId'] . "'";
    $tmpSql[] = "id_question = '" . $_POST['questionId']. "'"; 

    $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . ".". $tableName ." 
            SET " . implode(",", $tmpSql) ." ON DUPLICATE KEY UPDATE " . implode(",", $tmpSql) . ", deleted = 0";
    //  print $sql;
    $db->SQLexecute($sql);
    //print $sql;
    exit;
    
    
    
    
}

?>