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


include_once ("../include/global.inc.php");
include_once ("../include/inc_job.inc.php");


// JQuery parameters
if ($htmlDefaultCharset == "") : $htmlDefaultCharset = "iso-8859-1"; endif;
$metaCharset = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" . $htmlDefaultCharset . "\">\n";
$constMaincontentBackground = "#CDD9FD";
$colorWhite = "#FFFFFF";
$colorBlue = "#0000FF";
$dataTheme = "data-theme=\"b\"";
$dataPositionFixed = "data-position=\"fixed\"";
$dataTransition = "data-transition=\"fade\"";

// *********************************************

$dbhostArray = getGlobalDbEnvironments();
// Live server
$dbhostLiveArray = array("stb_live","htg_live","htm_live","hha_live","umzug_live","as_intern");
if ($dbhostArray["stb_live"] == "") : $dbhostArray["stb_live"] = "172.16.0.102:3391"; endif;
if ($dbhostArray["htg_live"] == "") : $dbhostArray["htg_live"] = "172.16.0.102:3511"; endif;
if ($dbhostArray["moebel_live"] == "") : $dbhostArray["moebel_live"] = "172.16.0.103:3611"; endif;
if ($dbhostArray["htm_live"] == "") : $dbhostArray["htm_live"] = "172.16.0.102:3651"; endif;
if ($dbhostArray["umzug_live"] == "") : $dbhostArray["umzug_live"] = "172.16.0.103:3661"; endif;
if ($dbhostArray["hha_live"] == "") : $dbhostArray["hha_live"] = "172.16.0.104:3281"; endif;
if ($dbhostArray["as_intern"] == "") : $dbhostArray["as_intern"] = "172.16.0.111:3711"; endif;
// Replicants
$dbhostReplicationArray = array("stb_repl_sb","stb_repl_xx","stb_repl_xz1","htg_repl_sb","htg_repl_xx","htg_repl_xz1","moebel_repl_sb","moebel_repl_xz1","htm_repl_xx","htm_repl_xz1","as_repl_xy","as_repl_xz1");
if ($dbhostArray["stb_repl_sb"] == "") : $dbhostArray["stb_repl_sb"] = "172.16.0.106:3399"; endif;
if ($dbhostArray["stb_repl_xx"] == "") : $dbhostArray["stb_repl_xx"] = "172.16.0.111:3391"; endif;
if ($dbhostArray["stb_repl_xz1"] == "") : $dbhostArray["stb_repl_xz1"] = "172.16.0.109:3391"; endif;
if ($dbhostArray["htg_repl_sb"] == "") : $dbhostArray["htg_repl_sb"] = "172.16.0.106:3519"; endif;
if ($dbhostArray["htg_repl_xx"] == "") : $dbhostArray["htg_repl_xx"] = "172.16.0.111:3511"; endif;
if ($dbhostArray["htg_repl_xz1"] == "") : $dbhostArray["htg_repl_xz1"] = "172.16.0.109:3511"; endif;
if ($dbhostArray["moebel_repl_sb"] == "") : $dbhostArray["moebel_repl_sb"] = "172.16.0.106:3611"; endif;
if ($dbhostArray["moebel_repl_xz1"] == "") : $dbhostArray["moebel_repl_xz1"] = "172.16.0.109:3611"; endif;
if ($dbhostArray["htm_repl_xx"] == "") : $dbhostArray["htm_repl_xx"] = "172.16.0.111:3651"; endif;
if ($dbhostArray["htm_repl_xz1"] == "") : $dbhostArray["htm_repl_xz1"] = "172.16.0.109:3651"; endif;
if ($dbhostArray["as_repl_xy"] == "") : $dbhostArray["as_repl_xy"] = "172.16.0.103:3711"; endif;
if ($dbhostArray["as_repl_xz1"] == "") : $dbhostArray["as_repl_xz1"] = "172.16.0.109:3711"; endif;
// Test server
$dbhostTestArray = array("stb_test_misc","htg_test_misc","moebel_test","htm_test");
if ($dbhostArray["stb_test_misc"] == "") : $dbhostArray["stb_test_misc"] = "172.16.0.104:3211"; endif;
if ($dbhostArray["htg_test_misc"] == "") : $dbhostArray["htg_test_misc"] = "172.16.0.104:3221"; endif;
if ($dbhostArray["moebel_test"] == "") : $dbhostArray["moebel_test"] = "172.16.0.103:3621"; endif;
if ($dbhostArray["stb_test_intern_misc"] == "") : $dbhostArray["stb_test_intern_misc"] = "172.16.0.104:3241"; endif;

