<?php
//ini_set("display_errors","1");

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


$table = "_plugin_dragandrop";

$sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "._Plugin  WHERE plugin_path = 'dragandrop'";
$res = $db->SQLexecute($sql);
$dat = $db->fetchAssoc($res);

$idPlugin = $dat['id_plugin'];



if($_POST['action'] == "get")
{
    $idProject = $_POST['projectId'];
    $idQuestion = $_POST['questionId'];
    $src = "1";
    
    if(strlen($_POST['srcquery']) > 0)
    {
        $srcFields = json_decode($_POST['srcfields']);
        foreach($srcFields as $field)
        {
            $srcQuery[] = "`" . $field . "`" . " LIKE '%". mysql_real_escape_string($_POST['srcquery']) ."%'";
        }
        $src = implode(" OR ", $srcQuery);
    }
  
    
 /*   $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "._Plugin WHERE id_plugin = " . $idPlugin;
    $res = $db->SQLexecute($sql);
    $zad = $db->fetchAssoc($res);*/
    
    //$tableName = "_plugin_slider";
    
    $sql = "SELECT * FROM " . DB_WARPIT_WEBCATI_BASE . "." . $table . " 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 . " AND (" . $src . ")  ORDER BY `group` ASC";
    $res2 = $db->SQLexecute($sql);
   
    
    while ($dat = $db->fetchAssoc($res2))
    {
        $labels[$dat['name']] = $dat['label'];
        $group[$dat['name']] = $dat['group'];
        $types[$dat['name']] = $dat['type'];
    }
    
   // print $sql;
    //print_r($labels);
    $dat = $db->fetchAssoc($res);
    foreach ($dat as $key => $value)
    {
        
        if(array_key_exists($key, $labels))
        {
            $data2['option'] = $key;
            $data2['label'] = $labels[$key];
            $data2['value'] = $value;
            $data2['tableName'] = $tableName;
            $data2['type'] = $types[$key];
            $data2['group'] = $group[$key];
            $dataResult [] = $data2;
        }
    }
    
    $result['success'] = TRUE;
    $result['results'] = sizeof($dataResult);
    $result['rows'] = $dataResult;
    
    print json_encode($result);
    
    exit;
    
    /*$sql = "SELECT * FROM " .  DB_WARPIT_WEBCATI_BASE . "." . $table . "  
    WHERE id_project = " . $_POST['projectId'] . " AND id_question = " . $_POST['questionId'] . " AND deleted = 0";
    
    
    //print $sql;
    $res= $db->SQLexecute($sql);
    while ($dat = $db->fetchAssoc($res)) {  
        $data[] = $dat;
    }
    
    if(count($data) == 0)
    {
        $sql = "SELECT * FROM " .  DB_WARPIT_WEBCATI_BASE . "._Plugin_label
        WHERE id_plugin = 9 ORDER BY order_by";
        $res= $db->SQLexecute($sql);
        while ($dat = $db->fetchAssoc($res)) {  
            $tmpData[$dat['name']] = $dat['default_value'];
        }
        
        $data[] = $tmpData;
    }
      //  print $sql;
    /*$result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    print json_encode($result);*/
    exit;
}
else if($_POST['action'] == "Save")
{
    
    $data = json_decode($_POST['data']);
   /* print "<pre>";
    print_r($data);
    print "</pre>";
    */foreach($data as $obj)
    {
        $tmpSql[] = $obj->option . " = '" . $obj->value . "'";
    }
    $tmpSql[] = "id_project = '" . $_POST['projectId'] . "'";
    $tmpSql[] = "id_question = '" . $_POST['questionId']. "'"; 

    
    $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . ".". $table ." 
            SET " . implode(",", $tmpSql) ." ON DUPLICATE KEY UPDATE " . implode(",", $tmpSql) . ", deleted = 0";
    $db->SQLexecute($sql);
    exit;
}else if($_POST['action'] == 'getGridData')
{
   /* $res[] = array('value' => 21);
    $res[] = array('value' => 21);
    $res[] = array('value' => 21);*/
    
    $inputData = explode(';', $_POST['data']);
    
    if(count($inputData) == 0)
    {
        
    }
    else if(count($inputData) == 1) 
    {
        $steps = intval($inputData[0]);
        for($i = 0; $i < $steps; $i++) $res[] = array('value' => '');
    }
    else 
    {
         if(!is_numeric($inputData[0]))
            return;
         
         $steps = intval($inputData[0]);
         
         $i = 0;
         $first = true;
         foreach($inputData as $val)
         {
             if($first)
             {
                 $first = false;
                 continue;
             }
             
             $res[] = array('value' => $val);    
         }
         
         if(count($res) != $steps)
         {
             $cnt = $steps - count($res);
             for($i = 0; $i < $cnt; $i++) $res[] = array('value' => '');
         }
         
         $res = array_slice($res, 0, $steps);
    }
    
    $result['success'] = TRUE;
    $result['results'] = sizeof($res);
    $result['rows'] = $res;
    print json_encode($result);
    exit;
}
else if($_POST['action'] == 'saveScaleValue')
{
     $tmpSql[] = "id_project = '" . $_POST['projectId'] . "'";
     $tmpSql[] = "id_question = '" . $_POST['questionId']. "'"; 
    
     $sql = "UPDATE " . DB_WARPIT_WEBCATI_BASE . ".". $table . " SET scale_values = '" . addslashes($_POST['data']) ."' WHERE " . implode(' AND ', $tmpSql);
     $db->SQLexecute($sql);
     exit;
}
?>