Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: tbl_addfield.php,v 2.24 2006/01/17 17:02:29 cybot_tm 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 require_once('./libraries/header.inc.php');
12  
13 // Check parameters
14 PMA_checkParameters(array('db', 'table'));
15  
16  
17 /**
18 * Defines the url to return to in case of error in a sql statement
19 */
20 $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table);
21  
22 /**
23 * The form used to define the field to add has been submitted
24 */
25 $abort = false;
26 if (isset($submit_num_fields)) {
27 if (isset($orig_after_field)) {
28 $after_field = $orig_after_field;
29 }
30 if (isset($orig_field_where)) {
31 $field_where = $orig_field_where;
32 }
33 $num_fields = $orig_num_fields + $added_fields;
34 $regenerate = TRUE;
35 } elseif (isset($do_save_data)) {
36 $query = '';
37  
38 // Transforms the radio button field_key into 3 arrays
39 $field_cnt = count($field_name);
40 for ($i = 0; $i < $field_cnt; ++$i) {
41 if (isset(${'field_key_' . $i})) {
42 if (${'field_key_' . $i} == 'primary_' . $i) {
43 $field_primary[] = $i;
44 }
45 if (${'field_key_' . $i} == 'index_' . $i) {
46 $field_index[] = $i;
47 }
48 if (${'field_key_' . $i} == 'unique_' . $i) {
49 $field_unique[] = $i;
50 }
51 } // end if
52 } // end for
53 // Builds the field creation statement and alters the table
54  
55 for ($i = 0; $i < $field_cnt; ++$i) {
56 // '0' is also empty for php :-(
57 if (empty($field_name[$i]) && $field_name[$i] != '0') {
58 continue;
59 }
60  
61 $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);
62  
63 if ($field_where != 'last') {
64 // Only the first field can be added somewhere other than at the end
65 if ($i == 0) {
66 if ($field_where == 'first') {
67 $query .= ' FIRST';
68 } else {
69 $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
70 }
71 } else {
72 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
73 }
74 }
75 $query .= ', ADD ';
76 } // end for
77 $query = preg_replace('@, ADD $@', '', $query);
78  
79 // To allow replication, we first select the db to use and then run queries
80 // on this db.
81 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
82 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
83 $error_create = FALSE;
84 PMA_DBI_try_query($sql_query) or $error_create = TRUE;
85  
86 if ($error_create == false) {
87  
88 $sql_query_cpy = $sql_query . ';';
89  
90 // Builds the primary keys statements and updates the table
91 $primary = '';
92 if (isset($field_primary)) {
93 $primary_cnt = count($field_primary);
94 for ($i = 0; $i < $primary_cnt; $i++) {
95 $j = $field_primary[$i];
96 if (isset($field_name[$j]) && strlen($field_name[$j])) {
97 $primary .= PMA_backquote($field_name[$j]) . ', ';
98 }
99 } // end for
100 $primary = preg_replace('@, $@', '', $primary);
101 if (strlen($primary)) {
102 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ');';
103 $result = PMA_DBI_query($sql_query);
104 $sql_query_cpy .= "\n" . $sql_query . ';';
105 }
106 } // end if
107  
108 // Builds the indexes statements and updates the table
109 $index = '';
110 if (isset($field_index)) {
111 $index_cnt = count($field_index);
112 for ($i = 0; $i < $index_cnt; $i++) {
113 $j = $field_index[$i];
114 if (isset($field_name[$j]) && strlen($field_name[$j])) {
115 $index .= PMA_backquote($field_name[$j]) . ', ';
116 }
117 } // end for
118 $index = preg_replace('@, $@', '', $index);
119 if (strlen($index)) {
120 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
121 $result = PMA_DBI_query($sql_query);
122 $sql_query_cpy .= "\n" . $sql_query . ';';
123 }
124 } // end if
125  
126 // Builds the uniques statements and updates the table
127 $unique = '';
128 if (isset($field_unique)) {
129 $unique_cnt = count($field_unique);
130 for ($i = 0; $i < $unique_cnt; $i++) {
131 $j = $field_unique[$i];
132 if (isset($field_name[$j]) && strlen($field_name[$j])) {
133 $unique .= PMA_backquote($field_name[$j]) . ', ';
134 }
135 } // end for
136 $unique = preg_replace('@, $@', '', $unique);
137 if (strlen($unique)) {
138 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
139 $result = PMA_DBI_query($sql_query);
140 $sql_query_cpy .= "\n" . $sql_query . ';';
141 }
142 } // end if
143  
144  
145 // Builds the fulltext statements and updates the table
146 $fulltext = '';
147 if (isset($field_fulltext)) {
148 $fulltext_cnt = count($field_fulltext);
149 for ($i = 0; $i < $fulltext_cnt; $i++) {
150 $j = $field_fulltext[$i];
151 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
152 } // end for
153 $fulltext = preg_replace('@, $@', '', $fulltext);
154 if (strlen($fulltext)) {
155 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
156 $result = PMA_DBI_query($sql_query);
157 $sql_query_cpy .= "\n" . $sql_query . ';';
158 }
159 } // end if
160  
161 // garvin: If comments were sent, enable relation stuff
162 require_once('./libraries/relation.lib.php');
163 require_once('./libraries/transformations.lib.php');
164  
165 $cfgRelation = PMA_getRelationsParam();
166  
167 // garvin: Update comment table, if a comment was set.
168 if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork'] && PMA_MYSQL_INT_VERSION < 40100) {
169 foreach ($field_comments AS $fieldindex => $fieldcomment) {
170 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
171 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, '', 'pmadb');
172 }
173 }
174 }
175  
176 // garvin: Update comment table for mime types [MIME]
177 if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
178 foreach ($field_mimetype AS $fieldindex => $mimetype) {
179 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
180 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
181 }
182 }
183 }
184  
185 // Go back to the structure sub-page
186 $sql_query = $sql_query_cpy;
187 unset($sql_query_cpy);
188 $message = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
189 $active_page = 'tbl_properties_structure.php';
190 require('./tbl_properties_structure.php');
191 } else {
192 PMA_mysqlDie('', '', '', $err_url, FALSE);
193 // garvin: An error happened while inserting/updating a table definition.
194 // to prevent total loss of that data, we embed the form once again.
195 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
196 $num_fields = $orig_num_fields;
197 if (isset($orig_after_field)) {
198 $after_field = $orig_after_field;
199 }
200 if (isset($orig_field_where)) {
201 $field_where = $orig_field_where;
202 }
203 $regenerate = true;
204 }
205 } // end do alter table
206  
207 /**
208 * Displays the form used to define the new field
209 */
210 if ($abort == FALSE) {
211 /**
212 * Gets tables informations
213 */
214 require_once('./libraries/tbl_properties_common.php');
215 require_once('./libraries/tbl_properties_table_info.inc.php');
216 /**
217 * Displays top menu links
218 */
219 $active_page = 'tbl_properties_structure.php';
220 require_once('./libraries/tbl_properties_links.inc.php');
221 /**
222 * Display the form
223 */
224 $action = 'tbl_addfield.php';
225 require_once('./libraries/tbl_properties.inc.php');
226  
227 // Diplays the footer
228 echo "\n";
229 require_once('./libraries/footer.inc.php');
230 }
231  
232 ?>