<?php
/*=======================================================================
 *
 * selection_list.php
 *
 * Autor: Marc Vollmann
 *
 * Generic script to make a selection from a list (tax, branch, etc.)
 *
 =======================================================================*/

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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "generic", "deactivateMenu", "listType", "callOpenerFunction", "orderByFieldNo"));

getLanguage(__FILE__);

$deactivateMenuStatic = "1";
if ($pageTitle == "") : $pageTitel = getLngt("AUSWAHL AUS DER LISTE"); endif;
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
if (!(authCheckEmployeeRights($emp_id, "0") || authCheckEmployeeRights($emp_id, "7"))) : gotoReferer("1"); endif;

if (!isset($listType)) : $listType = ""; endif;

$htmlClass01 = "class=\"smaller\"";   // input,select
$htmlClass02 = "class=\"f7np1\"";     // write

$doCloseWindow = "1";
$returnValueArray = array();
$out = "";
if ($listType != "") :

   $doCloseWindow = "0";
   $sqlquery = "";
   $tableFieldNumOfIdField = 0;
   $tableColFinishPageLink = "0";  // The column the finish page link will appear

   // Initialize value    
   if ($generic == "") : $generic = "1111"; endif;

   if ($listType == "branch") :
      $pageTitel = getLngt("BRANCHEN");
      $orderByFieldArray = array("br_key", "br_name");
      if ($orderByFieldNo != "0" && $orderByFieldNo != "1") :
         $orderByFieldNo = "1";
      endif;
      // $sqlquery = "SELECT br.br_id, br.br_key, br.br_name, br.br_group FROM branch AS br ORDER BY br." . $orderByFieldArray[$orderByFieldNo];
      $sqlquery = "SELECT br.br_id, br.br_key, br.br_name, br.br_group FROM branch AS br WHERE br.br_visible = '1' ORDER BY br." . $orderByFieldArray[$orderByFieldNo];
      $tableHeader = "  <td align=\"center\">" . "<a href=\"javascript:finishSort('0');\">" . getLngt("Schlüssel") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('1');\">" . getLngt("Branche") . "</a></td>";
      $tableFieldArray = array("br_key","br_name");
      $tableAlignArray = array("center","left");
      // $tableColFinishPageLink = "0";
      // $tableFieldNumOfIdField = 0;
      $tableFieldFinishPageArray = array("br_id","br_key","br_name");
      $returnValueArray = array("g_br_id", "g_br_key", "g_br_name");

   elseif ($listType == "tax") :
      $pageTitel = getLngt("STEUERSÄTZE");
      $orderByFieldArray = array("tx_id", "tx_sign", "tx_value");
      if ($orderByFieldNo != "0" && $orderByFieldNo != "1" && $orderByFieldNo != "2") :
         $orderByFieldNo = "0";
      endif;
      $sqlquery = "SELECT tx.tx_id, tx.txt_id, tx.tx_sign, tx.tx_value, tx.tx_remark FROM tax AS tx ORDER BY tx." . $orderByFieldArray[$orderByFieldNo];
      $tableHeader = "  <td align=\"center\">" . "<a href=\"javascript:finishSort('0');\">" . getLngt("Satz") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('1');\">" . getLngt("Kennzeichen") . "</a></td>";
      $tableFieldArray = array("tx_value","tx_sign","tx_remark");
      $tableAlignArray = array("right","center","left");
      // $tableColFinishPageLink = "0";
      // $tableFieldNumOfIdField = 0;
      $tableFieldFinishPageArray = array("tx_id","txt_id","tx_sign","tx_value");
      $returnValueArray = array("g_tx_id", "g_txt_id", "g_tx_sign", "g_tx_value");

   elseif ($listType == "employee" && $userTypeName == "hq") :
      $pageTitel = getLngt("MITARBEITER");
      $whereClause = "";
      if (!authCheckEmployeeRights($emp_id, "3")) : $whereClause = " AND emp.emp_id = '" . $emp_id . "'"; endif;
      $sqlquery = "SELECT emp.emp_id, usr.usr_name, usr.usr_firstname, usr.usr_email, usr.usr_phone, usr.usr_phone2, usr.usr_account, hq.hq_mnemonic"
                   . " FROM user AS usr, employee AS emp, headquarters AS hq"
                   . " WHERE usr.usr_type = '1' AND emp.usr_id = usr.usr_id AND emp.csc_id = '0' AND usr.hq_id = hq.hq_id AND hq.hq_id = '" . $hq_id . "'" . $whereClause
                   . " ORDER BY usr.usr_name";
      // $tableHeader = "<tr><td align=\"left\">" . getLngt("Name") . "</td><td align=\"left\">" . getLngt("Vorname") . "</td><td align=\"left\">" . getLngt("Niederlassung") . "</td><td align=\"left\">" . getLngt("E-mail") . "</td><td align=\"left\">" . getLngt("Telefon") . "</td><td align=\"left\">" . getLngt("Anmeldename") . "</td></tr>";                   
      $tableHeader = "  <td align=\"center\">" . "<a href=\"javascript:finishSort('0');\">" . getLngt("Name") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('1');\">" . getLngt("Vorname") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('2');\">" . getLngt("Niederlassung") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('3');\">" . getLngt("E-Mail") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('4');\">" . getLngt("Telefon") . "</a></td>"
                   . "  <td align=\"center\">" . "<a href=\"javascript:finishSort('5');\">" . getLngt("Anmeldename") . "</a></td>";
      $tableFieldArray = array("usr_name","usr_firstname","hq_mnemonic","usr_email","usr_phone","usr_account");
      $tableAlignArray = array("left","left","left","left","left","left");
      // $tableColFinishPageLink = "0";
      // $tableFieldNumOfIdField = 0;
      $tableFieldFinishPageArray = array("emp_id","usr_name","usr_firstname");
      $returnValueArray = array("g_sales_emp_id", "g_sales_usr_name", "g_sales_usr_firstname");

   else :
      $doCloseWindow = "1";
   endif;

   // Generate list from special statement
   if ($sqlquery != "") :
      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

       // Table with header
      $numOfRows = 1;
      $lineToggler = 0;
      $tableFieldArrayLen = count($tableFieldArray);
      $tableFieldFinishPageArrayLen = count($tableFieldFinishPageArray);

      // Check für additional link coloumns
      $genericStrLen = strlen($generic);

      // ATTENTION: The length will be checked against "$tableFieldFINISHPAGEArrayLen" but will be added to "$tableFieldArray" (!!!!)
      if ($genericStrLen > $tableFieldFinishPageArrayLen) :
         for ($j = $tableFieldFinishPageArrayLen; $j < $genericStrLen; $j++) :
             // Group link (<=> "2")
             if (substr($generic, $j,1) == "2") :
                $tableHeader .= "  <td align=\"center\">" . getLngt("Gruppen") . "</td>";
                array_push($tableFieldArray, "link_group");
                array_push($tableAlignArray, "center");
             endif;
         endfor;
         $tableFieldArrayLen = count($tableFieldArray);
      endif;
      $out .= "<tr>" . $tableHeader . "</tr>";
      while ($row = $result->fetch_assoc()):
            $numOfRows++;

         	  if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
            $cellColor = getListColor($numOfRows, $lineToggler);

            $out .= "<tr>";
            for ($j = 0; $j < $tableFieldArrayLen; $j++) :
                $align =  $tableAlignArray[$j];
                if ($align == "") : $align = "left"; endif;
                if ($tableColFinishPageLink == $j) :
                   $out .= "<td align=\"" . $align . "\" bgcolor=\"" . $cellColor ."\">&nbsp;<a href=\"javascript:finishPage('";
                   for ($i = 0; $i < $tableFieldFinishPageArrayLen; $i++) :
                       $out .= $row[$tableFieldFinishPageArray[$i]];
                       if ($i < ($tableFieldFinishPageArrayLen - 1)) : $out .= "','"; endif;
                   endfor;
                   $out .= "');\">";
                   $out .= $row[$tableFieldArray[$j]];
                   $out .= "</a></td>";
                else :
                   // Check for special link (e.g. group link)
                   if ($tableFieldArray[$j] == "link_group") :
                      $out .= "<td ".$htmlClass02." align=\"" . $align . "\" bgcolor=\"" . $cellColor ."\">&nbsp;" . "<a href=\"javascript:linkOpenGroups('br','" . ec($row[$tableFieldFinishPageArray[$tableFieldNumOfIdField]]) . "');\">" 
                           . "<img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\">"
                           . "</a></td>"
                           . "</td>";
                   else :
                      $out .= "<td align=\"" . $align . "\" bgcolor=\"" . $cellColor ."\">&nbsp;" . $row[$tableFieldArray[$j]] . "</td>";
                   endif;
                endif;
            endfor;            
            $out .= "</tr>";
      endwhile;
      $result->free();      
   endif;
