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

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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "generic", "f_hq_id", "f_grp_id", "f_grp_name", "showGroupMembers", "f_hq_visibility", "deactivateMenu"));

getLanguage(__FILE__);

$pageTitel = "GRUPPEN";
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
include_once ("../include/inc_filters.inc.php");
include_once ("../include/inc_group.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);
authCheckEmployeeRights($emp_id, "8", "1");
authCheckEmployeeRights($emp_id, "25", "1");

// Mandator filter
if ($f_hq_id == "") : $f_hq_id = array(); endif;
if (count($f_hq_id) == 0) : array_push($f_hq_id, $hq_id); endif;

// Initialize value
$outputFormatField = defineOutputFormats();
mcIsSet($generic, "1111");
mcIsSet($showGroupMembers);
mcIsSet($listOfEntries);
mcTrim($f_grp_name);

$hqIdToBeStored = $hq_id;
if ($f_hq_visibility == "" || count($f_hq_visibility) == 0) :
   $f_hq_visibility = "0";
else: 
   $f_hq_visibility = "1";
   $hqIdToBeStored = "0";
endif;

// *** Actions ***
if ($md_id != "" && $md_id > 0) :

   if ($f_act == "saveGroupNew") :
      if ($f_grp_name != "") :
         if (!existsEntry("groups",array("md_id",$md_id,"grp_name",$f_grp_name))) :
   
            insertStmt("groups", array("md_id",$md_id,"hq_id",$hqIdToBeStored,"grp_name",$f_grp_name));
            
            $f_grp_name = "";
         else :
            $statusMessage = getLngt("Die eingegebene Gruppe existiert schon, evtl. in einer anderen Niederlassung!");
         endif;
      else :
         $statusMessage = getLngt("Geben Sie bitte eine Gruppenbezeichnung ein!");
      endif;
   endif;

   if ($f_act == "saveGroupModified") :
      if ($f_grp_id != "" && $f_grp_name != "") :
         if (existsEntry("groups",array("md_id",$md_id,"grp_id",$f_grp_id))) :
            if (!existsEntry("groups",array("md_id",$md_id,"hq_id",$hq_id,"grp_name",$f_grp_name))) :
   
               updateStmt("groups","grp_id",$f_grp_id,array("md_id",$md_id,"hq_id",$hqIdToBeStored,"grp_name", $f_grp_name));
   
               $f_grp_name = "";
            else :
               $statusMessage = getLngt("Der eingegebene Gruppenname existiert schon, evtl. in einer anderen Niederlassung!");
            endif;
         else :
            $statusMessage = getLngt("Die eingegebene Gruppe existiert nicht!");
         endif;
      else :
         $statusMessage = getLngt("Geben Sie bitte eine Gruppenbezeichnung ein!");
      endif;
   endif;

   if ($f_act == "saveGroupRemove") :
      if ($f_grp_id != "") :
         if (existsEntry("groups",array("grp_id",$f_grp_id))) :
   
            // Check group for state "readonly"
            $grpReadonly = getFieldValueFromId("groups","grp_id",$f_grp_id,"grp_readonly");
            if ($grpReadonly == "0") :
   
               // Remove all customer relations
               $rowArray = getGroupMembers(array($f_grp_id), "cs", $f_hq_id);
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $csGroup = getFieldValueFromId("customer","cs_id",$rowArray[$j]["cs_id"],"cs_group");
                      $csGroup = removeItemFromFilterstring($csGroup, $f_grp_id);
                      updateStmt("customer", "cs_id", $rowArray[$j]["cs_id"], array("cs_group", $csGroup));
                  endfor;
               endif;
   
               // Remove all carrier relations
               $rowArray = getGroupMembers(array($f_grp_id), "cr", $f_hq_id);
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $crGroup = getFieldValueFromId("courier","cr_id",$rowArray[$j]["cr_id"],"cr_group");
                      $crGroup = removeItemFromFilterstring($crGroup, $f_grp_id);
                      updateStmt("courier", "cr_id", $rowArray[$j]["cr_id"], array("cr_group", $crGroup));
                  endfor;
               endif;
   
               // Remove all employee relations
               $rowArray = getGroupMembers(array($f_grp_id), "emp", $f_hq_id);
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $empGroup = getFieldValueFromId("employee","usr_id",$rowArray[$j]["usr_id"],"emp_group");
                      $empGroup = removeItemFromFilterstring($empGroup, $f_grp_id);
                      updateStmt("employee", "usr_id", $rowArray[$j]["usr_id"], array("emp_group", $empGroup));
                  endfor;
               endif;
   
               // Remove all article relations
               $rowArray = getGroupMembers(array($f_grp_id), "at", $f_hq_id);
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $atGroup = getFieldValueFromId("article","at_id",$rowArray[$j]["at_id"],"at_group");
                      $atGroup = removeItemFromFilterstring($atGroup, $f_grp_id);
                      updateStmt("article", "at_id", $rowArray[$j]["at_id"], array("at_group", $atGroup));
                  endfor;
               endif;
   
               // Remove all service relations
               $rowArray = getGroupMembers(array($f_grp_id), "srv", $f_hq_id, "1");
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $srvGroup = getFieldValueFromId("service","srv_id",$rowArray[$j]["srv_id"],"srv_group");
                      $srvGroup = removeItemFromFilterstring($srvGroup, $f_grp_id);
                      updateStmt("service", "srv_id", $rowArray[$j]["srv_id"], array("srv_group", $srvGroup));
                  endfor;
               endif;
   
               // Remove all servicetype relations
               $rowArray = getGroupMembers(array($f_grp_id), "srvt", $f_hq_id, "1");
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $srvtGroup = getFieldValueFromId("servicetype","srvt_id",$rowArray[$j]["srvt_id"],"srvt_group");
                      $srvtGroup = removeItemFromFilterstring($srvtGroup, $f_grp_id);
                      updateStmt("servicetype", "srvt_id", $rowArray[$j]["srvt_id"], array("srvt_group", $srvtGroup));
                  endfor;
               endif;
   
               // Remove all metafieldcategory relations
               $rowArray = getGroupMembers(array($f_grp_id), "mtfc", $f_hq_id, "1");
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $mtfcGroup = getFieldValueFromId("metafieldcategory","mtfc_id",$rowArray[$j]["mtfc_id"],"mtfc_group");
                      $mtfcGroup = removeItemFromFilterstring($mtfcGroup, $f_grp_id);
                      updateStmt("metafieldcategory", "mtfc_id", $rowArray[$j]["mtfc_id"], array("mtfc_group", $mtfcGroup));
                  endfor;
               endif;
   
               // Remove all metafieldtemplate relations
               $rowArray = getGroupMembers(array($f_grp_id), "mtft", $f_hq_id, "1");
               $rowArrayLen = count($rowArray);
               if ($rowArrayLen > 0) :
                  for ($j = 0; $j < $rowArrayLen; $j++) :
                      $mtftGroup = getFieldValueFromId("metafieldtemplate","mtft_id",$rowArray[$j]["mtft_id"],"mtft_group");
                      $mtftGroup = removeItemFromFilterstring($mtftGroup, $f_grp_id);
                      updateStmt("metafieldtemplate", "mtft_id", $rowArray[$j]["mtft_id"], array("mtft_group", $mtftGroup));
                  endfor;
               endif;
   
               // Remove group
               deleteStmt("groups","grp_id = '" . $f_grp_id . "'");
   
               $f_grp_id = "";
               $f_grp_name = "";
            else :
               $statusMessage = getLngt("Die angegebene Gruppe darf nicht gelöscht werden!");
            endif;
         else :
            $statusMessage = getLngt("Die eingegebene Gruppe existiert nicht!");
         endif;
      else :
         $statusMessage = getLngt("Wählen Sie die zu löschende Gruppe über die Spalte Bearbeiten aus!");
      endif;
   endif;
endif;


// Statement for groups
$whereClauseHq = " (grp.hq_id = '" . $hq_id . "' OR grp.hq_id = '0') ";
if ($f_hq_id != "" && count($f_hq_id) >= 1) :
   $whereClauseHq = " (grp.hq_id IN " . getSQLMandatorArray($f_hq_id) . " OR grp.hq_id = '0') ";
endif;
$sqlquery = "SELECT grp.grp_id, grp.grp_name, grp.grp_readonly, grp.hq_id, hq.hq_mnemonic"
          . " FROM groups AS grp"
          . "      LEFT JOIN headquarters AS hq ON grp.hq_id = hq.hq_id"
          . " WHERE grp.md_id = '" . $md_id . "' AND " . $whereClauseHq
          . " ORDER BY grp.grp_name";
$result = $db->query($sqlquery);
if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

 // Table with header
$numOfRows = 1;
$lineToggler = 0;
$tableOfRows = "<tr><td align=\"center\">" . getLngt("Gruppe") . "&nbsp;&nbsp;&nbsp;</td>";
if ($f_hq_id != "" && count($f_hq_id) >= 1) :
   $tableOfRows .= "<td align=\"center\">" . getLngt("Niederlassung") . "&nbsp;&nbsp;&nbsp;</td>";
endif;
$tableOfRows .= "<td align=\"center\">&nbsp;" . getLngt("Bearbeiten") . "&nbsp;</td>";
$tableOfRows .= "<td align=\"center\">&nbsp;" . getLngt("Mitglieder") . "&nbsp;</td></tr>";
while ($row = $result->fetch_assoc()):
      $numOfRows++;

   	  if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
      $cellColor = getListColor($numOfRows, $lineToggler);
	   
      $tableOfRows .= "<tr>";      
      $tableOfRows .= "<td align=\"left\" bgcolor=\"" . $cellColor ."\">&nbsp;" . $row["grp_name"] . "&nbsp;&nbsp;&nbsp;</td>";
      if ($row["hq_id"] == "0") :      
         $tableOfRows .= "<td align=\"left\" bgcolor=\"" . $cellColor ."\">&nbsp;" . getLngt("Alle") . "&nbsp;&nbsp;&nbsp;</td>";
      elseif ($f_hq_id != "" && count($f_hq_id) >= 1) :
         $tableOfRows .= "<td align=\"left\" bgcolor=\"" . $cellColor ."\">&nbsp;" . $row["hq_mnemonic"] . "&nbsp;&nbsp;&nbsp;</td>";
      endif;
      $tableOfRows .= "<td align=\"center\" bgcolor=\"" . $cellColor ."\">&nbsp;";
      if ($row["grp_readonly"] == "0") :
         $tableOfRows .= "<a href=\"javascript:editGrp('" . $row["grp_id"] . "','" . $row["grp_name"] . "','" . $row["hq_id"] . "');\"><img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\"></a>";
      endif;
      $tableOfRows .= "</td>";
      $tableOfRows .= "<td align=\"center\" bgcolor=\"" . $cellColor ."\">&nbsp;<a href=\"javascript:grpMembers('" . $row["grp_id"] . "');\"><img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\"></a></td>";
      // $tableOfRows .= "<td align=\"center\" bgcolor=\"" . $cellColor ."\">&nbsp;<a href=\"javascript:finishPage('" . $row["grp_id"] . "','" . $row["grp_name"] . "');\">" . $row["grp_name"] . "</a></td>";
      $tableOfRows .= "</tr>";
endwhile;
$result->free();


// Show members of the group if requested
if ($showGroupMembers == "1") :
   $grpName = getFieldValueFromId("groups","grp_id",$f_grp_id,"grp_name");
   $listOfEntries .= "<br>";
   $listOfEntries .= "<table class=\"f12bp1\" border=\"0\" cellpadding=\"0\">";
   $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("Mitglieder der Gruppe:") . " " . $grpName . "</td></tr>\n";
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   // Groups
   $rowArray = getGroupMembers(array($f_grp_id), "grp", $f_hq_id);
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Name&nbsp;"));
      $fieldArray = array("grp_name");
      $aligns = "l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "255";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";   
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("GRUPPEN:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else : 
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("GRUPPEN:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   // Customers
   $rowArray = getGroupMembers(array($f_grp_id), "cs", $f_hq_id);
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("EID&nbsp;"), getLngt("Firma&nbsp;"), getLngt("Zusatz&nbsp;"));
      $fieldArray = array("cs_eid", "cmp_comp", "cmp_comp2");
      $aligns = "l,l,l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";   
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("KUNDEN:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else : 
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("KUNDEN:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   // Couriers
   $rowArray = getGroupMembers(array($f_grp_id), "cr", $f_hq_id);
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("EID&nbsp;"), getLngt("Name&nbsp;"), getLngt("Vorname&nbsp;"));
      $fieldArray = array("cr_eid", "usr_name", "usr_firstname");
      $aligns = "l,l,l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("TRANSPORTEURE:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("TRANSPORTEURE:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";
   
   // Employees
   $rowArray = getGroupMembers(array($f_grp_id), "emp", $f_hq_id);
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Name&nbsp;"), getLngt("Vorname&nbsp;"));
      $fieldArray = array("usr_name", "usr_firstname");
      $aligns = "l,l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("MITARBEITER:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("MITARBEITER:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   // Branches
   $rowArray = getGroupMembers(array($f_grp_id), "br", $f_hq_id);
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Schlüssel&nbsp;"), getLngt("Bezeichnung&nbsp;"));
      $fieldArray = array("br_key", "br_name");
      $aligns = "l,l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("BRANCHEN:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("BRANCHEN:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   // Articles
   $rowArray = getGroupMembers(array($f_grp_id), "at", $f_hq_id);
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("EID&nbsp;"), getLngt("Name&nbsp;"));
      $fieldArray = array("at_eid", "at_name");
      $aligns = "l,l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("ARTIKEL:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("ARTIKEL:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";
 
   // Service
   $rowArray = getGroupMembers(array($f_grp_id), "srv", $f_hq_id, "1");
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Name&nbsp;"));
      $fieldArray = array("srv_name");
      $aligns = "l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("PREISTYPEN:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("PREISTYPEN:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";  
 
   // Servicetypes
   $rowArray = getGroupMembers(array($f_grp_id), "srvt", $f_hq_id, "1");
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Name&nbsp;"));
      $fieldArray = array("srvt_name");
      $aligns = "l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("LEISTUNGSTYPEN:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("LEISTUNGSTYPEN:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";   

   // Metafieldcategories
   $rowArray = getGroupMembers(array($f_grp_id), "mtfc", $f_hq_id, "1");
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Name&nbsp;"));
      $fieldArray = array("mtfc_description");
      $aligns = "l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("KATEGORIEN:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("KATEGORIEN:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   // Metafieldtemplates
   $rowArray = getGroupMembers(array($f_grp_id), "mtft", $f_hq_id, "1");
   $rowArrayLen = count($rowArray);
   if ($rowArrayLen > 0) :
      $titleArray = array(getLngt("Name&nbsp;"));
      $fieldArray = array("mtft_name");
      $aligns = "l";
      $alignArray = spliti(",",$aligns);
      $alignTitles = "left";
      $widths = "50";
      $widthArray = spliti(",",$widths);
      $summationField = "";
      $mode = "2";             // Output from ARRAY
      $sortDBField = "";       // Used in following include-file for sorting per column;
      include ("../include/inc_list_defineoutput.inc.php");
   endif;
   $listOfEntries .= "<table class=\"f10np1\" border=\"0\" cellpadding=\"0\">";
   if ($rowArrayLen > 0) :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("TEMPLATES:") . "</td></tr>\n";
      $listOfEntries .= $tableHeader . $tableBody;
   else :
      $listOfEntries .= "<tr><td colspan=\"2\">" . getLngt("TEMPLATES:") . " " . getLngt("-Keine-") . "</td></tr>\n";
   endif;
   $listOfEntries .= "</table>";
   $listOfEntries .= "<br><br>";

   $showGroupMembers = "";    // Init
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(grp_id,grp_name) {

  <?php if (substr($generic, 0,1) == "1") : ?> opener.document.forms[0].g_grp_id.value = grp_id;     <?php endif; ?>
  <?php if (substr($generic, 2,1) == "1") : ?> opener.document.forms[0].g_grp_name.value = grp_name; <?php endif; ?>
  self.close();
};

function finishPage2(f_act) {
  document.forms[0].f_act.value = f_act;
  document.forms[0].submit();
};

function finishPage3(f_act) {
	if (confirm('<?php echo getLngt("Möchten Sie die selektierte Gruppe wirklich entfernen? Bei allen Mitgliedern würden ebenfalls die Beziehungen gelöscht!") ?>')) {
     document.forms[0].f_act.value = f_act;
     document.forms[0].submit();
  };
};

function editGrp(grp_id, grp_name, hq_id) {
	document.forms[0].f_grp_id.value = grp_id;
  document.forms[0].f_grp_name.value = grp_name;
  if (hq_id == '0') {
     document.getElementsByName('f_hq_visibility[]')[0].checked = true;
  } else {
  	 document.getElementsByName('f_hq_visibility[]')[0].checked = false;
  }
};

function grpMembers(grp_id) {
	document.forms[0].f_grp_id.value = grp_id;
	document.forms[0].f_grp_name.value = '';
  document.forms[0].showGroupMembers.value = '1';
  document.forms[0].submit();
};

function openGroups() {
  var widthPopupWin = 800;
  var heightPopupWin = 700;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  var groupId = document.forms[0].f_grp_id.value;
  if (groupId != '') {
     popupWin = window.open("../admin/groupmembers.php?itemType=grp&itemId=" + groupId,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
  } else {
  	alert('<?php getLngt("Es wurde keine Gruppe ausgewählt!") ?>');
  }
};

-->
</script>
</head>

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

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

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

  <form action="../admin/group.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="generic" value="<?php echo $generic ?>">
  <input type="hidden" name="f_grp_id" value="<?php echo $f_grp_id ?>">
  <input type="hidden" name="showGroupMembers" value="<?php echo $showGroupMembers ?>">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">  

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

  <!-- Headquarters checkboxes -->
  <?php if (authCheckEmployeeRights($emp_id, "10")) : ?>
    <?php echo getHeadquartersCheckboxes($f_hq_id); ?>
    <?php echo htmlDivLineSpacer("10px", "", "left"); ?>
  <?php endif; ?>

  <div>
    <?php echo defineButtonType10(getLngt("Anzeigen"), "action_show", "finishPage2('');", "120", "left", "3"); ?>
  </div>  
  <?php echo htmlDivLineSpacer("20px", "", "left"); ?>

  <div>
    <table class="f10np1" border="0" cellpadding="0">
      <?php echo $tableOfRows ?>
    </table>    
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

  <div>
    <?php echo getLngt("(Neue) Gruppe:") ?>
  </div>
  <?php echo htmlDivLineSpacer("10px"); ?>

  <div>
    <div <?php echo setStyleHtmlDiv("150px","left"); ?>><?php echo getLngt("Bezeichnung:") ?></div>
    <div>
      <input type="text" name="f_grp_name" value="<?php echo $f_grp_name ?>" maxlength="100" size="30">
    </div>
  </div>
  <?php echo htmlDivLineSpacer("5px"); ?>
  <div>
    <div <?php echo setStyleHtmlDiv("200px","left"); ?>><?php echo getLngt("Für alle Zentralen sichtbar:") ?></div>
    <div>
      <input type="checkbox" name="f_hq_visibility[]" value="1">
    </div>
  </div>
  <?php echo htmlDivLineSpacer("20px", "", "left"); ?>

  <div>
    <?php echo defineButtonType10(getLngt("Hinzufügen"), "action_new", "finishPage2('saveGroupNew');", "120", "left", "3"); ?>
    <?php echo defineButtonType10(getLngt("Umbenennen"), "action_rename", "finishPage2('saveGroupModified');", "120", "left", "3"); ?>
    <?php echo defineButtonType10(getLngt("Löschen"), "action_remove", "finishPage3('saveGroupRemove');", "120", "left", "3"); ?>
    <?php echo defineButtonType08(getLngt("Gruppen"), "action_grp", "openGroups();", "120", "left", "3");?>
    <?php // echo defineButtonType10(getLngt("Schließen"), "action_close", "window.close();", "120", "left", "3"); ?>
  </div>  
  <?php echo htmlDivLineSpacer("30px", "", "left"); ?>

  <div>
    <?php echo $listOfEntries ?>
  </div>
  <?php echo htmlDivLineSpacer("20px"); ?>

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

</html>