pdfbulkexport.class.inc.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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: PDF export, based on the HTML export converted to PDF
  20. *
  21. * @copyright Copyright (C) 2015 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. class PDFBulkExport extends HTMLBulkExport
  25. {
  26. public function DisplayUsage(Page $oP)
  27. {
  28. $oP->p(" * pdf 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(" *\tpage_size: (optional) size of the page. One of A4, A3, Letter (default is 'A4').");
  31. $oP->p(" *\tpage_orientation: (optional) the orientation of the page. Either Portrait or Landscape (default is 'Portrait').");
  32. $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'");
  33. }
  34. public function EnumFormParts()
  35. {
  36. return array_merge(array('pdf_options' => array('pdf_options')), parent::EnumFormParts());
  37. }
  38. public function DisplayFormPart(WebPage $oP, $sPartId)
  39. {
  40. switch($sPartId)
  41. {
  42. case 'pdf_options':
  43. $oP->add('<fieldset><legend>'.Dict::S('Core:BulkExport:PDFOptions').'</legend>');
  44. $oP->add('<table class="export_parameters"><tr><td style="vertical-align:top">');
  45. $oP->add('<h3>'.Dict::S('Core:PDFBulkExport:PageFormat').'</h3>');
  46. $oP->add('<table>');
  47. $oP->add('<tr>');
  48. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageSize').'</td>');
  49. $oP->add('<td>'.$this->GetSelectCtrl('page_size', array('A3', 'A4', 'Letter'), 'Core:BulkExport:PageSize-', 'A4').'</td>');
  50. $oP->add('</tr>');
  51. $oP->add('<td>'.Dict::S('Core:BulkExport:PDFPageOrientation').'</td>');
  52. $oP->add('<td>'.$this->GetSelectCtrl('page_orientation', array('P', 'L'), 'Core:BulkExport:PageOrientation-', 'L').'</td>');
  53. $oP->add('</tr>');
  54. $oP->add('</table>');
  55. $oP->add('</td><td style="vertical-align:top">');
  56. $sDateTimeFormat = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  57. $sDefaultChecked = ($sDateTimeFormat == (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  58. $sCustomChecked = ($sDateTimeFormat !== (string)AttributeDateTime::GetFormat()) ? ' checked' : '';
  59. $oP->add('<h3>'.Dict::S('Core:BulkExport:DateTimeFormat').'</h3>');
  60. $sDefaultFormat = htmlentities((string)AttributeDateTime::GetFormat(), ENT_QUOTES, 'UTF-8');
  61. $sExample = htmlentities(date((string)AttributeDateTime::GetFormat()), ENT_QUOTES, 'UTF-8');
  62. $oP->add('<input type="radio" id="pdf_date_time_format_default" name="pdf_date_format_radio" value="default"'.$sDefaultChecked.'><label for="pdf_date_time_format_default"> '.Dict::Format('Core:BulkExport:DateTimeFormatDefault_Example', $sDefaultFormat, $sExample).'</label><br/>');
  63. $sFormatInput = '<input type="text" size="15" name="date_format" id="pdf_custom_date_time_format" title="" value="'.htmlentities($sDateTimeFormat, ENT_QUOTES, 'UTF-8').'"/>';
  64. $oP->add('<input type="radio" id="pdf_date_time_format_custom" name="pdf_date_format_radio" value="custom"'.$sCustomChecked.'><label for="pdf_date_time_format_custom"> '.Dict::Format('Core:BulkExport:DateTimeFormatCustom_Format', $sFormatInput).'</label>');
  65. $oP->add('</td></tr></table>');
  66. $oP->add('</fieldset>');
  67. $sJSTooltip = json_encode('<div id="date_format_tooltip">'.Dict::S('UI:CSVImport:CustomDateTimeFormatTooltip').'</div>');
  68. $oP->add_ready_script(
  69. <<<EOF
  70. $('#pdf_custom_date_time_format').tooltip({content: function() { return $sJSTooltip; } });
  71. $('#form_part_pdf_options').on('preview_updated', function() { FormatDatesInPreview('pdf', 'html'); });
  72. $('#pdf_custom_date_time_format').on('click', function() { $('#pdf_date_time_format_custom').prop('checked', true); FormatDatesInPreview('pdf', 'html'); }).on('keyup', function() { FormatDatesInPreview('pdf', 'html'); });
  73. EOF
  74. );
  75. break;
  76. default:
  77. return parent:: DisplayFormPart($oP, $sPartId);
  78. }
  79. }
  80. protected function GetSelectCtrl($sName, $aValues, $sDictPrefix, $sDefaultValue)
  81. {
  82. $sCurrentValue = utils::ReadParam($sName, $sDefaultValue, false, 'raw_data');
  83. $aLabels = array();
  84. foreach($aValues as $sVal)
  85. {
  86. $aLabels[$sVal] = Dict::S($sDictPrefix.$sVal);
  87. }
  88. asort($aLabels);
  89. $sHtml = '<select name="'.$sName.'">';
  90. foreach($aLabels as $sVal => $sLabel)
  91. {
  92. $sSelected = ($sVal == $sCurrentValue) ? 'selected' : '';
  93. $sHtml .= '<option value="'.$sVal.'" '.$sSelected.'>'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>';
  94. }
  95. $sHtml .= '</select>';
  96. return $sHtml;
  97. }
  98. public function ReadParameters()
  99. {
  100. parent::ReadParameters();
  101. $this->aStatusInfo['page_size'] = utils::ReadParam('page_size', 'A4', true, 'raw_data');
  102. $this->aStatusInfo['page_orientation'] = utils::ReadParam('page_orientation', 'L', true);
  103. $sDateFormatRadio = utils::ReadParam('pdf_date_format_radio', '');
  104. switch($sDateFormatRadio)
  105. {
  106. case 'default':
  107. // Export from the UI => format = same as is the UI
  108. $this->aStatusInfo['date_format'] = (string)AttributeDateTime::GetFormat();
  109. break;
  110. case 'custom':
  111. // Custom format specified from the UI
  112. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  113. break;
  114. default:
  115. // Export from the command line (or scripted) => default format is SQL, as in previous versions of iTop, unless specified otherwise
  116. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetSQLFormat(), true, 'raw_data');
  117. }
  118. }
  119. public function GetHeader()
  120. {
  121. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  122. $sData = parent::GetHeader();
  123. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  124. if ($hFile === false)
  125. {
  126. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  127. }
  128. fwrite($hFile, $sData."\n");
  129. fclose($hFile);
  130. return '';
  131. }
  132. public function GetNextChunk(&$aStatus)
  133. {
  134. $oPrevFormat = AttributeDateTime::GetFormat();
  135. AttributeDateTime::SetFormat(new DateTimeFormat($this->aStatusInfo['date_format']));
  136. $sData = parent::GetNextChunk($aStatus);
  137. AttributeDateTime::SetFormat($oPrevFormat);
  138. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  139. if ($hFile === false)
  140. {
  141. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  142. }
  143. fwrite($hFile, $sData."\n");
  144. fclose($hFile);
  145. return '';
  146. }
  147. public function GetFooter()
  148. {
  149. $sData = parent::GetFooter();
  150. require_once(APPROOT.'application/pdfpage.class.inc.php');
  151. $oPage = new PDFPage(Dict::Format('Core:BulkExportOf_Class', MetaModel::GetName($this->oSearch->GetClass())), $this->aStatusInfo['page_size'], $this->aStatusInfo['page_orientation']);
  152. $oPDF = $oPage->get_tcpdf();
  153. $oPDF->SetFont('dejavusans', '', 8, '', true);
  154. $oPage->add(file_get_contents($this->aStatusInfo['tmp_file']));
  155. $oPage->add($sData);
  156. $sPDF = $oPage->get_pdf();
  157. return $sPDF;
  158. }
  159. public function GetSupportedFormats()
  160. {
  161. return array('pdf' => Dict::S('Core:BulkExport:PDFFormat'));
  162. }
  163. public function GetMimeType()
  164. {
  165. return 'application/x-pdf';
  166. }
  167. public function GetFileExtension()
  168. {
  169. return 'pdf';
  170. }
  171. }