pdfbulkexport.class.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 format used in the user interface). Example: 'm/d/Y 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', AttributeDateTime::GetFormat(), true, 'raw_data');
  57. $sDefaultChecked = ($sDateTimeFormat == AttributeDateTime::GetFormat()) ? ' checked' : '';
  58. $sCustomChecked = ($sDateTimeFormat !== AttributeDateTime::GetFormat()) ? ' checked' : '';
  59. $oP->add('<h3>'.Dict::S('Core:BulkExport:DateTimeFormat').'</h3>');
  60. $sDefaultFormat = htmlentities(AttributeDateTime::GetFormat(), ENT_QUOTES, 'UTF-8');
  61. $sExample = htmlentities(date(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', 'custom');
  103. if ($sDateFormatRadio == 'default')
  104. {
  105. $this->aStatusInfo['date_format'] = AttributeDateTime::GetFormat();
  106. }
  107. else
  108. {
  109. $this->aStatusInfo['date_format'] = utils::ReadParam('date_format', AttributeDateTime::GetFormat(), true, 'raw_data');
  110. }
  111. }
  112. public function GetHeader()
  113. {
  114. $this->aStatusInfo['tmp_file'] = $this->MakeTmpFile('data');
  115. $sData = parent::GetHeader();
  116. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  117. if ($hFile === false)
  118. {
  119. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  120. }
  121. fwrite($hFile, $sData."\n");
  122. fclose($hFile);
  123. return '';
  124. }
  125. public function GetNextChunk(&$aStatus)
  126. {
  127. $sPrevFormat = AttributeDateTime::GetFormat();
  128. AttributeDateTime::SetFormat($this->aStatusInfo['date_format']);
  129. $sData = parent::GetNextChunk($aStatus);
  130. AttributeDateTime::SetFormat($sPrevFormat);
  131. $hFile = @fopen($this->aStatusInfo['tmp_file'], 'ab');
  132. if ($hFile === false)
  133. {
  134. throw new Exception('PDFBulkExport: Failed to open temporary data file: "'.$this->aStatusInfo['tmp_file'].'" for writing.');
  135. }
  136. fwrite($hFile, $sData."\n");
  137. fclose($hFile);
  138. return '';
  139. }
  140. public function GetFooter()
  141. {
  142. $sData = parent::GetFooter();
  143. require_once(APPROOT.'application/pdfpage.class.inc.php');
  144. $oPage = new PDFPage(Dict::Format('Core:BulkExportOf_Class', MetaModel::GetName($this->oSearch->GetClass())), $this->aStatusInfo['page_size'], $this->aStatusInfo['page_orientation']);
  145. $oPDF = $oPage->get_tcpdf();
  146. $oPDF->SetFont('dejavusans', '', 8, '', true);
  147. $oPage->add(file_get_contents($this->aStatusInfo['tmp_file']));
  148. $oPage->add($sData);
  149. $sPDF = $oPage->get_pdf();
  150. return $sPDF;
  151. }
  152. public function GetSupportedFormats()
  153. {
  154. return array('pdf' => Dict::S('Core:BulkExport:PDFFormat'));
  155. }
  156. public function GetMimeType()
  157. {
  158. return 'application/x-pdf';
  159. }
  160. public function GetFileExtension()
  161. {
  162. return 'pdf';
  163. }
  164. }