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

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

$database = DB_WARPIT_WEBCATI_BASE;;

$table = "_plugin_single_respons_with_image";

if($_POST['action'] == "get")
{
    $sql = "SELECT * FROM " .  $database . "." . $table . "  
    WHERE id_project = " . $_POST['projectId'] . " AND id_question = " . $_POST['questionId'] . " AND deleted = 0";
    
    $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'] == "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 " . $database . ".". $table ." 
            SET " . implode(",", $tmpSql) ." ON DUPLICATE KEY UPDATE " . implode(",", $tmpSql) . ", deleted = 0";
    //  print $sql;
    $db->SQLexecute($sql);
    exit;
}
?>