Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: tbl_create.php,v 2.26.2.1 2006/03/08 17:54:29 lem9 Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * Get some core libraries
7 */
8 require_once('./libraries/common.lib.php');
9  
10 $js_to_run = 'functions.js';
11  
12 if (isset($table)) {
13 $table = PMA_sanitize($table);
14 }
15  
16 require_once('./libraries/header.inc.php');
17  
18 // Check parameters
19 PMA_checkParameters(array('db', 'table'));
20  
21 /**
22 * Defines the url to return to in case of error in a sql statement
23 */
24 $err_url = $cfg['DefaultTabTable'] . '?' . PMA_generate_common_url($db, $table);
25  
26  
27 /**
28 * Selects the database to work with
29 */
30 PMA_DBI_select_db($db);
31  
32  
33 /**
34 * The form used to define the structure of the table has been submitted
35 */
36 $abort = FALSE;
37 if (isset($submit_num_fields)) {
38 $regenerate = TRUE;
39 $num_fields = $orig_num_fields + $added_fields;
40 } elseif (isset($do_save_data)) {
41 $sql_query = $query_cpy = '';
42  
43 // Transforms the radio button field_key into 3 arrays
44 $field_cnt = count($field_name);
45 for ($i = 0; $i < $field_cnt; ++$i) {
46 if (isset(${'field_key_' . $i})) {
47 if (${'field_key_' . $i} == 'primary_' . $i) {
48 $field_primary[] = $i;
49 }
50 if (${'field_key_' . $i} == 'index_' . $i) {
51 $field_index[] = $i;
52 }
53 if (${'field_key_' . $i} == 'unique_' . $i) {
54 $field_unique[] = $i;
55 }
56 } // end if
57 } // end for
58 // Builds the fields creation statements
59 for ($i = 0; $i < $field_cnt; $i++) {
60 // '0' is also empty for php :-(
61 if (empty($field_name[$i]) && $field_name[$i] != '0') {
62 continue;
63 }
64  
65 $query = PMA_generateFieldSpec($field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], isset($field_comments[$i]) ? $field_comments[$i] : '', $field_primary, $i);
66  
67 $query .= ', ';
68 $sql_query .= $query;
69 $query_cpy .= "\n" . ' ' . $query;
70 } // end for
71 unset($field_cnt);
72 unset($query);
73 $sql_query = preg_replace('@, $@', '', $sql_query);
74 $query_cpy = preg_replace('@, $@', '', $query_cpy);
75  
76 // Builds the primary keys statements
77 $primary = '';
78 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0);
79 for ($i = 0; $i < $primary_cnt; $i++) {
80 $j = $field_primary[$i];
81 if (isset($field_name[$j]) && strlen($field_name[$j])) {
82 $primary .= PMA_backquote($field_name[$j]) . ', ';
83 }
84 } // end for
85 unset($primary_cnt);
86 $primary = preg_replace('@, $@', '', $primary);
87 if (strlen($primary)) {
88 $sql_query .= ', PRIMARY KEY (' . $primary . ')';
89 $query_cpy .= ',' . "\n" . ' PRIMARY KEY (' . $primary . ')';
90 }
91 unset($primary);
92  
93 // Builds the indexes statements
94 $index = '';
95 $index_cnt = (isset($field_index) ? count($field_index) : 0);
96 for ($i = 0;$i < $index_cnt; $i++) {
97 $j = $field_index[$i];
98 if (isset($field_name[$j]) && strlen($field_name[$j])) {
99 $index .= PMA_backquote($field_name[$j]) . ', ';
100 }
101 } // end for
102 unset($index_cnt);
103 $index = preg_replace('@, $@', '', $index);
104 if (strlen($index)) {
105 $sql_query .= ', INDEX (' . $index . ')';
106 $query_cpy .= ',' . "\n" . ' INDEX (' . $index . ')';
107 }
108 unset($index);
109  
110 // Builds the uniques statements
111 $unique = '';
112 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0);
113 for ($i = 0; $i < $unique_cnt; $i++) {
114 $j = $field_unique[$i];
115 if (isset($field_name[$j]) && strlen($field_name[$j])) {
116 $unique .= PMA_backquote($field_name[$j]) . ', ';
117 }
118 } // end for
119 unset($unique_cnt);
120 $unique = preg_replace('@, $@', '', $unique);
121 if (strlen($unique)) {
122 $sql_query .= ', UNIQUE (' . $unique . ')';
123 $query_cpy .= ',' . "\n" . ' UNIQUE (' . $unique . ')';
124 }
125 unset($unique);
126  
127 // Builds the FULLTEXT statements
128 $fulltext = '';
129 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0);
130 for ($i = 0; $i < $fulltext_cnt; $i++) {
131 $j = $field_fulltext[$i];
132 if (isset($field_name[$j]) && strlen($field_name[$j])) {
133 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
134 }
135 } // end for
136  
137 $fulltext = preg_replace('@, $@', '', $fulltext);
138 if (strlen($fulltext)) {
139 $sql_query .= ', FULLTEXT (' . $fulltext . ')';
140 $query_cpy .= ',' . "\n" . ' FULLTEXT (' . $fulltext . ')';
141 }
142 unset($fulltext);
143  
144 // Builds the 'create table' statement
145 $sql_query = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $sql_query . ')';
146 $query_cpy = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $query_cpy . "\n" . ')';
147  
148 // Adds table type, character set and comments
149 if (!empty($tbl_type) && ($tbl_type != 'Default')) {
150 $sql_query .= ' ' . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type;
151 $query_cpy .= "\n" . PMA_ENGINE_KEYWORD . ' = ' . $tbl_type;
152 }
153 if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) {
154 $sql_query .= PMA_generateCharsetQueryPart($tbl_collation);
155 $query_cpy .= "\n" . PMA_generateCharsetQueryPart($tbl_collation);
156 }
157  
158 if (!empty($comment)) {
159 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
160 $query_cpy .= "\n" . 'COMMENT = \'' . PMA_sqlAddslashes($comment) . '\'';
161 }
162  
163 // Executes the query
164 $error_create = FALSE;
165 $result = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
166  
167 if ($error_create == FALSE) {
168 $sql_query = $query_cpy . ';';
169 unset($query_cpy);
170 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenCreated;
171  
172 // garvin: If comments were sent, enable relation stuff
173 require_once('./libraries/relation.lib.php');
174 require_once('./libraries/transformations.lib.php');
175  
176 $cfgRelation = PMA_getRelationsParam();
177  
178 // garvin: Update comment table, if a comment was set.
179 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
180 foreach ($field_comments AS $fieldindex => $fieldcomment) {
181 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
182 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
183 }
184 }
185 }
186  
187 // garvin: Update comment table for mime types [MIME]
188 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
189 foreach ($field_mimetype AS $fieldindex => $mimetype) {
190 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
191 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
192 }
193 }
194 }
195  
196 require('./' . $cfg['DefaultTabTable']);
197 $abort = TRUE;
198 exit();
199 } else {
200 PMA_mysqlDie('', '', '', $err_url, FALSE);
201 // garvin: An error happened while inserting/updating a table definition.
202 // to prevent total loss of that data, we embed the form once again.
203 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
204 $num_fields = $orig_num_fields;
205 $regenerate = TRUE;
206 }
207 } // end do create table
208  
209 /**
210 * Displays the form used to define the structure of the table
211 */
212 if ($abort == FALSE) {
213 if (isset($num_fields)) {
214 $num_fields = intval($num_fields);
215 }
216 // No table name
217 if (!isset($table) || trim($table) == '') {
218 PMA_mysqlDie($strTableEmpty, '', '', $err_url);
219 }
220 // No valid number of fields
221 elseif (empty($num_fields) || !is_int($num_fields)) {
222 PMA_mysqlDie($strFieldsEmpty, '', '', $err_url);
223 }
224 // Does table exist?
225 elseif (!(PMA_DBI_get_fields($db, $table) === FALSE)) {
226 PMA_mysqlDie(sprintf($strTableAlreadyExists, htmlspecialchars($table)), '', '', $err_url);
227 }
228 // Table name and number of fields are valid -> show the form
229 else {
230 $action = 'tbl_create.php';
231 require('./libraries/tbl_properties.inc.php');
232 // Displays the footer
233 echo "\n";
234 require_once('./libraries/footer.inc.php');
235 }
236 }
237  
238 ?>