<?php
  include_once ("../include/global.inc.php");
  include_once ("../include/auth.inc.php");


  // Check HTTP-Parameters
  getSecHttpVars("1",array("f_act", "mode", "submode", "db_table", "db_id_field", "search_value", "db_return_field", "db_op_field", "new_content", "clause", "wrap_html",
                           "id_01", "id_02", "id_03", "id_04", "id_05", "value_01", "value_02", "value_03", "value_04", "value_05"));
  
  if ($mode != "") :
     header("Content-Type: text/html; charset=ISO-8859-1\n");
  endif;
                           
// echo "alert('" . $mode . " ' + '" . $db_table . " ' + '" . $db_id_field . " ' + '" . $search_value . " ' + '" . $db_return_field . " ' + '" . $db_op_field . " ');";

  /*
  MODE : FUNCTIONALITY
     0 : Gets a value of a special field  by ID [getFieldValueFromId(....)]
     1 : Gets an array of field values by ID [getFieldsValueFromId(....)]
     2 : Gets the first value of a special field if at least one entry does exist [getOneStmt(....)]
     3 : Gets a vector of a specifield field
     4 : Gets the maximum value of a DB field regarding a where clause [getMaxOfField(....)]
     5 : Gets the value for a key in table "parameter" (getObjectBasedParameterValue($key, $objId, $hqId, $empId))
     6 : Multi select statement; gets the first value of a special field if at least one entry does exist
     7:  Gets a value of a field clause [getFieldValueFromClause(....)]
   100 : Update a field value by ID [updateStmt(....)]
   200 : Insert fields into table and return last insert ID [insertStmt(...); getLastInsertId();]
   300 : Write logdata into log database
   400 : Inserts address and returns address ID
   500 : Inserts entry in GDC if does not exist or update value
  */

  function wrap_html($aStr) {
  	global $wrap_html;
  	if ($wrap_html == "1") :
  	   // $aStr = my_str_check_html($aStr);
       $aStr = str_replace("%u20AC", '&euro;', $aStr);
    endif;
    return $aStr;
  }

  if ($submode == "") :
     $submode = "0";
  endif;

  if ($db_table != "") : $db_table = urldecode($db_table); else : $db_table = ""; endif;
  if ($db_return_field != "") : $db_return_field = urldecode($db_return_field); else : $db_return_field = ""; endif;
  if ($clause != "") : $clause = urldecode($clause); else : $clause = ""; endif;
  if ($value_01 != "") : $value_01 = urldecode($value_01); else : $value_01 = ""; endif; $value_01 = wrap_html($value_01);
  if ($value_02 != "") : $value_02 = urldecode($value_02); else : $value_02 = ""; endif; $value_02 = wrap_html($value_02);
  if ($value_03 != "") : $value_03 = urldecode($value_03); else : $value_03 = ""; endif; $value_03 = wrap_html($value_03);
  if ($value_04 != "") : $value_04 = urldecode($value_04); else : $value_04 = ""; endif; $value_04 = wrap_html($value_04);
  if ($value_05 != "") : $value_05 = urldecode($value_05); else : $value_05 = ""; endif; $value_05 = wrap_html($value_05);


  if ($mode == "0") :

     // Gets a value of a special field by ID
     $retValue = getFieldValueFromId($db_table,$db_id_field,$search_value,$db_return_field);
     echo "retValue = '" . my_str_check_js($retValue) . "';\n";

  elseif ($mode == "1") : 

     // Gets an array of field values by ID
     echo "retValue = new Array();\n";
     $db_return_field_array = spliti("---", $db_return_field);
     $db_return_field_array_len = count($db_return_field_array);
     if ($db_return_field_array_len > 0) :
        $fieldValues = getFieldsValueFromId($db_table,$db_id_field,$search_value,$db_return_field_array);
        $fieldValuesLen = count($fieldValues);
        if ($fieldValuesLen > 0) :
           for ($j = 0; $j < $fieldValuesLen; $j++) :
               echo "retValue['" . $db_return_field_array[$j] . "'] = '" . $fieldValues[$j] . "';\n";
           endfor;
        endif;
     endif;
     
  elseif ($mode == "2") :

     // Gets the first value of a special field if at least one entry does exist
     if ($submode == "0") :
        $retValue = getOneStmt("SELECT " . $db_return_field . " FROM " . $db_table . " WHERE " . $db_id_field . " = '" . $search_value . "' ", $db_return_field);
     elseif ($submode == "1") :
        $retValue = getOneStmt("SELECT " . $db_return_field . " FROM " . $db_table . " WHERE " . $clause, $db_return_field);
     else :
        $retValue = getOneStmt("SELECT " . $db_return_field . " FROM " . $db_table . " WHERE " . $db_id_field . " = '" . $search_value . "' ", $db_return_field);
     endif;
     echo "retValue = '" . my_str_check_js($retValue) . "';\n";


  elseif ($mode == "3") :

     // Gets a vector of a specifield field