// *********************************************

$sqlStmtArray = array();

$sqlStmtArray["cs"] = 
  array("cmp.cmp_id","cmp.cmp_comp","cmp.cmp_comp2","cmp.cmp_iln","cmp.cmp_tax_idno","cmp.cmp_stax_idno","cmp.tx_id","cmp.br_id",
        "cmp.cmp_hsno","cmp.cmp_bank","cmp.cmp_bankno","cmp.cmp_bankacc","cmp.cmp_iban","cmp.cmp_swift","cmp.cmp_hqlino","cmp.cmp_match","cmp.cmp_match2",
        "cmp.cmp_remark","cmp.cmp_remark2","cmp.cmp_newsletter","cmp.cmp_bankmode","cmp.cmp_postage","cmp.cmp_inv2hq",
        "cmp.cmp_authenticated","cmp.cmp_visible","cmp.cmp_cashmode",
        "cs.cs_id","cs.cs_eid","cs.hq_id","cs.cs_id_parent","cs.cs_discount","cs.cs_invmode","cs.cs_jbedit",
        "cs.cs_jbstatusmail2csc","cs.cs_jbstatusmail_price","cs.cs_jbstatusmail_emp","cs.cs_jbstatusmail_pdf",
        "cs.cs_jbstatusmail_fields","cs.cs_jbstatusmail","cs.cs_jbstatusmail2","cs.cs_jbstatusmail3",
        "cs.cs_commission_no","cs.cs_cr_fav_only","cs.cs_jobbatch","cs.cs_invtext_mode","cs.csc_id",
        "cs.csc_id_payer","cs.cs_fixprice_discount","cs.cs_prov","cs.cs_markup","cs.cs_markup_prov",
        "usr.usr_id","usr.usr_name","usr.usr_firstname","usr.usr_email",
        "usr.usr_phone","usr.usr_phone2","usr.usr_fax","usr.usr_account",
        "ad.ad_id","ad.ad_street","ad.ad_zipcode","ad.ad_city","ad.ad_country"
       );

$sqlStmtArray["cr"] = 
  array("cr.cr_id","cr.cr_eid","cr.cr_mobile_pda","usr.usr_id","usr.usr_firstname","usr.usr_name","usr.usr_account",
        "cmp.cmp_id", "cmp.cmp_comp", "cmp.cmp_comp2"
       );

$sqlStmtArray["crvh"] = 
  array("cr.cr_id","cr.cr_eid","usr.usr_id","usr.usr_account","usr.usr_firstname","usr.usr_name",
        "cmp.cmp_id","cmp.cmp_comp","cmp.cmp_comp2", 
        "crvh.crvh_id","crvh.vht_id","crvh.cr_id","crvh.crvh_sort","crvh.crvh_sid",
        "crvh.crvh_comm_no","crvh.crvh_vh_sign","crvh.crvh_vh_name","crvh.crvh_licence_date",
        "crvh.crvh_owner","crvh.crvh_env_badge_level","crvh.crvh_prov","crvh.crvh_payload",
        "crvh.crvh_totalweight","crvh.crvh_length","crvh.crvh_width","crvh.crvh_height",
        "crvh.crvh_position","crvh.crvh_mobile","crvh.crvh_insurance_name","crvh.crvh_insurance_no",
        "crvh.crvh_filter","crvh.crvh_remark","crvh.crvh_freetext_1","crvh.crvh_special"
       );

