<?php
/*=======================================================================
 *
 * language.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/

include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "f_lng_num"));

if ($f_lng_num != "" && is_numeric($f_lng_num)) :
   $languageSelected = $f_lng_num;
endif;

getLanguage(__FILE__);
$pageTitel = getLngt("SPRACHEN");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Select user-type for mode of security check 
$userType = "";
if (isset($emp_id) && isset($hq_id) && isset($usr_id)) :
   $userType = getFieldValueFromId("user","usr_id",$usr_id,"usr_type");
endif;

// Toggles list colors
function getTableColor($x, $y) {
	$bgCol = "";
  if ($x % 2 == 0 && $y == 0) : $bgCol = "#AAAAFF"; endif;
  if ($x % 2 == 0 && $y == 1) : $bgCol = "#AAAAFF"; endif;
  if ($x % 2 == 1 && $y == 0) : $bgCol = "#CCCCFF"; endif;
  if ($x % 2 == 1 && $y == 1) : $bgCol = "#CCCCFF"; endif;
  return $bgCol;
}

// *** Actions ***
if ($f_act == "saveLanguage") :
   if ($userType != "") :
      if (is_numeric($f_lng_num)) :
         if (existsEntry("parameter",array("par_key","SYSTEM_LANGUAGE_DEFAULT","emp_id",$emp_id,"hq_id",$hq_id))) :
            updateStmt("parameter", "emp_id", $emp_id, array("par_value", $f_lng_num), " par_key = 'SYSTEM_LANGUAGE_DEFAULT' AND hq_id = '" . $hq_id . "'");
         else:
            insertStmt("parameter", array("par_key", "SYSTEM_LANGUAGE_DEFAULT", "emp_id", $emp_id, "par_value", $f_lng_num, "hq_id", $hq_id));
         endif;
         $languageSelected = $f_lng_num;
      endif;
   endif;
endif;


// Statement for languages
$sqlquery = "SELECT lng.lng_num, lng.lng_text"
          . " FROM phoenix_special.language AS lng"
          . " ORDER BY lng.lng_num";

$result = $db->query($sqlquery);
if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

 // Table with header
$numOfRows = 1;
$lineToggler = 0;
$tableOfRows = "";
while ($row = $result->fetch_assoc()):
      $numOfRows++;

   	  if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
      $cellColor = getTableColor($numOfRows, $lineToggler);
	   
      $tableOfRows .= "<tr>\n";      
      $tableOfRows .= "<td align=\"center\" bgcolor=\"" . $cellColor ."\">&nbsp;<a href=\"javascript:finishPage('" . ec($row["lng_num"]) . "');\">" . getLngt($row["lng_text"]) . "</a></td>\n";
      $tableOfRows .= "</tr>\n";
endwhile;
$result->free();	      

?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">

<script type="text/javascript">
<!--
function initPage() {
	opener.submit();
};

function finishPage(f_lng_num) {
  document.forms[0].f_act.value = 'saveLanguage';
  document.forms[0].f_lng_num.value = f_lng_num;  
  document.forms[0].submit();
};
-->
</script>
</head>

<body onLoad="initPage();">

<div>

  <form action="../admin/language.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="f_lng_num" value="">

  <br><br>

  <table class="f12np1" border="0" cellpadding="0" width="100%">
    <?php echo $tableOfRows ?>
  </table>
 
  <br><br>
 
  <table border="0" cellpadding="0" width="100%">
    <tr>
      <td align="center">
        <input type="button" value="<?php echo getLngt("Schließen"); ?>" onClick="self.close()">
      </td>
    </tr>
  </table>

</form>
</div>
</body>

</html>