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

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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act_mtfl", "orderClause", "f_hq_id", "deactivateMenu", "f_searchmode", "statusMessage", "callMode", "callObjId",
                         "deactivateMenu", "generatePDF", "category", "objId", "categoryPageTitel", "f_search", "f_letter", "f_objIdRemove", 
                         "viewItemsMode", "presetFields"));

getLanguage(__FILE__);


$pageTitel = getLngt($categoryPageTitel);

mcIsSet($mtfvKeysArrayLen, 0);

mcIsSet($includeMode);
if ($includeMode == "") :
   include_once ("../admin/menu.php");
endif;
include_once ("../include/html.inc.php");

if ($category == "") : $category = "1"; endif;  // Contact
include_once ("../include/inc_metafield.inc.php");
include_once ('../groupware/vcard/class.vCard.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, "0", "1");

// View mode
if ($viewItemsMode == "") : $viewItemsMode = "1"; endif;
$outputViewMode = "";

// Special treatment according to the "mode of call"
$specialCallMode = "";
if ($callObjId != "") :
   // [1.] Customer contacts
   if ($category == "1") :
      if ($callMode == "1") :
         $tmpCsEid = getFieldValueFromId("customer", "cs_id", $callObjId, "cs_eid");
         $tmpCmpId = getFieldValueFromId("customer", "cs_id", $callObjId, "cmp_id");
         $tmpCmpComp = getFieldValueFromId("company", "cmp_id", $tmpCmpId, "cmp_comp");
         if ($viewItemsMode == "1") :
            $specialCallMode = "<div><span class=\"f10bp1\">" . getLngt("KUNDE") . ":&nbsp;&nbsp;&nbsp;" . $tmpCsEid . "&nbsp;&nbsp;&nbsp;(" . $tmpCmpComp . ")</span></div>\n";
         else :
            $specialCallMode = "<tr><td class=\"f10bp1\" colspan=\"" . $mtfck_mtfckIdsLen . "\">" . getLngt("KUNDE") . ":&nbsp;&nbsp;&nbsp;" . $tmpCsEid . "&nbsp;&nbsp;&nbsp;(" . $tmpCmpComp . ")<br><br></td></tr>\n";
         endif;
         if ($f_act_mtfl == "") :
            $f_act_mtfl = "search";
         endif;
      endif;
   endif;
endif;

$f_search = trim($f_search);

// Init mail address for VCards with the mail address of the user
$f_vcard_email = getFieldValueFromId("user", "usr_id", $usr_id, "usr_email");

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

// 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;


if ($presetFields != "") : $presetFields = urldecode($presetFields); endif;

$tableHeaderSearchFields = "";
// OUTPUT: Table header search fields
/*
$tableHeaderSearchFields .= "<td>&nbsp;</td>";
$searchString = "";
for ($j = 0; $j < $mtfck_mtfckIdsLen; $j++) :
   getSecHttpVars("1",array("f_" . $j));
   ${("f_" . $j)} = trim(${("f_" . $j)});
   $searchString .= ${("f_" . $j)};
   $cellColor = getListColor(1, 1);
   $tableHeaderSearchFields .= "<td bgcolor=\"" . $cellColor . "\">" . "<input type=\"text\" " . $htmlClass01 . " name=\"f_" . $j . "\" value=\"" . ${("f_" . $j)} . "\">" . "</td>\n";
endfor;
*/
// Usage of only one search field according to an OR-search in all fields
$tableHeaderSearchFields .= "<td colspan=\"" . $mtfck_mtfckIdsLen . "\">" . getLngt("Suchbegriff") . ":&nbsp;&nbsp;" . "<input type=\"text\" " . $htmlClass01 . " name=\"f_search\" value=\"" . $f_search . "\" size=\"40\" placeholder=\"" . getLngt("Suchbegriff") . "\">" . "<br><br></td>\n";


// OUTPUT: Table header column links
$tableHeaderLinks = "<td>Bearbeiten&nbsp;</td>";
for ($j = 0; $j < $mtfck_mtfckIdsLen; $j++) :
   $tableHeaderLinks .= "<td>" . $mtfk_allFieldNames[$mtfck_mtfkIds[$j]] . "&nbsp;&nbsp;</td>";

   /*
   $cellColor = getListColor(1, 1);
   $tableHeaderLinks .= "<a href=\"javascript:document.forms[0].orderClause.value='" . $mtfck_mtfckIds[$j] . "';document.forms[0].f_act_mtfl.value='search';document.forms[0].submit();\">&nbsp;" . $mtfk_allFieldNames[$mtfck_mtfkIds[$j]] . "&nbsp;</a>";
   */
endfor;
$tableHeaderLinks .= "<td>Löschen&nbsp;</td>";


// Remove specified object
if ($f_act_mtfl == "remove" && $f_objIdRemove != "") :

  TA("B");
  deleteStmt("metafieldvalue","mtfv_id = " . $f_objIdRemove);
  $f_act_mtfl = "search";

  TA("C");
  TA("E");
endif;


// Generate search-resultset
if ($f_searchmode == "") : $f_searchmode = "1"; endif;
if ($f_act_mtfl == "search") :
   $doSearch = TRUE;
   if (strlen($f_search) == 0 && $callMode == "" && $f_letter == "") :
      $doSearch = FALSE;
      if (getCountOfTable("metafieldvalue") <= (MAX_CARDINALITY * 10)) :
         $doSearch = TRUE;
      endif;
   endif;
   if ($doSearch) :
      if ($f_searchmode == "1") : $prefix = "%"; else : $prefix = ""; endif;
      $whereClause = "";
      $objIdArray = array();

      if ($callMode != "" && $callObjId != "") :

         // [1.] Customer contacts
         if ($category == "1") :
            if ($callMode == "1") :
               // Get the object IDs equal to
               $sqlquery = "SELECT DISTINCT mtfv.mtfv_id"
                            . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                            . " WHERE mtfv.mtfv_value = '" . $callObjId . "' AND"
                            . "       mtfck.mtfc_id = '" . $category . "'    AND"
                            . "       mtfv.mtfck_id = mtfck.mtfck_id         AND"
                            . "       mtfk.mtfk_id = mtfck.mtfk_id           AND"
                            . "       mtfk.mtfk_type = 'co2cs'";
            endif;
         endif;

      else :
         /* Use this to search in each field by AND (like lists for cs, cr, etc.)
         for ($j = 0; $j < $mtfck_mtfckIdsLen; $j++) :
             if ($whereClause != "" && ${("f_" . $j)} != "") : $whereClause .= " OR "; endif;
             if (${("f_" . $j)} != "") : $whereClause .= "(mtfv.mtfck_id = '" . $mtfck_mtfckIds[$j] . "' AND mtfv.mtfv_value LIKE '" . $prefix . ${("f_" . $j)} . "%')"; endif;
         endfor;
         if ($whereClause != "") : $whereClause = " WHERE " . $whereClause; endif;
         */

         if ($f_search != "") : $whereClause .= " AND mtfv.mtfv_value LIKE '" . $prefix . $f_search . "%'"; endif;
         if ($f_letter != "") : $whereClause .= " AND mtfv.mtfv_value LIKE '" . $f_letter . "%' AND (mtfk.mtfk_name = 'Name' OR mtfk.mtfk_type = 'surname')"; endif;

         // In the first step get the involved object IDs
         $sqlquery = "SELECT DISTINCT mtfv.mtfv_id"
                      . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                      . " WHERE mtfv.mtfck_id = mtfck.mtfck_id      AND"
                      . "       mtfck.mtfc_id = '" . $category . "' AND"
                      . "       mtfck.mtfk_id = mtfk.mtfk_id"
                      . $whereClause;

         // Special search for type co2cs, additional search in customer data
         if ($whereClause != "" && $f_letter == "") :
            $sqlquery2 = "SELECT DISTINCT mtfv.mtfv_id"
                          . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk, customer AS cs, company AS cmp"
                          . " WHERE mtfv.mtfck_id = mtfck.mtfck_id         AND"
                          . "       mtfck.mtfc_id = '" . $category . "'    AND"
                          . "       mtfk.mtfk_id = mtfck.mtfk_id           AND"
                          . "       mtfk.mtfk_type = 'co2cs'               AND"
                          . "       cs.cs_id = mtfv.mtfv_value             AND"
                          . "       cs.cmp_id = cmp.cmp_id             AND"
                          . "       (cs.cs_eid LIKE '" . $prefix . $f_search . "%' OR"
                          . "        cmp.cmp_comp LIKE '" . $prefix . $f_search . "%')";

            $sqlquery = "(" . $sqlquery . ") UNION (" . $sqlquery2 . ")";
         endif;
      endif;

      $result = $db->dbQ($sqlquery);
      while ($row = $result->fetch_assoc()):
            $objIdArray[] = $row["mtfv_id"];
      endwhile;
      $result->free();

      if (count($objIdArray) > 0) :

         // Next step get all object informations of all involved objects
         $whereClauseHQ = "       mtfck.hq_id IN " . getSQLMandatorArray($f_hq_id) . " ";
         if ($gHqId) : $whereClauseHQ = "       mtfck.hq_id = '0' "; endif;

         $sqlquery = "SELECT mtfv.mtfck_id, mtfv.mtfv_id, mtfv.mtfv_value, mtfk.mtfk_type"
                      . " FROM metafieldvalue AS mtfv, metafieldcategorykey AS mtfck, metafieldkey AS mtfk"
                      . " WHERE mtfv.mtfv_id IN (" . implode(",",$objIdArray) . ") AND"
                      . "       mtfck.mtfc_id = '" . $category . "' AND"
                      . "       mtfk.mtfk_id = mtfck.mtfk_id AND "
                      . "       mtfv.mtfck_id = mtfck.mtfck_id AND "
                      .         $whereClauseHQ
                      . " ORDER BY mtfv.mtfv_id, mtfck.mtfck_sort";

         $result = $db->dbQ($sqlquery);

         if ($viewItemsMode == "1") :

            if ($category == "1") :
               // *** [Contacts] ***

               // Converts formular DB data into an array and sorts it according to a special column
               $mtfvArray = array();
               $mtfvTypeArray = array();
               $constContactSortFields = getParameterValue("0", "MASK_CONTACT_SORT_FIELDS", "0");
               if ($constContactSortFields == "") : $constContactSortFields = "1,2"; endif;  // Sort contacts by "Name" (here first coumn)
               $mtfvArraySortColArray = spliti(",",$constContactSortFields);
               convertFormdataToArray();

               // Get fields of the header to be displayed (according to contact it will be the "name" and the "firstname")
               $constContactHeaderFieldsDisplayed = getParameterValue("0", "MASK_CONTACT_HEADER_FIELDS_DISPLAYED", "0");
               if ($constContactHeaderFieldsDisplayed == "") : $constContactHeaderFieldsDisplayed = "1,2"; endif;
               $contactHeaderFieldsDisplayedArray = spliti(",",$constContactHeaderFieldsDisplayed);

               // Get fields of the body to be displayed
               $constContactBodyFieldsDisplayed = getParameterValue("0", "MASK_CONTACT_BODY_FIELDS_DISPLAYED", "0");
               if ($constContactBodyFieldsDisplayed == "") : $constContactBodyFieldsDisplayed = "3,4,5,6,7,8,9,10"; endif;
               $contactBodyFieldsDisplayedArray = spliti(",",$constContactBodyFieldsDisplayed);

               // Get ADDRESS fields of the body to be displayed ([0]=street, [1]=housenumber, [2]=zipcode, [3]=city)
               $constContactAddressFieldsDisplayed = getParameterValue("0", "MASK_CONTACT_ADDRESS_FIELDS_DISPLAYED", "0");
               if ($constContactAddressFieldsDisplayed == "") : $constContactAddressFieldsDisplayed = "8,9"; endif;
               $contactAddressFieldsDisplayedArray = spliti(",",$constContactAddressFieldsDisplayed);

               $styleCell       = "float:left;width:350px;height:180px;border:4px;border-style:outset;border-color:#1b12b9;margin:2px;";
               $styleHeader     = "height:20px;background-color:#4e45ec;padding:5px;color:#FFFFFF;cursor:pointer;";
               $styleBody       = "height:110px;background-color:#97bcFF;padding:5px;";
               $styleFooter     = "height:20px;background-color:#6e65ff;padding:5px;color:#FFFFFF;";
               $styleFontHeader = "f10np1";
               $styleFontBody   = "f8np1";
               $styleFontFooter = "f8np1";

               for ($i = 0; $i < $mtfvKeysArrayLen; $i++) :
                   $VCard = array();
                   $outputViewMode .= "<div style=\"". $styleCell ."\">\n";
                   // Card header
                   $outputViewMode .= "  <div style=\"". $styleHeader ."\" onClick=\"javascript:openMetaFieldSpecial('" . ec($mtfvKeysArray[$i]) . "');\"><span class=\"" . $styleFontHeader . "\">" . substr($mtfvArray[$mtfvKeysArray[$i]][$contactHeaderFieldsDisplayedArray[0]] . ", " . $mtfvArray[$mtfvKeysArray[$i]][$contactHeaderFieldsDisplayedArray[1]], 0, 29) . "</span></div>\n";
                   $VCard["n_family"] = $mtfvArray[$mtfvKeysArray[$i]][$contactHeaderFieldsDisplayedArray[0]];
                     if ($VCard["n_family"] == "") : $VCard["n_family"] = "_"; endif;
                   $VCard["n_given"] = $mtfvArray[$mtfvKeysArray[$i]][$contactHeaderFieldsDisplayedArray[1]];
                     if ($VCard["n_given"] == "") : $VCard["n_given"] = "_"; endif;
                   // $VCard["fn"] = $mtfvArray[$mtfvKeysArray[$i]][$contactHeaderFieldsDisplayedArray[1]] . " " . $mtfvArray[$mtfvKeysArray[$i]][$contactHeaderFieldsDisplayedArray[0]];
                   $VCard["adr_one_street"] = $mtfvArray[$mtfvKeysArray[$i]][$contactAddressFieldsDisplayedArray[0]] . " " . $mtfvArray[$mtfvKeysArray[$i]][$contactAddressFieldsDisplayedArray[1]];
                   $VCard["adr_one_locality"] = $mtfvArray[$mtfvKeysArray[$i]][$contactAddressFieldsDisplayedArray[3]];
                   $VCard["adr_one_region"] = " ";
                   $VCard["adr_one_countryname"] = $mtfvArray[$mtfvKeysArray[$i]][$contactAddressFieldsDisplayedArray[4]];
                   $VCard["adr_one_postalcode"] = $mtfvArray[$mtfvKeysArray[$i]][$contactAddressFieldsDisplayedArray[2]];
/*
[n_family] => Simon
[n_given] => Frank
[n_middle] =>
[n_prefix] =>
[n_suffix] =>
[fn] => Frank Simon
[org_name] => Rotterdam Immobilien GmbH
[org_unit] =>
[title] => Bankfachwirt, Vertrieb
[tel_work] => 02173 9150 12
[tel_cell] => 0163 691 5012
[tel_fax] => 02173 9150 11
[address2] =>
[adr_one_street] => Rotterdam Immobilien GmbH Hauptstra=DFe 41
[adr_one_locality] => Langenfeld
[adr_one_region] =>
[adr_one_postalcode] => 40764
[adr_one_countryname] => Deutschland
[label] => Rotterdam Immobilien GmbH Hauptstra=DFe 41 Langenfeld 40764 Deutschland
[url] => http://www.rotterdam-bau.de
[email] => simon@rotterdam-bau.de
[email_type] => PREF
[tel_prefer] =>
[adr_one_type] =>
[adr_two_type] =>
*/

                   $tmpArray = $mtfvArray[$mtfvKeysArray[$i]];
                   $tmpArrayLen = count($tmpArray);
                   $outputViewMode .= "  <div style=\"". $styleBody ."\">";
                   for ($j = 0; $j < $tmpArrayLen; $j++) :
                       // Check column to be displayed
                       $displayCurrField = in_array($j, $contactBodyFieldsDisplayedArray);

                       // Card body
                       if ($displayCurrField) :
                          $outputViewMode .= "  <div style=\"float:left;width:80px;\"><span class=\"" . $styleFontBody . "\">" . $mtfk_allFieldNames[$mtfck_mtfkIds[$j]] . "</span></div>";
                          $outputViewMode .= "  <div style=\"float:left;width:10px;\"><span class=\"" . $styleFontBody . "\">:</span></div>";
                       endif;
                       $ftype = $mtfvTypeArray[$mtfvKeysArray[$i]][$j];
                       $valueToBeDisplayed = $mtfvArray[$mtfvKeysArray[$i]][$j];
                       if ($ftype == "co2cs") :
                          $tmpCsId = $valueToBeDisplayed;
                          $tmpCmpId = getFieldValueFromId("customer", "cs_id", $tmpCsId, "cmp_id");
                          $tmpCmpComp1 = trim(getFieldValueFromId("company", "cmp_id", $tmpCmpId, "cmp_comp"));
                          $VCard["org_name"] = $tmpCmpComp1;
                          $valueToBeDisplayed = getFieldValueFromId("customer", "cs_id", $valueToBeDisplayed, "cs_eid");
                          if ($valueToBeDisplayed != "" && $tmpCmpComp1 != "") :
                             $valueToBeDisplayed .= "<br>[" . trim($tmpCmpComp1) . "]";
                          endif;
                       elseif ($ftype == "mail_business" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b><a href=\"mailto:" . $valueToBeDisplayed . "\">" . $valueToBeDisplayed . "</a></b></span></div>";
                          endif;
                          $VCard["email"] = $valueToBeDisplayed;
                          $VCard["email_type"] = "PREF";
                       elseif ($ftype == "url_business" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b><a href=\"" . $valueToBeDisplayed . "\">" . $valueToBeDisplayed . "</a></b></span></div>";
                          endif;
                          $VCard["url_work"] = $valueToBeDisplayed;
                       elseif ($ftype == "mobile_business" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b>" . $valueToBeDisplayed . "</b></span></div>";
                          endif;
                          $VCard["tel_cell"] = $valueToBeDisplayed;
                       elseif ($ftype == "phone_business" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b>" . $valueToBeDisplayed . "</b></span></div>";
                          endif;
                          $VCard["tel_work"] = $valueToBeDisplayed;
                       elseif ($ftype == "fax_business" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b>" . $valueToBeDisplayed . "</b></span></div>";
                          endif;
                          $VCard["tel_fax"] = $valueToBeDisplayed;
                       elseif ($ftype == "phone_private" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b>" . $valueToBeDisplayed . "</b></span></div>";
                          endif;
                          // $VCard[] = $valueToBeDisplayed;
                       elseif ($ftype == "birthdate" && $valueToBeDisplayed != "") :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b>" . $valueToBeDisplayed . "</b></span></div>";
                          endif;
                          $VCard["bday"] = substr($valueToBeDisplayed,3,2) . "/" . substr($valueToBeDisplayed,0,2) . "/" . substr($valueToBeDisplayed,6,4);
                       else :
                          if ($displayCurrField) :
                             $outputViewMode .= "  <div><span class=\"" . $styleFontBody . "\"><b>" . $valueToBeDisplayed . "</b></span></div>";
                          endif;
                       endif;
                   endfor;
                   $outputViewMode .= "  </div>\n";
                   // Create vCard object
                   $vCardObj = new vCard();
                   $VCardString = $vCardObj->out($VCard);
                   // $VCardString = nl2br($VCardString);
                   $VCardStringJs = my_str_check_js($VCardString);
                   $VCardStringHttp = my_str_check_http($VCardString);

                   // Card footer
                   $outputViewMode .= "  <div style=\"". $styleFooter ."\"><span class=\"" . $styleFontFooter . "\">\n";
                   $outputViewMode .= "    &nbsp;<a href=\"javascript:vCardOfMetaFieldObject('" . $VCardStringJs . "');\"><img src=\"../images/vcard.gif\" border=\"0\" height=\"20\" width=\"20\"></a>&nbsp;&nbsp;&nbsp;";
                   $outputViewMode .= "    &nbsp;<a href=\"javascript:vCardSendPerMail('" . urlencode($VCardStringHttp) . "','" . urlencode($VCard["n_given"]) . " " . urlencode($VCard["n_family"]) . "','vcf');\"><img src=\"../images/vcard.gif\" border=\"0\" height=\"20\" width=\"20\"></a>&nbsp;&nbsp;&nbsp;";
                   $outputViewMode .= "    &nbsp;<a href=\"javascript:removeMetaFieldObject('" . ec($mtfvKeysArray[$i]) . "');\"><img src=\"../images/waste.png\" border=\"0\" height=\"20\" width=\"16\"></a>&nbsp;";
                   $outputViewMode .= "  </span></div>\n";
                   $outputViewMode .= "</div>\n";
               endfor;

            elseif ($category == "10") :
                   // *** [Newsletter] ***

                   // $viewItemsMode = "0";
                   $statusMessage = getLngt("Für die Newsletter steht keine Kartenansicht zur Verfügung! Bitte zur Listenansicht wechseln!");
            endif;
         else :

            // *** Default output as a list, table with header ***

            $lineToggler = 0;
            $remObjId = "";
            $tableOfRows .= "<tr>";
            while ($row = $result->fetch_assoc()):

                  if ($remObjId == "" || $remObjId != $row["mtfv_id"]) :
                     if ($remObjId != "") :
                        $tableOfRows .= "<td align=\"center\" ".$htmlClass02." bgcolor=\"" . $cellColor ."\"><a href=\"javascript:removeMetaFieldObject('" . ec($remObjId) . "');\">&nbsp;<img src=\"../images/waste.png\" border=\"0\" height=\"15\" width=\"12\">&nbsp;</a></td>";
                        $tableOfRows .= "</tr><tr>";
                     endif;
                     $numOfRows++;
                     $remObjId = $row["mtfv_id"];

                     if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
                     $cellColor = getListColor($numOfRows, $lineToggler);
                     $tableOfRows .= "<td align=\"center\" ".$htmlClass02." bgcolor=\"" . $cellColor ."\"><a href=\"javascript:openMetaFieldSpecial('" . ec($remObjId) . "');\">&nbsp;<img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\">&nbsp;</a></td>";
                  endif;

                  $valueToBeDisplayed = $row["mtfv_value"];

                  if ($row["mtfk_type"] == "co2cs") :
                     $tmpCsId = $valueToBeDisplayed;
                     $tmpCmpId = getFieldValueFromId("customer", "cs_id", $tmpCsId, "cmp_id");
                     $tmpCmpComp1 = trim(getFieldValueFromId("company", "cmp_id", $tmpCmpId, "cmp_comp"));

                     $valueToBeDisplayed = getFieldValueFromId("customer", "cs_id", $valueToBeDisplayed, "cs_eid");
                     if ($valueToBeDisplayed != "" && $tmpCmpComp1 != "") :
                        $valueToBeDisplayed .= "<br>[" . trim($tmpCmpComp1) . "]";
                     endif;

                  elseif ($row["mtfk_type"] == "checkboxes_hq") :
                     $tmpHqArray = spliti(",", $row["mtfv_value"]);
                     $tmpHqArrayLen = count($tmpHqArray);
                     for ($i = 0; $i < $tmpHqArrayLen; $i++) :
                         $tmpHqArray[$i] = getFieldValueFromId("headquarters","hq_id",$tmpHqArray[$i],"hq_mnemonic");
                     endfor;
                     $valueToBeDisplayed = implode(",",$tmpHqArray);

                  elseif (substr($row["mtfk_type"],0,7) == "select:") :  // SELECT FIELD
                     $subTypeArray = spliti(":",$row["mtfk_type"]);
                     $table = $subTypeArray[1];
                     if ($table == "vht") :
                        $valueToBeDisplayed = getFieldValueFromClause("metatype","mt_value","mt_type = 'vehicletype' AND mt_sort = '" . $row["mtfv_value"] . "' AND mt_objid = '0'");
                     endif;
                     if ($table == "grp") :
                        $valueToBeDisplayed = getFieldValueFromId("groups","grp_id",$row["mtfv_value"],"grp_name");
                     endif;
                     if ($table == "br") :
                        $valueToBeDisplayed = getFieldValueFromId("branch","br_id",$row["mtfv_value"],"br_name");
                     endif;
                  endif;

                  $tableOfRows .= "<td ".$htmlClass02." bgcolor=\"" . $cellColor ."\">&nbsp;" . $valueToBeDisplayed . "</td>";
            endwhile;
            $tableOfRows .= "<td align=\"center\" ".$htmlClass02." bgcolor=\"" . $cellColor ."\"><a href=\"javascript:removeMetaFieldObject('" . ec($remObjId) . "');\">&nbsp;<img src=\"../images/waste.png\" border=\"0\" height=\"15\" width=\"12\">&nbsp;</a></td>";
            $tableOfRows .= "</tr>";
            $result->free();
         endif;
      endif;

   else :
     $statusMessage = getLngt("Es erfolgte keine Suche!");
   endif;
endif;

$headerOps = "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
$headerOps .= "<a href=\"javascript:openMetaFieldSpecial('');\">&nbsp;" . "Neuer Eintrag" . "&nbsp;</a>";
$pageHeadline = "<span class=\"f12bp1_blue\">" . ($pageTitel != "" ? strtoupper($pageTitel) . "</span>" : strtoupper($mtfcDescription) . "</span>" . " &nbsp;&nbsp;&nbsp;&nbsp;" . $mtfObjAddressArr["comp"] . ($mtfObjAddressArr["comp2"] != "" ? " :: " . $mtfObjAddressArr["comp2"] : ""));
?>

<?php if ($includeMode == "") : ?>
<html>

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

<style type="text/css">
  <?php include_once ("../css/phoenix.css.php"); ?>
  <?php include_once ("../css/navigation.css.php"); ?>
  <?php include_once ("../css/mc.css.php"); ?>
</style>

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

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

function setFocusContactSearch() {
  document.forms[0].f_search.focus();
};

function clearFields() {
  if (document.forms[0].f_search) {document.forms[0].f_search.value = ''};
};
/*
function clearFields() {
	<?php
	  for ($j = 0; $j < $mtfck_mtfckIdsLen; $j++) :
		    echo "if (document.forms[0].f_" . $j . ") {document.forms[0].f_" . $j . ".value = ''};";
	  endfor;
	?>
};
*/

function openMetaFieldSpecial(objId) {
  var widthPopupWin = 1024;
  var heightPopupWin = 900;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  if (objId != '') {
     popupWin = window.open("../admin/metafield_special.php?deactivateMenu=1&category=<?php echo ec($category); ?>&specialPageTitle=<?php echo $categoryPageTitel ?>&objId=" + objId ,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
  } else {
  	// Preset field values for new entries only
  	 popupWin = window.open("../admin/metafield_special.php?deactivateMenu=1&category=<?php echo ec($category); ?>&specialPageTitle=<?php echo $categoryPageTitel ?>&objId=" + objId + "&callObjId=<?php echo $callObjId ?>&presetFields=<?php echo urlencode($presetFields) ?>" ,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
  }
};

function removeMetaFieldObject(objId) {
	if (confirm('<?php echo getLngt("Möchten Sie den Eintrag wirklich löschen?") ?>')) {
	   document.forms[0].f_act_mtfl.value='remove';
	   document.forms[0].f_objIdRemove.value=objId;
	   document.forms[0].submit();
	};
};

function vCardOfMetaFieldObject(VCard) {
	// var outVCard = VCard.join("<br>");
	// document.forms[0].f_vcard_text.value = outVCard;
	document.forms[0].f_vcard_text.value = VCard;
};

function vCardSendPerMail(VCard,file_name,file_ext) {
	// vCardOfMetaFieldObject(VCard);
	email = document.forms[0].f_vcard_email.value;
	if (email != '') {
	   if (confirm('<?php echo getLngt("Die angezeigte VCard wird an die folgende Mailadresse gesendet:") ?> ' + email)) {
        var widthPopupWin = 700;
        var heightPopupWin = 300;
        var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
        var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
        var popupWin;
        popupWin = window.open("../include/email_send.php?f_act=mailsend&f_closeWin=1&f_attachment=" + encodeURI(VCard) + "&f_subject=VCard&f_email=" + email + "&f_mimetype=text/x-vcard&f_fileName=" + encodeURI(file_name) + "&f_extension=" + file_ext ,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
     }
  } else {
  	alert('<?php echo getLngt("Bitte tragen Sie eine Emailadresse ein!") ?>');
  }
};

function searchContactPrefix(letter) {
  document.forms[0].f_act_mtfl.value = 'search';
  document.forms[0].f_letter.value = letter;
  document.forms[0].submit();
}
-->
</script>
</head>

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

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

  <div class="mc_page-header">
    <?php echo $pageHeadline ?>
  </div>

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

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

<?php endif; ?>

  <input type="hidden" name="f_act_mtfl" value="">
  <input type="hidden" name="category" value="<?php echo $category ?>">
  <input type="hidden" name="orderClause" value="<?php echo $orderClause ?>">
  <input type="hidden" name="categoryPageTitel" value="<?php echo $categoryPageTitel ?>">
  <input type="hidden" name="f_objIdRemove" value="">

  <input type="hidden" name="callMode" value="<?php echo $callMode ?>">
  <input type="hidden" name="callObjId" value="<?php echo $callObjId ?>">
  <input type="hidden" name="presetFields" value="<?php echo $presetFields ?>">

  <input type="hidden" name="f_letter" value="">

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

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

  <div>
    <a href="javascript:document.forms[0].f_act_mtfl.value='search';document.forms[0].submit();">&nbsp;<?php echo getLngt("Suchen") ?>&nbsp;</a>
    &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
    <a href="javascript:clearFields();"><?php echo getLngt("Suchbegriff zurücksetzen") ?></a>
    &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
    <?php echo getLngt("Option:") ?>&nbsp;&nbsp;
    <input type="radio" name="f_searchmode" value="0" <?php if ($f_searchmode == "0") : echo "checked"; endif; ?>> <?php echo getLngt("Präfix") ?>
    <input type="radio" name="f_searchmode" value="1" <?php if ($f_searchmode == "1") : echo "checked"; endif; ?>> <?php echo getLngt("Teilwort") ?>
    <?php echo $headerOps ?>
    &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
    <?php echo getLngt("Ansicht:") ?>&nbsp;&nbsp;
    <input type="radio" name="viewItemsMode" value="0" <?php if ($viewItemsMode == "0") : echo "checked"; endif; ?>> <?php echo getLngt("Liste") ?>
    <input type="radio" name="viewItemsMode" value="1" <?php if ($viewItemsMode == "1") : echo "checked"; endif; ?>> <?php echo getLngt("Karten") ?>
  </div>

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

  <?php if ($viewItemsMode == "1") :  ?>
    <div style="float:left;width:300px;"><input type="text" <?php echo $htmlClass01 ?> name="f_search" value="<?php echo $f_search ?>" size="35" placeholder="<?php echo getLngt("Suchbegriff") ?>"></div>
    <?php
      echo defineButtonType08(getLngt("A"), "action_A", "searchContactPrefix('A');", "20", "left", "2");
      echo defineButtonType08(getLngt("B"), "action_B", "searchContactPrefix('B');", "20", "left", "2");
      echo defineButtonType08(getLngt("C"), "action_C", "searchContactPrefix('C');", "20", "left", "2");
      echo defineButtonType08(getLngt("D"), "action_D", "searchContactPrefix('D');", "20", "left", "2");
      echo defineButtonType08(getLngt("E"), "action_E", "searchContactPrefix('E');", "20", "left", "2");
      echo defineButtonType08(getLngt("F"), "action_F", "searchContactPrefix('F');", "20", "left", "2");
      echo defineButtonType08(getLngt("G"), "action_G", "searchContactPrefix('G');", "20", "left", "2");
      echo defineButtonType08(getLngt("H"), "action_H", "searchContactPrefix('H');", "20", "left", "2");
      echo defineButtonType08(getLngt("I"), "action_I", "searchContactPrefix('I');", "20", "left", "2");
      echo defineButtonType08(getLngt("J"), "action_J", "searchContactPrefix('J');", "20", "left", "2");
      echo defineButtonType08(getLngt("K"), "action_K", "searchContactPrefix('K');", "20", "left", "2");
      echo defineButtonType08(getLngt("L"), "action_L", "searchContactPrefix('L');", "20", "left", "2");
      echo defineButtonType08(getLngt("M"), "action_M", "searchContactPrefix('M');", "20", "left", "2");
      echo defineButtonType08(getLngt("N"), "action_N", "searchContactPrefix('N');", "20", "left", "2");
      echo defineButtonType08(getLngt("O"), "action_O", "searchContactPrefix('O');", "20", "left", "2");
      echo defineButtonType08(getLngt("P"), "action_P", "searchContactPrefix('P');", "20", "left", "2");
      echo defineButtonType08(getLngt("Q"), "action_Q", "searchContactPrefix('Q');", "20", "left", "2");
      echo defineButtonType08(getLngt("R"), "action_R", "searchContactPrefix('R');", "20", "left", "2");
      echo defineButtonType08(getLngt("S"), "action_S", "searchContactPrefix('S');", "20", "left", "2");
      echo defineButtonType08(getLngt("T"), "action_T", "searchContactPrefix('T');", "20", "left", "2");
      echo defineButtonType08(getLngt("U"), "action_U", "searchContactPrefix('U');", "20", "left", "2");
      echo defineButtonType08(getLngt("V"), "action_V", "searchContactPrefix('V');", "20", "left", "2");
      echo defineButtonType08(getLngt("W"), "action_W", "searchContactPrefix('W');", "20", "left", "2");
      echo defineButtonType08(getLngt("X"), "action_X", "searchContactPrefix('X');", "20", "left", "2");
      echo defineButtonType08(getLngt("Y"), "action_Y", "searchContactPrefix('Y');", "20", "left", "2");
      echo defineButtonType08(getLngt("Z"), "action_Z", "searchContactPrefix('Z');", "20", "left", "2");
    ?>
    <?php echo htmlDivLineSpacer("20px"); ?>
    <?php echo $specialCallMode ?>
    <?php echo htmlDivLineSpacer("20px"); ?>
    <?php echo $outputViewMode ?>
    <?php echo htmlDivLineSpacer("20px", "", "left"); ?>

    <?php if ($mtfvKeysArrayLen > 0) : ?>
      <div><span class="f8np1"><?php echo getLngt("VCard:") ?></span></div>
      <?php echo htmlDivLineSpacer("5px"); ?>
      <div><textarea name="f_vcard_text" cols="70" rows="10"></textarea></div>
      <?php echo htmlDivLineSpacer("5px"); ?>
      <div><?php echo getLngt("Mailadresse:") ?> <input type="text" name="f_vcard_email" value="<?php echo $f_vcard_email ?>" size="49" maxlength="50"></div>
    <?php endif; ?>

  <?php else : ?>
    <div>
      <table class="f8np1" border="0" cellpadding="0">
        <tr>
          <?php echo $tableHeaderSearchFields ?>
        </tr>
        <?php echo $specialCallMode ?>
        <tr>
          <?php echo $tableHeaderLinks ?>
        </tr>
        <?php echo $tableOfRows ?>
      </table>
    </div>

  <?php endif; ?>

  <?php echo htmlDivLineSpacer("10px"); ?>
<!--
  <div>
    <?php echo getLngt("Anzahl Einträge:") ?> <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act_mtfl == "search" && $statusMessage == "") : echo " " . getLngt("(Keine Einträge gefunden.)"); endif; ?>
  </div>
-->
<?php if ($includeMode == "") : ?>
  </form>

  </div>

  </body>

  </html>
<?php endif; ?>