$sqlStmtArray["cs_list"] = 
  array("cmp.cmp_comp","cmp.cmp_comp2","cmp.cmp_iln","cmp.cmp_tax_idno","cmp.cmp_stax_idno",
        "cmp.cmp_hsno","cmp.cmp_bank","cmp.cmp_bankno","cmp.cmp_bankacc","cmp.cmp_iban","cmp.cmp_swift",
        "cmp.cmp_match","cmp.cmp_match2","cmp.cmp_remark","cmp.cmp_remark2",
        "cs.cs_eid","cs.cs_commission_no","cs.cs_jobbatch","cs.cs_invtext_mode","usr.usr_name","usr.usr_firstname","usr.usr_email",
        "usr.usr_phone","usr.usr_phone2","usr.usr_fax","usr.usr_account","ad.ad_street","ad.ad_zipcode","ad.ad_city"
       );

$sqlStmtArray["cr_list"] = 
  array("cr.cr_id","cr.cr_eid","usr.usr_firstname","usr.usr_name","usr.usr_account","cmp.cmp_comp","cmp.cmp_comp2");

$sqlStmtArray["crvh_list"] = 
  array("cr.cr_eid","usr.usr_firstname","usr.usr_name","cmp.cmp_comp", "cmp.cmp_comp2", 
        "crvh.crvh_sid","crvh.crvh_comm_no","crvh.crvh_vh_sign","crvh.crvh_vh_name","crvh.crvh_licence_date",
        "crvh.crvh_mobile","crvh.crvh_insurance_name","crvh.crvh_insurance_no",
        "crvh.crvh_filter","crvh.crvh_remark","crvh.crvh_freetext_1"
       );

$sqlStmtArray["jb"] = 
  array("jb.jb_id","jb.jb_status","jb.jb_ordertime","jb.jb_taketime","jb.jb_finishtime","jb.jb_export_time","jb.jb_booktime",
        "jb.jb_globaljob","jb.jb_reserv","jb.jb_permanent","jb.jb_permanent2","jb.jp_permenddat",
        "jb.jb_cr_fav","jb.jb_cr_filter","jb.jb_cr_filter_opt",
        "jb.cr_id","jb.cr_sid","jb.cr_id_order","jb.cr_id_permanent",
        "jb.jb_storno","jb.usr_id_storno",
        "jb.jb_fixprice","jb.jb_serviceprice","jb.jb_totalprice","jb.jb_cr_price","jb.jb_subtotalprice","jb.jb_cr_subprice","jb.jb_markup",
        "jb.jb_mediationarea_id","jb.jb_mediationarea_name","jb.jb_tourdata","jb.jb_id_parent","jb.jb_id_copy","jb.jb_id_copy_permanent",
        "jb.jb_automailsent","jb.jb_automailsent2","jb.jb_automailsent3",
        "jb.jb_cr_remark","jb.jb_dispoinfo","jb.jb_offer","jb.jb_version",
        "cs.cs_eid","cmp.cmp_comp","cmp.cmp_comp2",
        "cr.cr_eid","usr2.usr_name","usr2.usr_firstname","cmp2.cmp_comp AS cmp2_comp","cmp2.cmp_comp2 AS cmp2_comp2",
        "ad.ad_street","tr.tr_hsno","ad.ad_zipcode","ad.ad_city"
       );

$sqlStmtArray["jb_list"] = 
  array("jb.jb_id","jb.jb_status","jb.jb_ordertime","cs.cs_eid","cmp.cmp_comp",
        "cr.cr_eid","usr2.usr_name","usr2.usr_firstname","cmp2.cmp_comp",
        "ad.ad_street","tr.tr_hsno","ad.ad_zipcode","ad.ad_city"
       );

$sqlStmtArray["usr"] = 
  array("usr.usr_id","usr.usr_type","hq.hq_id","hq.hq_mnemonic","usr.usr_name","usr.usr_firstname","usr.usr_birthdate",
        "usr.usr_phone","usr.usr_phone2","usr.usr_fax","usr.usr_account",
        "emp.emp_id","emp.emp_rights"
       );

