<?php
/*=======================================================================
 *
 * inc_list_defineoutput2file.inc.php
 *
 * Autor: Marc Vollmann
 *
 =======================================================================*/   
   
// The following parameters have to be defined before including this functionality:
// $outputFormatField, $mode, $titleArray, $fieldArray, 
// $alignArray, $widthArray, $alignTitles, $summationField, $summationField2, $summationFieldArray, $delimiter
  
   // Initialisation
   $outputHeader = "";
   $outputLinesArray = array();
   $fieldLength = count($fieldArray);
   if (!isset($alignTitles) || $alignTitles == "") : $alignTitles = "center"; endif;

   $sumOfSummationField = 0;
   $sumOfSummationField2 = 0;
   $summationFieldArrayLen = count($summationFieldArray);
   for ($i = 0; $i < $summationFieldArrayLen; $i++) :
       $sumOfSummationFieldArray[$i] = 0;
   endfor;   
   if (!isset($summationField)) : $summationField = ""; endif;
   
   $summationFieldArray2Len = count($summationFieldArray2);
   for ($i = 0; $i < $summationFieldArray2Len; $i++) :
       $sumOfSummationFieldArray2[$i] = 0;
   endfor;
   if (!isset($summationField2)) : $summationField2 = ""; endif;
   
   if (!isset($postParserField)) : $postParserField = ""; endif;
   if (!isset($postParserField2)) : $postParserField2 = ""; endif;
   if (!isset($postParserField3)) : $postParserField3 = ""; endif;
   if (!isset($postParserField4)) : $postParserField4 = ""; endif;
   if (!isset($postParserField5)) : $postParserField5 = ""; endif;
   if (!isset($delimiter)) : $delimiter = "|"; endif;
   if (!isset($writeHeader)) : $writeHeader = ""; endif;
   
   if (!isset($fieldsToSetPrefix)) : $fieldsToSetPrefix = array(); endif;
   $fieldsToSetPrefixLen = count($fieldsToSetPrefix);

   // Header of the list
   if ($writeHeader != "") :
      for ($i = 0; $i < $fieldLength; $i++) :
          $titleArray[$i] = str_replace("&nbsp;", "", $titleArray[$i]);
          $outputHeader .= trim($titleArray[$i]) . $delimiter;
      endfor;
   endif;


   // Rows delivered
   $rowCounter = 0;

   // Entries of the list from DB
   if ($mode == "1") :      
      while ($row = $result->fetch_assoc()):
            $outputLinesArray[$rowCounter] = "";
            for ($i = 0; $i < $fieldLength; $i++) :
                           
                $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];
             
                // 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;
                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;
                */
             
                // Set prefix if requested
                if ($fieldsToSetPrefixLen > 0) :
                   for ($k = 0; $k < $fieldsToSetPrefixLen; $k += 2) :
                     if ($fieldsToSetPrefix[$k] == $tmpVarName) :
                        // ATTENTION: Only if the content begins with a number !!!
                        if (is_numeric(substr(${$tmpVarName},0,1))) :
                           ${$tmpVarName} = $fieldsToSetPrefix[$i + 1] . ${$tmpVarName};
                        endif;
                     endif;
                   endfor;
                endif;

                $out = formatOutput(${$tmpVarName},$outputFormatField[$tmpVarName]);
                $out = str_replace("&nbsp;", "", $out);

                if ($tmpVarName == $postParserField) : $outputLinesArray[$rowCounter] .= "<postparser>"; endif;
                if ($tmpVarName == $postParserField2) : $outputLinesArray[$rowCounter] .= "<postparser2>"; endif;
                if ($tmpVarName == $postParserField3) : $outputLinesArray[$rowCounter] .= "<postparser3>"; endif;
                if ($tmpVarName == $postParserField4) : $outputLinesArray[$rowCounter] .= "<postparser4>"; endif;
                if ($tmpVarName == $postParserField5) : $outputLinesArray[$rowCounter] .= "<postparser5>"; endif;
                $outputLinesArray[$rowCounter] .= $out;
                if ($tmpVarName == $postParserField) : $outputLinesArray[$rowCounter] .= "</postparser>"; endif;
                if ($tmpVarName == $postParserField2) : $outputLinesArray[$rowCounter] .= "</postparser2>"; endif;
                if ($tmpVarName == $postParserField3) : $outputLinesArray[$rowCounter] .= "</postparser3>"; endif;
                if ($tmpVarName == $postParserField4) : $outputLinesArray[$rowCounter] .= "</postparser4>"; endif;
                if ($tmpVarName == $postParserField5) : $outputLinesArray[$rowCounter] .= "</postparser5>"; endif;

                $outputLinesArray[$rowCounter] .= $delimiter;
            endfor;            
            $rowCounter++;
      endwhile;      
   endif;
   
   // Entries of the list from MATRIX
   if ($mode == "2") :
      $rowArrayLen = count($rowArray);
      $rowCounter = $rowArrayLen;
      for ($j = 0; $j < $rowArrayLen; $j++) :      
      
          $outputLinesArray[$j] = "";
          for ($i = 0; $i < $fieldLength; $i++) :

              $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 = $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;
              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;
              */

              // Set prefix if requested
              if ($fieldsToSetPrefixLen > 0) :
                 for ($k = 0; $k < $fieldsToSetPrefixLen; $k += 2) :
                   if ($fieldsToSetPrefix[$k] == $tmpVarName) :
                      // ATTENTION: Only if the content begins with a number !!!
                      if (is_numeric(substr(${$tmpVarName},0,1))) :
                         ${$tmpVarName} = $fieldsToSetPrefix[$i + 1] . ${$tmpVarName};
                      endif;
                   endif;
                 endfor;
              endif;

              $out = formatOutput(${$tmpVarName},$outputType);
              $out = str_replace("&nbsp;", "", $out);

              if ($tmpVarName == $postParserField) : $outputLinesArray[$j] .= "<postparser>"; endif;
              $outputLinesArray[$j] .= $out;
              if ($tmpVarName == $postParserField) : $outputLinesArray[$j] .= "</postparser>"; endif;

              $outputLinesArray[$j] .= $delimiter;
          endfor;          
      endfor;                                              
   endif;
?>
