<?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_gallery";

if($_POST['action'] == "get")
{
    $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 = 6";
        $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']);
    
    foreach($data as $key => $value)
    {
        $tmpSql[] = $key . " = '" . $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";
    //  print $sql;
    $db->SQLexecute($sql);
    exit;
}else if($_POST['action'] == 'getImageList')
{
        $sql = "SELECT gi.path, gi.id_plugin_gallery_image
            FROM " . DB_WARPIT_WEBCATI_BASE . "._plugin_gallery_image as gi
            JOIN " . DB_WARPIT_WEBCATI_BASE . ". _plugin_gallery as g
            ON gi.id_plugin_gallery = g.id_plugin_gallery
            WHERE id_question = " . $_POST['questionId'] . " AND id_project = " . $_POST['projectId'];
        
        $res= $db->SQLexecute($sql);
        while ($dat = $db->fetchAssoc($res)) {  
            $data[] = $dat;
        }
        
    
    $result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    print json_encode($result);
    exit;
}else if($_POST['action'] == 'defaultButtons')
{
        foreach($_POST as $key => $val)
        {
            if(strpos($key, 'button_image'))
            {
                $tmpSql[] = $key . " = ''";
            }
        }
    
        $sql = "UPDATE " . DB_WARPIT_WEBCATI_BASE . ".". $table ." SET " . implode(",", $tmpSql) . "
            WHERE id_question = " . $_POST['questionId'] . " AND id_project = " . $_POST['projectId'];
        $res= $db->SQLexecute($sql);
    
    $result['success'] = TRUE;
    $result['results'] = sizeof($data);
    $result['rows'] = $data;
    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;
}
else if($_POST['action'] == 'update')
{
    $sql = "UPDATE " . DB_WARPIT_WEBCATI_BASE . "."  .$_POST['table']." SET ".$_POST['field']." = '".$_POST['value']."' WHERE ".$_POST['idField']." = ".$_POST['id'];
   // print $sql;
   try{
    $db->SQLexecute($sql);
   }catch(Exception $e)
   {
    var_dump($db);
    print $e;
       
   }
    
    echo "{ success : true }";
    exit;
}
else if($_POST['action'] == 'insertPath')
{
    //$sql = "UPDATE " . DB_WARPIT_WEBCATI_BASE . "."  .$_POST['table']." SET ".$_POST['field']." = '".$_POST['value']."' WHERE ".$_POST['idField']." = ".$_POST['id'];
   $sql = "SELECT * FROM " .  DB_WARPIT_WEBCATI_BASE . "." . $table . "  
    WHERE id_project = " . $_POST['projectId'] . " AND id_question = " . $_POST['questionId'] . " AND deleted = 0 LIMIT 1";
    
    $res= $db->SQLexecute($sql);
    $dat = $db->fetchAssoc($res);
    $id = $dat['id_plugin_gallery'];
    
    // če nastavitve še ne obstajajo ustvarimo nov zapis
    if($db->NumRows($res) == FALSE)
    {
          $tmpSql[] = "id_project = '" . $_POST['projectId'] . "'";
          $tmpSql[] = "id_question = '" . $_POST['questionId']. "'"; 
          $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . ".". $table ." SET " . implode(',', $tmpSql); 
          $db->SQLexecute($sql);
          $id = $db->GetLastInsertId();
    }
                
    $sql = "INSERT INTO " . DB_WARPIT_WEBCATI_BASE . "._plugin_gallery_image SET path = '" . $_POST['path'] . "', id_plugin_gallery = " . $id;
    
    //print $sql;
   try{
    $db->SQLexecute($sql);
   }catch(Exception $e)
   {
    var_dump($db);
    print $e;
       
   }
    
    echo "{ success : true }";
    exit;
}else if($_POST['action'] == 'delete_image')
{
    $sql = "DELETE FROM  " . DB_WARPIT_WEBCATI_BASE . "._plugin_gallery_image WHERE id_plugin_gallery_image = " . $_POST['id'];
   // print $sql;
   try{
    $db->SQLexecute($sql);
   }catch(Exception $e)
   {
    var_dump($db);
    print $e. "</br>";
    print $sql;
       
   }
    
    echo "{ success : true }";
    exit;
}
?>