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



// Gets DB data
function getDBData ($dbTable, $id, $dbConnection = "") {
	global $db, $PHP_SELF;
  global $dbData, $jobData, $hq_id, $usr_id, $emp_id;
echo "<br><br>";
  if ($dbTable != "") :  
     if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;
     // Map virtual table names to real table names
     $dbTableGen = $dbTable;
     if ($dbTableGen == "gdc_jb" || $dbTableGen == "gdc_tr" || $dbTableGen == "gdc_jb_tr") :
        $dbTableGen = "genericdatacontainer";
     endif;
     if ($dbTableGen == "asd_as") :
        $dbTableGen = "assetdisposition";
     endif;
     if (substr($dbTableGen,0,4) == "asr_") :
        $dbAsrType = substr($dbTableGen,4) . "";
        $dbTableGen = "assetrelation";
     endif;
     if (substr($dbTableGen,0,4) == "log_") :
        $dbLogIndexField = substr($dbTableGen,4) . "_id";
        $dbTableGen = "log";
     endif;
     // Get DB fields
     $dbFields = getDBFields($dbTableGen);
     if ($dbFields != "") :
        $sqlStmt = "";
        if ($dbTable == "job") : 
           $sqlStmt = generateStatement($dbFields, "job AS jb", "jb.jb_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "tour") :
           // $sqlStmt = generateStatement($dbFields, "tour AS tr", "tr.jb_id = '" . $id . "'", "tr.tr_sort");
           $dbFields .= "," . getDBFields("address") . "," . getDBFields("ad_street_hsno");
           $sqlStmt = generateStatement($dbFields, "tour AS tr, address AS ad", "tr.jb_id = '" . $id . "' AND tr.ad_id = ad.ad_id", "tr.tr_sort");
        endif;
        if ($dbTable == "tourservice") :
           $sqlStmt = generateStatement($dbFields, "tourservice AS trs", "trs.jb_id = '" . $id . "'", "trs.tr_sort");
        endif;
        if ($dbTable == "tourarticle") :
           $dbFields .= "," . getDBFields("article");
           $sqlStmt = generateStatement($dbFields, "tourarticle AS trat LEFT JOIN article AS at ON trat.at_id = at.at_id", "trat.jb_id = '" . $id . "'", "trat.jb_id, trat.tr_sort");
        endif;
        if ($dbTable == "tourarticleprocess") :
           $sqlStmt = generateStatement("trat.trat_id," . $dbFields, "tourarticle AS trat, tourarticleprocess AS tratp", "trat.jb_id = '" . $id . "' AND trat.trat_id = tratp.trat_id", "tratp.trat_id");
        endif;
        if ($dbTable == "user") : 
           $sqlStmt = generateStatement($dbFields, "job AS jb, user AS usr", "jb.jb_id = '" . $id . "' AND jb.emp_id = usr.usr_id", "");
        endif;
        if ($dbTable == "genericdatacontainer") : 
           $sqlStmt = generateStatement($dbFields, "genericdatacontainer AS gdc", "gdc.gdc_obj_type = 'jb' AND gdc.gdc_obj_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "gdc_jb") : 
           $sqlStmt = generateStatement("gdc.gdc_obj_id, gdc.gdc_obj_type, " . $dbFields, "genericdatacontainer AS gdc", "gdc.gdc_obj_type = 'jb' AND gdc.gdc_obj_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "gdc_tr") : 
           $sqlStmt = generateStatement("gdc.gdc_obj_id, gdc.gdc_obj_type, " . $dbFields, "genericdatacontainer AS gdc", "gdc.gdc_obj_type = 'tr' AND gdc.gdc_obj_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "gdc_jb_tr") :
           // Get all "tr_id" from "id" ("id" <=> "jb_id")
           $tmpArray = getColVectorFromDB2ArrayByClause("tour", "tr_id", "jb_id = '" . $id . "'", "", "", "");
           if (count($tmpArray) > 0) :
              $sqlStmt = generateStatement($dbFields, "genericdatacontainer AS gdc", "gdc.gdc_obj_type = 'tr' AND gdc.gdc_obj_id IN (" . implode(",", $tmpArray) . ")", "");
           endif;
        endif;
        if ($dbTableGen == "log") :  // Here "$dbTableGen" because wrapped above ("log_jb", etc. to "log") with index field name "$dbLogIndexField"
           $dbFields .= "," . getDBFields("logoperation");
           $sqlStmt = generateStatement($dbFields, "phoenix_log.log AS log, phoenix_log.logoperation AS logo", "log." . $dbLogIndexField . " = '" . $id . "' AND log.logo_id = logo.logo_id", "log.log_createtime");
        endif;
        if ($dbTable == "address") :
           $sqlStmt = generateStatement($dbFields, "address AS ad", "ad.ad_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "article") :
           $sqlStmt = generateStatement($dbFields, "article AS at", "at.at_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "costcenter") :
           $sqlStmt = generateStatement($dbFields, "costcenter AS csc", "csc.csc_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "costcenteraddress") :
           $dbFields .= "," . getDBFields("address");
           $sqlStmt = generateStatement($dbFields, "costcenteraddress AS cscad, address AS ad", "cscad.csc_id = '" . $id . "' AND cscad.ad_id = ad.ad_id", "cscad.adt_id");
        endif;
        if ($dbTable == "customer") :
           $sqlStmt = generateStatement($dbFields, "customer AS cs", "cs.cs_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "courier") :
           $sqlStmt = generateStatement($dbFields, "courier AS cr", "cr.cr_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "company") :
           $sqlStmt = generateStatement($dbFields, "company AS cmp", "cmp.cmp_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "couriervehicle") :
           $sqlStmt = generateStatement($dbFields, "couriervehicle AS crvh", "crvh.crvh_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "courierfilter") :
           $sqlStmt = generateStatement($dbFields, "courierfilter AS crf", "crf.crf_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "asset") : 
           $sqlStmt = generateStatement($dbFields, "asset AS ast", "ast.as_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "assetrelation") : 
           $dbFields .= "," . getDBFields("asset");
           $sqlStmt = generateStatement($dbFields, "assetrelation AS asr, asset AS ast", "asr.as_id = '" . $id . "' AND asr.as_id_ref = ast.as_id", "asr.asr_type");
        endif;
        if ($dbTableGen == "assetrelation" && $dbAsrType != "") :  // Here "$dbTableGen" because wrapped above ("asr_owner", etc. to "assetrelation") with asset relation type "$dbAsrType"
           $dbFields .= "," . getDBFields("asset");
           $sqlStmt = generateStatement($dbFields, "assetrelation AS asr, asset AS ast", "asr.as_id = '" . $id . "' AND asr.asr_type = '" . $dbAsrType . "' AND asr.as_id_ref = ast.as_id", "asr.as_id_ref");
        endif;
        if ($dbTable == "assetdisposition") : 
           $sqlStmt = generateStatement($dbFields, "assetdisposition AS asd", "asd.asd_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "asd_as") : 
           $sqlStmt = generateStatement($dbFields, "assetdisposition AS asd", "asd.as_id = '" . $id . "'", "");
        endif;
        if ($sqlStmt != "") :
           $result = $dbConnection->query($sqlStmt);     
           if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
	         // Try to substitute "CONCAT(....) AS "
           $dbFields = preg_replace('/CONCAT\([^)]*\) AS /i', 'xxxx.', $dbFields);
           $tmpArray = spliti(",", $dbFields);
           $tmpArrayLen = count($tmpArray);
           $count = 0;
           while ($row = $result->fetch_assoc()):
                 for ($j = 0; $j < $tmpArrayLen; $j++) :
                     $tmpFieldName = getDBFieldsNameWithoutAlias($tmpArray[$j]);
// echo $j . ". " . $tmpArray[$j] . " | " . $tmpFieldName . "<br>";
                     if ($dbTable == "job") :
                        $dbData["job"][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["job"][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "tour") :
                        $dbData["tour"][$row["tr_sort"]][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["tour"][$row["tr_sort"]][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "tourservice") :
                        $dbData["tourservice"][$row["tr_sort"]][$count][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["tourservice"][$row["tr_sort"]][$count][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "tourarticle") :
                        $dbData["tourarticle"][$row["tr_sort"]][$count][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["tourarticle"][$row["tr_sort"]][$count][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "tourarticleprocess") :
                        $dbData["tourarticleprocess"][$row["trat_id"]][$row["tratp_type"]][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["tourarticleprocess"][$row["trat_id"]][$row["tratp_type"]][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "user") :
                        $dbData["user"][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["user"][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "genericdatacontainer") :
                        $dbData["gdc_jb"][$row["gdc_gen_fieldname"]][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["gdc_jb"][$row["gdc_gen_fieldname"]][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "gdc_jb" || $dbTable == "gdc_tr" || $dbTable == "gdc_jb_tr") :
                        $dbData["gdc"][$row["gdc_obj_type"]][$row["gdc_obj_id"]][$row["gdc_gen_fieldname"]][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["gdc"][$row["gdc_obj_type"]][$row["gdc_obj_id"]][$row["gdc_gen_fieldname"]][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "log_jb") :
                        $dbData["log"][$count][$tmpFieldName] = $row[$tmpFieldName];
                        $jobData["log"][$count][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "costcenteraddress") :
                        $dbData["costcenteraddress"]["addresstype_" . $row["adt_id"]][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "assetrelation" || substr($dbTable,0,4) == "asr_") :
                        $dbData[$dbTable][$count][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "assetdisposition" || $dbTable == "asd_as") :
                        $dbData["assetdisposition"][$count][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "address" || $dbTable == "article" || $dbTable == "costcenter" || $dbTable == "customer" || $dbTable == "courier" || $dbTable == "company" || $dbTable == "couriervehicle" || $dbTable == "asset") :
                        $dbData[$dbTable][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                 endfor;
                 $count++;
           endwhile;
           $result->free();
        endif;
     endif;
  endif;
}


// Gets DB data by search
function getDBDataBySearch ($dbTable, $dbFieldsSpecial = array(), $dbFromSpecial = array(), $dbWhereSpecial = array(), $whereClauseSpecial = "", $orderByClauseSpecial = "", $dbConnection = "") {
	global $db, $PHP_SELF;
  global $dbData, $hq_id, $usr_id, $emp_id;

  if ($dbTable != "") :  
     if (!is_object($dbConnection) || $dbConnection == "") : $dbConnection = $db; endif;     
     $dbTableGen = $dbTable;
     $dbFieldsSpecialLen = count($dbFieldsSpecial);
     $dbFromSpecialLen = count($dbFromSpecial);
     $dbWhereSpecialLen = count($dbWhereSpecial);
     // Map virtual table names to real table names
     // if ($dbTableGen == "gdc_jb") :
     //    $dbTableGen = "genericdatacontainer";
     // endif;
     // Get default DB fields if $parFields is empty
     $addDbDefaultFields = false;
     if ($dbFieldsSpecialLen > 0) :
        $dbFields = implode(",", $dbFieldsSpecial);
     else :
        $dbFields = getDBFields($dbTableGen);
        $addDbDefaultFields = true;
     endif;
     $dbFields = getDBFields($dbTableGen);

     if ($dbFields != "") :
        // Init
        $sqlStmt = "";
        $fromClause = "";
        $whereClause = "";

        // Generate FROM clause
        // [Better "implode()" but special cases with ALIAS could be added!]
        for ($i = 0; $i < $dbFromSpecialLen; $i++) :
          if ($fromClause != "") : $fromClause .= ", "; endif;
          $fromClause .= $dbFromSpecial[$i];
        endfor;

        // Generate WHERE clause
        $dbWhereKeyArray = array_keys($dbWhereSpecial);
        $dbWhereKeyArrayLen = count($dbWhereKeyArray);
        for ($i = 0; $i < $dbWhereKeyArrayLen; $i++) :
          if ($whereClause != "") : $whereClause .= " AND "; endif;
          $whereClause .= $dbWhereKeyArray[$i] . " = '" . my_str_check($dbWhereSpecial[$dbWhereKeyArray[$i]]) . "'";
        endfor;

        // Add special where clause if requested
        if ($whereClauseSpecial != ""):          
          if ($whereClause != "") : $whereClause .= " AND "; endif;
          $whereClause .= $whereClauseSpecial;
        endif;

        // Generate statement
        // The $whereClauseDefault represents ALL 1:1 references (!!!!)
        if ($dbTable == "job") : 
           // $sqlStmt = generateStatement($dbFields, "job AS jb", "jb.jb_id = '" . $id . "'", "");
        endif;
        if ($dbTable == "tour") :
           if ($addDbDefaultFields) : $dbFields .= "," . getDBFields("address") . "," . getDBFields("ad_street_hsno"); endif;
           $whereClauseDefault = "jb.jb_id = tr.jb_id AND tr.ad_id = ad.ad_id";
           if ($whereClause != "") : $whereClauseDefault .= " AND "; endif;
           $whereClause = $whereClauseDefault . $whereClause;
           $sqlStmt = generateStatement($dbFields, "tour AS tr, address AS ad", $whereClause, $orderByClauseSpecial);
        endif;
        // ...................

        if ($sqlStmt != "") :
           $result = $dbConnection->query($sqlStmt);     
           if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
           $tmpArray = explode(",", $dbFields);
           $tmpArrayLen = count($tmpArray);
           $count = 0;
           while ($row = $result->fetch_assoc()):
                 for ($j = 0; $j < $tmpArrayLen; $j++) :
                     $tmpFieldName = getDBFieldsNameWithoutAlias($tmpArray[$j]);
                     if ($dbTable == "job") :
                        // $dbData["job"][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                     if ($dbTable == "tour") :
                        $dbData["tour"][$row["tr_sort"]][$tmpFieldName] = $row[$tmpFieldName];
                     endif;
                 endfor;
                 $count++;
           endwhile;
           $result->free();
        endif;
     endif;
  endif;
}
?>
