<?php
/*=======================================================================
 *
 * inc_user.inc.php
 *
 * Autor: Marc Vollmann
 *  
 =======================================================================*/   


include_once ("../include/html.inc.php");


// Reset all user (employees) states
// $retMode : Return mode of the result ("" = Array / "1" = HTML list)
function resetUserStates () {
   global $db, $PHP_SELF, $hq_id, $emp_id, $usr_id;

   $contextArr = array("usr_state_dispo");
   $contextArrLen = count($contextArr);
   for ($i = 0; $i < $contextArrLen; $i++) :
       if (existsEntry("genericdatacontainer",array("gdc_obj_type","usr","gdc_obj_id",$usr_id,"gdc_gen_fieldname",$contextArr[$i]))) :
          updateStmt("genericdatacontainer","gdc_obj_type","usr",array("gdc_content", "0", "gdc_context", ""),"gdc_obj_id = '" . $usr_id . "' AND gdc_gen_fieldname = '" . $contextArr[$i] . "'");
       endif;
   endfor;
}


// Gets the user (employees) having activated "Dispo" (button)
// $retMode : Return mode of the result ("" = Array / "1" = HTML list)
function getUserByState ($context, $content = "1", $retMode = "", $title = "") {
   global $db, $PHP_SELF, $hq_id;

   $retArray = array();

   $sqlquery = "SELECT usr.usr_id, usr.usr_name, usr.usr_firstname"
                      . " FROM user AS usr, genericdatacontainer AS gdc"
                      . " WHERE gdc.gdc_obj_type = 'usr' AND gdc.gdc_gen_fieldname = '" . $context . "' AND gdc.gdc_content = '" . $content . "' AND"
                      . "       gdc.gdc_obj_id = usr.usr_id AND usr.hq_id = '" . $hq_id . "' AND usr.usr_type = '1'"
                      . " ORDER BY usr.usr_name, usr.usr_firstname";

   $result = $db->query($sqlquery);
   if ($db->connect_errno) die ("$PHP_SELF: <br>$sqlquery<br>" . $db->connect_error);

   // Generate output with checkboxes
   $output = "<div style=\"margin: 20px;\">\n";
   $output .= "<table>\n";
   $output .= "  <tr>\n";
   $output .= "    <td colspan=\"2\"><span class=\"f10bp1_blue\">" . getLngt($title). "</span></td>\n";
   $output .= "  </tr>";
   $count = 0;
   $lineToggler = 0;
   while ($row = $result->fetch_assoc()):
         $retArray[] = array($row["usr_id"], $row["usr_name"], $row["usr_firstname"]);

   	     if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
         $cellColor = getListColorGeneric($count, $lineToggler);

         $output .= "  <tr>\n";
         $output .= "    <td bgcolor=\"" . $cellColor ."\">" . $row["usr_firstname"]. "</td>\n";
         $output .= "    <td bgcolor=\"" . $cellColor ."\">" . $row["usr_name"]. "</td>\n";
         $output .= "  </tr>";

         $count++;
   endwhile;
   if ($count == 0) :
       $output .= "<tr>\n";
       $output .= "  <td colspan=\"2\"><span style=\"color:red\">" . getLngt("Zur Zeit ist die " . $title . " nicht besetzt."). "</span></td>\n";
       $output .= "</tr>";
   endif;
   $output .= "</table>\n";
   $output .= "</div>\n";
   $result->free();   

   if ($retMode == "1") :
      return $output;
   else :
      return $retArray;
   endif;
}


