Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_addfield.php,v 2.3 2003/11/26 22:52:24 rabus Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00005 00009 require_once('./libraries/grab_globals.lib.php'); 00010 $js_to_run = 'functions.js'; 00011 require_once('./header.inc.php'); 00012 00013 // Check parameters 00014 PMA_checkParameters(array('db', 'table')); 00015 00016 00020 $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table); 00021 00022 00026 $abort = false; 00027 if (isset($submit)) { 00028 $query = ''; 00029 00030 // Transforms the radio button field_key into 3 arrays 00031 $field_cnt = count($field_name); 00032 for ($i = 0; $i < $field_cnt; ++$i) { 00033 if (isset(${'field_key_' . $i})) { 00034 if (${'field_key_' . $i} == 'primary_' . $i) { 00035 $field_primary[] = $i; 00036 } 00037 if (${'field_key_' . $i} == 'index_' . $i) { 00038 $field_index[] = $i; 00039 } 00040 if (${'field_key_' . $i} == 'unique_' . $i) { 00041 $field_unique[] = $i; 00042 } 00043 } // end if 00044 } // end for 00045 // Builds the field creation statement and alters the table 00046 for ($i = 0; $i < $field_cnt; ++$i) { 00047 if (empty($field_name[$i])) { 00048 continue; 00049 } 00050 00051 $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i]; 00052 if ($field_length[$i] != '' 00053 && !preg_match('@^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$@i', $field_type[$i])) { 00054 $query .= '(' . $field_length[$i] . ')'; 00055 } 00056 if ($field_attribute[$i] != '') { 00057 $query .= ' ' . $field_attribute[$i]; 00058 } else if (PMA_MYSQL_INT_VERSION >= 40100 && $field_charset[$i] != '') { 00059 $query .= ' CHARACTER SET ' . $field_charset[$i]; 00060 } 00061 if ($field_default[$i] != '') { 00062 if (strtoupper($field_default[$i]) == 'NULL') { 00063 $query .= ' DEFAULT NULL'; 00064 } else { 00065 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\''; 00066 } 00067 } 00068 if ($field_null[$i] != '') { 00069 $query .= ' ' . $field_null[$i]; 00070 } 00071 if ($field_extra[$i] != '') { 00072 $query .= ' ' . $field_extra[$i]; 00073 // An auto_increment field must be use as a primary key 00074 if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) { 00075 $primary_cnt = count($field_primary); 00076 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) { 00077 // void 00078 } // end for 00079 if ($field_primary[$j] == $i) { 00080 $query .= ' PRIMARY KEY'; 00081 unset($field_primary[$j]); 00082 } // end if 00083 } // end if (auto_increment) 00084 } 00085 00086 if ($after_field != '--end--') { 00087 // Only the first field can be added somewhere else than at the end 00088 if ($i == 0) { 00089 if ($after_field == '--first--') { 00090 $query .= ' FIRST'; 00091 } else { 00092 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field)); 00093 } 00094 } else { 00095 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]); 00096 } 00097 } 00098 $query .= ', ADD '; 00099 } // end for 00100 $query = preg_replace('@, ADD $@', '', $query); 00101 00102 // To allow replication, we first select the db to use and then run queries 00103 // on this db. 00104 $sql_query = 'USE ' . PMA_backquote($db); 00105 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url); 00106 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query; 00107 $error_create = false; 00108 $result = PMA_mysql_query($sql_query) or $error_create = true; 00109 00110 if ($error_create == false) { 00111 00112 $sql_query_cpy = $sql_query . ';'; 00113 00114 // Builds the primary keys statements and updates the table 00115 $primary = ''; 00116 if (isset($field_primary)) { 00117 $primary_cnt = count($field_primary); 00118 for ($i = 0; $i < $primary_cnt; $i++) { 00119 $j = $field_primary[$i]; 00120 if (!empty($field_name[$j])) { 00121 $primary .= PMA_backquote($field_name[$j]) . ', '; 00122 } 00123 } // end for 00124 $primary = preg_replace('@, $@', '', $primary); 00125 if (!empty($primary)) { 00126 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')'; 00127 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url); 00128 $sql_query_cpy .= "\n" . $sql_query . ';'; 00129 } 00130 } // end if 00131 00132 // Builds the indexes statements and updates the table 00133 $index = ''; 00134 if (isset($field_index)) { 00135 $index_cnt = count($field_index); 00136 for ($i = 0; $i < $index_cnt; $i++) { 00137 $j = $field_index[$i]; 00138 if (!empty($field_name[$j])) { 00139 $index .= PMA_backquote($field_name[$j]) . ', '; 00140 } 00141 } // end for 00142 $index = preg_replace('@, $@', '', $index); 00143 if (!empty($index)) { 00144 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')'; 00145 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url); 00146 $sql_query_cpy .= "\n" . $sql_query . ';'; 00147 } 00148 } // end if 00149 00150 // Builds the uniques statements and updates the table 00151 $unique = ''; 00152 if (isset($field_unique)) { 00153 $unique_cnt = count($field_unique); 00154 for ($i = 0; $i < $unique_cnt; $i++) { 00155 $j = $field_unique[$i]; 00156 if (!empty($field_name[$j])) { 00157 $unique .= PMA_backquote($field_name[$j]) . ', '; 00158 } 00159 } // end for 00160 $unique = preg_replace('@, $@', '', $unique); 00161 if (!empty($unique)) { 00162 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')'; 00163 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url); 00164 $sql_query_cpy .= "\n" . $sql_query . ';'; 00165 } 00166 } // end if 00167 00168 00169 // Builds the fulltext statements and updates the table 00170 $fulltext = ''; 00171 if (isset($field_fulltext)) { 00172 $fulltext_cnt = count($field_fulltext); 00173 for ($i = 0; $i < $fulltext_cnt; $i++) { 00174 $j = $field_fulltext[$i]; 00175 $fulltext .= PMA_backquote($field_name[$j]) . ', '; 00176 } // end for 00177 $fulltext = preg_replace('@, $@', '', $fulltext); 00178 if (!empty($fulltext)) { 00179 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')'; 00180 $result = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url); 00181 $sql_query_cpy .= "\n" . $sql_query . ';'; 00182 } 00183 } // end if 00184 00185 // garvin: If comments were sent, enable relation stuff 00186 require_once('./libraries/relation.lib.php'); 00187 require_once('./libraries/transformations.lib.php'); 00188 00189 $cfgRelation = PMA_getRelationsParam(); 00190 00191 // garvin: Update comment table, if a comment was set. 00192 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) { 00193 foreach($field_comments AS $fieldindex => $fieldcomment) { 00194 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment); 00195 } 00196 } 00197 00198 // garvin: Update comment table for mime types [MIME] 00199 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) { 00200 foreach($field_mimetype AS $fieldindex => $mimetype) { 00201 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]); 00202 } 00203 } 00204 00205 // Go back to the structure sub-page 00206 $sql_query = $sql_query_cpy; 00207 unset($sql_query_cpy); 00208 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered; 00209 $active_page = 'tbl_properties_structure.php'; 00210 require('./tbl_properties_structure.php'); 00211 } else { 00212 PMA_mysqlDie('', '', '', $err_url, FALSE); 00213 // garvin: An error happened while inserting/updating a table definition. 00214 // to prevent total loss of that data, we embed the form once again. 00215 // The variable $regenerate will be used to restore data in tbl_properties.inc.php 00216 $num_fields = $orig_num_fields; 00217 if (isset($orig_after_field)) { 00218 $after_field = $orig_after_field; 00219 } 00220 $regenerate = true; 00221 } 00222 } // end do alter table 00223 00227 if ($abort == FALSE) { 00228 $action = 'tbl_addfield.php'; 00229 require('./tbl_properties.inc.php'); 00230 00231 // Diplays the footer 00232 echo "\n"; 00233 require_once('./footer.inc.php'); 00234 } 00235 00236 ?>