Revert initial commit
[phpmyadmin/blinky.git] / tbl_addfield.php
blob500a3daeebbf5eefb6650ecc104fb503200739e3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin
7 */
9 /**
10 * Get some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Table.class.php';
15 $GLOBALS['js_include'][] = 'functions.js';
16 require_once './libraries/header.inc.php';
18 // Check parameters
19 PMA_checkParameters(array('db', 'table'));
22 /**
23 * Defines the url to return to in case of error in a sql statement
25 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
27 /**
28 * The form used to define the field to add has been submitted
30 $abort = false;
32 // check number of fields to be created
33 if (isset($_REQUEST['submit_num_fields'])) {
34 if (isset($_REQUEST['orig_after_field'])) {
35 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
37 if (isset($_REQUEST['orig_field_where'])) {
38 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
40 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
41 $regenerate = true;
42 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
43 $num_fields = (int) $_REQUEST['num_fields'];
44 } else {
45 $num_fields = 1;
48 if (isset($_REQUEST['do_save_data'])) {
49 $query = '';
50 $definitions = array();
52 // Transforms the radio button field_key into 3 arrays
53 $field_cnt = count($_REQUEST['field_name']);
54 $field_primary = array();
55 $field_index = array();
56 $field_unique = array();
57 $field_fulltext = array();
58 for ($i = 0; $i < $field_cnt; ++$i) {
59 if (isset($_REQUEST['field_key'][$i])
60 && strlen($_REQUEST['field_name'][$i])) {
61 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
62 $field_primary[] = $i;
64 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
65 $field_index[] = $i;
67 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
68 $field_unique[] = $i;
70 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
71 $field_fulltext[] = $i;
73 } // end if
74 } // end for
76 // Builds the field creation statement and alters the table
77 for ($i = 0; $i < $field_cnt; ++$i) {
78 // '0' is also empty for php :-(
79 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
80 continue;
83 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
84 $_REQUEST['field_name'][$i],
85 $_REQUEST['field_type'][$i],
86 $_REQUEST['field_length'][$i],
87 $_REQUEST['field_attribute'][$i],
88 isset($_REQUEST['field_collation'][$i])
89 ? $_REQUEST['field_collation'][$i]
90 : '',
91 isset($_REQUEST['field_null'][$i])
92 ? $_REQUEST['field_null'][$i]
93 : 'NOT NULL',
94 $_REQUEST['field_default_type'][$i],
95 $_REQUEST['field_default_value'][$i],
96 isset($_REQUEST['field_extra'][$i])
97 ? $_REQUEST['field_extra'][$i]
98 : false,
99 isset($_REQUEST['field_comments'][$i])
100 ? $_REQUEST['field_comments'][$i]
101 : '',
102 $field_primary,
106 if ($_REQUEST['field_where'] != 'last') {
107 // Only the first field can be added somewhere other than at the end
108 if ($i == 0) {
109 if ($_REQUEST['field_where'] == 'first') {
110 $definition .= ' FIRST';
111 } else {
112 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
114 } else {
115 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
118 $definitions[] = $definition;
119 } // end for
121 // Builds the primary keys statements and updates the table
122 if (count($field_primary)) {
123 $fields = array();
124 foreach ($field_primary as $field_nr) {
125 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
127 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
128 unset($fields);
131 // Builds the indexes statements and updates the table
132 if (count($field_index)) {
133 $fields = array();
134 foreach ($field_index as $field_nr) {
135 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
137 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
138 unset($fields);
141 // Builds the uniques statements and updates the table
142 if (count($field_unique)) {
143 $fields = array();
144 foreach ($field_unique as $field_nr) {
145 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
147 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
148 unset($fields);
151 // Builds the fulltext statements and updates the table
152 if (count($field_fulltext)) {
153 $fields = array();
154 foreach ($field_fulltext as $field_nr) {
155 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
157 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
158 unset($fields);
161 // To allow replication, we first select the db to use and then run queries
162 // on this db.
163 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
164 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
165 $result = PMA_DBI_try_query($sql_query);
167 if ($result === true) {
168 // If comments were sent, enable relation stuff
169 require_once './libraries/relation.lib.php';
170 require_once './libraries/transformations.lib.php';
172 // Update comment table for mime types [MIME]
173 if (isset($_REQUEST['field_mimetype'])
174 && is_array($_REQUEST['field_mimetype'])
175 && $cfg['BrowseMIME']) {
176 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
177 if (isset($_REQUEST['field_name'][$fieldindex])
178 && strlen($_REQUEST['field_name'][$fieldindex])) {
179 PMA_setMIME($db, $table,
180 $_REQUEST['field_name'][$fieldindex],
181 $mimetype,
182 $_REQUEST['field_transformation'][$fieldindex],
183 $_REQUEST['field_transformation_options'][$fieldindex]);
188 // Go back to the structure sub-page
189 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
190 $message->addParam($table);
191 $active_page = 'tbl_structure.php';
192 require './tbl_structure.php';
193 } else {
194 PMA_mysqlDie('', '', '', $err_url, false);
195 // An error happened while inserting/updating a table definition.
196 // to prevent total loss of that data, we embed the form once again.
197 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
198 $num_fields = $_REQUEST['orig_num_fields'];
199 if (isset($_REQUEST['orig_after_field'])) {
200 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
202 if (isset($_REQUEST['orig_field_where'])) {
203 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
205 $regenerate = true;
207 } // end do alter table
210 * Displays the form used to define the new field
212 if ($abort == false) {
214 * Gets tables informations
216 require_once './libraries/tbl_common.php';
217 require_once './libraries/tbl_info.inc.php';
219 * Displays top menu links
221 $active_page = 'tbl_structure.php';
222 require_once './libraries/tbl_links.inc.php';
224 * Display the form
226 $action = 'tbl_addfield.php';
227 require_once './libraries/tbl_properties.inc.php';
229 // Diplays the footer
230 require_once './libraries/footer.inc.php';