// Get ticker messages of the user to have to read
function getUserMessages ($retMode = "", $tifIdFrom = "0") {
   global $db, $PHP_SELF, $hq_id, $emp_id;

   $tickerArray = array();
   $outputJs = "";
   $output = "";
   $currentTime = getDateTime("0");

   $whereClause = "";
   if ($tifIdFrom != "" && is_numeric($tifIdFrom) && $tifIdFrom > "0") :
      $whereClause = " tif.tif_id > '" . $tifIdFrom . "' AND ";
   endif;

   $sqlquery = "SELECT tif.tif_id, tif.tif_createtime, tif.tif_endtime, tif.tif_text, tif.tif_subject, tif.emp_id_creator, tif.hq_id"
               . " FROM phoenix_group.tickerforum AS tif"
               . " WHERE tif.tif_starttime <= '" . $currentTime . "' AND '" . $currentTime . "' <= tif.tif_endtime AND"
               .         $whereClause
               . "       (tif.emp_id = '" . $emp_id . "' OR tif.emp_id = '0') AND"
               . "       (tif.hq_id = '" . $hq_id . "' OR tif.hq_id = '0')"
               . " ORDER BY tif.tif_createtime DESC";
   $result = $db->dbQ($sqlquery);

   $output = "<div style=\"margin: 20px;\">\n";
   $output .= "<table>\n";
   $output .= "  <tr>\n";
   $output .= "    <td colspan=\"3\"><span class=\"f10bp1_blue\">" . getLngt("Mitteilungen"). "</span></td>\n";
   $output .= "  </tr>";

   $count = 0;
   $lineToggler = 0;
   while ($row = $result->fetch_assoc()):
         $outputText = trim($row["tif_text"]);
         $outputText = str_replace("'", "", $outputText);
         $outputText = str_replace('"', '', $outputText);
         $outputText = nl2br($outputText);

         $tickerText = trim($row["tif_text"]);
         $tickerText = str_replace("\n", " ", $tickerText);
         $tickerText = str_replace("\r", " ", $tickerText);
         $tickerText = str_replace("'", "", $tickerText);
         $tickerText = str_replace('"', '', $tickerText);
         if ($tickerText != "") :
            $tickerArray[] = "+++ " . $tickerText . " +++";
            
   	        if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
            $cellColor = getListColorGeneric($count, $lineToggler);

            if ($count == 0) :
               $output .= "  <tr>\n";
               $output .= "    <td style=\"vertical-align:top;\" bgcolor=\"" . $cellColor ."\"><b>" . "Ersteller" . "</b></td>\n";
               $output .= "    <td style=\"vertical-align:top;\" bgcolor=\"" . $cellColor ."\"><b>" . "Zeitpunkt" . "</b></td>\n";
               $output .= "    <td style=\"vertical-align:top;\" bgcolor=\"" . $cellColor ."\"><b>" . "Nachricht" . "</b></td>\n";
               $output .= "  </tr>";
            endif;
            
            $tifCreattime = $row["tif_createtime"];
            $empIdCreator = $row["emp_id_creator"];
            $usrIdCreator = getFieldValueFromId("employee","emp_id",$empIdCreator,"usr_id");
            $usrNameCreator = getFieldValueFromId("user","usr_id",$usrIdCreator,"usr_name");
            $usrFirstnameCreator = getFieldValueFromId("user","usr_id",$usrIdCreator,"usr_firstname");
            
            $output .= "  <tr>\n";
            $output .= "    <td style=\"vertical-align:top;\" bgcolor=\"" . $cellColor ."\">" . $usrFirstnameCreator . "&nbsp;" . $usrNameCreator . "</td>\n";
            $output .= "    <td style=\"vertical-align:top;\" bgcolor=\"" . $cellColor ."\">" . $tifCreattime . "</td>\n";
            $output .= "    <td style=\"vertical-align:top;\" bgcolor=\"" . $cellColor ."\">" . $outputText . "</td>\n";
            $output .= "  </tr>";
            
            $count++;
         endif;
   endwhile;
   if ($count == 0) :
       $output .= "<tr>\n";
       $output .= "  <td colspan=\"3\"><span style=\"color:red\">" . getLngt("Zur Zeit liegen keine Mitteilungen vor."). "</span></td>\n";
       $output .= "</tr>";
   endif;
   $output .= "</table>\n";
   $output .= "</div>\n";
   // $result->free();
   mysqli_free_result($result);
   
   $tickerArrayLen = count($tickerArray);
   $tmpTicker = "";
   for ($j = 0; $j < $tickerArrayLen; $j++) :
       $tmpTicker .= "\"" . $tickerArray[$j] . "\"";
       if ($j < ($tickerArrayLen - 1)) :
          $tmpTicker .= ",";
       endif;
   endfor;

   if ($retMode == "1") :
      if ($tmpTicker == "")
        $tmpTicker = "\"\"";
      $outputJs .= "tl = new Array (" . $tmpTicker . ");";
      $outputJs .= "max = tl.length;";
      return $outputJs;
   elseif ($retMode == "2") :
      return $tickerArrayLen;
   elseif ($retMode == "3") :
      return $output;
   else :
      return $tickerArray;
   endif;    
}
?>
