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

// The following parameters have to be defined before including this functionality:
// $outputFormatField, $mode, $sortDBField, $titleArray, $fieldArray, 
// $alignArray, $widthArray, $alignTitles, $summationField, $summationField2, $summationFieldArray


   // Initialisation
   $tableHeader = "";
   $tableBody = "";
   $fieldLength = count($fieldArray);
   if (!isset($alignTitles) || $alignTitles == "") : $alignTitles = "center"; endif;
   if (!isset($javaScriptFunctionNameForSort) || $javaScriptFunctionNameForSort == "") : $javaScriptFunctionNameForSort = "finishPageSort"; endif;
   mcIsSet($widthArray, array());
   mcIsSet($alignArray, array());
   mcIsSet($checkDifferences, false);

   $sumOfSummationField = 0; $sumOfSummationField2 = 0; $sumOfSummationField3 = 0;
   $sumOfSummationField4 = 0; $sumOfSummationField5 = 0; $sumOfSummationField6 = 0;
   $sumOfSummationField7 = 0; $sumOfSummationField8 = 0; $sumOfSummationField9 = 0;

   mcIsSet($summationFieldArray, array());
   $summationFieldArrayLen = count($summationFieldArray);
   for ($i = 0; $i < $summationFieldArrayLen; $i++) :
       $sumOfSummationFieldArray[$i] = 0;
   endfor;
   mcIsSet($summationField);
   
   mcIsSet($summationFieldArray2, array());
   $summationFieldArray2Len = count($summationFieldArray2);
   for ($i = 0; $i < $summationFieldArray2Len; $i++) :
       $sumOfSummationFieldArray2[$i] = 0;
   endfor;
   mcIsSet($summationField2);
   
   mcIsSet($summationFieldArray3, array());
   $summationFieldArray3Len = count($summationFieldArray3);
   for ($i = 0; $i < $summationFieldArray3Len; $i++) :
       $sumOfSummationFieldArray3[$i] = 0;
   endfor;
   mcIsSet($summationField3);
   
   mcIsSet($summationFieldArray4, array());
   $summationFieldArray4Len = count($summationFieldArray4);
   for ($i = 0; $i < $summationFieldArray4Len; $i++) :
       $sumOfSummationFieldArray4[$i] = 0;
   endfor;
   mcIsSet($summationField4);
   
   mcIsSet($summationFieldArray5, array());
   $summationFieldArray5Len = count($summationFieldArray5);
   for ($i = 0; $i < $summationFieldArray5Len; $i++) :
       $sumOfSummationFieldArray5[$i] = 0;
   endfor;
   mcIsSet($summationField5);

   mcIsSet($summationFieldArray6, array());
   $summationFieldArray6Len = count($summationFieldArray6);
   for ($i = 0; $i < $summationFieldArray6Len; $i++) :
       $sumOfSummationFieldArray6[$i] = 0;
   endfor;
   mcIsSet($summationField6);

   mcIsSet($summationFieldArray7, array());
   $summationFieldArray7Len = count($summationFieldArray7);
   for ($i = 0; $i < $summationFieldArray7Len; $i++) :
       $sumOfSummationFieldArray7[$i] = 0;
   endfor;
   mcIsSet($summationField7);
   
   mcIsSet($summationFieldArray8, array());
   $summationFieldArray8Len = count($summationFieldArray8);
   for ($i = 0; $i < $summationFieldArray8Len; $i++) :
       $sumOfSummationFieldArray8[$i] = 0;
   endfor;
   mcIsSet($summationField8);

   mcIsSet($summationFieldArray9, array());
   $summationFieldArray9Len = count($summationFieldArray9);
   for ($i = 0; $i < $summationFieldArray9Len; $i++) :
       $sumOfSummationFieldArray9[$i] = 0;
   endfor;
   mcIsSet($summationField9);

   mcIsSet($postParserField);
   mcIsSet($postParserField2);
   mcIsSet($postParserField3);

   if (!isset($specialOutputFormat) || $specialOutputFormat == "") : $specialOutputFormat = "1"; endif;
   
   mcIsSet($rowLinkType);
   mcIsSet($rowLinkField);
   mcIsSet($colSortOffsetArray, array());
   mcIsSet($convertOutputArray, array());
   mcIsSet($titleFieldToBeChanged);

   mcIsSet($noDisplayBoolCondition, array("","",""));
   $noDspBoolCndField = $noDisplayBoolCondition[0];
   $noDspBoolCndOp = $noDisplayBoolCondition[1];
   $noDspBoolCndValue = $noDisplayBoolCondition[2];


   // Header of the list
   $tableHeader = "<tr>";
   for ($i = 0; $i < $fieldLength; $i++) :
       if (trim($titleArray[$i]) == "") : 
          $titleArray[$i] = "&nbsp;"; 
       elseif ($titleFieldToBeChanged != "" && trim($titleArray[$i]) == $titleFieldToBeChanged) :
          $titleArray[$i] = $titleFieldNewValue;
       endif;
       if ($sortDBField == "" || $fieldSortArray[$i] == "") :
          $tableHeader .= "<td align=\"" . $alignTitles . "\">" . $titleArray[$i] . "</td>";
       else :
          // Important: There has to be a JS-function "finishPageSort()" defined in including page to submit the ORDER BY value
          mcArrIsSet($colSortOffsetArray, $i);
          $x = $i;
          if ($colSortOffsetArray[$i] != "" && is_numeric($colSortOffsetArray[$i])) : $x = $colSortOffsetArray[$i]; endif;
          $tableHeader .= "<td align=\"" . $alignTitles . "\"><a href=\"javascript:" . $javaScriptFunctionNameForSort . "('" . $x . "');\">" . $titleArray[$i] . "</a></td>";
       endif;
   endfor;
   $tableHeader .= "</tr>";

   if ($checkDifferences) :
      $preRowArr = array();
   endif;

   // Rows delivered
   $rowCounter = 0;

   // Entries of the list from DB
   if ($mode == "1") :      
      while ($row = $result->fetch_assoc()):

            $mcDisplayRow = true;
            if ($noDspBoolCndField != "" && $noDspBoolCndOp != "" && $noDspBoolCndValue != "") :
               $mcDisplayRow = mcCheckCondition($row[$noDspBoolCndField], $noDspBoolCndOp, $noDspBoolCndValue);