$sqlStmtArray["usr_list"] = 
  array("usr.usr_id","usr.usr_type","hq.hq_id","hq.hq_mnemonic","usr.usr_name","usr.usr_firstname","usr.usr_birthdate",
        "usr.usr_phone","usr.usr_phone2","usr.usr_fax","usr.usr_account"
       );

$sqlStmtArray["phone"] = 
  array("hq.hq_id","hq.hq_mnemonic","cr.cr_id","cr.cr_eid","cr.cr_mobile_pda",
        "usr.usr_id","usr.usr_account","usr.usr_firstname","usr.usr_name",
        "cmp.cmp_id","cmp.cmp_comp","cmp.cmp_comp2","crvh.crvh_id","crvh.vht_id","crvh.crvh_sid"
       );
       
$sqlStmtArray["log"] = 
  array("log.log_createtime","log.logo_id","logo.logo_op_name","logo.logo_op_shortname","log.hq_id","hq.hq_mnemonic","log.jb_id","log.usr_id","usr.usr_name","usr.usr_firstname",
        "log.cr_id","cr.cr_eid","log.cr_sid","log.cs_id","cs.cs_eid","log.at_id","at.at_eid","log.emp_id","log.logo_description"
       );


// Format mobile number
function eregPhoneNoToCDA ($phoneNo) {
	global $hq_id;
	// Get phone prefix of the country
  $parCountryPhonePrefix = getParameterValue("0", "COUNTRY_FON_PREFIX", $hq_id);
  if ($parCountryPhonePrefix == "") :
     $parCountryPhonePrefix = getParameterValue("0", "COUNTRY_FON_PREFIX", "0");
     if ($parCountryPhonePrefix == "") :
        $parCountryPhonePrefix = "49";
     endif;
  endif;
  // Prepare
  if ($phoneNo != "") :
     $phoneNo = preg_replace ("/ /", "", $phoneNo);  // Remove blanks
     $phoneNo = preg_replace ("/-/", "", $phoneNo);  // Remove sign "-"
     $phoneNo = preg_replace ("/^\+/", "00", $phoneNo);  // Remove country prefix sign "+" with "00"
     $phoneNo = preg_replace ("/^00" . $parCountryPhonePrefix . "/", "0", $phoneNo);  // Remove country prefix with "0"
     $phoneNo = preg_replace ("/^00/", "", $phoneNo);  // Remove leading "00"
     $phoneNo = preg_replace ("/^0/", "", $phoneNo);  // Remove leading "0"
  endif;
  return $phoneNo;
}