/*     
getColVectorFromDB2ArrayByClause($table, $pValName, $pWhereClause, $pKeyName = "", $pSortName = "")
getColVectorFromDB2ArrayByClause("metafieldkey", "mtfk_id", "", "", $sort)
getColVectorFromDB2ArrayByClause("metafieldvalue", "mtfv_value", "mtfv_id = '" . $objId . "' AND mtfck_id IN (" . implode($mtfck_mtfckIds, ",") . ")", "mtfck_id", "");     
*/       

  elseif ($mode == "4") :

     // Gets the maximum value of a DB field regarding a where clause
     $retValue = getMaxOfField($db_table, $db_id_field, $clause);
     echo "retValue = '" . $retValue . "';\n";


  elseif ($mode == "5") :

     // Gets the value for a key in table "parameter" (getObjectBasedParameterValue($key, $objId, $hqId, $empId))
     $retValue = getObjectBasedParameterValue($search_value, $id_01, $id_02, $id_03);
     echo "retValue = '" . $retValue . "';\n";


  elseif ($mode == "6") :

     // Gets the first value of a special field if at least one entry does exist
     $retValue = "";
     $whereClause = "";
     if ($id_01 != "" && $value_01 != "") :
        $whereClause .= " " . $id_01 . " = '" . $value_01 . "' ";
     endif;
     if ($id_02 != "" && $value_02 != "") : 
        if ($whereClause != "") : $whereClause .= " AND "; endif;
        $whereClause .= " " . $id_02 . " = '" . $value_02 . "' "; 
     endif;
     if ($id_03 != "" && $value_03 != "") :
        if ($whereClause != "") : $whereClause .= " AND "; endif;
        $whereClause .= " " . $id_03 . " = '" . $value_03 . "' "; 
     endif;
     if ($id_04 != "" && $value_04 != "") :
        if ($whereClause != "") : $whereClause .= " AND "; endif;
        $whereClause .= " " . $id_04 . " = '" . $value_04 . "' ";
     endif;
     if ($id_05 != "" && $value_05 != "") :
        if ($whereClause != "") : $whereClause .= " AND "; endif;
        $whereClause .= " " . $id_05 . " = '" . $value_05 . "' ";
     endif;
     if ($db_table != "" && $db_return_field != "" && $whereClause != "") :
        $retValue = getOneStmt("SELECT " . $db_return_field . " FROM " . $db_table . " WHERE " . $whereClause, $db_return_field);
     endif;
     echo "retValue = '" . my_str_check_js($retValue) . "';\n";


  elseif ($mode == "7") :

     // Gets a value of a field clause
     $whereClause = "";
     for ($i = 1; $i <= 5; $i++) :
         $tmpKey = ${("id_" . pad($i,2))};
         $tmpValue = ${("value_" . pad($i,2))};
         if ($tmpKey != "" && $tmpValue != "") :
            if ($whereClause != "") : $whereClause .= " AND "; endif;
            $whereClause .= $tmpKey . "= '" . $tmpValue . "'";
         endif;
     endfor;
     $retValue = getFieldValueFromClause($db_table,$db_return_field,$whereClause);
     echo "retValue = '" . my_str_check_js($retValue) . "';\n";  


  elseif ($mode == "100") :

     // Update a field value by ID
     $new_content = urldecode($new_content);
     $new_content = strWrapJs($new_content);
     if ($submode == "1") :
        if ($clause != "") : $clause .= " AND "; endif;
        $clause .= $db_id_field . " = '" . $search_value . "'";
        $old_content = getOneStmt("SELECT " . $db_op_field . " FROM " . $db_table . " WHERE " . $clause, $db_op_field);
        $old_content = trim($old_content);
        if ($old_content != "") : $old_content = " |||| " . $old_content; endif;
        $new_content .= $old_content;
     endif;
     updateStmt($db_table, $db_id_field, $search_value, array($db_op_field, $new_content), $clause);


  elseif ($mode == "200") :

     // Insert fields into table and return last insert ID
     $id_new = "-1";
     $db_op_field = urldecode($db_op_field);
     $db_op_field = strWrapJs($db_op_field);
     $db_op_field = utf8_decode($db_op_field);
     $db_op_field_array = spliti("---", $db_op_field);
     $db_op_field_array_len = count($db_op_field_array);
     if ($db_op_field_array_len > 0) :
        insertStmt($db_table, $db_op_field_array);
        $id_new = getLastInsertId();
        if ($id_new == "") : $id_new = "0"; endif;
     endif;
     echo "retValue = '" . $id_new . "';\n";


  elseif ($mode == "300") :

      // Write logdata into log database
      // db_id_field  : logo_id
      // db_op_field  : jb (= job)
      // search_value : jb_id, ....
      // id_01, id_02, .... : Key
      // value_01, value_02, .... : Value
      if ($db_id_field != "" && is_numeric($db_id_field) && $db_op_field != "" && $search_value != "" && is_numeric($search_value)) :
         $logDescription = "";
         for ($i = 1; $i <= 5; $i++) :
             $tmpKey = ${("id_" . pad($i,2))};
             $tmpValue = ${("value_" . pad($i,2))};
             if ($tmpKey != "" && $tmpValue != "") :
                if ($logDescription != "") : $logDescription .= "|"; endif;
                $logDescription .= $tmpKey . "=" . $tmpValue;
             endif;
         endfor;
         $hqId = "0"; $usrId = "0"; $empId = "0";
         if (isset($hq_id)) : $hqId = $hq_id; endif;
         if (isset($usr_id)) : $usrId = $usr_id; endif;
         if (isset($emp_id)) : $empId = $emp_id; endif;
         $jbId = "0"; $crId = "0"; $csId = "0"; $atId = "0";
         if ($db_op_field == "jb") :
            $jbId = $search_value; 
            $hqId = getFieldValueFromId("job", "jb_id", $jbId, "hq_id");
         endif;
         if ($db_op_field == "cr") :
            $crId = $search_value;
            $hqId = getFieldValueFromId("courier", "cr_id", $crId, "hq_id");
         endif;
         if ($db_op_field == "cs") :
            $csId = $search_value;
            $hqId = getFieldValueFromId("customer", "cs_id", $csId, "hq_id");
         endif;
         if ($db_op_field == "at") : 
            $atId = $search_value;
            $hqId = getFieldValueFromId("article", "at_id", $atId, "hq_id");
         endif;
         writeToLogDB($db_id_field,$hqId,$jbId,$usrId,$crId,"",$csId,$logDescription,$atId,"0",$empId);
      endif;


  elseif ($mode == "400") :

      // Insert address and returns address ID
      // value_01 : street
      // value_02 : f_ad_zipcode
      // value_03 : f_ad_city
      // value_04 : f_ad_country
      $retValue = "0";
      if ($value_01 != "" && $value_02 != "" && $value_03 != "") :
         $tmpArray = insertAddress($value_01, $value_02, $value_03, "", $value_04, true);
         $retValue = $tmpArray[0];
         if ($retValue == "") : 
            $retValue = "0"; 
         endif;
      endif;
      echo "retValue = '" . $retValue . "';\n";


  elseif ($mode == "500") :

      // Update or Insert item in GDC
      // value_01 : gdc_obj_type
      // value_02 : gdc_obj_id
      // value_03 : gdc_gen_fieldname
      // value_04 : gdc_content
      // value_05 : gdc_context
      $retValue = "0";
      if ($value_01 != "" && $value_02 != "" && $value_03 != "") :
         if (existsEntry("genericdatacontainer",array("gdc_obj_type",$value_01,"gdc_obj_id",$value_02,"gdc_gen_fieldname",$value_03))) :
            updateStmt("genericdatacontainer","gdc_obj_type",$value_01,array("gdc_content", $value_04, "gdc_context", $value_05),"gdc_obj_id = '" . $value_02 . "' AND gdc_gen_fieldname = '" . $value_03 . "'");
         else :
            insertStmt("genericdatacontainer", array("gdc_obj_type", $value_01, "gdc_obj_id", $value_02, "gdc_gen_fieldname", $value_03, "gdc_content", $value_04, "gdc_context", $value_05));
         endif;      
         // if ($retValue == "") : 
            $retValue = "1"; 
         // endif;
      endif;
      echo "retValue = '" . $retValue . "';\n";


  endif;
?>
