Translation update, thanks to Nicky 726.
[phpmyadmin/last10db.git] / tbl_create.php
bloba8404fae2ae1f071ded106bd3eb7653f23f6d706
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 $strTableHasBeenCreated
11 * @uses PMA_Table::generateFieldSpec()
12 * @uses PMA_checkParameters()
13 * @uses PMA_generateCharsetQueryPart()
14 * @uses PMA_sqlAddslashes()
15 * @uses PMA_DBI_try_query()
16 * @uses PMA_getRelationsParam()
17 * @uses PMA_setMIME()
18 * @uses PMA_mysqlDie()
19 * @uses PMA_generate_common_url()
20 * @uses PMA_DBI_get_columns()
21 * @uses PMA_DBI_select_db()
22 * @uses PMA_backquote()
23 * @uses $_REQUEST['do_save_data']
24 * @uses $_REQUEST['submit_num_fields']
25 * @uses $_REQUEST['orig_num_fields']
26 * @uses $_REQUEST['added_fields']
27 * @uses $_REQUEST['num_fields']
28 * @uses preg_replace()
29 * @uses count()
30 * @uses is_array()
31 * @uses strlen()
32 * @uses sprintf()
33 * @uses htmlspecialchars()
34 * @version $Id$
37 /**
38 * Get some core libraries
40 require_once './libraries/common.inc.php';
41 require_once './libraries/Table.class.php';
43 $GLOBALS['js_include'][] = 'functions.js';
45 require_once './libraries/header.inc.php';
47 // Check parameters
48 // @todo PMA_checkParameters does not check db and table proper with strlen()
49 PMA_checkParameters(array('db', 'table'));
51 /**
52 * Defines the url to return to in case of error in a sql statement
54 if (! strlen($table)) {
55 // No table name
56 PMA_mysqlDie($strTableEmpty, '', '',
57 'db_structure.php?' . PMA_generate_common_url($db));
58 } elseif (PMA_DBI_get_columns($db, $table)) {
59 // table exists already
60 PMA_mysqlDie(sprintf($strTableAlreadyExists, htmlspecialchars($table)), '',
61 '', 'db_structure.php?' . PMA_generate_common_url($db));
64 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
66 // check number of fields to be created
67 if (isset($_REQUEST['submit_num_fields'])) {
68 $regenerate = true; // for libraries/tbl_properties.inc.php
69 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
70 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
71 $num_fields = (int) $_REQUEST['num_fields'];
72 } else {
73 $num_fields = 2;
76 /**
77 * Selects the database to work with
79 PMA_DBI_select_db($db);
81 /**
82 * The form used to define the structure of the table has been submitted
84 if (isset($_REQUEST['do_save_data'])) {
85 $sql_query = '';
87 // Transforms the radio button field_key into 3 arrays
88 $field_cnt = count($_REQUEST['field_name']);
89 for ($i = 0; $i < $field_cnt; ++$i) {
90 if (isset($_REQUEST['field_key'][$i])) {
91 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
92 $field_primary[] = $i;
94 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
95 $field_index[] = $i;
97 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
98 $field_unique[] = $i;
100 } // end if
101 } // end for
103 // Builds the fields creation statements
104 for ($i = 0; $i < $field_cnt; $i++) {
105 // '0' is also empty for php :-(
106 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
107 continue;
110 $query = PMA_Table::generateFieldSpec(
111 $_REQUEST['field_name'][$i],
112 $_REQUEST['field_type'][$i],
113 $_REQUEST['field_length'][$i],
114 $_REQUEST['field_attribute'][$i],
115 isset($_REQUEST['field_collation'][$i])
116 ? $_REQUEST['field_collation'][$i]
117 : '',
118 isset($_REQUEST['field_null'][$i])
119 ? $_REQUEST['field_null'][$i]
120 : 'NOT NULL',
121 $_REQUEST['field_default_type'][$i],
122 $_REQUEST['field_default_value'][$i],
123 isset($_REQUEST['field_extra'][$i])
124 ? $_REQUEST['field_extra'][$i]
125 : false,
126 isset($_REQUEST['field_comments'][$i])
127 ? $_REQUEST['field_comments'][$i]
128 : '',
129 $field_primary,
130 $i);
132 $query .= ', ';
133 $sql_query .= $query;
134 } // end for
135 unset($field_cnt, $query);
136 $sql_query = preg_replace('@, $@', '', $sql_query);
138 // Builds the primary keys statements
139 $primary = '';
140 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
141 for ($i = 0; $i < $primary_cnt; $i++) {
142 $j = $field_primary[$i];
143 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
144 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
146 } // end for
147 unset($primary_cnt);
148 $primary = preg_replace('@, $@', '', $primary);
149 if (strlen($primary)) {
150 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
152 unset($primary);
154 // Builds the indexes statements
155 $index = '';
156 $index_cnt = (isset($field_index) ? count($field_index) : 0);
157 for ($i = 0;$i < $index_cnt; $i++) {
158 $j = $field_index[$i];
159 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
160 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
162 } // end for
163 unset($index_cnt);
164 $index = preg_replace('@, $@', '', $index);
165 if (strlen($index)) {
166 $sql_query .= ', INDEX (' . $index . ')';
168 unset($index);
170 // Builds the uniques statements
171 $unique = '';
172 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
173 for ($i = 0; $i < $unique_cnt; $i++) {
174 $j = $field_unique[$i];
175 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
176 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
178 } // end for
179 unset($unique_cnt);
180 $unique = preg_replace('@, $@', '', $unique);
181 if (strlen($unique)) {
182 $sql_query .= ', UNIQUE (' . $unique . ')';
184 unset($unique);
186 // Builds the FULLTEXT statements
187 $fulltext = '';
188 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
189 for ($i = 0; $i < $fulltext_cnt; $i++) {
190 $j = $field_fulltext[$i];
191 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
192 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
194 } // end for
196 $fulltext = preg_replace('@, $@', '', $fulltext);
197 if (strlen($fulltext)) {
198 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
200 unset($fulltext);
202 // Builds the 'create table' statement
203 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
204 . ' (' . $sql_query . ')';
206 // Adds table type, character set, comments and partition definition
207 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
208 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
210 if (!empty($_REQUEST['tbl_collation'])) {
211 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
213 if (!empty($_REQUEST['comment'])) {
214 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\'';
216 if (!empty($_REQUEST['partition_definition'])) {
217 $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']);
220 // Executes the query
221 $result = PMA_DBI_try_query($sql_query);
223 if ($result) {
225 // garvin: If comments were sent, enable relation stuff
226 require_once './libraries/relation.lib.php';
227 require_once './libraries/transformations.lib.php';
229 // garvin: Update comment table for mime types [MIME]
230 if (isset($_REQUEST['field_mimetype'])
231 && is_array($_REQUEST['field_mimetype'])
232 && $cfg['BrowseMIME']) {
233 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
234 if (isset($_REQUEST['field_name'][$fieldindex])
235 && strlen($_REQUEST['field_name'][$fieldindex])) {
236 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
237 $_REQUEST['field_transformation'][$fieldindex],
238 $_REQUEST['field_transformation_options'][$fieldindex]);
243 $message = PMA_Message::success('strTableHasBeenCreated');
244 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
246 $display_query = $sql_query;
247 $sql_query = '';
249 // do not switch to sql.php - as there is no row to be displayed on a new table
250 if ($cfg['DefaultTabTable'] === 'sql.php') {
251 require './tbl_structure.php';
252 } else {
253 require './' . $cfg['DefaultTabTable'];
255 exit;
256 } else {
257 PMA_mysqlDie('', '', '', $err_url, false);
258 // garvin: An error happened while inserting/updating a table definition.
259 // to prevent total loss of that data, we embed the form once again.
260 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
261 $num_fields = $_REQUEST['orig_num_fields'];
262 $regenerate = true;
264 } // end do create table
267 * Displays the form used to define the structure of the table
269 $action = 'tbl_create.php';
270 require './libraries/tbl_properties.inc.php';
271 // Displays the footer
272 require_once './libraries/footer.inc.php';