function execSqlResult ($db, $sqlStmt, $sqlFieldsArray = array(), $retArray = array(), $mode = "") {
	global $PHP_SELF;
  if ($sqlStmt != "" && count($sqlFieldsArray) > 0) :
     $sqlFieldsLen = count($sqlFieldsArray);
     $result = $db->query($sqlStmt);
     if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

     if ($result->numRows() > 0) :
        // Check for alias notation (e.g. "cmp2.cmp_comp AS cmp2_comp") and substitute (=> "cmp.cmp2_comp") for output
        for ($i = 0; $i < $sqlFieldsLen; $i++) :
            if (!(strpos($sqlFieldsArray[$i], " AS ") === FALSE)) :
               $sqlFieldsArray[$i] = substr($sqlFieldsArray[$i], 0, strrpos($sqlFieldsArray[$i],".") + 1) . substr($sqlFieldsArray[$i], strrpos($sqlFieldsArray[$i]," AS ") + 4);
            endif;
        endfor;
   
        // Output as list
        if ($mode == "2") :
           $tableHeader = "<tr>";
           for ($i = 0; $i < $sqlFieldsLen; $i++) :
               $dbField = $sqlFieldsArray[$i];
               if (!(strpos($dbField, ".") === FALSE)) : $dbField = substr($dbField, strrpos($dbField,".") + 1); endif;
               $tableHeader .= "<td>" . $dbField . "</td>\n";
           endfor;
           $tableHeader .= "</tr>\n";
           $retArray[] = $tableHeader;
        endif;

        while ($row = $result->fetch_assoc()):
              if ($mode == "2") :  // HTML table for the list output
                 $tableBody = "<tr>";
                 for ($i = 0; $i < $sqlFieldsLen; $i++) :
                     $dbField = $sqlFieldsArray[$i];
                     if (!(strpos($dbField, ".") === FALSE)) : $dbField = substr($dbField, strrpos($dbField,".") + 1); endif;
                     $tableBody .= "<td>" . $row[$dbField] . "</td>\n";
                 endfor;
                 $tableBody .= "</tr>\n";
                 $retArray[] = $tableBody;
              else :
                for ($i = 0; $i < $sqlFieldsLen; $i++) :
                    $dbField = $sqlFieldsArray[$i];
                    if (!(strpos($dbField, ".") === FALSE)) : $dbField = substr($dbField, strrpos($dbField,".") + 1); endif;
                    if ($mode == "1") :  // HTML table for key-value structure
                       $retArray[] = "<tr><td>" . $dbField . "</td><td>&nbsp;&nbsp;</td><td>" . $row[$dbField] . "</td></tr>\n";
                    else :
                      $retArray[] = array($dbField, $row[$dbField]);
                    endif;
                endfor;
              endif;
        endwhile;
     endif;
     $result->free();
  endif;
  return $retArray;
}


