is*() methods now return boolean values;
[phpmyadmin.git] / tbl_addfield.php
blobe0a8122e321143b8d2bd127a3abb580de24b36fe
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 for ($i = 0; $i < $field_cnt; ++$i) {
58 if (isset($_REQUEST['field_key'][$i])
59 && strlen($_REQUEST['field_name'][$i])) {
60 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
61 $field_primary[] = $i;
63 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
64 $field_index[] = $i;
66 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
67 $field_unique[] = $i;
69 } // end if
70 } // end for
72 // Builds the field creation statement and alters the table
73 for ($i = 0; $i < $field_cnt; ++$i) {
74 // '0' is also empty for php :-(
75 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
76 continue;
79 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
80 $_REQUEST['field_name'][$i],
81 $_REQUEST['field_type'][$i],
82 $_REQUEST['field_length'][$i],
83 $_REQUEST['field_attribute'][$i],
84 isset($_REQUEST['field_collation'][$i])
85 ? $_REQUEST['field_collation'][$i]
86 : '',
87 isset($_REQUEST['field_null'][$i])
88 ? $_REQUEST['field_null'][$i]
89 : 'NOT NULL',
90 $_REQUEST['field_default_type'][$i],
91 $_REQUEST['field_default_value'][$i],
92 isset($_REQUEST['field_extra'][$i])
93 ? $_REQUEST['field_extra'][$i]
94 : false,
95 isset($_REQUEST['field_comments'][$i])
96 ? $_REQUEST['field_comments'][$i]
97 : '',
98 $field_primary,
102 if ($_REQUEST['field_where'] != 'last') {
103 // Only the first field can be added somewhere other than at the end
104 if ($i == 0) {
105 if ($_REQUEST['field_where'] == 'first') {
106 $definition .= ' FIRST';
107 } else {
108 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
110 } else {
111 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
114 $definitions[] = $definition;
115 } // end for
117 // Builds the primary keys statements and updates the table
118 if (count($field_primary)) {
119 $fields = array();
120 foreach ($field_primary as $field_nr) {
121 $fields[] = $_REQUEST['field_name'][$field_nr];
123 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
126 // Builds the indexes statements and updates the table
127 if (count($field_index)) {
128 $fields = array();
129 foreach ($field_index as $field_nr) {
130 $fields[] = $_REQUEST['field_name'][$field_nr];
132 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
135 // Builds the uniques statements and updates the table
136 if (count($field_unique)) {
137 $fields = array();
138 foreach ($field_unique as $field_nr) {
139 $fields[] = $_REQUEST['field_name'][$field_nr];
141 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
144 // Builds the fulltext statements and updates the table
145 if (isset($field_fulltext) && count($field_fulltext)) {
146 $fields = array();
147 foreach ($field_fulltext as $field_nr) {
148 $fields[] = $_REQUEST['field_name'][$field_nr];
150 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
153 // To allow replication, we first select the db to use and then run queries
154 // on this db.
155 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
156 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
157 $result = PMA_DBI_try_query($sql_query);
159 if ($result === true) {
160 // garvin: If comments were sent, enable relation stuff
161 require_once './libraries/relation.lib.php';
162 require_once './libraries/transformations.lib.php';
164 // garvin: Update comment table for mime types [MIME]
165 if (isset($_REQUEST['field_mimetype'])
166 && is_array($_REQUEST['field_mimetype'])
167 && $cfg['BrowseMIME']) {
168 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
169 if (isset($_REQUEST['field_name'][$fieldindex])
170 && strlen($_REQUEST['field_name'][$fieldindex])) {
171 PMA_setMIME($db, $table,
172 $_REQUEST['field_name'][$fieldindex],
173 $mimetype,
174 $_REQUEST['field_transformation'][$fieldindex],
175 $_REQUEST['field_transformation_options'][$fieldindex]);
180 // Go back to the structure sub-page
181 $message = PMA_Message::success('strTableAlteredSuccessfully');
182 $message->addParam($table);
183 $active_page = 'tbl_structure.php';
184 require './tbl_structure.php';
185 } else {
186 PMA_mysqlDie('', '', '', $err_url, false);
187 // garvin: An error happened while inserting/updating a table definition.
188 // to prevent total loss of that data, we embed the form once again.
189 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
190 $num_fields = $_REQUEST['orig_num_fields'];
191 if (isset($_REQUEST['orig_after_field'])) {
192 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
194 if (isset($_REQUEST['orig_field_where'])) {
195 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
197 $regenerate = true;
199 } // end do alter table
202 * Displays the form used to define the new field
204 if ($abort == false) {
206 * Gets tables informations
208 require_once './libraries/tbl_common.php';
209 require_once './libraries/tbl_info.inc.php';
211 * Displays top menu links
213 $active_page = 'tbl_structure.php';
214 require_once './libraries/tbl_links.inc.php';
216 * Display the form
218 $action = 'tbl_addfield.php';
219 require_once './libraries/tbl_properties.inc.php';
221 // Diplays the footer
222 require_once './libraries/footer.inc.php';