Esempio n. 1
0
 public function outputAndResetTotals($levelIdx)
 {
     if (!$this->outputTotalsInCSVOrTSVFormat && ($this->outputter->outputFormat == 'csv' || $this->outputter->outputFormat == 'tsv')) {
         return;
     }
     $level =& $this->levels[$levelIdx];
     $levelName = $level->name;
     if (!isset($this->totals[$levelName])) {
         return;
     }
     $tot = $this->totals[$levelName];
     if ($this->outputter->outputFormat == 'pdf') {
         // Be sure the row will fit on this page; if not (or if the PDF document
         // has not been opened yet), go to next page and output heading.
         if ($this->outputter->pdf === null) {
             $this->outputHeading(true);
         } else {
             $this->outputter->pdf->SetFont($this->pdfTotalsFontFamily, $this->pdfTotalsFontStyle, $this->pdfTotalsFontSize);
             if ($this->outputter->pdf->y + $this->outputter->getPDFLineSpacing() > $this->outputter->pdf->PageBreakTrigger) {
                 $this->outputHeading(true);
             }
         }
     }
     // If we need a heading, output it now, but don't force a page break.
     if (!$this->headingOutput) {
         $this->outputHeading();
     }
     // Create a copy of $tot into $crow, calculating any calculated fields.
     $crow = new stdClass();
     foreach ($this->columns as &$col) {
         $colName = $col->name;
         if (!in_array($levelName, $col->outputTotalsAtLevels)) {
             continue;
         }
         // If this is a calculated column, call the function to calculate the value.
         // Otherwise, just get the value.
         if ($col->valueCalcCallback !== null) {
             $colval = call_user_func($col->valueCalcCallback, $tot, $this, $col, $level);
         } else {
             $colval = $tot->{$colName};
         }
         $crow->{$colName} = $colval;
     }
     unset($col);
     // Release reference to last element
     $sep = '';
     $rowData = '';
     $colNum = -1;
     if ($this->outputter->outputFormat == 'html') {
         $this->ensureHTMLTableSection('tbody');
         $rowData .= '<tr' . ($this->htmlTBodyTotalsTRClass != '' ? ' class="' . $this->htmlTBodyTotalsTRClass . '"' : '') . '>';
     }
     if ($this->outputter->outputFormat == 'pdf') {
         $this->outputter->pdf->SetFont($this->pdfTotalsFontFamily, $this->pdfTotalsFontStyle, $this->pdfTotalsFontSize);
         $this->outputter->setPDFRGBTextColor($this->pdfTotalsTextRGB);
         $pdfTotalColSpacing = $this->pdfColumnSpacing * (count($this->columns) - 1);
         $totalRelativeWidth = 0.0;
         foreach ($this->columns as &$col) {
             $totalRelativeWidth += $col->relativeWidth;
         }
         unset($col);
         // Release reference to last element
     }
     // Output totals columns.
     for ($ci = 0, $nc = count($this->columns); $ci < $nc; $ci++) {
         $col =& $this->columns[$ci];
         $colNum++;
         $colName = $col->name;
         if ($colName == $level->totalsDescriptionLeftColumnName) {
             $nTotDescCols = $level->totalsDescriptionColumnSpan;
             switch ($this->outputter->outputFormat) {
                 case 'html':
                     $rowData .= '<td' . ($this->htmlTBodyTotalsTDClass != '' ? ' class="' . $this->htmlTBodyTotalsTDClass . '"' : '') . ($this->htmlTBodyTotalsTDNoWrap != '' ? ' nowrap="' . $this->htmlTBodyTotalsTDNoWrap . '"' : '') . ' align="' . $level->totalsDescriptionAlign . '"' . ($nTotDescCols > 1 ? ' colspan="' . $nTotDescCols . '"' : '') . '>' . htmlspecialchars($level->totalsDescription) . ':</td>';
                     break;
                 case 'csv':
                     $rowData .= $sep . ReportOutputter::encodeCSV($level->totalsDescription . ':');
                     if ($sep == '') {
                         $sep = ',';
                     }
                     for ($i = 1; $i < $nTotDescCols; $i++) {
                         $rowData .= $sep;
                     }
                     break;
                 case 'tsv':
                     $rowData .= $sep . ReportOutputter::encodeTSV($level->totalsDescription . ':');
                     if ($sep == '') {
                         $sep = "\t";
                     }
                     for ($i = 1; $i < $nTotDescCols; $i++) {
                         $rowData .= $sep;
                     }
                     break;
                 case 'xls':
                     $cell = $this->outputter->worksheet->getCellByColumnAndRow($colNum, $this->outputter->worksheetRowNum);
                     $cell->setValueExplicit($level->totalsDescription . ':', PHPExcel_Cell_DataType::TYPE_STRING);
                     $style = $this->outputter->worksheet->getStyleByColumnAndRow($colNum, $this->outputter->worksheetRowNum);
                     $style->getAlignment()->setHorizontal($level->totalsDescriptionAlign);
                     $style->getFont()->setBold(true);
                     if ($nTotDescCols > 1) {
                         $this->outputter->worksheet->mergeCellsByColumnAndRow($colNum, $this->outputter->worksheetRowNum, $colNum + ($nTotDescCols - 1), $this->outputter->worksheetRowNum);
                     }
                     break;
                 case 'pdf':
                     $colWidth = 0;
                     for ($ci2 = $ci, $nci = $ci + $nTotDescCols; $ci2 < $nci; $ci2++) {
                         $col2 =& $this->columns[$ci2];
                         $colWidth += (int) ((double) $col2->relativeWidth / (double) $totalRelativeWidth * (double) ($this->outputter->getPDFPageWidth() - $pdfTotalColSpacing));
                         if ($ci2 > $ci) {
                             $colWidth += $this->pdfColumnSpacing;
                         }
                     }
                     unset($col2);
                     // Release reference to last element
                     $this->outputter->pdf->Cell($colWidth, $this->outputter->getPDFLineSpacing(), $level->totalsDescription . ':', 0, 0, strtoupper($level->totalsDescriptionAlign[0]));
                     $this->outputter->pdf->x += $this->pdfColumnSpacing;
                     break;
             }
             $ci += $nTotDescCols - 1;
             $colNum += $nTotDescCols - 1;
         } else {
             // if ($colName == $level->totalsDescriptionLeftColumnName)
             $isTotCol = in_array($levelName, $col->outputTotalsAtLevels);
             if ($isTotCol) {
                 $colval = $crow->{$colName};
                 switch ($col->format) {
                     case 'number':
                         if ($colval < 0 && $col->suppressNegative || $colval == 0 && $col->suppressZero || $colval > 0 && $col->suppressPositive) {
                             $dispval = '';
                         } else {
                             if ($col->formatCallback !== null) {
                                 $dispval = call_user_func($col->formatCallback, $tot, $this, $col, $level, $colval);
                             } else {
                                 if (!$col->useThousandsSeparator) {
                                     $dispval = number_format((double) $colval, $col->decimalPlaces, '.', '');
                                 } else {
                                     $dispval = number_format((double) $colval, $col->decimalPlaces);
                                 }
                             }
                         }
                         break;
                     default:
                         if ($col->formatCallback !== null) {
                             $dispval = call_user_func($col->formatCallback, $tot, $this, $col, $level, $colval);
                         } else {
                             $dispval = $colval;
                         }
                         break;
                 }
             } else {
                 $dispval = '';
             }
             switch ($this->outputter->outputFormat) {
                 case 'html':
                     $rowData .= '<td' . ($this->htmlTBodyTotalsTDClass != '' ? ' class="' . $this->htmlTBodyTotalsTDClass . '"' : '') . ($this->htmlTBodyTotalsTDNoWrap != '' ? ' nowrap="' . $this->htmlTBodyTotalsTDNoWrap . '"' : '') . ' align="' . $col->align . '">' . (trim($dispval) != '' ? htmlspecialchars($dispval) : '&nbsp;') . '</td>';
                     break;
                 case 'csv':
                     $rowData .= $sep . ReportOutputter::encodeCSV($dispval);
                     if ($sep == '') {
                         $sep = ',';
                     }
                     break;
                 case 'tsv':
                     $rowData .= $sep . ReportOutputter::encodeTSV($dispval);
                     if ($sep == '') {
                         $sep = "\t";
                     }
                     break;
                 case 'xls':
                     if (!$isTotCol) {
                         break;
                     }
                     $cell = $this->outputter->worksheet->getCellByColumnAndRow($colNum, $this->outputter->worksheetRowNum);
                     $cell->setValueExplicit($dispval, PHPExcel_Cell_DataType::TYPE_STRING);
                     $style = $this->outputter->worksheet->getStyleByColumnAndRow($colNum, $this->outputter->worksheetRowNum);
                     $style->getAlignment()->setHorizontal($col->align);
                     $style->getFont()->setBold(true);
                     break;
                 case 'pdf':
                     $colWidth = (int) ((double) $col->relativeWidth / (double) $totalRelativeWidth * (double) ($this->outputter->getPDFPageWidth() - $pdfTotalColSpacing));
                     $this->outputter->pdf->Cell($colWidth, $this->outputter->getPDFLineSpacing(), $dispval, 0, 0, strtoupper($col->align[0]));
                     $this->outputter->pdf->x += $this->pdfColumnSpacing;
                     break;
             }
         }
         // if ($colName == $level->totalsDescriptionLeftColumnName) ... else
     }
     unset($col);
     // Release reference to last element
     switch ($this->outputter->outputFormat) {
         case 'html':
             $rowData .= '</tr>';
             $this->outputter->outputText($rowData);
             break;
         case 'csv':
         case 'tsv':
             $rowData .= "\r\n";
             $this->outputter->outputText($rowData);
             break;
         case 'xls':
             $this->outputter->worksheetRowNum++;
             break;
         case 'pdf':
             $this->outputter->pdf->Ln();
             break;
     }
     // Reset totals for this level by unsetting the object.
     unset($this->totals[$levelName]);
     // Force the next detail row to be odd.
     $this->oddRow = true;
 }