Ubuntu/debian package updates:
[openemr.git] / phpmyadmin / tbl_create.php
blob44e950eab1cbb77a8ea7a3826e58bf48c2dc72ba
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @uses $cfg['DefaultTabDatabase']
5 * @uses $GLOBALS['table']
6 * @uses $GLOBALS['db']
7 * @uses $strTableEmpty
8 * @uses $strTableAlreadyExists
9 * @uses $strTable
10 * @uses $strHasBeenCreated
11 * @uses PMA_ENGINE_KEYWORD
12 * @uses PMA_MYSQL_INT_VERSION
13 * @uses PMA_Table::generateFieldSpec()
14 * @uses PMA_checkParameters()
15 * @uses PMA_generateCharsetQueryPart()
16 * @uses PMA_sqlAddslashes()
17 * @uses PMA_DBI_try_query()
18 * @uses PMA_getRelationsParam()
19 * @uses PMA_setComment()
20 * @uses PMA_setMIME()
21 * @uses PMA_mysqlDie()
22 * @uses PMA_generate_common_url()
23 * @uses PMA_DBI_get_columns()
24 * @uses PMA_DBI_select_db()
25 * @uses PMA_backquote()
26 * @uses $_REQUEST['do_save_data']
27 * @uses $_REQUEST['submit_num_fields']
28 * @uses $_REQUEST['orig_num_fields']
29 * @uses $_REQUEST['added_fields']
30 * @uses $_REQUEST['num_fields']
31 * @uses preg_replace()
32 * @uses count()
33 * @uses is_array()
34 * @uses strlen()
35 * @uses sprintf()
36 * @uses htmlspecialchars()
37 * @version $Id$
40 /**
41 * Get some core libraries
43 require_once './libraries/common.inc.php';
44 require_once './libraries/Table.class.php';
46 $js_to_run = 'functions.js';
48 require_once './libraries/header.inc.php';
50 // Check parameters
51 // @todo PMA_checkParameters does not check db and table proper with strlen()
52 PMA_checkParameters(array('db', 'table'));
54 /**
55 * Defines the url to return to in case of error in a sql statement
57 if (! strlen($table)) {
58 // No table name
59 PMA_mysqlDie($strTableEmpty, '', '',
60 'db_structure.php?' . PMA_generate_common_url($db));
61 } elseif (PMA_DBI_get_columns($db, $table)) {
62 // table exists already
63 PMA_mysqlDie(sprintf($strTableAlreadyExists, htmlspecialchars($table)), '',
64 '', 'db_structure.php?' . PMA_generate_common_url($db));
67 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
69 /**
70 * Selects the database to work with
72 PMA_DBI_select_db($db);
74 /**
75 * The form used to define the structure of the table has been submitted
77 if (isset($_REQUEST['do_save_data'])) {
78 $sql_query = '';
80 // Transforms the radio button field_key into 3 arrays
81 $field_cnt = count($field_name);
82 for ($i = 0; $i < $field_cnt; ++$i) {
83 if (isset(${'field_key_' . $i})) {
84 if (${'field_key_' . $i} == 'primary_' . $i) {
85 $field_primary[] = $i;
87 if (${'field_key_' . $i} == 'index_' . $i) {
88 $field_index[] = $i;
90 if (${'field_key_' . $i} == 'unique_' . $i) {
91 $field_unique[] = $i;
93 } // end if
94 } // end for
95 // Builds the fields creation statements
96 for ($i = 0; $i < $field_cnt; $i++) {
97 // '0' is also empty for php :-(
98 if (empty($field_name[$i]) && $field_name[$i] != '0') {
99 continue;
102 $query = PMA_Table::generateFieldSpec($field_name[$i], $field_type[$i],
103 $field_length[$i], $field_attribute[$i],
104 isset($field_collation[$i]) ? $field_collation[$i] : '',
105 $field_null[$i], $field_default[$i],
106 isset($field_default_current_timestamp[$i]), $field_extra[$i],
107 isset($field_comments[$i]) ? $field_comments[$i] : '',
108 $field_primary, $i);
110 $query .= ', ';
111 $sql_query .= $query;
112 } // end for
113 unset($field_cnt, $query);
114 $sql_query = preg_replace('@, $@', '', $sql_query);
116 // Builds the primary keys statements
117 $primary = '';
118 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
119 for ($i = 0; $i < $primary_cnt; $i++) {
120 $j = $field_primary[$i];
121 if (isset($field_name[$j]) && strlen($field_name[$j])) {
122 $primary .= PMA_backquote($field_name[$j]) . ', ';
124 } // end for
125 unset($primary_cnt);
126 $primary = preg_replace('@, $@', '', $primary);
127 if (strlen($primary)) {
128 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
130 unset($primary);
132 // Builds the indexes statements
133 $index = '';
134 $index_cnt = (isset($field_index) ? count($field_index) : 0);
135 for ($i = 0;$i < $index_cnt; $i++) {
136 $j = $field_index[$i];
137 if (isset($field_name[$j]) && strlen($field_name[$j])) {
138 $index .= PMA_backquote($field_name[$j]) . ', ';
140 } // end for
141 unset($index_cnt);
142 $index = preg_replace('@, $@', '', $index);
143 if (strlen($index)) {
144 $sql_query .= ', INDEX (' . $index . ')';
146 unset($index);
148 // Builds the uniques statements
149 $unique = '';
150 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
151 for ($i = 0; $i < $unique_cnt; $i++) {
152 $j = $field_unique[$i];
153 if (isset($field_name[$j]) && strlen($field_name[$j])) {
154 $unique .= PMA_backquote($field_name[$j]) . ', ';
156 } // end for
157 unset($unique_cnt);
158 $unique = preg_replace('@, $@', '', $unique);
159 if (strlen($unique)) {
160 $sql_query .= ', UNIQUE (' . $unique . ')';
162 unset($unique);
164 // Builds the FULLTEXT statements
165 $fulltext = '';
166 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
167 for ($i = 0; $i < $fulltext_cnt; $i++) {
168 $j = $field_fulltext[$i];
169 if (isset($field_name[$j]) && strlen($field_name[$j])) {
170 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
172 } // end for
174 $fulltext = preg_replace('@, $@', '', $fulltext);
175 if (strlen($fulltext)) {
176 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
178 unset($fulltext);
180 // Builds the 'create table' statement
181 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
182 . ' (' . $sql_query . ')';
184 // Adds table type, character set and comments
185 if (!empty($tbl_type) && ($tbl_type != 'Default')) {
186 $sql_query .= ' ' . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type;
188 if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) {
189 $sql_query .= PMA_generateCharsetQueryPart($tbl_collation);
192 if (!empty($comment)) {
193 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
196 // Executes the query
197 $result = PMA_DBI_try_query($sql_query);
199 if ($result) {
201 // garvin: If comments were sent, enable relation stuff
202 require_once './libraries/relation.lib.php';
203 require_once './libraries/transformations.lib.php';
205 $cfgRelation = PMA_getRelationsParam();
207 // garvin: Update comment table, if a comment was set.
208 if (isset($field_comments) && is_array($field_comments)
209 && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
210 foreach ($field_comments as $fieldindex => $fieldcomment) {
211 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
212 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
217 // garvin: Update comment table for mime types [MIME]
218 if (isset($field_mimetype) && is_array($field_mimetype)
219 && $cfgRelation['commwork'] && $cfgRelation['mimework']
220 && $cfg['BrowseMIME']) {
221 foreach ($field_mimetype as $fieldindex => $mimetype) {
222 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
223 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype,
224 $field_transformation[$fieldindex],
225 $field_transformation_options[$fieldindex]);
230 $message = $strTable . ' '
231 . htmlspecialchars(PMA_backquote($db) . '.' . PMA_backquote($table))
232 . ' ' . $strHasBeenCreated;
233 $display_query = $sql_query;
234 $sql_query = '';
236 // do not switch to sql.php - as there is no row to be displayed on a new table
237 if ($cfg['DefaultTabTable'] === 'sql.php') {
238 require './tbl_structure.php';
239 } else {
240 require './' . $cfg['DefaultTabTable'];
242 exit;
243 } else {
244 PMA_mysqlDie('', '', '', $err_url, false);
245 // garvin: An error happened while inserting/updating a table definition.
246 // to prevent total loss of that data, we embed the form once again.
247 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
248 $num_fields = $orig_num_fields;
250 } // end do create table
253 * Displays the form used to define the structure of the table
255 // check number of fields to be created
256 if (isset($_REQUEST['submit_num_fields'])) {
257 $regenerate = true; // for libraries/tbl_properties.inc.php
258 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
259 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
260 $num_fields = (int) $_REQUEST['num_fields'];
261 } else {
262 $num_fields = 1;
265 $action = 'tbl_create.php';
266 require './libraries/tbl_properties.inc.php';
267 // Displays the footer
268 require_once './libraries/footer.inc.php';