<?php
  /**
   * MiniClick
   * Gestionnaire de tableau collaboratif
   * Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
   *
   * @package miniclick
   * @version 5.0.0
   */
  //Test de l'appel par le main ou un descendant 
if($GLOBALS['main'] != 1)
  {
    die("Cannot acces directly to this page");
  }
?>
<script type="text/javascript">
function checkAll()
{
  var inpNodes = document.getElementById('tableau1').getElementsByTagName('input');
  for(var i in inpNodes)
    {
      var inp = inpNodes[i];
      if (inp.type == 'checkbox'){
        inp.checked = true;
      }
    }
}
</script>
<?php

session_start();
create_droit_table($_SESSION['app_id']);

function create_droit_table($app_id)
{
  $query_client_id = "SELECT Client_Id FROM ".DB_NAME.".USER WHERE User_Id='".$_SESSION['user_id']."'";
  $client_ids = mysql_query($query_client_id);
  $client_id = mysql_fetch_object($client_ids);
  $cid = $client_id->Client_Id;

  $client_query = "SELECT * FROM ".DB_NAME.".USER WHERE "
    .DB_NAME.".USER.Client_Id='$cid' ORDER BY Nom,Prenom";


  $dest=htmlentities($_SERVER['REQUEST_URI']);

  if($_POST['action'] == "VAL")
    {
      valide_table($app_id);
    }
  echo "<div id=\"tableau_droit\">";
  $headers = array("User");
  //Description de la table
  $fields = mysql_query("SELECT * FROM ".DB_NAME.".APP");
  while($field = mysql_fetch_object($fields))
    {
      $nom = $field->Nom;
      if($nom != "Scripts" && $nom != "Vues" && $nom != "Config")
        {
          array_push($headers,$nom);
        }
    }
  //Debut de la table 
  echo "<form id=\"droit\" name=\"droit\" action=\"".stripslashes($dest)."\" method=\"post\">";
  echo "<input type=\"hidden\" id=\"action\" name=\"action\" />";
  echo "<table id=\"tableau\" border='0' cellspacing='1' cellpadding='0'>";
  echo "<tr>";
  for($i = 0 ; $i < count($headers) ; $i++)
    {
      echo "<th>".$headers[$i]."</th>";
    }
  
  $users = mysql_query($client_query);

  $back = 0;
  while($user = mysql_fetch_object($users))
    {  
      $back = 1 - $back;
      if($back==1)
        {
          $colorback = "style = \"background-color: rgb(255, 255, 255);\""; 
        }else{
        $colorback = "style = \"background-color: rgb(225, 225, 225);\"";
      }
      echo "<tr $colorback>";
      echo "<td>$user->Nom $user->Prenom</td>";
      for($i = 1 ; $i < count($headers) ; $i++)
        {
          echo "<td>";
          $perms = mysql_query("SELECT * FROM ".DB_NAME.".APP_ACCESS,".DB_NAME.".APP WHERE User_Id='"
                               .$user->User_Id."' AND ".DB_NAME.".APP.App_Id = "
                               .DB_NAME.".APP_ACCESS.App_Id AND ".DB_NAME.".APP.Nom='$headers[$i]'");
          echo "<input type=\"checkbox\" size=\"3\" name='".$headers[$i]."$user->User_Id'";
          if($perm = mysql_fetch_object($perms))
            {
              echo " checked ";
            }
          echo " />";
          echo "</td>";
        }
      echo "</tr>";
    }
  echo "</table>";
  echo "<a href='javascript:document.getElementById(\"action\").value=\"VAL\";"
    ."document.getElementById(\"droit\").submit();' >Valider</a>";
  echo "</form>";
  echo "<a href=\"javascript:checkAll();\">Tout cocher</a>";
  echo "</div>";
}



function valide_table($app_id)
{

  $query_client_id = "SELECT Client_Id FROM ".DB_NAME.".USER WHERE User_Id='".$_SESSION['user_id']."'";
  $client_ids = mysql_query($query_client_id);
  $client_id = mysql_fetch_object($client_ids);
  $cid = $client_id->Client_Id;

  $headers = array("User");
  //Description de la table
  $fields = mysql_query("SELECT * FROM ".DB_NAME.".APP");
  while($field = mysql_fetch_object($fields))
    {
      $nom = $field->Nom;
      if($nom != "Scripts" && $nom != "Vues" && $nom != "Config")
        {
          array_push($headers,$nom);
        }
    }

  $client_query = "SELECT * FROM ".DB_NAME.".USER WHERE ".DB_NAME.".USER.Client_Id='$cid' ORDER BY Nom,Prenom";
  $users = mysql_query($client_query);

  while($user = mysql_fetch_object($users))
    {  
      $delete_access = "DELETE FROM ".DB_NAME.".APP_ACCESS WHERE User_Id='".$user->User_Id."'";
      mysql_query($delete_access);
      for($i = 1 ; $i < count($headers) ; $i++)
        {
          if($_POST[$headers[$i].$user->User_Id] == "on")
            {
              $query_module = "SELECT App_Id FROM ".DB_NAME.".APP WHERE Nom='".$headers[$i]."'";
              $modules = mysql_query($query_module);
              $module = mysql_fetch_object($modules);
              $insert_module = "INSERT INTO ".DB_NAME.".APP_ACCESS VALUES ('".$user->User_Id."','".$module->App_Id."',1)";
              mysql_query($insert_module);
            }
        }
    }
}
?>
