ExportPdf.php 11 KB

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