spreadsheetbulkexport.class.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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: "spreadsheet" export: a simplified HTML export in which the date/time columns are split in two column: date AND time
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class SpreadsheetBulkExport extends TabularBulkExport
  25. {
  26. public function DisplayUsage(Page $oP)
  27. {
  28. $oP->p(" * spreadsheet format options:");
  29. $oP->p(" *\tfields: (mandatory) the comma separated list of field codes to export (e.g: name,org_id,service_name...).");
  30. $oP->p(" *\tno_localize: (optional) pass 1 to retrieve the raw (untranslated) values for enumerated fields. Default: 0.");
  31. $oP->p(" *\tdate_format: the format to use when exporting date and time fields (default = the SQL format). e.g. 'Y-m-d H:i:s'");
  32. }
  33. public function EnumFormParts()
  34. {
  35. return array_merge(parent::EnumFormParts(), array('spreadsheet_options' => array('no-localize') ,'interactive_fields_spreadsheet' => array('interactive_fields_spreadsheet')));
  36. }
  37. public function DisplayFormPart(WebPage $oP, $sPartId)
  38. {
  39. switch($sPartId)
  40. {
  41. case 'interactive_fields_spreadsheet':
  42. $this->GetInteractiveFieldsWidget($oP, 'interactive_fields_spreadsheet');
  43. break;
  44. case 'spreadsheet_options':
  45. $sChecked = (utils::ReadParam('no_localize', 0) == 1) ? ' checked ' : '';
  46. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:SpreadsheetOptions').'</legend>');
  47. $oP->add('<table>');
  48. $oP->add('<tr>');
  49. $oP->add('<td><input type="checkbox" id="spreadsheet_no_localize" name="no_localize" value="1"'.$sChecked.'><label for="spreadsheet_no_localize"> '.Dict::S('Core:BulkExport:OptionNoLocalize').'</label></td>');
  50. $sDateTimeFormat = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  51. $sDefaultChecked = ($sDateTimeFormat == (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  52. $sCustomChecked = ($sDateTimeFormat !== (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  53. $oP->add('<td>');
  54. $oP->add('<h3>'.Dict::S('Core:BulkExport:DateTimeFormat').'</h3>');
  55. $sDefaultFormat = htmlentities((string)AttributeDateTime::GetFormat(), ENT_QUOTES, 'UTF-8');
  56. $sExample = htmlentities(date((string)AttributeDateTime::GetFormat()), ENT_QUOTES, 'UTF-8');
  57. $oP->add('<input type="radio" id="spreadsheet_date_time_format_default" name="spreadsheet_date_format_radio" value="default"'.$sDefaultChecked.'><label for="spreadsheet_date_time_format_default"> '.Dict::Format('Core:BulkExport:DateTimeFormatDefault_Example', $sDefaultFormat, $sExample).'</label><br/>');
  58. $sFormatInput = '<input type="text" size="15" name="date_format" id="spreadsheet_custom_date_time_format" title="" value="'.htmlentities($sDateTimeFormat, ENT_QUOTES, 'UTF-8').'"/>';
  59. $oP->add('<input type="radio" id="spreadsheet_date_time_format_custom" name="spreadsheet_date_format_radio" value="custom"'.$sCustomChecked.'><label for="spreadsheet_date_time_format_custom"> '.Dict::Format('Core:BulkExport:DateTimeFormatCustom_Format', $sFormatInput).'</label>');
  60. $oP->add('</td>');
  61. $oP->add('</tr>');
  62. $oP->add('</table>');
  63. $oP->add('</fieldset>');
  64. $sJSTooltip = json_encode('<div class="date_format_tooltip">'.Dict::S('UI:CSVImport:CustomDateTimeFormatTooltip').'</div>');
  65. $oP->add_ready_script(
  66. <<<EOF
  67. $('#spreadsheet_custom_date_time_format').tooltip({content: function() { return $sJSTooltip; } });
  68. $('#form_part_spreadsheet_options').on('preview_updated', function() { FormatDatesInPreview('spreadsheet', 'spreadsheet'); });
  69. $('#spreadsheet_custom_date_time_format').on('click', function() { $('#spreadsheet_date_time_format_custom').prop('checked', true); });
  70. $('#spreadsheet_custom_date_time_format').on('click', function() { $('#spreadsheet_date_time_format_custom').prop('checked', true); FormatDatesInPreview('spreadsheet', 'spreadsheet'); }).on('keyup', function() { FormatDatesInPreview('spreadsheet', 'spreadsheet'); });
  71. EOF
  72. );
  73. break;
  74. default:
  75. return parent:: DisplayFormPart($oP, $sPartId);
  76. }
  77. }
  78. public function ReadParameters()
  79. {
  80. parent::ReadParameters();
  81. $sDateFormatRadio = utils::ReadParam('spreadsheet_date_format_radio', '');
  82. switch($sDateFormatRadio)
  83. {
  84. case 'default':
  85. // Export from the UI => format = same as is the UI
  86. $this->aStatusInfo['date_format'] = (string)AttributeDateTime::GetFormat();
  87. break;
  88. case 'custom':
  89. // Custom format specified from the UI
  90. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  91. break;
  92. default:
  93. // Export from the command line (or scripted) => default format is SQL, as in previous versions of iTop, unless specified otherwise
  94. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetSQLFormat(), true, 'raw_data');
  95. }
  96. }
  97. protected function GetSampleData($oObj, $sAttCode)
  98. {
  99. if ($sAttCode != 'id')
  100. {
  101. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  102. if ($oAttDef instanceof AttributeDateTime) // AttributeDate is derived from AttributeDateTime
  103. {
  104. $sClass = (get_class($oAttDef) == 'AttributeDateTime') ? 'user-formatted-date-time' : 'user-formatted-date';
  105. return '<div class="'.$sClass.'" data-date="'.$oObj->Get($sAttCode).'">'.htmlentities($oAttDef->GetEditValue($oObj->Get($sAttCode), $oObj), ENT_QUOTES, 'UTF-8').'</div>';
  106. }
  107. }
  108. return $this->GetValue($oObj, $sAttCode);
  109. }
  110. protected function GetValue($oObj, $sAttCode)
  111. {
  112. switch($sAttCode)
  113. {
  114. case 'id':
  115. $sRet = $oObj->GetKey();
  116. break;
  117. default:
  118. $value = $oObj->Get($sAttCode);
  119. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  120. if ($value instanceof ormCaseLog)
  121. {
  122. $sRet = str_replace("\n", "<br/>", htmlentities($value->__toString(), ENT_QUOTES, 'UTF-8'));
  123. }
  124. elseif ($value instanceof ormStopWatch)
  125. {
  126. $sRet = $value->GetTimeSpent();
  127. }
  128. elseif ($oAttDef instanceof AttributeString)
  129. {
  130. $sRet = $oObj->GetAsHTML($sAttCode);
  131. }
  132. elseif ($oAttDef instanceof AttributeCustomFields)
  133. {
  134. $sRet = $oObj->GetAsHTML($sAttCode);
  135. }
  136. else
  137. {
  138. if ($this->bLocalizeOutput)
  139. {
  140. $sRet = htmlentities($oObj->GetEditValue(), ENT_QUOTES, 'UTF-8');
  141. }
  142. else
  143. {
  144. $sRet = htmlentities((string)$value, ENT_QUOTES, 'UTF-8');
  145. }
  146. }
  147. }
  148. return $sRet;
  149. }
  150. public function SetHttpHeaders(WebPage $oPage)
  151. {
  152. // Integration within MS-Excel web queries + HTTPS + IIS:
  153. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  154. // Then the fix is to force the reset of header values Pragma and Cache-control
  155. $oPage->add_header("Pragma:", true);
  156. $oPage->add_header("Cache-control:", true);
  157. }
  158. public function GetHeader()
  159. {
  160. $oSet = new DBObjectSet($this->oSearch);
  161. $this->aStatusInfo['status'] = 'running';
  162. $this->aStatusInfo['position'] = 0;
  163. $this->aStatusInfo['total'] = $oSet->Count();
  164. $aData = array();
  165. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  166. {
  167. $sColLabel = $aFieldSpec['sColLabel'];
  168. if ($aFieldSpec['sAttCode'] != 'id')
  169. {
  170. $oAttDef = MetaModel::GetAttributeDef($aFieldSpec['sClass'], $aFieldSpec['sAttCode']);
  171. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  172. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  173. {
  174. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Date').')';
  175. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Time').')';
  176. }
  177. else
  178. {
  179. $aData[] = $sColLabel;
  180. }
  181. }
  182. else
  183. {
  184. $aData[] = $sColLabel;
  185. }
  186. }
  187. $sData = '';
  188. $sData .= '<style>table br {mso-data-placement:same-cell;}</style>'; // Trick for Excel: keep line breaks inside the same cell !
  189. $sData .= "<table border=\"1\">\n";
  190. $sData .= "<tr>\n";
  191. foreach($aData as $sLabel)
  192. {
  193. $sData .= "<td>".$sLabel."</td>\n";
  194. }
  195. $sData .= "</tr>\n";
  196. return $sData;
  197. }
  198. public function GetNextChunk(&$aStatus)
  199. {
  200. $sRetCode = 'run';
  201. $iPercentage = 0;
  202. $oSet = new DBObjectSet($this->oSearch);
  203. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  204. $this->OptimizeColumnLoad($oSet);
  205. $sExportDateTimeFormat = $this->aStatusInfo['date_format'];
  206. // Date & time formats
  207. $oDateTimeFormat = new DateTimeFormat($sExportDateTimeFormat);
  208. $oDateFormat = new DateTimeFormat($oDateTimeFormat->ToDateFormat());
  209. $oTimeFormat = new DateTimeFormat($oDateTimeFormat->ToTimeFormat());
  210. $iCount = 0;
  211. $sData = '';
  212. $iPreviousTimeLimit = ini_get('max_execution_time');
  213. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  214. while($aRow = $oSet->FetchAssoc())
  215. {
  216. set_time_limit($iLoopTimeLimit);
  217. $sData .= "<tr>";
  218. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  219. {
  220. $sAlias = $aFieldSpec['sAlias'];
  221. $sAttCode = $aFieldSpec['sAttCode'];
  222. $sField = '';
  223. $oObj = $aRow[$sAlias];
  224. if ($oObj == null)
  225. {
  226. $sData .= "<td x:str></td>";
  227. continue;
  228. }
  229. switch($sAttCode)
  230. {
  231. case 'id':
  232. $sField = $oObj->GetKey();
  233. $sData .= "<td>$sField</td>";
  234. break;
  235. default:
  236. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  237. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  238. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  239. {
  240. // Split the date and time in two columns
  241. $sDate = $oDateFormat->Format($oObj->Get($sAttCode));
  242. $sTime = $oTimeFormat->Format($oObj->Get($sAttCode));
  243. $sData .= "<td>$sDate</td>";
  244. $sData .= "<td>$sTime</td>";
  245. }
  246. else if (get_class($oFinalAttDef) == 'AttributeDate')
  247. {
  248. $sDate = $oDateFormat->Format($oObj->Get($sAttCode));
  249. $sData .= "<td>$sDate</td>";
  250. }
  251. else if($oAttDef instanceof AttributeCaseLog)
  252. {
  253. $rawValue = $oObj->Get($sAttCode);
  254. $sField = str_replace("\n", "<br/>", htmlentities($rawValue->__toString(), ENT_QUOTES, 'UTF-8'));
  255. // Trick for Excel: treat the content as text even if it begins with an equal sign
  256. $sData .= "<td x:str>$sField</td>";
  257. }
  258. else if($oAttDef instanceof AttributeString)
  259. {
  260. $sField = $oObj->GetAsHTML($sAttCode, $this->bLocalizeOutput);
  261. $sData .= "<td x:str>$sField</td>";
  262. }
  263. else
  264. {
  265. $rawValue = $oObj->Get($sAttCode);
  266. if ($this->bLocalizeOutput)
  267. {
  268. $sField = htmlentities($oFinalAttDef->GetEditValue($rawValue), ENT_QUOTES, 'UTF-8');
  269. }
  270. else
  271. {
  272. $sField = htmlentities($rawValue, ENT_QUOTES, 'UTF-8');
  273. }
  274. $sData .= "<td>$sField</td>";
  275. }
  276. }
  277. }
  278. $sData .= "</tr>";
  279. $iCount++;
  280. }
  281. set_time_limit($iPreviousTimeLimit);
  282. $this->aStatusInfo['position'] += $this->iChunkSize;
  283. if ($this->aStatusInfo['total'] == 0)
  284. {
  285. $iPercentage = 100;
  286. }
  287. else
  288. {
  289. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  290. }
  291. if ($iCount < $this->iChunkSize)
  292. {
  293. $sRetCode = 'done';
  294. }
  295. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  296. return $sData;
  297. }
  298. public function GetFooter()
  299. {
  300. $sData = "</table>\n";
  301. return $sData;
  302. }
  303. public function GetSupportedFormats()
  304. {
  305. return array('spreadsheet' => Dict::S('Core:BulkExport:SpreadsheetFormat'));
  306. }
  307. public function GetMimeType()
  308. {
  309. return 'text/html';
  310. }
  311. public function GetFileExtension()
  312. {
  313. return 'html';
  314. }
  315. }