Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_addfield.php,v 2.14 2005/04/01 17:03:32 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 require_once('./header.inc.php'); 00011 00012 // Check parameters 00013 PMA_checkParameters(array('db', 'table')); 00014 00015 00019 $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table); 00020 00024 $abort = false; 00025 if (isset($submit_num_fields)) { 00026 if (isset($orig_after_field)) { 00027 $after_field = $orig_after_field; 00028 } 00029 if (isset($orig_field_where)) { 00030 $field_where = $orig_field_where; 00031 } 00032 $num_fields = $orig_num_fields + $added_fields; 00033 $regenerate = TRUE; 00034 } else if (isset($do_save_data)) { 00035 $query = ''; 00036 00037 // Transforms the radio button field_key into 3 arrays 00038 $field_cnt = count($field_name); 00039 for ($i = 0; $i < $field_cnt; ++$i) { 00040 if (isset(${'field_key_' . $i})) { 00041 if (${'field_key_' . $i} == 'primary_' . $i) { 00042 $field_primary[] = $i; 00043 } 00044 if (${'field_key_' . $i} == 'index_' . $i) { 00045 $field_index[] = $i; 00046 } 00047 if (${'field_key_' . $i} == 'unique_' . $i) { 00048 $field_unique[] = $i; 00049 } 00050 } // end if 00051 } // end for 00052 // Builds the field creation statement and alters the table 00053 00054 // TODO: check to see if this logic is exactly the same 00055 // as in tbl_create.php, and move to an include file 00056 00057 for ($i = 0; $i < $field_cnt; ++$i) { 00058 // '0' is also empty for php :-( 00059 if (empty($field_name[$i]) && $field_name[$i] != '0') { 00060 continue; 00061 } 00062 00063 $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; 00064 if ($field_length[$i] != '' 00065 && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) { 00066 $query .= '(' . $field_length[$i] . ')'; 00067 } 00068 if ($field_attribute[$i] != '') { 00069 $query .= ' ' . $field_attribute[$i]; 00070 } else if (PMA_MYSQL_INT_VERSION >= 40100 && isset($field_collation[$i]) && $field_collation[$i] != '') { 00071 list($tmp_charset) = explode('_', $field_collation[$i]); 00072 $query .= ' CHARACTER SET ' . $tmp_charset . ' COLLATE ' . $field_collation[$i]; 00073 unset($tmp_charset); 00074 } 00075 00076 if (isset($field_default_current_timestamp[$i]) && $field_default_current_timestamp[$i]) { 00077 $query .= ' DEFAULT CURRENT_TIMESTAMP'; 00078 } elseif ($field_default[$i] != '') { 00079 if (strtoupper($field_default[$i]) == 'NULL') { 00080 $query .= ' DEFAULT NULL'; 00081 } else { 00082 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\''; 00083 } 00084 } 00085 if ($field_null[$i] != '') { 00086 $query .= ' ' . $field_null[$i]; 00087 } 00088 if ($field_extra[$i] != '') { 00089 $query .= ' ' . $field_extra[$i]; 00090 // An auto_increment field must be use as a primary key 00091 if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) { 00092 $primary_cnt = count($field_primary); 00093 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) { 00094 // void 00095 } // end for 00096 if ($field_primary[$j] == $i) { 00097 $query .= ' PRIMARY KEY'; 00098 unset($field_primary[$j]); 00099 } // end if 00100 } // end if (auto_increment) 00101 } 00102 00103 if ($field_where != 'last') { 00104 // Only the first field can be added somewhere other than at the end 00105 if ($i == 0) { 00106 if ($field_where == 'first') { 00107 $query .= ' FIRST'; 00108 } else { 00109 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field)); 00110 } 00111 } else { 00112 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]); 00113 } 00114 } 00115 $query .= ', ADD '; 00116 } // end for 00117 $query = preg_replace('@, ADD $@', '', $query); 00118 00119 // To allow replication, we first select the db to use and then run queries 00120 // on this db. 00121 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url); 00122 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query; 00123 $error_create = FALSE; 00124 PMA_DBI_try_query($sql_query) or $error_create = TRUE; 00125 00126 if ($error_create == false) { 00127 00128 $sql_query_cpy = $sql_query . ';'; 00129 00130 // Builds the primary keys statements and updates the table 00131 $primary = ''; 00132 if (isset($field_primary)) { 00133 $primary_cnt = count($field_primary); 00134 for ($i = 0; $i < $primary_cnt; $i++) { 00135 $j = $field_primary[$i]; 00136 if (!empty($field_name[$j])) { 00137 $primary .= PMA_backquote($field_name[$j]) . ', '; 00138 } 00139 } // end for 00140 $primary = preg_replace('@, $@', '', $primary); 00141 if (!empty($primary)) { 00142 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ');'; 00143 $result = PMA_DBI_query($sql_query); 00144 $sql_query_cpy .= "\n" . $sql_query . ';'; 00145 } 00146 } // end if 00147 00148 // Builds the indexes statements and updates the table 00149 $index = ''; 00150 if (isset($field_index)) { 00151 $index_cnt = count($field_index); 00152 for ($i = 0; $i < $index_cnt; $i++) { 00153 $j = $field_index[$i]; 00154 if (!empty($field_name[$j])) { 00155 $index .= PMA_backquote($field_name[$j]) . ', '; 00156 } 00157 } // end for 00158 $index = preg_replace('@, $@', '', $index); 00159 if (!empty($index)) { 00160 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')'; 00161 $result = PMA_DBI_query($sql_query); 00162 $sql_query_cpy .= "\n" . $sql_query . ';'; 00163 } 00164 } // end if 00165 00166 // Builds the uniques statements and updates the table 00167 $unique = ''; 00168 if (isset($field_unique)) { 00169 $unique_cnt = count($field_unique); 00170 for ($i = 0; $i < $unique_cnt; $i++) { 00171 $j = $field_unique[$i]; 00172 if (!empty($field_name[$j])) { 00173 $unique .= PMA_backquote($field_name[$j]) . ', '; 00174 } 00175 } // end for 00176 $unique = preg_replace('@, $@', '', $unique); 00177 if (!empty($unique)) { 00178 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')'; 00179 $result = PMA_DBI_query($sql_query); 00180 $sql_query_cpy .= "\n" . $sql_query . ';'; 00181 } 00182 } // end if 00183 00184 00185 // Builds the fulltext statements and updates the table 00186 $fulltext = ''; 00187 if (isset($field_fulltext)) { 00188 $fulltext_cnt = count($field_fulltext); 00189 for ($i = 0; $i < $fulltext_cnt; $i++) { 00190 $j = $field_fulltext[$i]; 00191 $fulltext .= PMA_backquote($field_name[$j]) . ', '; 00192 } // end for 00193 $fulltext = preg_replace('@, $@', '', $fulltext); 00194 if (!empty($fulltext)) { 00195 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')'; 00196 $result = PMA_DBI_query($sql_query); 00197 $sql_query_cpy .= "\n" . $sql_query . ';'; 00198 } 00199 } // end if 00200 00201 // garvin: If comments were sent, enable relation stuff 00202 require_once('./libraries/relation.lib.php'); 00203 require_once('./libraries/transformations.lib.php'); 00204 00205 $cfgRelation = PMA_getRelationsParam(); 00206 00207 // garvin: Update comment table, if a comment was set. 00208 // lem9: FIXME: here we take care of native comments and 00209 // pmadb-style comments, however, in the case of 00210 // native comments, users do not see the COMMENT clause 00211 // when SQL query is displayed 00212 if (isset($field_comments) && is_array($field_comments) && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) { 00213 foreach ($field_comments AS $fieldindex => $fieldcomment) { 00214 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); 00215 } 00216 } 00217 00218 // garvin: Update comment table for mime types [MIME] 00219 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) { 00220 foreach ($field_mimetype AS $fieldindex => $mimetype) { 00221 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]); 00222 } 00223 } 00224 00225 // Go back to the structure sub-page 00226 $sql_query = $sql_query_cpy; 00227 unset($sql_query_cpy); 00228 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered; 00229 $active_page = 'tbl_properties_structure.php'; 00230 require('./tbl_properties_structure.php'); 00231 } else { 00232 PMA_mysqlDie('', '', '', $err_url, FALSE); 00233 // garvin: An error happened while inserting/updating a table definition. 00234 // to prevent total loss of that data, we embed the form once again. 00235 // The variable $regenerate will be used to restore data in tbl_properties.inc.php 00236 $num_fields = $orig_num_fields; 00237 if (isset($orig_after_field)) { 00238 $after_field = $orig_after_field; 00239 } 00240 if (isset($orig_field_where)) { 00241 $field_where = $orig_field_where; 00242 } 00243 $regenerate = true; 00244 } 00245 } // end do alter table 00246 00250 if ($abort == FALSE) { 00254 require('./tbl_properties_common.php'); 00255 require('./tbl_properties_table_info.php'); 00259 $active_page = 'tbl_properties_structure.php'; 00260 require('./tbl_properties_links.php'); 00264 $action = 'tbl_addfield.php'; 00265 require('./tbl_properties.inc.php'); 00266 00267 // Diplays the footer 00268 echo "\n"; 00269 require_once('./footer.inc.php'); 00270 } 00271 00272 ?>