Translation update done using Pootle.
[phpmyadmin-themes.git] / tbl_create.php
blob0d15822ac2bdacb67d3843b36ceadb11162efbff
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 PMA_Table::generateFieldSpec()
8 * @uses PMA_checkParameters()
9 * @uses PMA_generateCharsetQueryPart()
10 * @uses PMA_sqlAddslashes()
11 * @uses PMA_DBI_try_query()
12 * @uses PMA_getRelationsParam()
13 * @uses PMA_setMIME()
14 * @uses PMA_mysqlDie()
15 * @uses PMA_generate_common_url()
16 * @uses PMA_DBI_get_columns()
17 * @uses PMA_DBI_select_db()
18 * @uses PMA_backquote()
19 * @uses $_REQUEST['do_save_data']
20 * @uses $_REQUEST['submit_num_fields']
21 * @uses $_REQUEST['orig_num_fields']
22 * @uses $_REQUEST['added_fields']
23 * @uses $_REQUEST['num_fields']
24 * @uses preg_replace()
25 * @uses count()
26 * @uses is_array()
27 * @uses strlen()
28 * @uses sprintf()
29 * @uses htmlspecialchars()
30 * @package phpMyAdmin
33 /**
34 * Get some core libraries
36 require_once './libraries/common.inc.php';
38 $action = 'tbl_create.php';
40 require_once './libraries/header.inc.php';
42 // Check parameters
43 PMA_checkParameters(array('db'));
45 /* Check if database name is empty */
46 if (strlen($db) == 0) {
47 PMA_mysqlDie(__('The database name is empty!'), '', '', 'main.php');
50 /**
51 * Defines the url to return to in case of error in a sql statement
53 if (PMA_DBI_get_columns($db, $table)) {
54 // table exists already
55 PMA_mysqlDie(sprintf(__('Table %s already exists!'), htmlspecialchars($table)), '',
56 '', 'db_structure.php?' . PMA_generate_common_url($db));
59 $err_url = 'tbl_create.php?' . PMA_generate_common_url($db, $table);
61 // check number of fields to be created
62 if (isset($_REQUEST['submit_num_fields'])) {
63 $regenerate = true; // for libraries/tbl_properties.inc.php
64 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
65 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
66 $num_fields = (int) $_REQUEST['num_fields'];
67 } else {
68 $num_fields = 2;
71 /**
72 * Selects the database to work with
74 if (!PMA_DBI_select_db($db)) {
75 PMA_mysqlDie(sprintf(__('\'%s\' database does not exist.'), htmlspecialchars($db)),
76 '', '', 'main.php');
79 /**
80 * The form used to define the structure of the table has been submitted
82 if (isset($_REQUEST['do_save_data'])) {
83 $sql_query = '';
85 // Transforms the radio button field_key into 3 arrays
86 $field_cnt = count($_REQUEST['field_name']);
87 for ($i = 0; $i < $field_cnt; ++$i) {
88 if (isset($_REQUEST['field_key'][$i])) {
89 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
90 $field_primary[] = $i;
92 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
93 $field_index[] = $i;
95 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
96 $field_unique[] = $i;
98 } // end if
99 } // end for
101 // Builds the fields creation statements
102 for ($i = 0; $i < $field_cnt; $i++) {
103 // '0' is also empty for php :-(
104 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
105 continue;
108 $query = PMA_Table::generateFieldSpec(
109 $_REQUEST['field_name'][$i],
110 $_REQUEST['field_type'][$i],
111 $_REQUEST['field_length'][$i],
112 $_REQUEST['field_attribute'][$i],
113 isset($_REQUEST['field_collation'][$i])
114 ? $_REQUEST['field_collation'][$i]
115 : '',
116 isset($_REQUEST['field_null'][$i])
117 ? $_REQUEST['field_null'][$i]
118 : 'NOT NULL',
119 $_REQUEST['field_default_type'][$i],
120 $_REQUEST['field_default_value'][$i],
121 isset($_REQUEST['field_extra'][$i])
122 ? $_REQUEST['field_extra'][$i]
123 : false,
124 isset($_REQUEST['field_comments'][$i])
125 ? $_REQUEST['field_comments'][$i]
126 : '',
127 $field_primary,
128 $i);
130 $query .= ', ';
131 $sql_query .= $query;
132 } // end for
133 unset($field_cnt, $query);
134 $sql_query = preg_replace('@, $@', '', $sql_query);
136 // Builds the primary keys statements
137 $primary = '';
138 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
139 for ($i = 0; $i < $primary_cnt; $i++) {
140 $j = $field_primary[$i];
141 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
142 $primary .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
144 } // end for
145 unset($primary_cnt);
146 $primary = preg_replace('@, $@', '', $primary);
147 if (strlen($primary)) {
148 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
150 unset($primary);
152 // Builds the indexes statements
153 $index = '';
154 $index_cnt = (isset($field_index) ? count($field_index) : 0);
155 for ($i = 0;$i < $index_cnt; $i++) {
156 $j = $field_index[$i];
157 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
158 $index .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
160 } // end for
161 unset($index_cnt);
162 $index = preg_replace('@, $@', '', $index);
163 if (strlen($index)) {
164 $sql_query .= ', INDEX (' . $index . ')';
166 unset($index);
168 // Builds the uniques statements
169 $unique = '';
170 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
171 for ($i = 0; $i < $unique_cnt; $i++) {
172 $j = $field_unique[$i];
173 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
174 $unique .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
176 } // end for
177 unset($unique_cnt);
178 $unique = preg_replace('@, $@', '', $unique);
179 if (strlen($unique)) {
180 $sql_query .= ', UNIQUE (' . $unique . ')';
182 unset($unique);
184 // Builds the FULLTEXT statements
185 $fulltext = '';
186 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
187 for ($i = 0; $i < $fulltext_cnt; $i++) {
188 $j = $field_fulltext[$i];
189 if (isset($_REQUEST['field_name'][$j]) && strlen($_REQUEST['field_name'][$j])) {
190 $fulltext .= PMA_backquote($_REQUEST['field_name'][$j]) . ', ';
192 } // end for
194 $fulltext = preg_replace('@, $@', '', $fulltext);
195 if (strlen($fulltext)) {
196 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
198 unset($fulltext);
200 // Builds the 'create table' statement
201 $sql_query = 'CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table)
202 . ' (' . $sql_query . ')';
204 // Adds table type, character set, comments and partition definition
205 if (!empty($_REQUEST['tbl_type']) && ($_REQUEST['tbl_type'] != 'Default')) {
206 $sql_query .= ' ENGINE = ' . $_REQUEST['tbl_type'];
208 if (!empty($_REQUEST['tbl_collation'])) {
209 $sql_query .= PMA_generateCharsetQueryPart($_REQUEST['tbl_collation']);
211 if (!empty($_REQUEST['comment'])) {
212 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($_REQUEST['comment']) . '\'';
214 if (!empty($_REQUEST['partition_definition'])) {
215 $sql_query .= ' ' . PMA_sqlAddslashes($_REQUEST['partition_definition']);
217 $sql_query .= ';';
219 // Executes the query
220 $result = PMA_DBI_try_query($sql_query);
222 if ($result) {
224 // If comments were sent, enable relation stuff
225 require_once './libraries/transformations.lib.php';
227 // Update comment table for mime types [MIME]
228 if (isset($_REQUEST['field_mimetype'])
229 && is_array($_REQUEST['field_mimetype'])
230 && $cfg['BrowseMIME']) {
231 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
232 if (isset($_REQUEST['field_name'][$fieldindex])
233 && strlen($_REQUEST['field_name'][$fieldindex])) {
234 PMA_setMIME($db, $table, $_REQUEST['field_name'][$fieldindex], $mimetype,
235 $_REQUEST['field_transformation'][$fieldindex],
236 $_REQUEST['field_transformation_options'][$fieldindex]);
241 $message = PMA_Message::success(__('Table %1$s has been created.'));
242 $message->addParam(PMA_backquote($db) . '.' . PMA_backquote($table));
244 if($GLOBALS['is_ajax_request'] == true) {
247 * construct the html for the newly created table's row to be appended
248 * to the list of tables.
250 * Logic taken from db_structure.php
253 $tbl_url_params = array();
254 $tbl_url_params['db'] = $db;
255 $tbl_url_params['table'] = $table;
256 $is_show_stats = $cfg['ShowStats'];
258 $tbl_stats_result = PMA_DBI_query('SHOW TABLE STATUS FROM '
259 . PMA_backquote($db) . ' LIKE \'' . addslashes($table) . '\';');
260 $tbl_stats = PMA_DBI_fetch_assoc($tbl_stats_result);
261 PMA_DBI_free_result($tbl_stats_result);
262 unset($tbl_stats_result);
264 if ($is_show_stats) {
265 $sum_size = (double) 0;
266 $overhead_size = (double) 0;
267 $overhead_check = '';
269 $tblsize = doubleval($tbl_stats['Data_length']) + doubleval($tbl_stats['Index_length']);
270 $sum_size += $tblsize;
271 list($formatted_size, $unit) = PMA_formatByteDown($tblsize, 3, ($tblsize > 0) ? 1 : 0);
272 if (isset($tbl_stats['Data_free']) && $tbl_stats['Data_free'] > 0) {
273 list($formatted_overhead, $overhead_unit) = PMA_formatByteDown($tbl_stats['Data_free'], 3, ($tbl_stats['Data_free'] > 0) ? 1 : 0);
274 $overhead_size += $tbl_stats['Data_free'];
277 if (isset($formatted_overhead)) {
278 $overhead = $formatted_overhead . ' ' . $overhead_unit;
279 unset($formatted_overhead);
280 } else {
281 $overhead = '-';
285 $new_table_string = '<tr>' . "\n";
286 $new_table_string .= '<td align="center"> <input type="checkbox" id="checkbox_tbl_" name="selected_tbl[]" value="'.htmlspecialchars($table).'" /> </td>' . "\n";
288 $new_table_string .= '<th>';
289 $new_table_string .= '<a href="sql.php' . PMA_generate_common_url($tbl_url_params) . '">'. $table . '</a>';
291 if (PMA_Tracker::isActive()) {
292 $truename = str_replace(' ', '&nbsp;', htmlspecialchars($table));
293 if (PMA_Tracker::isTracked($db, $truename)) {
294 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '"><img class="icon" width="14" height="14" src="' . $pmaThemeImage . 'eye.png" alt="' . __('Tracking is active.') . '" title="' . __('Tracking is active.') . '" /></a>';
295 } elseif (PMA_Tracker::getVersion($db, $truename) > 0) {
296 $new_table_string .= '<a href="tbl_tracking.php' . PMA_generate_common_url($tbl_url_params) . '"><img class="icon" width="14" height="14" src="' . $pmaThemeImage . 'eye_grey.png" alt="' . __('Tracking is not active.') . '" title="' . __('Tracking is not active.') . '" /></a>';
298 unset($truename);
300 $new_table_string .= '</th>' . "\n";
302 $new_table_string .= '<td> <img class="icon" width="16" height="16" src="' .$pmaThemeImage . 'bd_browse.png" alt="' . __('Browse') . '" title="' . __('Browse') . '" /> </td>' . "\n";
304 $new_table_string .= '<td> <a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '"> ';
305 $new_table_string .= '<img class="icon" width="16" height="16" src="' .$pmaThemeImage . 'b_props.png" alt="' . __('Structure') . '" title="' . __('Structure') . '" />';
306 $new_table_string .= '</a> </td>' . "\n";
308 $new_table_string .= '<td> <img class="icon" width="16" height="16" src="' .$pmaThemeImage . 'bd_select.png" alt="' . __('Search') . '" title="' . __('Search') . '" /> </td>' . "\n";
310 $new_table_string .= '<td> <a href="tbl_change.php' . PMA_generate_common_url($tbl_url_params) . '"> ';
311 $new_table_string .= '<img class="icon" width="16" height="16" src="' .$pmaThemeImage . 'b_insrow.png" alt="' . __('Insert') . '" title="' . __('Insert') . '" />';
312 $new_table_string .= '</a> </td>' . "\n";
314 $new_table_string .= '<td> <img class="icon" width="16" height="16" src="' .$pmaThemeImage . 'bd_empty.png" alt="' . __('Empty') . '" title="' . __('Empty') . '" /> </td>' . "\n";
316 $new_table_string .= '<td> <a class="drop_table_anchor" href="sql.php' . PMA_generate_common_url($tbl_url_params) . '&amp;sql_query=';
317 $new_table_string .= urlencode('DROP TABLE ' . PMA_backquote($table));
318 $new_table_string .= '">';
319 $new_table_string .= '<img class="icon" width="16" height="16" src="' .$pmaThemeImage . 'b_drop.png" alt="' . __('Drop') . '" title="' . __('Drop') . '" />';
320 $new_table_string .= '</a> </td>' . "\n";
322 $new_table_string .= '<td class="value">' . $tbl_stats['Rows'] . '</td>' . "\n";
324 $new_table_string .= '<td nowrap="nowrap">' . $tbl_stats['Engine'] . '</td>' . "\n";
326 $new_table_string .= '<td> <dfn title="' . PMA_getCollationDescr($tbl_stats['Collation']) . '">'. $tbl_stats['Collation'] .'</dfn></td>' . "\n";
328 if($is_show_stats) {
329 $new_table_string .= '<td class="value"> <a href="tbl_structure.php' . PMA_generate_common_url($tbl_url_params) . '#showusage" >' . $formatted_size . ' ' . $unit . '</a> </td>' . "\n" ;
330 $new_table_string .= '<td class="value">' . $overhead . '</td>' . "\n" ;
333 $new_table_string .= '</tr>' . "\n";
335 $extra_data['new_table_string'] = $new_table_string;
337 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
340 $display_query = $sql_query;
341 $sql_query = '';
343 // read table info on this newly created table, in case
344 // the next page is Structure
345 $reread_info = true;
346 require './libraries/tbl_info.inc.php';
348 // do not switch to sql.php - as there is no row to be displayed on a new table
349 if ($cfg['DefaultTabTable'] === 'sql.php') {
350 require './tbl_structure.php';
351 } else {
352 require './' . $cfg['DefaultTabTable'];
354 exit;
355 } else {
356 PMA_mysqlDie('', '', '', $err_url, false);
357 // An error happened while inserting/updating a table definition.
358 // to prevent total loss of that data, we embed the form once again.
359 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
360 $num_fields = $_REQUEST['orig_num_fields'];
361 $regenerate = true;
363 } // end do create table
366 * Displays the form used to define the structure of the table
368 require './libraries/tbl_properties.inc.php';
369 // Displays the footer
370 require './libraries/footer.inc.php';