Translation update done using Pootle.
[phpmyadmin/dkf.git] / tbl_create.php
blob02c637dff7e76d8f5193304c5f8373d563cdc2ae
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 __('The table name is empty!')
8 * @uses __('Table %s already exists!')
9 * @uses __('Table')
10 * @uses __('Table %1 has been created.')
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$
35 * @package phpMyAdmin
38 /**
39 * Get some core libraries
41 require_once './libraries/common.inc.php';
42 require_once './libraries/Table.class.php';
44 $GLOBALS['js_include'][] = 'functions.js';
45 $action = 'tbl_create.php';
47 require_once './libraries/header.inc.php';
49 // Check parameters
50 PMA_checkParameters(array('db'));
52 /* Check if database name is empty */
53 if (strlen($db) == 0) {
54 PMA_mysqlDie(__('The database name is empty!'), '', '', 'main.php');
57 /**
58 * Defines the url to return to in case of error in a sql statement
60 if (PMA_DBI_get_columns($db, $table)) {
61 // table exists already
62 PMA_mysqlDie(sprintf(__('Table %s already exists!'), htmlspecialchars($table)), '',
63 '', 'db_structure.php?' . PMA_generate_common_url($db));
66 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
68 // check number of fields to be created
69 if (isset($_REQUEST['submit_num_fields'])) {
70 $regenerate = true; // for libraries/tbl_properties.inc.php
71 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
72 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
73 $num_fields = (int) $_REQUEST['num_fields'];
74 } else {
75 $num_fields = 2;
78 /**
79 * Selects the database to work with
81 if (!PMA_DBI_select_db($db)) {
82 PMA_mysqlDie(sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
83 '', '', 'main.php');
86 /**
87 * The form used to define the structure of the table has been submitted
89 if (isset($_REQUEST['do_save_data'])) {
90 $sql_query = '';
92 // Transforms the radio button field_key into 3 arrays
93 $field_cnt = count($_REQUEST['field_name']);
94 for ($i = 0; $i < $field_cnt; ++$i) {
95 if (isset($_REQUEST['field_key'][$i])) {
96 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
97 $field_primary[] = $i;
99 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
100 $field_index[] = $i;
102 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
103 $field_unique[] = $i;
105 } // end if
106 } // end for
108 // Builds the fields creation statements
109 for ($i = 0; $i < $field_cnt; $i++) {
110 // '0' is also empty for php :-(
111 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
112 continue;
115 $query = PMA_Table::generateFieldSpec(
116 $_REQUEST['field_name'][$i],
117 $_REQUEST['field_type'][$i],
118 $_REQUEST['field_length'][$i],
119 $_REQUEST['field_attribute'][$i],
120 isset($_REQUEST['field_collation'][$i])
121 ? $_REQUEST['field_collation'][$i]
122 : '',
123 isset($_REQUEST['field_null'][$i])
124 ? $_REQUEST['field_null'][$i]
125 : 'NOT NULL',
126 $_REQUEST['field_default_type'][$i],
127 $_REQUEST['field_default_value'][$i],
128 isset($_REQUEST['field_extra'][$i])
129 ? $_REQUEST['field_extra'][$i]
130 : false,
131 isset($_REQUEST['field_comments'][$i])
132 ? $_REQUEST['field_comments'][$i]
133 : '',
134 $field_primary,
135 $i);
137 $query .= ', ';
138 $sql_query .= $query;
139 } // end for
140 unset($field_cnt, $query);
141 $sql_query = preg_replace('@, $@', '', $sql_query);
143 // Builds the primary keys statements
144 $primary = '';
145 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
146 for ($i = 0; $i < $primary_cnt; $i++) {
147 $j = $field_primary[$i];
148 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
149 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
151 } // end for
152 unset($primary_cnt);
153 $primary = preg_replace('@, $@', '', $primary);
154 if (strlen($primary)) {
155 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
157 unset($primary);
159 // Builds the indexes statements
160 $index = '';
161 $index_cnt = (isset($field_index) ? count($field_index) : 0);
162 for ($i = 0;$i < $index_cnt; $i++) {
163 $j = $field_index[$i];
164 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
165 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
167 } // end for
168 unset($index_cnt);
169 $index = preg_replace('@, $@', '', $index);
170 if (strlen($index)) {
171 $sql_query .= ', INDEX (' . $index . ')';
173 unset($index);
175 // Builds the uniques statements
176 $unique = '';
177 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
178 for ($i = 0; $i < $unique_cnt; $i++) {
179 $j = $field_unique[$i];
180 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
181 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
183 } // end for
184 unset($unique_cnt);
185 $unique = preg_replace('@, $@', '', $unique);
186 if (strlen($unique)) {
187 $sql_query .= ', UNIQUE (' . $unique . ')';
189 unset($unique);
191 // Builds the FULLTEXT statements
192 $fulltext = '';
193 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
194 for ($i = 0; $i < $fulltext_cnt; $i++) {
195 $j = $field_fulltext[$i];
196 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
197 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
199 } // end for
201 $fulltext = preg_replace('@, $@', '', $fulltext);
202 if (strlen($fulltext)) {
203 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
205 unset($fulltext);
207 // Builds the 'create table' statement
208 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
209 . ' (' . $sql_query . ')';
211 // Adds table type, character set, comments and partition definition
212 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
213 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
215 if (!empty($_REQUEST['tbl_collation'])) {
216 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
218 if (!empty($_REQUEST['comment'])) {
219 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\'';
221 if (!empty($_REQUEST['partition_definition'])) {
222 $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']);
224 $sql_query .= ';';
226 // Executes the query
227 $result = PMA_DBI_try_query($sql_query);
229 if ($result) {
231 // If comments were sent, enable relation stuff
232 require_once './libraries/relation.lib.php';
233 require_once './libraries/transformations.lib.php';
235 // Update comment table for mime types [MIME]
236 if (isset($_REQUEST['field_mimetype'])
237 && is_array($_REQUEST['field_mimetype'])
238 && $cfg['BrowseMIME']) {
239 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
240 if (isset($_REQUEST['field_name'][$fieldindex])
241 && strlen($_REQUEST['field_name'][$fieldindex])) {
242 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
243 $_REQUEST['field_transformation'][$fieldindex],
244 $_REQUEST['field_transformation_options'][$fieldindex]);
249 $message = PMA_Message::success(__('Table %1$s has been created.'));
250 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
252 $display_query = $sql_query;
253 $sql_query = '';
255 // read table info on this newly created table, in case
256 // the next page is Structure
257 $reread_info = true;
258 require './libraries/tbl_info.inc.php';
260 // do not switch to sql.php - as there is no row to be displayed on a new table
261 if ($cfg['DefaultTabTable'] === 'sql.php') {
262 require './tbl_structure.php';
263 } else {
264 require './' . $cfg['DefaultTabTable'];
266 exit;
267 } else {
268 PMA_mysqlDie('', '', '', $err_url, false);
269 // An error happened while inserting/updating a table definition.
270 // to prevent total loss of that data, we embed the form once again.
271 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
272 $num_fields = $_REQUEST['orig_num_fields'];
273 $regenerate = true;
275 } // end do create table
278 * Displays the form used to define the structure of the table
280 require './libraries/tbl_properties.inc.php';
281 // Displays the footer
282 require_once './libraries/footer.inc.php';