spreadsheetbulkexport.class.inc.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. $sRet = $oObj->GetAsHTML($sAttCode);
  162. }
  163. else
  164. {
  165. if ($this->bLocalizeOutput)
  166. {
  167. $sRet = htmlentities($oObj->GetEditValue(), ENT_QUOTES, 'UTF-8');
  168. }
  169. else
  170. {
  171. $sRet = htmlentities((string)$value, ENT_QUOTES, 'UTF-8');
  172. }
  173. }
  174. }
  175. return $sRet;
  176. }
  177. public function SetHttpHeaders(WebPage $oPage)
  178. {
  179. // Integration within MS-Excel web queries + HTTPS + IIS:
  180. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  181. // Then the fix is to force the reset of header values Pragma and Cache-control
  182. $oPage->add_header("Pragma:", true);
  183. $oPage->add_header("Cache-control:", true);
  184. }
  185. public function GetHeader()
  186. {
  187. $oSet = new DBObjectSet($this->oSearch);
  188. $this->aStatusInfo['status'] = 'running';
  189. $this->aStatusInfo['position'] = 0;
  190. $this->aStatusInfo['total'] = $oSet->Count();
  191. $aData = array();
  192. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  193. {
  194. $sColLabel = $aFieldSpec['sColLabel'];
  195. if ($aFieldSpec['sAttCode'] != 'id')
  196. {
  197. $oAttDef = MetaModel::GetAttributeDef($aFieldSpec['sClass'], $aFieldSpec['sAttCode']);
  198. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  199. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  200. {
  201. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Date').')';
  202. $aData[] = $sColLabel.' ('.Dict::S('UI:SplitDateTime-Time').')';
  203. }
  204. else
  205. {
  206. $aData[] = $sColLabel;
  207. }
  208. }
  209. else
  210. {
  211. $aData[] = $sColLabel;
  212. }
  213. }
  214. $sData = '';
  215. $sData .= '<style>table br {mso-data-placement:same-cell;}</style>'; // Trick for Excel: keep line breaks inside the same cell !
  216. $sData .= "<table border=\"1\">\n";
  217. $sData .= "<tr>\n";
  218. foreach($aData as $sLabel)
  219. {
  220. $sData .= "<td>".$sLabel."</td>\n";
  221. }
  222. $sData .= "</tr>\n";
  223. return $sData;
  224. }
  225. public function GetNextChunk(&$aStatus)
  226. {
  227. $sRetCode = 'run';
  228. $iPercentage = 0;
  229. $oSet = new DBObjectSet($this->oSearch);
  230. $oSet->SetLimit($this->iChunkSize, $this->aStatusInfo['position']);
  231. $this->OptimizeColumnLoad($oSet);
  232. $sExportDateTimeFormat = $this->aStatusInfo['date_format'];
  233. $bFormattedText = (array_key_exists('formatted_text', $this->aStatusInfo) ? $this->aStatusInfo['formatted_text'] : false);
  234. // Date & time formats
  235. $oDateTimeFormat = new DateTimeFormat($sExportDateTimeFormat);
  236. $oDateFormat = new DateTimeFormat($oDateTimeFormat->ToDateFormat());
  237. $oTimeFormat = new DateTimeFormat($oDateTimeFormat->ToTimeFormat());
  238. $iCount = 0;
  239. $sData = '';
  240. $iPreviousTimeLimit = ini_get('max_execution_time');
  241. $iLoopTimeLimit = MetaModel::GetConfig()->Get('max_execution_time_per_loop');
  242. while($aRow = $oSet->FetchAssoc())
  243. {
  244. set_time_limit($iLoopTimeLimit);
  245. $sData .= "<tr>";
  246. foreach($this->aStatusInfo['fields'] as $iCol => $aFieldSpec)
  247. {
  248. $sAlias = $aFieldSpec['sAlias'];
  249. $sAttCode = $aFieldSpec['sAttCode'];
  250. $sField = '';
  251. $oObj = $aRow[$sAlias];
  252. if ($oObj == null)
  253. {
  254. $sData .= "<td x:str></td>";
  255. continue;
  256. }
  257. switch($sAttCode)
  258. {
  259. case 'id':
  260. $sField = $oObj->GetKey();
  261. $sData .= "<td>$sField</td>";
  262. break;
  263. default:
  264. $oAttDef = MetaModel::GetAttributeDef(get_class($oObj), $sAttCode);
  265. $oFinalAttDef = $oAttDef->GetFinalAttDef();
  266. if (get_class($oFinalAttDef) == 'AttributeDateTime')
  267. {
  268. // Split the date and time in two columns
  269. $sDate = $oDateFormat->Format($oObj->Get($sAttCode));
  270. $sTime = $oTimeFormat->Format($oObj->Get($sAttCode));
  271. $sData .= "<td>$sDate</td>";
  272. $sData .= "<td>$sTime</td>";
  273. }
  274. else if (get_class($oFinalAttDef) == 'AttributeDate')
  275. {
  276. $sDate = $oDateFormat->Format($oObj->Get($sAttCode));
  277. $sData .= "<td>$sDate</td>";
  278. }
  279. else if($oAttDef instanceof AttributeCaseLog)
  280. {
  281. $rawValue = $oObj->Get($sAttCode);
  282. $sField = str_replace("\n", "<br/>", htmlentities($rawValue->__toString(), ENT_QUOTES, 'UTF-8'));
  283. // Trick for Excel: treat the content as text even if it begins with an equal sign
  284. $sData .= "<td x:str>$sField</td>";
  285. }
  286. elseif ($oAttDef instanceof AttributeText)
  287. {
  288. if ($bFormattedText)
  289. {
  290. // Replace paragraphs (<p...>...</p>, etc) by line breaks (<br/>) since Excel (pre-2016) splits the cells when there is a paragraph
  291. $sField = static::HtmlToSpreadsheet($oObj->GetAsHTML($sAttCode));
  292. }
  293. else
  294. {
  295. // Convert to plain text
  296. $sField = utils::HtmlToText($oObj->GetAsHTML($sAttCode));
  297. }
  298. $sData .= "<td x:str>$sField</td>";
  299. }
  300. else if($oAttDef instanceof AttributeString)
  301. {
  302. $sField = $oObj->GetAsHTML($sAttCode, $this->bLocalizeOutput);
  303. $sData .= "<td x:str>$sField</td>";
  304. }
  305. else
  306. {
  307. $rawValue = $oObj->Get($sAttCode);
  308. if ($this->bLocalizeOutput)
  309. {
  310. $sField = htmlentities($oFinalAttDef->GetEditValue($rawValue), ENT_QUOTES, 'UTF-8');
  311. }
  312. else
  313. {
  314. $sField = htmlentities($rawValue, ENT_QUOTES, 'UTF-8');
  315. }
  316. $sData .= "<td>$sField</td>";
  317. }
  318. }
  319. }
  320. $sData .= "</tr>";
  321. $iCount++;
  322. }
  323. set_time_limit($iPreviousTimeLimit);
  324. $this->aStatusInfo['position'] += $this->iChunkSize;
  325. if ($this->aStatusInfo['total'] == 0)
  326. {
  327. $iPercentage = 100;
  328. }
  329. else
  330. {
  331. $iPercentage = floor(min(100.0, 100.0*$this->aStatusInfo['position']/$this->aStatusInfo['total']));
  332. }
  333. if ($iCount < $this->iChunkSize)
  334. {
  335. $sRetCode = 'done';
  336. }
  337. $aStatus = array('code' => $sRetCode, 'message' => Dict::S('Core:BulkExport:RetrievingData'), 'percentage' => $iPercentage);
  338. return $sData;
  339. }
  340. public function GetFooter()
  341. {
  342. $sData = "</table>\n";
  343. return $sData;
  344. }
  345. public function GetSupportedFormats()
  346. {
  347. return array('spreadsheet' => Dict::S('Core:BulkExport:SpreadsheetFormat'));
  348. }
  349. public function GetMimeType()
  350. {
  351. return 'text/html';
  352. }
  353. public function GetFileExtension()
  354. {
  355. return 'html';
  356. }
  357. /**
  358. * Cleanup all markup displayed as line breaks (except <br> tags) since this
  359. * causes Excel (pre-2016) to generate extra lines in the table, thus breaking
  360. * the tabular disposition of the export
  361. * Note: Excel 2016 also refuses line breaks, so the only solution for this case is alas plain text
  362. * @param string $sHtml The HTML to cleanup
  363. * @return string The cleaned HTML
  364. */
  365. public static function HtmlToSpreadsheet($sHtml)
  366. {
  367. if (trim(strip_tags($sHtml)) === '')
  368. {
  369. // Display this value as an empty cell in the table
  370. return '&nbsp;';
  371. }
  372. // The tags listed here are a subset of the whitelist defined in HTMLDOMSanitizer
  373. // 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/>
  374. // in order to preserve any inline style/attribute of the removed tag
  375. $aTagsToReplace = array(
  376. 'pre', 'div', 'p', 'hr', 'center', 'h1', 'h2', 'h3', 'h4', 'li', 'fieldset', 'legend', 'nav', 'section', 'tr', 'caption',
  377. );
  378. // Tags to completely remove from the markup
  379. $aTagsToRemove = array(
  380. 'table', 'thead', 'tbody', 'ul', 'ol', 'td', 'th',
  381. );
  382. // Remove the englobing <div class="HTML" >...</div> to prevent an extra line break
  383. $sHtml = preg_replace('|^<div class="HTML" >(.*)</div>$|s', '$1', $sHtml); // Must use the "s" (. matches newline) modifier
  384. foreach($aTagsToReplace as $sTag)
  385. {
  386. $sHtml = preg_replace("|<{$sTag} ?([^>]*)>|is", '<span $1>', $sHtml);
  387. $sHtml = preg_replace("|</{$sTag}>|i", '</span><br/>', $sHtml);
  388. }
  389. foreach($aTagsToRemove as $sTag)
  390. {
  391. $sHtml = preg_replace("|<{$sTag} ?([^>]*)>|is", '', $sHtml);
  392. $sHtml = preg_replace("|</{$sTag}>|i", '', $sHtml);
  393. }
  394. // Remove any trailing <br/>, if any, to prevent an extra line break
  395. $sHtml = preg_replace("|<br/>$|", '', $sHtml);
  396. return $sHtml;
  397. }
  398. }