Exemplo n.º 1
0
 protected function _internalRender($name)
 {
     $vars = $this->getVars();
     if (!empty($vars['data']) && count($vars['data'])) {
         $headers = $this->getHeaders();
         if (empty($headers)) {
             foreach ($vars['data'] as $key => $value) {
                 $this->_processHeaders($headers, $value);
             }
         }
         $headers = $this->_filterData($headers);
         $resultData = $vars['data'];
         $file = $this->getStream();
         if ($file === false) {
             throw new GlobalServiceException("Impossible to create csv file");
         }
         // UTF-8 BOM (Excel issues)
         fwrite($file, "");
         $fileHeaders = $this->getFileHeaders();
         foreach ($fileHeaders as $fileHeader) {
             if (fputcsv($file, $fileHeader, $this->getSeparator()) === false) {
                 throw new GlobalServiceException("Impossible to write file header on csv file");
             }
         }
         if (fputcsv($file, $headers, $this->getSeparator()) === false) {
             throw new GlobalServiceException("Impossible to write headers on csv file");
         }
         foreach ($resultData as $item) {
             $itemCsv = array();
             foreach ($headers as $key => $caption) {
                 $value = App_Util_Array::getItem($item, $key);
                 $itemCsv[] = $value !== null ? $this->_filterValue($key, $value, $item) : '';
             }
             if ($this->_skipEmptyItems && App_Util_Array::isEmpty($itemCsv)) {
                 continue;
             }
             if (fputcsv($file, $itemCsv, $this->getSeparator()) === false) {
                 throw new GlobalServiceException("Impossible to write row on csv file");
             }
         }
         if (method_exists($resultData, 'getSecondList') && ($secondList = $resultData->getSecondList())) {
             if (fputcsv($file, array(), $this->getSeparator()) === false) {
                 throw new GlobalServiceException("Impossible to write second list separator on csv file");
             }
             foreach ($secondList as $item) {
                 if (fputcsv($file, $item, $this->getSeparator()) === false) {
                     throw new GlobalServiceException("Impossible to write second list on csv file");
                 }
             }
         }
         $fileFooters = $this->getFileFooters();
         foreach ($fileFooters as $fileFooter) {
             if (fputcsv($file, $fileFooter, $this->getSeparator()) === false) {
                 throw new GlobalServiceException("Impossible to write file footer on csv file");
             }
         }
     }
 }