ExportPdf.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Produce a PDF report (export) from a query
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage PDF
  8. */
  9. declare(strict_types=1);
  10. namespace PhpMyAdmin\Plugins\Export;
  11. use PhpMyAdmin\Export;
  12. use PhpMyAdmin\Plugins\Export\Helpers\Pdf;
  13. use PhpMyAdmin\Plugins\ExportPlugin;
  14. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
  15. use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyRootGroup;
  16. use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
  17. use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
  18. use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
  19. /**
  20. * Skip the plugin if TCPDF is not available.
  21. */
  22. if (! class_exists('TCPDF')) {
  23. $GLOBALS['skip_import'] = true;
  24. return;
  25. }
  26. /**
  27. * Handles the export for the PDF class
  28. *
  29. * @package PhpMyAdmin-Export
  30. * @subpackage PDF
  31. */
  32. class ExportPdf extends ExportPlugin
  33. {
  34. /**
  35. * PhpMyAdmin\Plugins\Export\Helpers\Pdf instance
  36. *
  37. * @var Pdf
  38. */
  39. private $_pdf;
  40. /**
  41. * PDF Report Title
  42. *
  43. * @var string
  44. */
  45. private $_pdfReportTitle;
  46. /**
  47. * Constructor
  48. */
  49. public function __construct()
  50. {
  51. parent::__construct();
  52. // initialize the specific export PDF variables
  53. $this->initSpecificVariables();
  54. $this->setProperties();
  55. }
  56. /**
  57. * Initialize the local variables that are used for export PDF
  58. *
  59. * @return void
  60. */
  61. protected function initSpecificVariables()
  62. {
  63. if (! empty($_POST['pdf_report_title'])) {
  64. $this->_setPdfReportTitle($_POST['pdf_report_title']);
  65. }
  66. $this->_setPdf(new Pdf('L', 'pt', 'A3'));
  67. }
  68. /**
  69. * Sets the export PDF properties
  70. *
  71. * @return void
  72. */
  73. protected function setProperties()
  74. {
  75. $exportPluginProperties = new ExportPluginProperties();
  76. $exportPluginProperties->setText('PDF');
  77. $exportPluginProperties->setExtension('pdf');
  78. $exportPluginProperties->setMimeType('application/pdf');
  79. $exportPluginProperties->setForceFile(true);
  80. $exportPluginProperties->setOptionsText(__('Options'));
  81. // create the root group that will be the options field for
  82. // $exportPluginProperties
  83. // this will be shown as "Format specific options"
  84. $exportSpecificOptions = new OptionsPropertyRootGroup(
  85. "Format Specific Options"
  86. );
  87. // general options main group
  88. $generalOptions = new OptionsPropertyMainGroup("general_opts");
  89. // create primary items and add them to the group
  90. $leaf = new TextPropertyItem(
  91. "report_title",
  92. __('Report title:')
  93. );
  94. $generalOptions->addProperty($leaf);
  95. // add the group to the root group
  96. $exportSpecificOptions->addProperty($generalOptions);
  97. // what to dump (structure/data/both) main group
  98. $dumpWhat = new OptionsPropertyMainGroup(
  99. "dump_what",
  100. __('Dump table')
  101. );
  102. $leaf = new RadioPropertyItem("structure_or_data");
  103. $leaf->setValues(
  104. [
  105. 'structure' => __('structure'),
  106. 'data' => __('data'),
  107. 'structure_and_data' => __('structure and data'),
  108. ]
  109. );
  110. $dumpWhat->addProperty($leaf);
  111. // add the group to the root group
  112. $exportSpecificOptions->addProperty($dumpWhat);
  113. // set the options for the export plugin property item
  114. $exportPluginProperties->setOptions($exportSpecificOptions);
  115. $this->properties = $exportPluginProperties;
  116. }
  117. /**
  118. * Outputs export header
  119. *
  120. * @return bool Whether it succeeded
  121. */
  122. public function exportHeader()
  123. {
  124. $pdf_report_title = $this->_getPdfReportTitle();
  125. $pdf = $this->_getPdf();
  126. $pdf->Open();
  127. $attr = [
  128. 'titleFontSize' => 18,
  129. 'titleText' => $pdf_report_title,
  130. ];
  131. $pdf->setAttributes($attr);
  132. $pdf->setTopMargin(30);
  133. return true;
  134. }
  135. /**
  136. * Outputs export footer
  137. *
  138. * @return bool Whether it succeeded
  139. */
  140. public function exportFooter()
  141. {
  142. $pdf = $this->_getPdf();
  143. // instead of $pdf->Output():
  144. return $this->export->outputHandler($pdf->getPDFData());
  145. }
  146. /**
  147. * Outputs database header
  148. *
  149. * @param string $db Database name
  150. * @param string $db_alias Aliases of db
  151. *
  152. * @return bool Whether it succeeded
  153. */
  154. public function exportDBHeader($db, $db_alias = '')
  155. {
  156. return true;
  157. }
  158. /**
  159. * Outputs database footer
  160. *
  161. * @param string $db Database name
  162. *
  163. * @return bool Whether it succeeded
  164. */
  165. public function exportDBFooter($db)
  166. {
  167. return true;
  168. }
  169. /**
  170. * Outputs CREATE DATABASE statement
  171. *
  172. * @param string $db Database name
  173. * @param string $export_type 'server', 'database', 'table'
  174. * @param string $db_alias Aliases of db
  175. *
  176. * @return bool Whether it succeeded
  177. */
  178. public function exportDBCreate($db, $export_type, $db_alias = '')
  179. {
  180. return true;
  181. }
  182. /**
  183. * Outputs the content of a table in NHibernate format
  184. *
  185. * @param string $db database name
  186. * @param string $table table name
  187. * @param string $crlf the end of line sequence
  188. * @param string $error_url the url to go back in case of error
  189. * @param string $sql_query SQL query for obtaining data
  190. * @param array $aliases Aliases of db/table/columns
  191. *
  192. * @return bool Whether it succeeded
  193. */
  194. public function exportData(
  195. $db,
  196. $table,
  197. $crlf,
  198. $error_url,
  199. $sql_query,
  200. array $aliases = []
  201. ) {
  202. $db_alias = $db;
  203. $table_alias = $table;
  204. $this->initAlias($aliases, $db_alias, $table_alias);
  205. $pdf = $this->_getPdf();
  206. $attr = [
  207. 'currentDb' => $db,
  208. 'currentTable' => $table,
  209. 'dbAlias' => $db_alias,
  210. 'tableAlias' => $table_alias,
  211. 'aliases' => $aliases,
  212. 'purpose' => __('Dumping data'),
  213. ];
  214. $pdf->setAttributes($attr);
  215. $pdf->mysqlReport($sql_query);
  216. return true;
  217. } // end of the 'PMA_exportData()' function
  218. /**
  219. * Outputs table structure
  220. *
  221. * @param string $db database name
  222. * @param string $table table name
  223. * @param string $crlf the end of line sequence
  224. * @param string $error_url the url to go back in case of error
  225. * @param string $export_mode 'create_table', 'triggers', 'create_view',
  226. * 'stand_in'
  227. * @param string $export_type 'server', 'database', 'table'
  228. * @param bool $do_relation whether to include relation comments
  229. * @param bool $do_comments whether to include the pmadb-style column
  230. * comments as comments in the structure;
  231. * this is deprecated but the parameter is
  232. * left here because export.php calls
  233. * PMA_exportStructure() also for other
  234. * export types which use this parameter
  235. * @param bool $do_mime whether to include mime comments
  236. * @param bool $dates whether to include creation/update/check dates
  237. * @param array $aliases aliases for db/table/columns
  238. *
  239. * @return bool Whether it succeeded
  240. */
  241. public function exportStructure(
  242. $db,
  243. $table,
  244. $crlf,
  245. $error_url,
  246. $export_mode,
  247. $export_type,
  248. $do_relation = false,
  249. $do_comments = false,
  250. $do_mime = false,
  251. $dates = false,
  252. array $aliases = []
  253. ) {
  254. $db_alias = $db;
  255. $table_alias = $table;
  256. $purpose = null;
  257. $this->initAlias($aliases, $db_alias, $table_alias);
  258. $pdf = $this->_getPdf();
  259. // getting purpose to show at top
  260. switch ($export_mode) {
  261. case 'create_table':
  262. $purpose = __('Table structure');
  263. break;
  264. case 'triggers':
  265. $purpose = __('Triggers');
  266. break;
  267. case 'create_view':
  268. $purpose = __('View structure');
  269. break;
  270. case 'stand_in':
  271. $purpose = __('Stand in');
  272. } // end switch
  273. $attr = [
  274. 'currentDb' => $db,
  275. 'currentTable' => $table,
  276. 'dbAlias' => $db_alias,
  277. 'tableAlias' => $table_alias,
  278. 'aliases' => $aliases,
  279. 'purpose' => $purpose,
  280. ];
  281. $pdf->setAttributes($attr);
  282. /**
  283. * comment display set true as presently in pdf
  284. * format, no option is present to take user input.
  285. */
  286. $do_comments = true;
  287. switch ($export_mode) {
  288. case 'create_table':
  289. $pdf->getTableDef(
  290. $db,
  291. $table,
  292. $do_relation,
  293. $do_comments,
  294. $do_mime,
  295. false,
  296. $aliases
  297. );
  298. break;
  299. case 'triggers':
  300. $pdf->getTriggers($db, $table);
  301. break;
  302. case 'create_view':
  303. $pdf->getTableDef(
  304. $db,
  305. $table,
  306. $do_relation,
  307. $do_comments,
  308. $do_mime,
  309. false,
  310. $aliases
  311. );
  312. break;
  313. case 'stand_in':
  314. /* export a stand-in definition to resolve view dependencies
  315. * Yet to develop this function
  316. * $pdf->getTableDefStandIn($db, $table, $crlf);
  317. */
  318. } // end switch
  319. return true;
  320. }
  321. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  322. /**
  323. * Gets the PhpMyAdmin\Plugins\Export\Helpers\Pdf instance
  324. *
  325. * @return Pdf
  326. */
  327. private function _getPdf()
  328. {
  329. return $this->_pdf;
  330. }
  331. /**
  332. * Instantiates the PhpMyAdmin\Plugins\Export\Helpers\Pdf class
  333. *
  334. * @param Pdf $pdf The instance
  335. *
  336. * @return void
  337. */
  338. private function _setPdf($pdf)
  339. {
  340. $this->_pdf = $pdf;
  341. }
  342. /**
  343. * Gets the PDF report title
  344. *
  345. * @return string
  346. */
  347. private function _getPdfReportTitle()
  348. {
  349. return $this->_pdfReportTitle;
  350. }
  351. /**
  352. * Sets the PDF report title
  353. *
  354. * @param string $pdfReportTitle PDF report title
  355. *
  356. * @return void
  357. */
  358. private function _setPdfReportTitle($pdfReportTitle)
  359. {
  360. $this->_pdfReportTitle = $pdfReportTitle;
  361. }
  362. }