excelbulkexport.class.inc.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. // Copyright (C) 2015 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Bulk export: Excel (xlsx) export
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. require_once(APPROOT.'application/xlsxwriter.class.php');
  25. class ExcelBulkExport extends TabularBulkExport
  26. {
  27. protected $sData;
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->aStatusInfo['status'] = 'not_started';
  32. $this->aStatusInfo['position'] = 0;
  33. }
  34. public function Cleanup()
  35. {
  36. @unlink($this->aStatusInfo['tmp_file']);
  37. parent::Cleanup();
  38. }
  39. public function DisplayUsage(Page $oP)
  40. {
  41. $oP->p(" * xlsx format options:");
  42. $oP->p(" *\tfields: the comma separated list of field codes to export (e.g: name,org_id,service_name...).");
  43. }
  44. public function EnumFormParts()
  45. {
  46. return array_merge(parent::EnumFormParts(), array('interactive_fields_xlsx' => array('interactive_fields_xlsx')));
  47. }
  48. public function DisplayFormPart(WebPage $oP, $sPartId)
  49. {
  50. switch($sPartId)
  51. {
  52. case 'interactive_fields_xlsx':
  53. $this->GetInteractiveFieldsWidget($oP, 'interactive_fields_xlsx');
  54. break;
  55. default:
  56. return parent:: DisplayFormPart($oP, $sPartId);
  57. }
  58. }
  59. protected function SuggestField($sClass, $sAttCode)
  60. {
  61. switch($sAttCode)
  62. {
  63. case 'id': // replace 'id' by 'friendlyname'
  64. $sAttCode = 'friendlyname';
  65. break;
  66. default:
  67. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttCode);
  68. if ($oAttDef instanceof AttributeExternalKey)
  69. {
  70. $sAttCode .= '_friendlyname';
  71. }
  72. }
  73. return parent::SuggestField($sClass, $sAttCode);
  74. }
  75. protected function GetSampleData($oObj, $sAttCode)
  76. {
  77. return '<div class="text-preview">'.htmlentities($this->GetValue($oObj, $sAttCode), ENT_QUOTES, 'UTF-8').'</div>';
  78. }
  79. protected function GetValue($oObj, $sAttCode)
  80. {
  81. switch($sAttCode)
  82. {
  83. case 'id':
  84. $sRet = $oObj->GetKey();
  85. break;
  86. default:
  87. $value = $oObj->Get($sAttCode);
  88. if ($value instanceOf ormCaseLog)
  89. {
  90. // Extract the case log as text and remove the "===" which make Excel think that the cell contains a formula the next time you edit it!
  91. $sRet = trim(preg_replace('/========== ([^=]+) ============/', '********** $1 ************', $value->GetText()));
  92. }
  93. else if ($value instanceOf DBObjectSet)
  94. {
  95. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  96. $sRet = $oAttDef->GetAsCSV($value, '', '', $oObj);
  97. }
  98. else
  99. {
  100. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  101. $sRet = $oAttDef->GetEditValue($value, $oObj);
  102. }
  103. }
  104. return $sRet;
  105. }
  106. public function GetHeader()
  107. {
  108. $oSet = new DBObjectSet($this->oSearch);
  109. $this->aStatusInfo['status'] = 'retrieving';
  110. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  111. $this->aStatusInfo['position'] = 0;
  112. $this->aStatusInfo['total'] = $oSet->Count();
  113. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  114. {
  115. $sExtendedAttCode = $aFieldSpec['sFieldSpec'];
  116. $sAttCode = $aFieldSpec['sAttCode'];
  117. $sColLabel = $aFieldSpec['sColLabel'];
  118. switch($sAttCode)
  119. {
  120. case 'id':
  121. $sType = '0';
  122. break;
  123. default:
  124. $oAttDef = MetaModel::GetAttributeDef($aFieldSpec['sClass'], $aFieldSpec['sAttCode']);
  125. $sType = 'string';
  126. if($oAttDef instanceof AttributeDateTime)
  127. {
  128. $sType = 'datetime';
  129. }
  130. }
  131. $aTableHeaders[] = array('label' => $sColLabel, 'type' => $sType);
  132. }
  133. $sRow = json_encode($aTableHeaders);
  134. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  135. if ($hFile === false)
  136. {
  137. throw new Exception('ExcelBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  138. }
  139. fwrite($hFile, $sRow."\n");
  140. fclose($hFile);
  141. return '';
  142. }
  143. public function GetNextChunk(&$aStatus)
  144. {
  145. $sRetCode = 'run';
  146. $iPercentage = 0;
  147. $hFile = fopen($this->aStatusInfo['tmp_file'], 'ab');
  148. $oSet = new DBObjectSet($this->oSearch);
  149. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  150. $this->OptimizeColumnLoad($oSet);
  151. $iCount = 0;
  152. $iPreviousTimeLimit = ini_get('max_execution_time');
  153. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  154. while($aRow = $oSet->FetchAssoc())
  155. {
  156. set_time_limit($iLoopTimeLimit);
  157. $aData = array();
  158. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  159. {
  160. $sAlias = $aFieldSpec['sAlias'];
  161. $sAttCode = $aFieldSpec['sAttCode'];
  162. $oObj = $aRow[$sAlias];
  163. $sField = '';
  164. if ($oObj)
  165. {
  166. $sField = $this->GetValue($oObj, $sAttCode);
  167. }
  168. $aData[] = $sField;
  169. }
  170. fwrite($hFile, json_encode($aData)."\n");
  171. $iCount++;
  172. }
  173. set_time_limit($iPreviousTimeLimit);
  174. $this->aStatusInfo['position'] += $this->iChunkSize;
  175. if ($this->aStatusInfo['total'] == 0)
  176. {
  177. $iPercentage = 100;
  178. $sRetCode = 'done'; // Next phase (GetFooter) will be to build the xlsx file
  179. }
  180. else
  181. {
  182. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  183. }
  184. if ($iCount < $this->iChunkSize)
  185. {
  186. $sRetCode = 'done';
  187. }
  188. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  189. return ''; // The actual XLSX file is built in GetFooter();
  190. }
  191. public function GetFooter()
  192. {
  193. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'rb');
  194. if ($hFile === false)
  195. {
  196. throw new Exception('ExcelBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for reading.');
  197. }
  198. $sHeaders = fgets($hFile);
  199. $aHeaders = json_decode($sHeaders, true);
  200. $aData = array();
  201. while($sLine = fgets($hFile))
  202. {
  203. $aRow = json_decode($sLine);
  204. $aData[] = $aRow;
  205. }
  206. fclose($hFile);
  207. $fStartExcel = microtime(true);
  208. $writer = new XLSXWriter();
  209. $writer->setAuthor(UserRights::GetUserFriendlyName());
  210. $aHeaderTypes = array();
  211. $aHeaderNames = array();
  212. foreach($aHeaders as $Header)
  213. {
  214. $aHeaderNames[] = $Header['label'];
  215. $aHeaderTypes[] = $Header['type'];
  216. }
  217. $writer->writeSheet($aData,'Sheet1', $aHeaderTypes, $aHeaderNames);
  218. $fExcelTime = microtime(true) - $fStartExcel;
  219. //$this->aStatistics['excel_build_duration'] = $fExcelTime;
  220. $fTime = microtime(true);
  221. $data = $writer->writeToString();
  222. $fExcelSaveTime = microtime(true) - $fTime;
  223. //$this->aStatistics['excel_write_duration'] = $fExcelSaveTime;
  224. @unlink($this->aStatusInfo['tmp_file']);
  225. return $data;
  226. }
  227. public function GetMimeType()
  228. {
  229. return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  230. }
  231. public function GetFileExtension()
  232. {
  233. return 'xlsx';
  234. }
  235. public function GetSupportedFormats()
  236. {
  237. return array('xlsx' => Dict::S('Core:BulkExport:XLSXFormat'));
  238. }
  239. }