spreadsheetbulkexport.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. $oP->p(" *\tformatted_text: set to 1 to formatted text fields with their HTML markup, 0 to remove formatting. Default is 1 (= formatted text)");
  33. }
  34. public function EnumFormParts()
  35. {
  36. return array_merge(parent::EnumFormParts(), array('spreadsheet_options' => array('no-localize') ,'interactive_fields_spreadsheet' => array('interactive_fields_spreadsheet')));
  37. }
  38. public function DisplayFormPart(WebPage $oP, $sPartId)
  39. {
  40. switch($sPartId)
  41. {
  42. case 'interactive_fields_spreadsheet':
  43. $this->GetInteractiveFieldsWidget($oP, 'interactive_fields_spreadsheet');
  44. break;
  45. case 'spreadsheet_options':
  46. $sChecked = (utils::ReadParam('no_localize', 0) == 1) ? ' checked ' : '';
  47. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:SpreadsheetOptions').'</legend>');
  48. $oP->add('<table>');
  49. $oP->add('<tr>');
  50. $oP->add('<td style="vertical-align:top">');
  51. $sChecked = (utils::ReadParam('formatted_text', 1) == 1) ? ' checked ' : '';
  52. $oP->add('<h3>'.Dict::S('Core:BulkExport:TextFormat').'</h3>');
  53. $oP->add('<input type="hidden" name="formatted_text" value="0">'); // Trick to pass the zero value if the checkbox below is unchecked, since we want the default value to be "1"
  54. $oP->add('<input type="checkbox" id="spreadsheet_formatted_text" name="formatted_text" value="1"'.$sChecked.'><label for="spreadsheet_formatted_text"> '.Dict::S('Core:BulkExport:OptionFormattedText').'</label><br/><br/>');
  55. $oP->add('<input type="checkbox" id="spreadsheet_no_localize" name="no_localize" value="1"'.$sChecked.'><label for="spreadsheet_no_localize"> '.Dict::S('Core:BulkExport:OptionNoLocalize').'</label>');
  56. $oP->add('</td>');
  57. $sDateTimeFormat = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  58. $sDefaultChecked = ($sDateTimeFormat == (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  59. $sCustomChecked = ($sDateTimeFormat !== (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  60. $oP->add('<td>');
  61. $oP->add('<h3>'.Dict::S('Core:BulkExport:DateTimeFormat').'</h3>');
  62. $sDefaultFormat = htmlentities((string)AttributeDateTime::GetFormat(), ENT_QUOTES, 'UTF-8');
  63. $sExample = htmlentities(date((string)AttributeDateTime::GetFormat()), ENT_QUOTES, 'UTF-8');
  64. $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/>');
  65. $sFormatInput = '<input type="text" size="15" name="date_format" id="spreadsheet_custom_date_time_format" title="" value="'.htmlentities($sDateTimeFormat, ENT_QUOTES, 'UTF-8').'"/>';
  66. $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>');
  67. $oP->add('</td>');
  68. $oP->add('</tr>');
  69. $oP->add('</table>');
  70. $oP->add('</fieldset>');
  71. $sJSTooltip = json_encode('<div class="date_format_tooltip">'.Dict::S('UI:CSVImport:CustomDateTimeFormatTooltip').'</div>');
  72. $oP->add_ready_script(
  73. <<<EOF
  74. $('#spreadsheet_custom_date_time_format').tooltip({content: function() { return $sJSTooltip; } });
  75. $('#form_part_spreadsheet_options').on('preview_updated', function() { FormatDatesInPreview('spreadsheet', 'spreadsheet'); });
  76. $('#spreadsheet_date_time_format_default').on('click', function() { FormatDatesInPreview('spreadsheet', 'spreadsheet'); });
  77. $('#spreadsheet_date_time_format_custom').on('click', function() { FormatDatesInPreview('spreadsheet', 'spreadsheet'); });
  78. $('#spreadsheet_custom_date_time_format').on('click', function() { $('#spreadsheet_date_time_format_custom').prop('checked', true); });
  79. $('#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'); });
  80. EOF
  81. );
  82. break;
  83. default:
  84. return parent:: DisplayFormPart($oP, $sPartId);
  85. }
  86. }
  87. public function ReadParameters()
  88. {
  89. parent::ReadParameters();
  90. $this->aStatusInfo['formatted_text'] = (bool)utils::ReadParam('formatted_text', 1, true);
  91. $sDateFormatRadio = utils::ReadParam('spreadsheet_date_format_radio', '');
  92. switch($sDateFormatRadio)
  93. {
  94. case 'default':
  95. // Export from the UI => format = same as is the UI
  96. $this->aStatusInfo['date_format'] = (string)AttributeDateTime::GetFormat();
  97. break;
  98. case 'custom':
  99. // Custom format specified from the UI
  100. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  101. break;
  102. default:
  103. // Export from the command line (or scripted) => default format is SQL, as in previous versions of iTop, unless specified otherwise
  104. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetSQLFormat(), true, 'raw_data');
  105. }
  106. }
  107. protected function GetSampleData($oObj, $sAttCode)
  108. {
  109. if ($sAttCode != 'id')
  110. {
  111. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  112. if ($oAttDef instanceof AttributeDateTime) // AttributeDate is derived from AttributeDateTime
  113. {
  114. $sClass = (get_class($oAttDef) == 'AttributeDateTime') ? 'user-formatted-date-time' : 'user-formatted-date';
  115. return '<div class="'.$sClass.'" data-date="'.$oObj->Get($sAttCode).'">'.htmlentities($oAttDef->GetEditValue($oObj->Get($sAttCode), $oObj), ENT_QUOTES, 'UTF-8').'</div>';
  116. }
  117. }
  118. return $this->GetValue($oObj, $sAttCode);
  119. }
  120. protected function GetValue($oObj, $sAttCode)
  121. {
  122. $bFormattedText = (array_key_exists('formatted_text', $this->aStatusInfo) ? $this->aStatusInfo['formatted_text'] : false);
  123. switch($sAttCode)
  124. {
  125. case 'id':
  126. $sRet = $oObj->GetKey();
  127. break;
  128. default:
  129. $value = $oObj->Get($sAttCode);
  130. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  131. if ($value instanceof ormCaseLog)
  132. {
  133. $sRet = str_replace("\n", "<br/>", htmlentities($value->__toString(), ENT_QUOTES, 'UTF-8'));
  134. }
  135. elseif ($value instanceof ormStopWatch)
  136. {
  137. $sRet = $value->GetTimeSpent();
  138. }
  139. elseif ($value instanceof ormDocument)
  140. {
  141. $sRet = '';
  142. }
  143. elseif ($oAttDef instanceof AttributeText)
  144. {
  145. if ($bFormattedText)
  146. {
  147. // Replace paragraphs (<p...>...</p>, etc) by line breaks (<br/>) since Excel (pre-2016) splits the cells when there is a paragraph
  148. $sRet = static::HtmlToSpreadsheet($oObj->GetAsHTML($sAttCode));
  149. }
  150. else
  151. {
  152. $sRet = utils::HtmlToText($oObj->GetAsHTML($sAttCode));
  153. }
  154. }
  155. elseif ($oAttDef instanceof AttributeString)
  156. {
  157. $sRet = $oObj->GetAsHTML($sAttCode);
  158. }
  159. elseif ($oAttDef instanceof AttributeCustomFields)
  160. {
  161. // Stick to the weird implementation made in GetNextChunk
  162. $sRet = utils::TextToHtml($oObj->GetEditValue($sAttCode));
  163. }
  164. else
  165. {
  166. if ($this->bLocalizeOutput)
  167. {
  168. $sRet = htmlentities($oObj->GetEditValue(), ENT_QUOTES, 'UTF-8');
  169. }
  170. else
  171. {
  172. $sRet = htmlentities((string)$value, ENT_QUOTES, 'UTF-8');
  173. }
  174. }
  175. }
  176. return $sRet;
  177. }
  178. public function SetHttpHeaders(WebPage $oPage)
  179. {
  180. // Integration within MS-Excel web queries + HTTPS + IIS:
  181. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  182. // Then the fix is to force the reset of header values Pragma and Cache-control
  183. $oPage->add_header("Pragma:", true);
  184. $oPage->add_header("Cache-control:", true);
  185. }
  186. public function GetHeader()
  187. {
  188. $oSet = new DBObjectSet($this->oSearch);
  189. $this->aStatusInfo['status'] = 'running';
  190. $this->aStatusInfo['position'] = 0;
  191. $this->aStatusInfo['total'] = $oSet->Count();
  192. $aData = array();
  193. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  194. {
  195. $sColLabel = $aFieldSpec['sColLabel'];
  196. if ($aFieldSpec['sAttCode'] != 'id')
  197. {
  198. $oAttDef = MetaModel::GetAttributeDef($aFieldSpec['sClass'], $aFieldSpec['sAttCode']);
  199. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  200. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  201. {
  202. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Date').')';
  203. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Time').')';
  204. }
  205. else
  206. {
  207. $aData[] = $sColLabel;
  208. }
  209. }
  210. else
  211. {
  212. $aData[] = $sColLabel;
  213. }
  214. }
  215. $sData = '';
  216. $sData .= '<style>table br {mso-data-placement:same-cell;}</style>'; // Trick for Excel: keep line breaks inside the same cell !
  217. $sData .= "<table border=\"1\">\n";
  218. $sData .= "<tr>\n";
  219. foreach($aData as $sLabel)
  220. {
  221. $sData .= "<td>".$sLabel."</td>\n";
  222. }
  223. $sData .= "</tr>\n";
  224. return $sData;
  225. }
  226. public function GetNextChunk(&$aStatus)
  227. {
  228. $sRetCode = 'run';
  229. $iPercentage = 0;
  230. $oSet = new DBObjectSet($this->oSearch);
  231. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  232. $this->OptimizeColumnLoad($oSet);
  233. $sExportDateTimeFormat = $this->aStatusInfo['date_format'];
  234. $bFormattedText = (array_key_exists('formatted_text', $this->aStatusInfo) ? $this->aStatusInfo['formatted_text'] : false);
  235. // Date & time formats
  236. $oDateTimeFormat = new DateTimeFormat($sExportDateTimeFormat);
  237. $oDateFormat = new DateTimeFormat($oDateTimeFormat->ToDateFormat());
  238. $oTimeFormat = new DateTimeFormat($oDateTimeFormat->ToTimeFormat());
  239. $iCount = 0;
  240. $sData = '';
  241. $iPreviousTimeLimit = ini_get('max_execution_time');
  242. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  243. while($aRow = $oSet->FetchAssoc())
  244. {
  245. set_time_limit($iLoopTimeLimit);
  246. $sData .= "<tr>";
  247. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  248. {
  249. $sAlias = $aFieldSpec['sAlias'];
  250. $sAttCode = $aFieldSpec['sAttCode'];
  251. $sField = '';
  252. $oObj = $aRow[$sAlias];
  253. if ($oObj == null)
  254. {
  255. $sData .= "<td x:str></td>";
  256. continue;
  257. }
  258. switch($sAttCode)
  259. {
  260. case 'id':
  261. $sField = $oObj->GetKey();
  262. $sData .= "<td>$sField</td>";
  263. break;
  264. default:
  265. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  266. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  267. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  268. {
  269. // Split the date and time in two columns
  270. $sDate = $oDateFormat->Format($oObj->Get($sAttCode));
  271. $sTime = $oTimeFormat->Format($oObj->Get($sAttCode));
  272. $sData .= "<td>$sDate</td>";
  273. $sData .= "<td>$sTime</td>";
  274. }
  275. else if (get_class($oFinalAttDef) == 'AttributeDate')
  276. {
  277. $sDate = $oDateFormat->Format($oObj->Get($sAttCode));
  278. $sData .= "<td>$sDate</td>";
  279. }
  280. else if($oAttDef instanceof AttributeCaseLog)
  281. {
  282. $rawValue = $oObj->Get($sAttCode);
  283. $sField = str_replace("\n", "<br/>", htmlentities($rawValue->__toString(), ENT_QUOTES, 'UTF-8'));
  284. // Trick for Excel: treat the content as text even if it begins with an equal sign
  285. $sData .= "<td x:str>$sField</td>";
  286. }
  287. elseif ($oAttDef instanceof AttributeText)
  288. {
  289. if ($bFormattedText)
  290. {
  291. // Replace paragraphs (<p...>...</p>, etc) by line breaks (<br/>) since Excel (pre-2016) splits the cells when there is a paragraph
  292. $sField = static::HtmlToSpreadsheet($oObj->GetAsHTML($sAttCode));
  293. }
  294. else
  295. {
  296. // Convert to plain text
  297. $sField = utils::HtmlToText($oObj->GetAsHTML($sAttCode));
  298. }
  299. $sData .= "<td x:str>$sField</td>";
  300. }
  301. elseif ($oAttDef instanceof AttributeCustomFields)
  302. {
  303. // GetAsHTML returns a table that would not fit
  304. $sField = utils::TextToHtml($oObj->GetEditValue($sAttCode));
  305. $sData .= "<td x:str>$sField</td>";
  306. }
  307. else if($oAttDef instanceof AttributeString)
  308. {
  309. $sField = $oObj->GetAsHTML($sAttCode, $this->bLocalizeOutput);
  310. $sData .= "<td x:str>$sField</td>";
  311. }
  312. else
  313. {
  314. $rawValue = $oObj->Get($sAttCode);
  315. if ($this->bLocalizeOutput)
  316. {
  317. $sField = htmlentities($oFinalAttDef->GetEditValue($rawValue), ENT_QUOTES, 'UTF-8');
  318. }
  319. else
  320. {
  321. $sField = htmlentities($rawValue, ENT_QUOTES, 'UTF-8');
  322. }
  323. $sData .= "<td>$sField</td>";
  324. }
  325. }
  326. }
  327. $sData .= "</tr>";
  328. $iCount++;
  329. }
  330. set_time_limit($iPreviousTimeLimit);
  331. $this->aStatusInfo['position'] += $this->iChunkSize;
  332. if ($this->aStatusInfo['total'] == 0)
  333. {
  334. $iPercentage = 100;
  335. }
  336. else
  337. {
  338. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  339. }
  340. if ($iCount < $this->iChunkSize)
  341. {
  342. $sRetCode = 'done';
  343. }
  344. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  345. return $sData;
  346. }
  347. public function GetFooter()
  348. {
  349. $sData = "</table>\n";
  350. return $sData;
  351. }
  352. public function GetSupportedFormats()
  353. {
  354. return array('spreadsheet' => Dict::S('Core:BulkExport:SpreadsheetFormat'));
  355. }
  356. public function GetMimeType()
  357. {
  358. return 'text/html';
  359. }
  360. public function GetFileExtension()
  361. {
  362. return 'html';
  363. }
  364. /**
  365. * Cleanup all markup displayed as line breaks (except <br> tags) since this
  366. * causes Excel (pre-2016) to generate extra lines in the table, thus breaking
  367. * the tabular disposition of the export
  368. * Note: Excel 2016 also refuses line breaks, so the only solution for this case is alas plain text
  369. * @param string $sHtml The HTML to cleanup
  370. * @return string The cleaned HTML
  371. */
  372. public static function HtmlToSpreadsheet($sHtml)
  373. {
  374. if (trim(strip_tags($sHtml)) === '')
  375. {
  376. // Display this value as an empty cell in the table
  377. return '&nbsp;';
  378. }
  379. // The tags listed here are a subset of the whitelist defined in HTMLDOMSanitizer
  380. // Tags causing a visual "line break" in the displayed page (i.e. display: block) are to be replaced by a <span> followed by a <br/>
  381. // in order to preserve any inline style/attribute of the removed tag
  382. $aTagsToReplace = array(
  383. 'pre', 'div', 'p', 'hr', 'center', 'h1', 'h2', 'h3', 'h4', 'li', 'fieldset', 'legend', 'nav', 'section', 'tr', 'caption',
  384. );
  385. // Tags to completely remove from the markup
  386. $aTagsToRemove = array(
  387. 'table', 'thead', 'tbody', 'ul', 'ol', 'td', 'th',
  388. );
  389. // Remove the englobing <div class="HTML" >...</div> to prevent an extra line break
  390. $sHtml = preg_replace('|^<div class="HTML" >(.*)</div>$|s', '$1', $sHtml); // Must use the "s" (. matches newline) modifier
  391. foreach($aTagsToReplace as $sTag)
  392. {
  393. $sHtml = preg_replace("|<{$sTag} ?([^>]*)>|is", '<span $1>', $sHtml);
  394. $sHtml = preg_replace("|</{$sTag}>|i", '</span><br/>', $sHtml);
  395. }
  396. foreach($aTagsToRemove as $sTag)
  397. {
  398. $sHtml = preg_replace("|<{$sTag} ?([^>]*)>|is", '', $sHtml);
  399. $sHtml = preg_replace("|</{$sTag}>|i", '', $sHtml);
  400. }
  401. // Remove any trailing <br/>, if any, to prevent an extra line break
  402. $sHtml = preg_replace("|<br/>$|", '', $sHtml);
  403. return $sHtml;
  404. }
  405. }