<?php

if(!isset($_SESSION)) session_start();

include("loginCheckPoint.php");
include_once("authorizations.php");
require_once("demosettings.php");
include_once 'write_log.php';
include("parseUtils.php");
include("slotToCaption.php");
//require_once("sparql.php");
//include_once 'searchWord.php';
$mysqli = sqlCreateConnection("manageConf");

if (isset($_REQUEST["insertData"])) {
    $operation = $_REQUEST["operation"];
    $page = $_REQUEST["page"];
    $data = $_REQUEST["ListPageColumn"];
    if ($page == "ListFiles") {
        if ($operation == "column") {
            $details = "";
            foreach ($data as $v) {
                $details .= $v . ";";
            }
            $res = sqlQuery($mysqli, "DELETE FROM configurationmanage WHERE page=\"$page\" AND operation=\"$operation\"");
            $sql = "INSERT INTO configurationmanage (page,operation,details) 
					VALUES (\"$page\",\"$operation\",\"$details\")";
            sqlQuery($mysqli, $sql);
        }
    }
} else if (isset($_REQUEST["deleteEntity"])) {
    $sql = "DELETE FROM entitiesmanage WHERE entity=\"" . $_REQUEST["entity"] . "\" AND name=\"" . $_REQUEST["name"] . "\"";
    $res = $mysqli->query($sql);
} else if (isset($_REQUEST["getData"])) {
    $sql = "SHOW COLUMNS FROM files";
    $res = $mysqli->query($sql);
    //////
    //  arr for manualy sort column - need to change the order on slotToCaption.php
    /////
    $arr = array();
    if (!empty($res)) {
        while ($row = $res->fetch_object()) {
            if (array_key_exists($row->Field, $listPageColumn)) {
            //$response["rows"][] = array("name" => $listPageColumn[$row->Field]);
                $arr[$row->Field] = $listPageColumn[$row->Field];
            }
        }
    }
    foreach ($listPageColumn as $k => $v) {
        if (array_key_exists($k, $arr)) {
            $response["rows"][] = array("name" => $v);
        }
    }
    if (ob_get_length()) {
        ob_clean();
    }
    echo json_encode($response);
} else if (isset($_REQUEST["getListColumns"])) {
    $sql = "SELECT details FROM configurationmanage WHERE page=\"ListFiles\" AND operation=\"column\"";
    $res = $mysqli->query($sql);
    if (!empty($res)) {
        $row = $res->fetch_object();
        $e = explode(";", $row->details);
        foreach ($e as $v) {
            if (strlen($v) > 0) {
                $response["rows"][] = array("name" => $v);
            }
        }
    }
    if (ob_get_length()) {
        ob_clean();
    }
    sqlClose($mysqli, "manageCon");
    echo json_encode($response);
}