Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_create.php,v 2.16 2005/08/04 19:24:16 lem9 Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00008 require_once('./libraries/grab_globals.lib.php'); 00009 $js_to_run = 'functions.js'; 00010 00011 require_once('./libraries/common.lib.php'); 00012 00013 if (isset($table)) { 00014 $table = PMA_sanitize($table); 00015 } 00016 00017 require_once('./header.inc.php'); 00018 00019 // Check parameters 00020 PMA_checkParameters(array('db', 'table')); 00021 00025 $err_url = $cfg['DefaultTabTable'] . '?' . PMA_generate_common_url($db, $table); 00026 00027 00031 PMA_DBI_select_db($db); 00032 00033 00037 $abort = FALSE; 00038 if (isset($submit_num_fields)) { 00039 $regenerate = TRUE; 00040 $num_fields = $orig_num_fields + $added_fields; 00041 } else if (isset($do_save_data)) { 00042 $sql_query = $query_cpy = ''; 00043 00044 // Transforms the radio button field_key into 3 arrays 00045 $field_cnt = count($field_name); 00046 for ($i = 0; $i < $field_cnt; ++$i) { 00047 if (isset(${'field_key_' . $i})) { 00048 if (${'field_key_' . $i} == 'primary_' . $i) { 00049 $field_primary[] = $i; 00050 } 00051 if (${'field_key_' . $i} == 'index_' . $i) { 00052 $field_index[] = $i; 00053 } 00054 if (${'field_key_' . $i} == 'unique_' . $i) { 00055 $field_unique[] = $i; 00056 } 00057 } // end if 00058 } // end for 00059 // Builds the fields creation statements 00060 for ($i = 0; $i < $field_cnt; $i++) { 00061 // '0' is also empty for php :-( 00062 if (empty($field_name[$i]) && $field_name[$i] != '0') { 00063 continue; 00064 } 00065 // TODO: maybe move this logic and the one of PMA_generateAlterTable() 00066 // to a central place 00067 00068 $query = PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; 00069 if ($field_length[$i] != '' 00070 && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) { 00071 $query .= '(' . $field_length[$i] . ')'; 00072 } 00073 if ($field_attribute[$i] != '') { 00074 $query .= ' ' . $field_attribute[$i]; 00075 } else if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($field_collation[$i])) { 00076 $query .= PMA_generateCharsetQueryPart($field_collation[$i]); 00077 } 00078 if (isset($field_default_current_timestamp[$i]) && $field_default_current_timestamp[$i]) { 00079 $query .= ' DEFAULT CURRENT_TIMESTAMP'; 00080 } elseif ($field_default[$i] != '') { 00081 if (strtoupper($field_default[$i]) == 'NULL') { 00082 $query .= ' DEFAULT NULL'; 00083 } else { 00084 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\''; 00085 } 00086 } 00087 if ($field_null[$i] != '') { 00088 $query .= ' ' . $field_null[$i]; 00089 } 00090 if ($field_extra[$i] != '') { 00091 $query .= ' ' . $field_extra[$i]; 00092 } 00093 $query .= ', '; 00094 $sql_query .= $query; 00095 $query_cpy .= "\n" . ' ' . $query; 00096 } // end for 00097 unset($field_cnt); 00098 unset($query); 00099 $sql_query = preg_replace('@, $@', '', $sql_query); 00100 $query_cpy = preg_replace('@, $@', '', $query_cpy); 00101 00102 // Builds the primary keys statements 00103 $primary = ''; 00104 $primary_cnt = (isset($field_primary) ? count($field_primary) : 0); 00105 for ($i = 0; $i < $primary_cnt; $i++) { 00106 $j = $field_primary[$i]; 00107 if (!empty($field_name[$j])) { 00108 $primary .= PMA_backquote($field_name[$j]) . ', '; 00109 } 00110 } // end for 00111 unset($primary_cnt); 00112 $primary = preg_replace('@, $@', '', $primary); 00113 if (!empty($primary)) { 00114 $sql_query .= ', PRIMARY KEY (' . $primary . ')'; 00115 $query_cpy .= ',' . "\n" . ' PRIMARY KEY (' . $primary . ')'; 00116 } 00117 unset($primary); 00118 00119 // Builds the indexes statements 00120 $index = ''; 00121 $index_cnt = (isset($field_index) ? count($field_index) : 0); 00122 for ($i = 0;$i < $index_cnt; $i++) { 00123 $j = $field_index[$i]; 00124 if (!empty($field_name[$j])) { 00125 $index .= PMA_backquote($field_name[$j]) . ', '; 00126 } 00127 } // end for 00128 unset($index_cnt); 00129 $index = preg_replace('@, $@', '', $index); 00130 if (!empty($index)) { 00131 $sql_query .= ', INDEX (' . $index . ')'; 00132 $query_cpy .= ',' . "\n" . ' INDEX (' . $index . ')'; 00133 } 00134 unset($index); 00135 00136 // Builds the uniques statements 00137 $unique = ''; 00138 $unique_cnt = (isset($field_unique) ? count($field_unique) : 0); 00139 for ($i = 0; $i < $unique_cnt; $i++) { 00140 $j = $field_unique[$i]; 00141 if (!empty($field_name[$j])) { 00142 $unique .= PMA_backquote($field_name[$j]) . ', '; 00143 } 00144 } // end for 00145 unset($unique_cnt); 00146 $unique = preg_replace('@, $@', '', $unique); 00147 if (!empty($unique)) { 00148 $sql_query .= ', UNIQUE (' . $unique . ')'; 00149 $query_cpy .= ',' . "\n" . ' UNIQUE (' . $unique . ')'; 00150 } 00151 unset($unique); 00152 00153 // Builds the FULLTEXT statements 00154 $fulltext = ''; 00155 $fulltext_cnt = (isset($field_fulltext) ? count($field_fulltext) : 0); 00156 for ($i = 0; $i < $fulltext_cnt; $i++) { 00157 $j = $field_fulltext[$i]; 00158 if (!empty($field_name[$j])) { 00159 $fulltext .= PMA_backquote($field_name[$j]) . ', '; 00160 } 00161 } // end for 00162 00163 $fulltext = preg_replace('@, $@', '', $fulltext); 00164 if (!empty($fulltext)) { 00165 $sql_query .= ', FULLTEXT (' . $fulltext . ')'; 00166 $query_cpy .= ',' . "\n" . ' FULLTEXT (' . $fulltext . ')'; 00167 } 00168 unset($fulltext); 00169 00170 // Builds the 'create table' statement 00171 $sql_query = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $sql_query . ')'; 00172 $query_cpy = 'CREATE TABLE ' . PMA_backquote($table) . ' (' . $query_cpy . "\n" . ')'; 00173 00174 // Adds table type, character set and comments 00175 if (!empty($tbl_type) && ($tbl_type != 'Default')) { 00176 $sql_query .= ' TYPE = ' . $tbl_type; 00177 $query_cpy .= "\n" . 'TYPE = ' . $tbl_type; 00178 } 00179 if (PMA_MYSQL_INT_VERSION >= 40100 && !empty($tbl_collation)) { 00180 $sql_query .= PMA_generateCharsetQueryPart($tbl_collation); 00181 $query_cpy .= "\n" . PMA_generateCharsetQueryPart($tbl_collation); 00182 } 00183 00184 if (!empty($comment)) { 00185 $sql_query .= ' COMMENT = \'' . PMA_sqlAddslashes($comment) . '\''; 00186 $query_cpy .= "\n" . 'COMMENT = \'' . PMA_sqlAddslashes($comment) . '\''; 00187 } 00188 00189 // Executes the query 00190 $error_create = FALSE; 00191 $result = PMA_DBI_try_query($sql_query) or $error_create = TRUE; 00192 00193 if ($error_create == FALSE) { 00194 $sql_query = $query_cpy . ';'; 00195 unset($query_cpy); 00196 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenCreated; 00197 00198 // garvin: If comments were sent, enable relation stuff 00199 require_once('./libraries/relation.lib.php'); 00200 require_once('./libraries/transformations.lib.php'); 00201 00202 $cfgRelation = PMA_getRelationsParam(); 00203 00204 // garvin: Update comment table, if a comment was set. 00205 if (isset($field_comments) && is_array($field_comments) && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { 00206 foreach ($field_comments AS $fieldindex => $fieldcomment) { 00207 // do not try to set a comment if the field name is empty 00208 if (!empty($field_name[$fieldindex])) { 00209 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); 00210 } 00211 } 00212 } 00213 00214 // garvin: Update comment table for mime types [MIME] 00215 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) { 00216 foreach ($field_mimetype AS $fieldindex => $mimetype) { 00217 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]); 00218 } 00219 } 00220 00221 require('./' . $cfg['DefaultTabTable']); 00222 $abort = TRUE; 00223 exit(); 00224 } else { 00225 PMA_mysqlDie('', '', '', $err_url, FALSE); 00226 // garvin: An error happened while inserting/updating a table definition. 00227 // to prevent total loss of that data, we embed the form once again. 00228 // The variable $regenerate will be used to restore data in tbl_properties.inc.php 00229 $num_fields = $orig_num_fields; 00230 $regenerate = TRUE; 00231 } 00232 } // end do create table 00233 00237 if ($abort == FALSE) { 00238 if (isset($num_fields)) { 00239 $num_fields = intval($num_fields); 00240 } 00241 // No table name 00242 if (!isset($table) || trim($table) == '') { 00243 PMA_mysqlDie($strTableEmpty, '', '', $err_url); 00244 } 00245 // No valid number of fields 00246 else if (empty($num_fields) || !is_int($num_fields)) { 00247 PMA_mysqlDie($strFieldsEmpty, '', '', $err_url); 00248 } 00249 // Table name and number of fields are valid -> show the form 00250 else { 00251 $action = 'tbl_create.php'; 00252 require('./tbl_properties.inc.php'); 00253 // Displays the footer 00254 echo "\n"; 00255 require_once('./footer.inc.php'); 00256 } 00257 } 00258 00259 ?>