Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportJson.class.php
blob299e248c02ca1fb04f3160796b0a005fa2e0c45e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of methods used to build dumps of tables as JSON
6 * @package PhpMyAdmin-Export
7 * @subpackage JSON
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 /**
17 * Handles the export for the JSON format
19 * @package PhpMyAdmin-Export
20 * @subpackage JSON
22 class ExportJson extends ExportPlugin
24 /**
25 * Constructor
27 public function __construct()
29 $this->setProperties();
32 /**
33 * Sets the export JSON properties
35 * @return void
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/items/HiddenPropertyItem.class.php";
45 $exportPluginProperties = new ExportPluginProperties();
46 $exportPluginProperties->setText('JSON');
47 $exportPluginProperties->setExtension('json');
48 $exportPluginProperties->setMimeType('text/plain');
49 $exportPluginProperties->setOptionsText(__('Options'));
51 // create the root group that will be the options field for
52 // $exportPluginProperties
53 // this will be shown as "Format specific options"
54 $exportSpecificOptions = new OptionsPropertyRootGroup();
55 $exportSpecificOptions->setName("Format Specific Options");
57 // general options main group
58 $generalOptions = new OptionsPropertyMainGroup();
59 $generalOptions->setName("general_opts");
60 // create primary items and add them to the group
61 $leaf = new HiddenPropertyItem();
62 $leaf->setName("structure_or_data");
63 $generalOptions->addProperty($leaf);
64 // add the main group to the root group
65 $exportSpecificOptions->addProperty($generalOptions);
67 // set the options for the export plugin property item
68 $exportPluginProperties->setOptions($exportSpecificOptions);
69 $this->properties = $exportPluginProperties;
72 /**
73 * This method is called when any PluginManager to which the observer
74 * is attached calls PluginManager::notify()
76 * @param SplSubject $subject The PluginManager notifying the observer
77 * of an update.
79 * @return void
81 public function update (SplSubject $subject)
85 /**
86 * Outputs export header
88 * @return bool Whether it succeeded
90 public function exportHeader ()
92 PMA_exportOutputHandler(
93 '/**' . $GLOBALS['crlf']
94 . ' Export to JSON plugin for PHPMyAdmin' . $GLOBALS['crlf']
95 . ' @version 0.1' . $GLOBALS['crlf']
96 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
98 return true;
102 * Outputs export footer
104 * @return bool Whether it succeeded
106 public function exportFooter ()
108 return true;
112 * Outputs database header
114 * @param string $db Database name
116 * @return bool Whether it succeeded
118 public function exportDBHeader ($db)
120 PMA_exportOutputHandler('// Database \'' . $db . '\'' . $GLOBALS['crlf']);
121 return true;
125 * Outputs database footer
127 * @param string $db Database name
129 * @return bool Whether it succeeded
131 public function exportDBFooter ($db)
133 return true;
137 * Outputs CREATE DATABASE statement
139 * @param string $db Database name
141 * @return bool Whether it succeeded
143 public function exportDBCreate($db)
145 return true;
149 * Outputs the content of a table in JSON format
151 * @param string $db database name
152 * @param string $table table name
153 * @param string $crlf the end of line sequence
154 * @param string $error_url the url to go back in case of error
155 * @param string $sql_query SQL query for obtaining data
157 * @return bool Whether it succeeded
159 public function exportData($db, $table, $crlf, $error_url, $sql_query)
161 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
162 $columns_cnt = PMA_DBI_num_fields($result);
164 // Get field information
165 $fields_meta = PMA_DBI_get_fields_meta($result);
167 for ($i = 0; $i < $columns_cnt; $i++) {
168 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
170 unset($i);
172 $buffer = '';
173 $record_cnt = 0;
174 while ($record = PMA_DBI_fetch_row($result)) {
176 $record_cnt++;
178 // Output table name as comment if this is the first record of the table
179 if ($record_cnt == 1) {
180 $buffer .= '// ' . $db . '.' . $table . $crlf . $crlf;
181 $buffer .= '[{';
182 } else {
183 $buffer .= ', {';
186 for ($i = 0; $i < $columns_cnt; $i++) {
187 $isLastLine = ($i + 1 >= $columns_cnt);
188 $column = $columns[$i];
189 if (is_null($record[$i])) {
190 $buffer .= '"' . addslashes($column)
191 . '": null'
192 . (! $isLastLine ? ',' : '');
193 } elseif ($fields_meta[$i]->numeric) {
194 $buffer .= '"' . addslashes($column)
195 . '": '
196 . $record[$i]
197 . (! $isLastLine ? ',' : '');
198 } else {
199 $buffer .= '"' . addslashes($column)
200 . '": "'
201 . addslashes($record[$i])
202 . '"'
203 . (! $isLastLine ? ',' : '');
207 $buffer .= '}';
210 if ($record_cnt) {
211 $buffer .= ']';
213 if (! PMA_exportOutputHandler($buffer)) {
214 return false;
217 PMA_DBI_free_result($result);
218 return true;