// echo $row[$noDspBoolCndField] . "&nbsp;" . $noDspBoolCndOp . "&nbsp;" . $noDspBoolCndValue . "&nbsp;&nbsp;&nbsp;&nbsp;" . "mcDisplayRow = " . $mcDisplayRow . "<br>";
            endif;

            if ($mcDisplayRow) :
               $rowCounter++;
               
               $rowTr = "<tr>";
               if ($rowLinkField != "") :
                  $rowTr = "<tr onclick=\"openLinkByObject('" . $rowLinkType . "','" . ec($row[$rowLinkField]) . "')\">";
               endif;            
               $tableBody .= $rowTr;
               
               for ($i = 0; $i < $fieldLength; $i++) :
               
                   if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#BBBBFF"; endif;
                   if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#CCCCFF"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#DDDDDD"; endif;
                   if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#EEEEEE"; endif;         
                     
                   $tmpVarName = $fieldArray[$i];
                   // Check if parameter is a combined value like "stat.business_volume" 
                   // then split for suffix and get only "business_volume"
                   $tmpVarNameSuffix = $tmpVarName;
                   if (!(strpos($tmpVarName, ".") === FALSE)) : 
                      $tmpVarNameSuffix = substr($tmpVarName, strrpos($tmpVarName,".") + 1);
                   endif;
               
                   ${$tmpVarName} = $row[$tmpVarName];
                   // HACK CADDY 2026-02-11 BEGIN
                   if ($fieldArray[$i] == "trs_srv_name" && ${$tmpVarName} == "Fixpreis"):
                     $tmp_addendum = $db->getOne("SELECT RIGHT(trs_srvt_name, LENGTH(trs_srvt_name) -4) FROM tourservice WHERE jb_id = " . $job_id . " AND trs_srv_name = 'Fixpreis' AND LEFT(trs_srvt_name, 4) = '[M] '");
                     if ($tmp_addendum !=  "")
                       ${$tmpVarName} .= " (" . $tmp_addendum . ")";
                   endif;
                   // HACK CADDY 2026-02-11 END
                   // Optional summation of one or more fields
                   if ($summationField != "" && ($tmpVarName == $summationField) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField += ${$tmpVarName};
                   endif;
                   if ($summationField2 != "" && ($tmpVarName == $summationField2) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField2 += ${$tmpVarName};
                   endif;
                   if ($summationField3 != "" && ($tmpVarName == $summationField3) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField3 += ${$tmpVarName};
                   endif;
                   if ($summationField4 != "" && ($tmpVarName == $summationField4) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField4 += ${$tmpVarName};
                   endif;
                   if ($summationField5 != "" && ($tmpVarName == $summationField5) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField5 += ${$tmpVarName};
                   endif;
                   if ($summationField6 != "" && ($tmpVarName == $summationField6) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField6 += ${$tmpVarName};
                   endif;
                   if ($summationField7 != "" && ($tmpVarName == $summationField7) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField7 += ${$tmpVarName};
                   endif;
                   if ($summationField8 != "" && ($tmpVarName == $summationField8) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField8 += ${$tmpVarName};
                   endif;
                   if ($summationField9 != "" && ($tmpVarName == $summationField9) && is_numeric(${$tmpVarName})) :
                      $sumOfSummationField9 += ${$tmpVarName};
                   endif;
               
                   for ($k = 0; $k < $summationFieldArrayLen; $k++) :
                       if ($summationFieldArray[$k] != "" && ($tmpVarName == $summationFieldArray[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray2Len; $k++) :
                       if ($summationFieldArray2[$k] != "" && ($tmpVarName == $summationFieldArray2[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray2[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray3Len; $k++) :
                       if ($summationFieldArray3[$k] != "" && ($tmpVarName == $summationFieldArray3[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray3[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray4Len; $k++) :
                       if ($summationFieldArray4[$k] != "" && ($tmpVarName == $summationFieldArray4[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray4[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray5Len; $k++) :
                       if ($summationFieldArray5[$k] != "" && ($tmpVarName == $summationFieldArray5[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray5[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray6Len; $k++) :
                       if ($summationFieldArray6[$k] != "" && ($tmpVarName == $summationFieldArray6[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray6[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray7Len; $k++) :
                       if ($summationFieldArray7[$k] != "" && ($tmpVarName == $summationFieldArray7[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray7[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray8Len; $k++) :
                       if ($summationFieldArray8[$k] != "" && ($tmpVarName == $summationFieldArray8[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray8[$k] += ${$tmpVarName};
                       endif;
                   endfor;
                   for ($k = 0; $k < $summationFieldArray9Len; $k++) :
                       if ($summationFieldArray9[$k] != "" && ($tmpVarName == $summationFieldArray9[$k]) && is_numeric(${$tmpVarName})) :
                          $sumOfSummationFieldArray9[$k] += ${$tmpVarName};
                       endif;
                   endfor;
               
                   // $out = formatOutput(${$tmpVarName},$outputFormatField[$tmpVarName],$specialOutputFormat);
                   $out = formatOutput(${$tmpVarName},mcArrIsSet($outputFormatField, $tmpVarName),$specialOutputFormat);
               
                   mcArrIsSet($convertOutputArray, $i);
                   if ($convertOutputArray[$i] == "1") : $out = nl2br($out); endif;
               
                   $widthStr = "";
                   mcArrIsSet($widthArray, $i);
                   if ($widthArray[$i] != "" && $widthArray[$i] != 0) : $widthStr = " width=\"" . $widthArray[$i] . "\" "; endif;
               
                   mcArrIsSet($alignArray, $i);
                   $tableBody .= "<td bgcolor=\"" . $cellColor ."\" " . $widthStr . "align=\"" . setAlign($alignArray[$i]) . "\">";
                   if ($tmpVarName == $postParserField) : $tableBody .= "<postparser>"; endif;
                   if ($tmpVarName == $postParserField2) : $tableBody .= "<postparser2>"; endif;
                   if ($tmpVarName == $postParserField3) : $tableBody .= "<postparser3>"; endif;
                   if ($checkDifferences && ${$tmpVarName} != $preRowArr[$tmpVarName]) : 
                      $out = "<b>" . $out . "</b>";
                   endif;
                   $tableBody .= $out;
                   if ($tmpVarName == $postParserField) : $tableBody .= "</postparser>"; endif;
                   if ($tmpVarName == $postParserField2) : $tableBody .= "</postparser2>"; endif;
                   if ($tmpVarName == $postParserField3) : $tableBody .= "</postparser3>"; endif;
                   $tableBody .= "&nbsp;&nbsp;</td>";
               endfor;
               
               $tableBody .= "</tr>\n";
               
               if ($checkDifferences) :
                  $preRowArr = $row;
               endif;
            endif;  // display row
      endwhile;      
   endif;
   
   // Entries of the list from MATRIX
   if ($mode == "2") :
      $rowArrayLen = count($rowArray);
      $rowCounter = $rowArrayLen;
      for ($j = 0; $j < $rowArrayLen; $j++) :      

          $mcDisplayRow = true;
          if ($noDspBoolCndField != "" && $noDspBoolCndOp != "" && $noDspBoolCndValue != "") :
             $mcDisplayRow = mcCheckCondition($row[$noDspBoolCndField], $noDspBoolCndOp, $noDspBoolCndValue);
          endif;

          if ($mcDisplayRow) :
             $tableBody .= "<tr>";
             for ($i = 0; $i < $fieldLength; $i++) :
                 
                 if ($rowCounter % 2 == 0 && $i % 2 == 0) : $cellColor = "#BBBBFF"; endif;
                 if ($rowCounter % 2 == 0 && $i % 2 == 1) : $cellColor = "#CCCCFF"; endif;
                 if ($rowCounter % 2 == 1 && $i % 2 == 0) : $cellColor = "#DDDDDD"; endif;
                 if ($rowCounter % 2 == 1 && $i % 2 == 1) : $cellColor = "#EEEEEE"; endif;              
             
                 $tmpVarName = $fieldArray[$i];
             
                 // Check if parameter is a combined value like "stat.business_volume" 
                 // then split for suffix and get only "business_volume"
                 $tmpVarNameSuffix = $tmpVarName;
                 if (!(strpos($tmpVarName, ".") === FALSE)) : 
                    $tmpVarNameSuffix = substr($tmpVarName, strrpos($tmpVarName,".") + 1);
                 endif;
             
                 $outputType = mcArrIsSet($outputFormatField, $tmpVarNameSuffix);
                 
                 // Special treatment:
                 if ($tmpVarName == "stat.jb_finishtime") : $outputType = "varchar"; endif;
             
                 // ${$tmpVarName} = $rowArray[$j][$tmpVarName];
                 ${$tmpVarName} = $rowArray[$j][$tmpVarNameSuffix];  // current content of the field
                
                 // Optional summation of one or more fields
                 if ($summationField != "" && ($tmpVarName == $summationField) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField += ${$tmpVarName};
                 endif;
                 if ($summationField2 != "" && ($tmpVarName == $summationField2) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField2 += ${$tmpVarName};
                 endif;
                 if ($summationField3 != "" && ($tmpVarName == $summationField3) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField3 += ${$tmpVarName};
                 endif;
                 if ($summationField4 != "" && ($tmpVarName == $summationField4) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField4 += ${$tmpVarName};
                 endif;
                 if ($summationField5 != "" && ($tmpVarName == $summationField5) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField5 += ${$tmpVarName};
                 endif;
                 if ($summationField6 != "" && ($tmpVarName == $summationField6) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField6 += ${$tmpVarName};
                 endif;
                 if ($summationField7 != "" && ($tmpVarName == $summationField7) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField7 += ${$tmpVarName};
                 endif;
                 if ($summationField8 != "" && ($tmpVarName == $summationField8) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField8 += ${$tmpVarName};
                 endif;
                 if ($summationField9 != "" && ($tmpVarName == $summationField9) && is_numeric(${$tmpVarName})) :
                    $sumOfSummationField9 += ${$tmpVarName};
                 endif;
             
                 for ($k = 0; $k < $summationFieldArrayLen; $k++) :
                     if ($summationFieldArray[$k] != "" && ($tmpVarName == $summationFieldArray[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray2Len; $k++) :
                     if ($summationFieldArray2[$k] != "" && ($tmpVarName == $summationFieldArray2[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray2[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray3Len; $k++) :
                     if ($summationFieldArray3[$k] != "" && ($tmpVarName == $summationFieldArray3[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray3[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray4Len; $k++) :
                     if ($summationFieldArray4[$k] != "" && ($tmpVarName == $summationFieldArray4[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray4[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray5Len; $k++) :
                     if ($summationFieldArray5[$k] != "" && ($tmpVarName == $summationFieldArray5[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray5[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray6Len; $k++) :
                     if ($summationFieldArray6[$k] != "" && ($tmpVarName == $summationFieldArray6[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray6[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray7Len; $k++) :
                     if ($summationFieldArray7[$k] != "" && ($tmpVarName == $summationFieldArray7[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray7[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray8Len; $k++) :
                     if ($summationFieldArray8[$k] != "" && ($tmpVarName == $summationFieldArray8[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray8[$k] += ${$tmpVarName};
                     endif;
                 endfor;
                 for ($k = 0; $k < $summationFieldArray9Len; $k++) :
                     if ($summationFieldArray9[$k] != "" && ($tmpVarName == $summationFieldArray9[$k]) && is_numeric(${$tmpVarName})) :
                        $sumOfSummationFieldArray9[$k] += ${$tmpVarName};
                     endif;
                 endfor;
             
                 $out = formatOutput(${$tmpVarName},$outputType,$specialOutputFormat);
             
                 mcArrIsSet($convertOutputArray, $i);
                 if ($convertOutputArray[$i] == "1") : $out = nl2br($out); endif;
             
                 $widthStr = "";
                 mcArrIsSet($widthArray, $i);
                 if ($widthArray[$i] != "" && $widthArray[$i] != 0) : $widthStr = " width=\"" . $widthArray[$i] . "\" "; endif;
             
                 mcArrIsSet($alignArray, $i);
                 $tableBody .= "<td bgcolor=\"" . $cellColor ."\" " . $widthStr . "align=\"" . setAlign($alignArray[$i]) . "\">";
                 if ($tmpVarName == $postParserField) : $tableBody .= "<postparser>"; endif;
                 if ($tmpVarName == $postParserField2) : $tableBody .= "<postparser2>"; endif;
                 if ($tmpVarName == $postParserField3) : $tableBody .= "<postparser3>"; endif;
                 $tableBody .= $out;
                 if ($tmpVarName == $postParserField) : $tableBody .= "</postparser>"; endif;
                 if ($tmpVarName == $postParserField2) : $tableBody .= "</postparser2>"; endif;
                 if ($tmpVarName == $postParserField3) : $tableBody .= "</postparser3>"; endif;
                 $tableBody .= "&nbsp;&nbsp;</td>";              
             endfor;
             
             $tableBody .= "</tr>\n";
          endif;  // display row
      endfor;                                              
   endif;
?>
