<?php

session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
require_once ("connection_sql.php");



////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
header('Content-Type: text/xml');



/////////////////////////////////////// GetValue //////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Registration /////////////////////////////////////////////////////////////////////////
if (isset($_GET["Command"])) {
if ($_GET["Command"] == "select_permission") {
    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";



    $ResponseXML = "";


    $ResponseXML .= " <salesdetails>";

    $ResponseXML .= "<balance_table><![CDATA[<table width=\"100%\" border=\"0\" class=\"form-matrix-table\">";
    $ResponseXML .= "<table  width=\"100%\" border=\"1\" class=\"form-matrix-table\">";

    $i = 1;
    $ResponseXML .= " <tr>
      <th scope=\"col\">Form Name</th>
      <th scope=\"col\">Category</th>
	  <th scope=\"col\">View</th>
      <th scope=\"col\">Feed</th>
      <th scope=\"col\">Delete</th>
      <th scope=\"col\">SP Permission</th>
      
    </tr>";

    $sql1 = "Select * from a_doc_acc order by docid";
     foreach ($conn->query($sql1) as $row1) {
        $chkview = "chkview" . $i;
        $chkfeed = "chkfeed" . $i;
        $chkmod = "chkmod" . $i;
        $chkprice = "chkprice" . $i;
        

        $sql = "Select * from a_userpermission_acc   where username='" . trim($_GET["user_name"]) . "' and docid=" . $row1["docid"] . "";
        $result = $conn->query($sql);
       if ($row = $result->fetch()) {
            $ResponseXML .= "	<tr>
      					<td>" . $row1["docid"] . ". " . $row1["docname"] . "</td>
						<td>" . $row1["grp"] . "</td>";
            if ($row["doc_view"] == 1) {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkview . "\" id=\"" . $chkview . "\" checked=\"checked\" /></td>";
            } else {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkview . "\" id=\"" . $chkview . "\" /></td>";
            }

            if ($row["doc_feed"] == 1) {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkfeed . "\" id=\"" . $chkfeed . "\" checked=\"checked\"/></td>";
            } else {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkfeed . "\" id=\"" . $chkfeed . "\" /></td>";
            }

            if ($row["doc_mod"] == 1) {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkmod . "\" id=\"" . $chkmod . "\"checked=\"checked\" /></td>";
            } else {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkmod . "\" id=\"" . $chkmod . "\" /></td>";
            }
            if ($row["price_edit"] == 1) {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkprice . "\" id=\"" . $chkprice . "\" checked=\"checked\"/></td>";
            } else {
                $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkprice . "\" id=\"" . $chkprice . "\" /></td>";
            }
            


            $ResponseXML .= "</tr>";
        } else {
            $ResponseXML .= "	<tr>
      					<td>" . $row1["docid"] . ". " . $row1["docname"] . "</td>
						<td>" . $row1["grp"] . "</td>";

            $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkview . "\" id=\"" . $chkview . "\" /></td>";
            $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkfeed . "\" id=\"" . $chkfeed . "\" /></td>";
            $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkmod . "\" id=\"" . $chkmod . "\" /></td>";
            $ResponseXML .= "<td align=\"center\"><input type=\"checkbox\" name=\"" . $chkprice . "\" id=\"" . $chkprice . "\" /></td>";
          
            $ResponseXML .= "</tr>";
        }
        $i = $i + 1;
    }

    $ResponseXML .= "   </table>]]></balance_table>";
    $ResponseXML .= "<mcount><![CDATA[" . $i . "]]></mcount>";

    $ResponseXML .= " </salesdetails>";
    echo $ResponseXML;
}
}

if (isset($_POST["Command"])) {
    if ($_POST["Command"] == "save_inv") {
        $pass = "";

        $sql1 = "select * from a_user_mast_acc where user_name='" . $_POST["user_name"] . "'";
       $result1 = $conn->query($sql1);
    if ($row1 = $result1->fetch()) {
            $pass = $row1["user_pass"];
            $admin = $row1["user_level"];
            $dev =0;
        }

        $sql1 = "delete from a_userpermission_acc where username='" . $_POST["user_name"] . "'";
         $conn->exec($sql1);

        $i = 1;
        $sql1 = "Select * from a_doc_acc order by docid";
         foreach ($conn->query($sql1) as $row1) {
            
            $chkview = "chkview" . $i;
            $chkfeed = "chkfeed" . $i;
            $chkmod = "chkmod" . $i;
            $chkprice = "chkprice" . $i;
          

            if ($_POST[$chkview] == "true") {
                $view = 1;
            } else {
                $view = 0;
            }

            if ($_POST[$chkfeed] == "true") {
                $feed = 1;
            } else {
                $feed = 0;
            }

            if ($_POST[$chkmod] == "true") {
                $mod = 1;
            } else {
                $mod = 0;
            }

            if ($_POST[$chkprice] == "true") {
                $price = 1;
            } else {
                $price = 0;
            }

            

            $sql = "insert into a_userpermission_acc(username, userpass, grp, docid, doc_view, doc_feed, doc_mod, price_edit, admin, dev) values 
            ('" . $_POST["user_name"] . "', '" . $pass . "', '" . $row1["grp"] . "', '" . $row1["docid"] . "', '" . $view . "', '" . $feed . "', '" . $mod . "', '" . $price . "', '" . $admin . "', '" . $dev . "')";  
            $result = $conn->query($sql);

            $i = $i + 1;
        }
        
        $sqllog = "insert into a_entry_log(refno, username, docname, trnType, stime, sdate,company) values 
        ('" . $_POST["user_name"] . "', '" . $_SESSION["CURRENT_USER"] . "', 'User Privilage', 'Update', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "','".$_SESSION['company']."')";
        $resultlog = $conn->query($sqllog);
        echo "Saved";
        
          $conn->commit();
    }
}
?>