Translation update done using Pootle.
[phpmyadmin.git] / tbl_addfield.php
blob55a980c95dbe105c6f0ea6c128e379c635da4bb3
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
8 /**
9 * Get some core libraries
11 require_once './libraries/common.inc.php';
13 require_once './libraries/header.inc.php';
15 // Check parameters
16 PMA_checkParameters(array('db', 'table'));
19 /**
20 * Defines the url to return to in case of error in a sql statement
22 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
24 /**
25 * The form used to define the field to add has been submitted
27 $abort = false;
29 // check number of fields to be created
30 if (isset($_REQUEST['submit_num_fields'])) {
31 if (isset($_REQUEST['orig_after_field'])) {
32 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
34 if (isset($_REQUEST['orig_field_where'])) {
35 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
37 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
38 $regenerate = true;
39 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
40 $num_fields = (int) $_REQUEST['num_fields'];
41 } else {
42 $num_fields = 1;
45 if (isset($_REQUEST['do_save_data'])) {
46 $query = '';
47 $definitions = array();
49 // Transforms the radio button field_key into 3 arrays
50 $field_cnt = count($_REQUEST['field_name']);
51 $field_primary = array();
52 $field_index = array();
53 $field_unique = array();
54 $field_fulltext = array();
55 for ($i = 0; $i < $field_cnt; ++$i) {
56 if (isset($_REQUEST['field_key'][$i])
57 && strlen($_REQUEST['field_name'][$i])) {
58 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
59 $field_primary[] = $i;
61 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
62 $field_index[] = $i;
64 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
65 $field_unique[] = $i;
67 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
68 $field_fulltext[] = $i;
70 } // end if
71 } // end for
73 // Builds the field creation statement and alters the table
74 for ($i = 0; $i < $field_cnt; ++$i) {
75 // '0' is also empty for php :-(
76 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
77 continue;
80 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
81 $_REQUEST['field_name'][$i],
82 $_REQUEST['field_type'][$i],
83 $_REQUEST['field_length'][$i],
84 $_REQUEST['field_attribute'][$i],
85 isset($_REQUEST['field_collation'][$i])
86 ? $_REQUEST['field_collation'][$i]
87 : '',
88 isset($_REQUEST['field_null'][$i])
89 ? $_REQUEST['field_null'][$i]
90 : 'NOT NULL',
91 $_REQUEST['field_default_type'][$i],
92 $_REQUEST['field_default_value'][$i],
93 isset($_REQUEST['field_extra'][$i])
94 ? $_REQUEST['field_extra'][$i]
95 : false,
96 isset($_REQUEST['field_comments'][$i])
97 ? $_REQUEST['field_comments'][$i]
98 : '',
99 $field_primary,
103 if ($_REQUEST['field_where'] != 'last') {
104 // Only the first field can be added somewhere other than at the end
105 if ($i == 0) {
106 if ($_REQUEST['field_where'] == 'first') {
107 $definition .= ' FIRST';
108 } else {
109 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
111 } else {
112 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
115 $definitions[] = $definition;
116 } // end for
118 // Builds the primary keys statements and updates the table
119 if (count($field_primary)) {
120 $fields = array();
121 foreach ($field_primary as $field_nr) {
122 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
124 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
125 unset($fields);
128 // Builds the indexes statements and updates the table
129 if (count($field_index)) {
130 $fields = array();
131 foreach ($field_index as $field_nr) {
132 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
134 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
135 unset($fields);
138 // Builds the uniques statements and updates the table
139 if (count($field_unique)) {
140 $fields = array();
141 foreach ($field_unique as $field_nr) {
142 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
144 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
145 unset($fields);
148 // Builds the fulltext statements and updates the table
149 if (count($field_fulltext)) {
150 $fields = array();
151 foreach ($field_fulltext as $field_nr) {
152 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
154 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
155 unset($fields);
158 // To allow replication, we first select the db to use and then run queries
159 // on this db.
160 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
161 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
162 $result = PMA_DBI_try_query($sql_query);
164 if ($result === true) {
165 // If comments were sent, enable relation stuff
166 include_once './libraries/transformations.lib.php';
168 // Update comment table for mime types [MIME]
169 if (isset($_REQUEST['field_mimetype'])
170 && is_array($_REQUEST['field_mimetype'])
171 && $cfg['BrowseMIME']) {
172 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
173 if (isset($_REQUEST['field_name'][$fieldindex])
174 && strlen($_REQUEST['field_name'][$fieldindex])) {
175 PMA_setMIME($db, $table,
176 $_REQUEST['field_name'][$fieldindex],
177 $mimetype,
178 $_REQUEST['field_transformation'][$fieldindex],
179 $_REQUEST['field_transformation_options'][$fieldindex]);
184 // Go back to the structure sub-page
185 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
186 $message->addParam($table);
188 if ( $GLOBALS['is_ajax_request'] == true) {
189 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
190 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
193 $active_page = 'tbl_structure.php';
194 include './tbl_structure.php';
195 } else {
196 PMA_mysqlDie('', '', '', $err_url, false);
197 // An error happened while inserting/updating a table definition.
198 // to prevent total loss of that data, we embed the form once again.
199 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
200 $num_fields = $_REQUEST['orig_num_fields'];
201 if (isset($_REQUEST['orig_after_field'])) {
202 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
204 if (isset($_REQUEST['orig_field_where'])) {
205 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
207 $regenerate = true;
209 } // end do alter table
212 * Displays the form used to define the new field
214 if ($abort == false) {
216 * Gets tables informations
218 include_once './libraries/tbl_common.php';
219 include_once './libraries/tbl_info.inc.php';
221 * Displays top menu links
223 $active_page = 'tbl_structure.php';
224 if ($GLOBALS['is_ajax_request'] != true) {
225 include_once './libraries/tbl_links.inc.php';
228 * Display the form
230 $action = 'tbl_addfield.php';
231 include_once './libraries/tbl_properties.inc.php';
233 // Diplays the footer
234 include './libraries/footer.inc.php';