pdfbulkexport.class.inc.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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="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="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. $('#pdf_custom_date_time_format').on('click', function() { $('#pdf_date_time_format_custom').prop('checked', true); });
  72. EOF
  73. );
  74. break;
  75. default:
  76. return parent:: DisplayFormPart($oP, $sPartId);
  77. }
  78. }
  79. protected function GetSelectCtrl($sName, $aValues, $sDictPrefix, $sDefaultValue)
  80. {
  81. $sCurrentValue = utils::ReadParam($sName, $sDefaultValue, false, 'raw_data');
  82. $aLabels = array();
  83. foreach($aValues as $sVal)
  84. {
  85. $aLabels[$sVal] = Dict::S($sDictPrefix.$sVal);
  86. }
  87. asort($aLabels);
  88. $sHtml = '<select name="'.$sName.'">';
  89. foreach($aLabels as $sVal => $sLabel)
  90. {
  91. $sSelected = ($sVal == $sCurrentValue) ? 'selected' : '';
  92. $sHtml .= '<option value="'.$sVal.'" '.$sSelected.'>'.htmlentities($sLabel, ENT_QUOTES, 'UTF-8').'</option>';
  93. }
  94. $sHtml .= '</select>';
  95. return $sHtml;
  96. }
  97. public function ReadParameters()
  98. {
  99. parent::ReadParameters();
  100. $this->aStatusInfo['page_size'] = utils::ReadParam('page_size', 'A4', true, 'raw_data');
  101. $this->aStatusInfo['page_orientation'] = utils::ReadParam('page_orientation', 'L', true);
  102. $sDateFormatRadio = utils::ReadParam('date_format_radio', '');
  103. switch($sDateFormatRadio)
  104. {
  105. case 'default':
  106. // Export from the UI => format = same as is the UI
  107. $this->aStatusInfo['date_format'] = (string)AttributeDateTime::GetFormat();
  108. break;
  109. case 'custom':
  110. // Custom format specified from the UI
  111. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetFormat(), true, 'raw_data');
  112. break;
  113. default:
  114. // Export from the command line (or scripted) => default format is SQL, as in previous versions of iTop, unless specified otherwise
  115. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', (string)AttributeDateTime::GetSQLFormat(), true, 'raw_data');
  116. }
  117. }
  118. public function GetHeader()
  119. {
  120. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  121. $sData = parent::GetHeader();
  122. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  123. if ($hFile === false)
  124. {
  125. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  126. }
  127. fwrite($hFile, $sData."\n");
  128. fclose($hFile);
  129. return '';
  130. }
  131. public function GetNextChunk(&$aStatus)
  132. {
  133. $oPrevFormat = AttributeDateTime::GetFormat();
  134. AttributeDateTime::SetFormat(new DateTimeFormat($this->aStatusInfo['date_format']));
  135. $sData = parent::GetNextChunk($aStatus);
  136. AttributeDateTime::SetFormat($oPrevFormat);
  137. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  138. if ($hFile === false)
  139. {
  140. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  141. }
  142. fwrite($hFile, $sData."\n");
  143. fclose($hFile);
  144. return '';
  145. }
  146. public function GetFooter()
  147. {
  148. $sData = parent::GetFooter();
  149. require_once(APPROOT.'application/pdfpage.class.inc.php');
  150. $oPage = new PDFPage(Dict::Format('Core:BulkExportOf_Class', MetaModel::GetName($this->oSearch->GetClass())), $this->aStatusInfo['page_size'], $this->aStatusInfo['page_orientation']);
  151. $oPDF = $oPage->get_tcpdf();
  152. $oPDF->SetFont('dejavusans', '', 8, '', true);
  153. $oPage->add(file_get_contents($this->aStatusInfo['tmp_file']));
  154. $oPage->add($sData);
  155. $sPDF = $oPage->get_pdf();
  156. return $sPDF;
  157. }
  158. public function GetSupportedFormats()
  159. {
  160. return array('pdf' => Dict::S('Core:BulkExport:PDFFormat'));
  161. }
  162. public function GetMimeType()
  163. {
  164. return 'application/x-pdf';
  165. }
  166. public function GetFileExtension()
  167. {
  168. return 'pdf';
  169. }
  170. }