/public_html/sites/all/modules/civicrm/CRM/Core/Report/Excel.php

https://github.com/timstephenson/NatureBridge · PHP · 194 lines · 126 code · 23 blank · 45 comment · 31 complexity · 14edbc001f12f1c94f660c70d2bf00b1 MD5 · raw file

  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. require_once 'CRM/Utils/String.php';
  28. class CRM_Core_Report_Excel {
  29. /**
  30. * Code copied from phpMyAdmin (v2.6.1-pl3)
  31. * File: PHPMYADMIN/libraries/export/csv.php
  32. * Function: PMA_exportData
  33. *
  34. * Outputs a result set with a given header
  35. * in the string buffer result
  36. *
  37. * @param string $header (reference ) column headers
  38. * @param string $rows (reference ) result set rows
  39. * @param boolean $print should the output be printed
  40. *
  41. * @return mixed empty if output is printed, else output
  42. *
  43. * @access public
  44. */
  45. function makeCSVTable( &$header, &$rows, $titleHeader = null, $print = true, $outputHeader = true )
  46. {
  47. if ( $titleHeader ) {
  48. echo $titleHeader;
  49. }
  50. $result = '';
  51. $config = CRM_Core_Config::singleton( );
  52. $seperator = $config->fieldSeparator;
  53. $enclosed = '"';
  54. $escaped = $enclosed;
  55. $add_character = "\015\012";
  56. $schema_insert = '';
  57. foreach ( $header as $field ) {
  58. if ($enclosed == '') {
  59. $schema_insert .= stripslashes($field);
  60. } else {
  61. $schema_insert .=
  62. $enclosed
  63. . str_replace($enclosed, $escaped . $enclosed, stripslashes($field))
  64. . $enclosed;
  65. }
  66. $schema_insert .= $seperator;
  67. } // end while
  68. if ( $outputHeader ) {
  69. // need to add PMA_exportOutputHandler functionality out here, rather than
  70. // doing it the moronic way of assembling a buffer
  71. $out = trim(substr($schema_insert, 0, -1)) . $add_character;
  72. if ( $print ) {
  73. echo $out;
  74. } else {
  75. $result .= $out;
  76. }
  77. }
  78. $i = 0;
  79. $fields_cnt = count($header);
  80. foreach ( $rows as $row ) {
  81. $schema_insert = '';
  82. $colNo = 0;
  83. foreach ( $row as $j => $value ) {
  84. if (!isset($value) || is_null($value)) {
  85. $schema_insert .= '';
  86. } else if ($value == '0' || $value != '') {
  87. // loic1 : always enclose fields
  88. //$value = ereg_replace("\015(\012)?", "\012", $value);
  89. $value = preg_replace("/\015(\012)?/", "\012", $value);
  90. if ($enclosed == '') {
  91. $schema_insert .= $value;
  92. } else {
  93. if ( ( substr( $value, 0, 1 ) == CRM_Core_DAO::VALUE_SEPARATOR )&&
  94. ( substr( $value, -1, 1 ) == CRM_Core_DAO::VALUE_SEPARATOR ) ) {
  95. $strArray = explode( CRM_Core_DAO::VALUE_SEPARATOR, $value );
  96. foreach( $strArray as $key => $val ) {
  97. if ( trim( $val ) == '' ) {
  98. unset( $strArray[$key] );
  99. }
  100. }
  101. $str = implode( $seperator, $strArray );
  102. $value = &$str;
  103. }
  104. $schema_insert .=
  105. $enclosed
  106. . str_replace($enclosed, $escaped . $enclosed, $value)
  107. . $enclosed;
  108. }
  109. } else {
  110. $schema_insert .= '';
  111. }
  112. if ($colNo < $fields_cnt-1) {
  113. $schema_insert .= $seperator;
  114. }
  115. $colNo++;
  116. } // end for
  117. $out = $schema_insert . $add_character;
  118. if ( $print ) {
  119. echo $out;
  120. } else {
  121. $result .= $out;
  122. }
  123. ++$i;
  124. } // end for
  125. if ( $print ) {
  126. return;
  127. } else {
  128. return $result;
  129. }
  130. } // end of the 'getTableCsv()' function
  131. function writeHTMLFile ( $fileName, &$header, &$rows, $titleHeader = null, $outputHeader = true ) {
  132. if ( $outputHeader ) {
  133. require_once 'CRM/Utils/System.php';
  134. CRM_Utils_System::download( CRM_Utils_String::munge( $fileName ),
  135. 'application/vnd.ms-excel',
  136. CRM_Core_DAO::$_nullObject,
  137. 'xls',
  138. false );
  139. }
  140. echo "<table><thead><tr>";
  141. foreach ( $header as $field ) {
  142. echo "<th>$field</th>";
  143. } // end while
  144. echo "</tr></thead><tbody>";
  145. $i = 0;
  146. $fields_cnt = count($header);
  147. foreach ( $rows as $row ) {
  148. $schema_insert = '';
  149. $colNo = 0;
  150. echo "<tr>";
  151. foreach ( $row as $j => $value ) {
  152. echo "<td>".htmlentities ($value,ENT_COMPAT,'UTF-8')."</td>";
  153. } // end for
  154. echo "</tr>";
  155. } // end for
  156. echo "</tbody></table>";
  157. }
  158. function writeCSVFile( $fileName, &$header, &$rows, $titleHeader = null, $outputHeader = true ) {
  159. if ( $outputHeader ) {
  160. require_once 'CRM/Utils/System.php';
  161. CRM_Utils_System::download( CRM_Utils_String::munge( $fileName ),
  162. 'text/x-csv',
  163. CRM_Core_DAO::$_nullObject,
  164. 'csv',
  165. false );
  166. }
  167. if ( ! empty( $rows ) ) {
  168. self::makeCSVTable( $header, $rows, $titleHeader, true, $outputHeader );
  169. }
  170. }
  171. }