ExportOds.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used to build OpenDocument Spreadsheet dumps of tables
  5. *
  6. * @package PhpMyAdmin-Export
  7. * @subpackage ODS
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /* Get the export interface */
  13. require_once 'libraries/plugins/ExportPlugin.class.php';
  14. $GLOBALS['ods_buffer'] = '';
  15. require_once 'libraries/opendocument.lib.php';
  16. /**
  17. * Handles the export for the ODS class
  18. *
  19. * @package PhpMyAdmin-Export
  20. * @subpackage ODS
  21. */
  22. class ExportOds extends ExportPlugin
  23. {
  24. /**
  25. * Constructor
  26. */
  27. public function __construct()
  28. {
  29. $this->setProperties();
  30. }
  31. /**
  32. * Sets the export ODS properties
  33. *
  34. * @return void
  35. */
  36. protected function setProperties()
  37. {
  38. $props = 'libraries/properties/';
  39. include_once "$props/plugins/ExportPluginProperties.class.php";
  40. include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
  41. include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
  42. include_once "$props/options/items/TextPropertyItem.class.php";
  43. include_once "$props/options/items/BoolPropertyItem.class.php";
  44. include_once "$props/options/items/HiddenPropertyItem.class.php";
  45. $exportPluginProperties = new ExportPluginProperties();
  46. $exportPluginProperties->setText('OpenDocument Spreadsheet');
  47. $exportPluginProperties->setExtension('ods');
  48. $exportPluginProperties->setMimeType(
  49. 'application/vnd.oasis.opendocument.spreadsheet'
  50. );
  51. $exportPluginProperties->setForceFile(true);
  52. $exportPluginProperties->setOptionsText(__('Options'));
  53. // create the root group that will be the options field for
  54. // $exportPluginProperties
  55. // this will be shown as "Format specific options"
  56. $exportSpecificOptions = new OptionsPropertyRootGroup();
  57. $exportSpecificOptions->setName("Format Specific Options");
  58. // general options main group
  59. $generalOptions = new OptionsPropertyMainGroup();
  60. $generalOptions->setName("general_opts");
  61. // create primary items and add them to the group
  62. $leaf = new TextPropertyItem();
  63. $leaf->setName("null");
  64. $leaf->setText(__('Replace NULL with:'));
  65. $generalOptions->addProperty($leaf);
  66. $leaf = new BoolPropertyItem();
  67. $leaf->setName("columns");
  68. $leaf->setText(__('Put columns names in the first row'));
  69. $generalOptions->addProperty($leaf);
  70. $leaf = new HiddenPropertyItem();
  71. $leaf->setName("structure_or_data");
  72. $generalOptions->addProperty($leaf);
  73. // add the main group to the root group
  74. $exportSpecificOptions->addProperty($generalOptions);
  75. // set the options for the export plugin property item
  76. $exportPluginProperties->setOptions($exportSpecificOptions);
  77. $this->properties = $exportPluginProperties;
  78. }
  79. /**
  80. * This method is called when any PluginManager to which the observer
  81. * is attached calls PluginManager::notify()
  82. *
  83. * @param SplSubject $subject The PluginManager notifying the observer
  84. * of an update.
  85. *
  86. * @return void
  87. */
  88. public function update (SplSubject $subject)
  89. {
  90. }
  91. /**
  92. * Outputs export header
  93. *
  94. * @return bool Whether it succeeded
  95. */
  96. public function exportHeader ()
  97. {
  98. $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
  99. . '<office:document-content '
  100. . $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
  101. . '<office:automatic-styles>'
  102. . '<number:date-style style:name="N37"'
  103. . ' number:automatic-order="true">'
  104. . '<number:month number:style="long"/>'
  105. . '<number:text>/</number:text>'
  106. . '<number:day number:style="long"/>'
  107. . '<number:text>/</number:text>'
  108. . '<number:year/>'
  109. . '</number:date-style>'
  110. . '<number:time-style style:name="N43">'
  111. . '<number:hours number:style="long"/>'
  112. . '<number:text>:</number:text>'
  113. . '<number:minutes number:style="long"/>'
  114. . '<number:text>:</number:text>'
  115. . '<number:seconds number:style="long"/>'
  116. . '<number:text> </number:text>'
  117. . '<number:am-pm/>'
  118. . '</number:time-style>'
  119. . '<number:date-style style:name="N50"'
  120. . ' number:automatic-order="true"'
  121. . ' number:format-source="language">'
  122. . '<number:month/>'
  123. . '<number:text>/</number:text>'
  124. . '<number:day/>'
  125. . '<number:text>/</number:text>'
  126. . '<number:year/>'
  127. . '<number:text> </number:text>'
  128. . '<number:hours number:style="long"/>'
  129. . '<number:text>:</number:text>'
  130. . '<number:minutes number:style="long"/>'
  131. . '<number:text> </number:text>'
  132. . '<number:am-pm/>'
  133. . '</number:date-style>'
  134. . '<style:style style:name="DateCell" style:family="table-cell"'
  135. . ' style:parent-style-name="Default" style:data-style-name="N37"/>'
  136. . '<style:style style:name="TimeCell" style:family="table-cell"'
  137. . ' style:parent-style-name="Default" style:data-style-name="N43"/>'
  138. . '<style:style style:name="DateTimeCell" style:family="table-cell"'
  139. . ' style:parent-style-name="Default" style:data-style-name="N50"/>'
  140. . '</office:automatic-styles>'
  141. . '<office:body>'
  142. . '<office:spreadsheet>';
  143. return true;
  144. }
  145. /**
  146. * Outputs export footer
  147. *
  148. * @return bool Whether it succeeded
  149. */
  150. public function exportFooter ()
  151. {
  152. $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
  153. . '</office:body>'
  154. . '</office:document-content>';
  155. if (! PMA_exportOutputHandler(
  156. PMA_createOpenDocument(
  157. 'application/vnd.oasis.opendocument.spreadsheet',
  158. $GLOBALS['ods_buffer']
  159. )
  160. )) {
  161. return false;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Outputs database header
  167. *
  168. * @param string $db Database name
  169. *
  170. * @return bool Whether it succeeded
  171. */
  172. public function exportDBHeader ($db)
  173. {
  174. return true;
  175. }
  176. /**
  177. * Outputs database footer
  178. *
  179. * @param string $db Database name
  180. *
  181. * @return bool Whether it succeeded
  182. */
  183. public function exportDBFooter ($db)
  184. {
  185. return true;
  186. }
  187. /**
  188. * Outputs CREATE DATABASE statement
  189. *
  190. * @param string $db Database name
  191. *
  192. * @return bool Whether it succeeded
  193. */
  194. public function exportDBCreate($db)
  195. {
  196. return true;
  197. }
  198. /**
  199. * Outputs the content of a table in NHibernate format
  200. *
  201. * @param string $db database name
  202. * @param string $table table name
  203. * @param string $crlf the end of line sequence
  204. * @param string $error_url the url to go back in case of error
  205. * @param string $sql_query SQL query for obtaining data
  206. *
  207. * @return bool Whether it succeeded
  208. */
  209. public function exportData($db, $table, $crlf, $error_url, $sql_query)
  210. {
  211. global $what;
  212. // Gets the data from the database
  213. $result = $GLOBALS['dbi']->query(
  214. $sql_query, null, PMA_DatabaseInterface::QUERY_UNBUFFERED
  215. );
  216. $fields_cnt = $GLOBALS['dbi']->numFields($result);
  217. $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
  218. $field_flags = array();
  219. for ($j = 0; $j < $fields_cnt; $j++) {
  220. $field_flags[$j] = $GLOBALS['dbi']->fieldFlags($result, $j);
  221. }
  222. $GLOBALS['ods_buffer'] .=
  223. '<table:table table:name="' . htmlspecialchars($table) . '">';
  224. // If required, get fields name at the first line
  225. if (isset($GLOBALS[$what . '_columns'])) {
  226. $GLOBALS['ods_buffer'] .= '<table:table-row>';
  227. for ($i = 0; $i < $fields_cnt; $i++) {
  228. $GLOBALS['ods_buffer'] .=
  229. '<table:table-cell office:value-type="string">'
  230. . '<text:p>'
  231. . htmlspecialchars(
  232. stripslashes($GLOBALS['dbi']->fieldName($result, $i))
  233. )
  234. . '</text:p>'
  235. . '</table:table-cell>';
  236. } // end for
  237. $GLOBALS['ods_buffer'] .= '</table:table-row>';
  238. } // end if
  239. // Format the data
  240. while ($row = $GLOBALS['dbi']->fetchRow($result)) {
  241. $GLOBALS['ods_buffer'] .= '<table:table-row>';
  242. for ($j = 0; $j < $fields_cnt; $j++) {
  243. if (! isset($row[$j]) || is_null($row[$j])) {
  244. $GLOBALS['ods_buffer'] .=
  245. '<table:table-cell office:value-type="string">'
  246. . '<text:p>'
  247. . htmlspecialchars($GLOBALS[$what . '_null'])
  248. . '</text:p>'
  249. . '</table:table-cell>';
  250. } elseif (stristr($field_flags[$j], 'BINARY')
  251. && $fields_meta[$j]->blob
  252. ) {
  253. // ignore BLOB
  254. $GLOBALS['ods_buffer'] .=
  255. '<table:table-cell office:value-type="string">'
  256. . '<text:p></text:p>'
  257. . '</table:table-cell>';
  258. } elseif ($fields_meta[$j]->type == "date") {
  259. $GLOBALS['ods_buffer'] .=
  260. '<table:table-cell office:value-type="date"'
  261. . ' office:date-value="'
  262. . date("Y-m-d", strtotime($row[$j]))
  263. . '" table:style-name="DateCell">'
  264. . '<text:p>'
  265. . htmlspecialchars($row[$j])
  266. . '</text:p>'
  267. . '</table:table-cell>';
  268. } elseif ($fields_meta[$j]->type == "time") {
  269. $GLOBALS['ods_buffer'] .=
  270. '<table:table-cell office:value-type="time"'
  271. . ' office:time-value="'
  272. . date("\P\TH\Hi\Ms\S", strtotime($row[$j]))
  273. . '" table:style-name="TimeCell">'
  274. . '<text:p>'
  275. . htmlspecialchars($row[$j])
  276. . '</text:p>'
  277. . '</table:table-cell>';
  278. } elseif ($fields_meta[$j]->type == "datetime") {
  279. $GLOBALS['ods_buffer'] .=
  280. '<table:table-cell office:value-type="date"'
  281. . ' office:date-value="'
  282. . date("Y-m-d\TH:i:s", strtotime($row[$j]))
  283. . '" table:style-name="DateTimeCell">'
  284. . '<text:p>'
  285. . htmlspecialchars($row[$j])
  286. . '</text:p>'
  287. . '</table:table-cell>';
  288. } elseif (($fields_meta[$j]->numeric
  289. && $fields_meta[$j]->type != 'timestamp'
  290. && ! $fields_meta[$j]->blob) || $fields_meta[$j]->type == 'real'
  291. ) {
  292. $GLOBALS['ods_buffer'] .=
  293. '<table:table-cell office:value-type="float"'
  294. . ' office:value="' . $row[$j] . '" >'
  295. . '<text:p>'
  296. . htmlspecialchars($row[$j])
  297. . '</text:p>'
  298. . '</table:table-cell>';
  299. } else {
  300. $GLOBALS['ods_buffer'] .=
  301. '<table:table-cell office:value-type="string">'
  302. . '<text:p>'
  303. . htmlspecialchars($row[$j])
  304. . '</text:p>'
  305. . '</table:table-cell>';
  306. }
  307. } // end for
  308. $GLOBALS['ods_buffer'] .= '</table:table-row>';
  309. } // end while
  310. $GLOBALS['dbi']->freeResult($result);
  311. $GLOBALS['ods_buffer'] .= '</table:table>';
  312. return true;
  313. }
  314. }
  315. ?>