function getSqlResult ($type, $searchClause, $headquartes = "", $mode = "", $search_value_org = "") {
	global $db, $sqlStmtArray, $dbConnectionArray;
	$retArray = array();
	$sqlStmt = "";
	if ($type != "" && $searchClause != "") :	   

	   $sqlFieldsArray = $sqlStmtArray[$type];

	   // Overwrite search clause because list view is requested
	   if ($mode == "2" && $searchClause != "" && strlen($searchClause) >= 3) :
	      $sqlFieldsArray = $sqlStmtArray[$type . "_list"];
	      $searchClauseList = "";
	      $sqlFieldsArrayLen = count($sqlFieldsArray);
	      for ($i = 0; $i < $sqlFieldsArrayLen; $i++) :
	          $searchClauseList .= $sqlFieldsArray[$i] . " LIKE '%" . $searchClause . "%'";
	          if ($i < ($sqlFieldsArrayLen - 1)) : $searchClauseList .= " OR "; endif;
	      endfor;
	      if ($searchClauseList != "") : $searchClauseList = " (" . $searchClauseList . ") "; endif;
	      $searchClause = $searchClauseList;
	   endif;

     $searchClauseWithoutConjunction = $searchClause;
	   if ($searchClause != "") : $searchClause .= " AND "; endif;

	   $sqlFields = implode(",", $sqlFieldsArray);

     if ($type == "cs") :
        $sqlStmt = "SELECT " . $sqlFields
                         . " FROM company AS cmp, customer AS cs, employee AS emp, user AS usr, address AS ad"
                         . " WHERE " . $searchClause . ($headquartes != "" ? " cs.hq_id = '" . $headquartes . "' AND " : "")
                         . "       cmp.cmp_id = cs.cmp_id   AND"
                         . "       cs.cs_admin = emp.emp_id AND"
                         . "       emp.usr_id = usr.usr_id  AND"
                         . "       cmp.ad_id = ad.ad_id";
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);
     endif;

     if ($type == "cr") :
        $sqlStmt = "SELECT " . $sqlFields
                         . " FROM courier AS cr, user AS usr, company AS cmp"
                         . " WHERE " . $searchClause . ($headquartes != "" ? " cr.hq_id = '" . $headquartes . "' AND " : "")
                         . "       usr.usr_id = cr.usr_id AND"
                         . "       cmp.cmp_id = cr.cmp_id ";
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);

        // Add vehicles for a single courier. Do NOT execute if the courier list has been requested.
        if ($mode != "2") :
           $sqlFieldsArray = array("crvh.crvh_id","crvh.vht_id","crvh.cr_id","crvh.crvh_sort","crvh.crvh_sid","crvh.crvh_comm_no","crvh.crvh_vh_sign","crvh.crvh_vh_name");
           $sqlStmt = "SELECT " . implode(",", $sqlFieldsArray)
                            . " FROM couriervehicle AS crvh, courier AS cr"
                            . " WHERE " . $searchClause . ($headquartes != "" ? " cr.hq_id = '" . $headquartes . "' AND " : "")
                            . "       cr.cr_id = crvh.cr_id"
                            . " ORDER BY crvh.crvh_sid";
           $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);
        endif;
     endif;

     if ($type == "crvh") :
        $sqlStmt = "SELECT " . $sqlFields
                         . " FROM couriervehicle AS crvh, courier AS cr, user AS usr, company AS cmp"
                         . " WHERE " . $searchClause . ($headquartes != "" ? " cr.hq_id = '" . $headquartes . "' AND " : "")
                         . "       cr.cr_id = crvh.cr_id  AND"
                         . "       usr.usr_id = cr.usr_id AND"
                         . "       cmp.cmp_id = cr.cmp_id ";
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);
     endif;

     if ($type == "phone") :
        $ip = "";
        if ($search_value_org != "") :
           $ip = $dbConnectionArray["as_intern"]->getOne("SELECT cda.ip FROM mobiledata.cda AS cda WHERE cda.vodafone_nummer LIKE '%" . $search_value_org . "' ");
        endif;
        $sqlStmt = "SELECT " . $sqlFields . ", '" . $ip . "' AS mobile_ip"
                         . " FROM courier AS cr LEFT JOIN couriervehicle AS crvh ON cr.cr_id = crvh.cr_id LEFT JOIN user AS usr ON usr.usr_id = cr.usr_id, company AS cmp, headquarters AS hq"
                         . " WHERE " . $searchClause . ($headquartes != "" ? " cr.hq_id = '" . $headquartes . "' AND " : "")
                         . "       hq.hq_id = cr.hq_id    AND"
                         . "       cmp.cmp_id = cr.cmp_id ";
        // Extend array regarding the IP
        $sqlFieldsArray[] = "cr.mobile_ip";
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);
        if ($ip != "" && count($retArray) == 0) :
           $sqlFieldsArray = array("cda.vodafone_nummer","cda.ip");
           $sqlStmtPhone = "SELECT cda.vodafone_nummer, cda.ip FROM mobiledata.cda AS cda WHERE cda.vodafone_nummer LIKE '%" . $search_value_org . "' ";
           $retArray = execSqlResult($dbConnectionArray["as_intern"], $sqlStmtPhone, $sqlFieldsArray, $retArray, $mode);
        endif;
     endif;

     if ($type == "jb") :
        $sqlStmt = "SELECT " . $sqlFields
                         . " FROM job AS jb LEFT JOIN courier AS cr ON jb.cr_id = cr.cr_id "
                         . "                LEFT JOIN user AS usr2 ON cr.usr_id = usr2.usr_id"
                         . "                LEFT JOIN company AS cmp2 ON cr.cmp_id = cmp2.cmp_id,"
                         . "      tour AS tr, address AS ad, costcenter AS csc, customer AS cs, company AS cmp, user AS usr, employee AS emp"
                         . " WHERE " . $searchClause . ($headquartes != "" ? " cr.hq_id = '" . $headquartes . "' AND " : "")
                         . "       jb.jb_id = tr.jb_id          AND"
                         . "       tr.tr_sort = '1'             AND"
                         . "       tr.ad_id = ad.ad_id          AND"
                         . "       jb.csc_id_payer = csc.csc_id AND"
                         . "       csc.cs_id = cs.cs_id         AND"
                         . "       cs.cs_admin = emp.emp_id     AND"
                         . "       emp.usr_id = usr.usr_id      AND"
                         . "       cs.cmp_id = cmp.cmp_id";
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);
        if ($search_value_org != "" && count($retArray) > 0) :
           /*
           $sqlFieldsArray = $sqlStmtArray["log"];
           $sqlFields = implode(",", $sqlFieldsArray);
           $sqlStmtLogForJob = "SELECT " . $sqlFields . " FROM phoenix_log.log AS log WHERE log.jb_id = '" . $search_value_org . "' ORDER BY log.log_createtime";
           $retArray = execSqlResult($db, $sqlStmtLogForJob, $sqlFieldsArray, $retArray, "2");
           */
           $retArray = array_merge($retArray, array("</table><br><table>"), getSqlResult("log", "log.jb_id = '" . $search_value_org . "'", "", "", $search_value_org));
        endif;
     endif;

     if ($type == "usr") :
        $tmpFieldClause = ""; $tmpFromClause = ""; $tmpWhereClause = "";
        if ($mode != "2") :
           if (substr($searchClause,0,10) == "usr.usr_id") :
              $usrType = getFieldValueFromId("user", "usr_id", substr($searchClause,14,-6), "usr_type");
           endif;
           if (substr($searchClause,0,15) == "usr.usr_account") :
              $usrType = getFieldValueFromId("user", "usr_account", substr($searchClause,19,-6), "usr_type");
           endif;
           if ($usrType != "") :
              if ($usrType == "1") :
                 // Employee
                 // $sqlFields = implode(",", $sqlStmtArray["usr"]);
                 $tmpFromClause = ", employee AS emp, headquarters AS hq ";
                 $tmpWhereClause = " emp.usr_id = usr.usr_id AND usr.hq_id = hq.hq_id ";
              endif;
              if ($usrType == "2") :
                 // Customer
                 $sqlFieldsArray = $sqlStmtArray["cs"];
                 $sqlFields = implode(",", $sqlFieldsArray);
                 $tmpFromClause = ", company AS cmp, customer AS cs, employee AS emp, address AS ad, headquarters AS hq ";
                 $tmpWhereClause = " cmp.cmp_id = cs.cmp_id AND cs.cs_admin = emp.emp_id AND emp.usr_id = usr.usr_id AND usr.hq_id = hq.hq_id AND cmp.ad_id = ad.ad_id ";
              endif;
              if ($usrType == "3") :
                 // Courier
                 $sqlFieldsArray = $sqlStmtArray["cr"];
                 $sqlFields = implode(",", $sqlFieldsArray);
                 $tmpFromClause = ", courier AS cr, company AS cmp, headquarters AS hq ";
                 $tmpWhereClause = " usr.usr_id = cr.usr_id AND usr.hq_id = hq.hq_id AND cmp.cmp_id = cr.cmp_id ";
              endif;
           endif;
        endif;
        $sqlStmt = "SELECT " . $sqlFields . $tmpFieldClause
                         . " FROM user AS usr" . $tmpFromClause
                         . " WHERE " . $searchClause . $tmpWhereClause;
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, $mode);
     endif;

     if ($type == "log") :
        $sqlStmt = "SELECT " . $sqlFields
                         . " FROM phoenix_log.log AS log"
                         . "              LEFT JOIN phoenix_log.logoperation AS logo ON logo.logo_id = log.logo_id"
                         . "              LEFT JOIN phoenix.headquarters AS hq ON hq.hq_id = log.hq_id"
                         . "              LEFT JOIN phoenix.user AS usr ON usr.usr_id = log.usr_id"
                         . "              LEFT JOIN phoenix.courier AS cr ON cr.cr_id = log.cr_id"
                         . "              LEFT JOIN phoenix.customer AS cs ON cs.cs_id = log.cs_id"
                         . "              LEFT JOIN phoenix.article AS at ON at.at_id = log.at_id"
                         . " WHERE " . $searchClauseWithoutConjunction
                         . " ORDER BY log.log_createtime";
        $retArray = execSqlResult($db, $sqlStmt, $sqlFieldsArray, $retArray, "2");
     endif;
  endif;
  return $retArray;
}                     


?>