endif;
?>

<html>

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

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

// GENERIC function
function finishPage(par_00,par_01,par_02,par_03,par_04,par_05,par_06,par_07,par_08,par_09) {
  <?php 
    $returnValueArrayLen = count($returnValueArray);
    for ($i = 0; $i < $returnValueArrayLen; $i++) :
        if (substr($generic, $i,1) == "1") : echo "opener.document.forms[0]." . $returnValueArray[$i] . ".value = par_" . pad($i, 2, "0") . ";"; endif;
    endfor;
  
    // Call opener function  
    if ($callOpenerFunction != "") :
       echo "opener." . $callOpenerFunction . ";";
    endif;
  ?>
  self.close();
};

function executeOnLoad () {
	// If no type specified or type is unknown then close the window immediately after loading the page
	if ('<?php echo $doCloseWindow ?>' == '1') {
		 self.close();
	}
}

function finishSort (sortNo) {
  document.forms[0].orderByFieldNo.value = sortNo;
  document.forms[0].submit();
}

function linkOpenGroups(itemType, itemId) {
  var widthPopupWin = 800;
  var heightPopupWin = 700;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/groupmembers.php?itemType=" + itemType + "&itemId=" + itemId,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>executeOnLoad();displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

<form action="../admin/selection_list.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="generic" value="<?php echo $generic ?>">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
  <input type="hidden" name="callOpenerFunction" value="<?php echo $callOpenerFunction ?>">
  <input type="hidden" name="listType" value="<?php echo $listType ?>">
  <input type="hidden" name="orderByFieldNo" value="<?php echo $orderByFieldNo ?>">

  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <div class="f12bp1_blue"><?php echo $pageTitle ?></div>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>
  
  <div>
    <table class="f8np1" border="0" cellpadding="0">
      <?php echo $out ?>
    </table>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

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

</html>