Translated using Weblate (Spanish)
[phpmyadmin.git] / tbl_addfield.php
blob259581025b7029ea41047051339f330057fc0bf5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays add field form and handles it
6 * @package PhpMyAdmin
7 */
9 /**
10 * Get some core libraries
12 require_once 'libraries/common.inc.php';
14 $response = PMA_Response::getInstance();
15 $header = $response->getHeader();
16 $scripts = $header->getScripts();
17 $scripts->addFile('tbl_structure.js');
19 // Check parameters
20 PMA_Util::checkParameters(array('db', 'table'));
23 /**
24 * Defines the url to return to in case of error in a sql statement
26 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
28 /**
29 * The form used to define the field to add has been submitted
31 $abort = false;
33 // check number of fields to be created
34 if (isset($_REQUEST['submit_num_fields'])) {
35 if (isset($_REQUEST['orig_after_field'])) {
36 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
38 if (isset($_REQUEST['orig_field_where'])) {
39 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
41 $num_fields = $_REQUEST['orig_num_fields'] + $_REQUEST['added_fields'];
42 $regenerate = true;
43 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
44 $num_fields = (int) $_REQUEST['num_fields'];
45 } else {
46 $num_fields = 1;
49 if (isset($_REQUEST['do_save_data'])) {
50 //avoid an incorrect calling of PMA_updateColumns() via
51 //tbl_structure.php below
52 unset($_REQUEST['do_save_data']);
54 $query = '';
55 $definitions = array();
57 // Transforms the radio button field_key into 3 arrays
58 $field_cnt = count($_REQUEST['field_name']);
59 $field_primary = array();
60 $field_index = array();
61 $field_unique = array();
62 $field_fulltext = array();
63 for ($i = 0; $i < $field_cnt; ++$i) {
64 if (isset($_REQUEST['field_key'][$i])
65 && strlen($_REQUEST['field_name'][$i])
66 ) {
67 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
68 $field_primary[] = $i;
70 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
71 $field_index[] = $i;
73 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
74 $field_unique[] = $i;
76 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
77 $field_fulltext[] = $i;
79 } // end if
80 } // end for
82 // Builds the field creation statement and alters the table
83 for ($i = 0; $i < $field_cnt; ++$i) {
84 // '0' is also empty for php :-(
85 if (empty($_REQUEST['field_name'][$i])
86 && $_REQUEST['field_name'][$i] != '0'
87 ) {
88 continue;
91 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
92 $_REQUEST['field_name'][$i],
93 $_REQUEST['field_type'][$i],
94 $i,
95 $_REQUEST['field_length'][$i],
96 $_REQUEST['field_attribute'][$i],
97 isset($_REQUEST['field_collation'][$i])
98 ? $_REQUEST['field_collation'][$i]
99 : '',
100 isset($_REQUEST['field_null'][$i])
101 ? $_REQUEST['field_null'][$i]
102 : 'NOT NULL',
103 $_REQUEST['field_default_type'][$i],
104 $_REQUEST['field_default_value'][$i],
105 isset($_REQUEST['field_extra'][$i])
106 ? $_REQUEST['field_extra'][$i]
107 : false,
108 isset($_REQUEST['field_comments'][$i])
109 ? $_REQUEST['field_comments'][$i]
110 : '',
111 $field_primary
114 if ($_REQUEST['field_where'] != 'last') {
115 // Only the first field can be added somewhere other than at the end
116 if ($i == 0) {
117 if ($_REQUEST['field_where'] == 'first') {
118 $definition .= ' FIRST';
119 } else {
120 $definition .= ' AFTER '
121 . PMA_Util::backquote($_REQUEST['after_field']);
123 } else {
124 $definition .= ' AFTER '
125 . PMA_Util::backquote($_REQUEST['field_name'][$i-1]);
128 $definitions[] = $definition;
129 } // end for
131 // Builds the primary keys statements and updates the table
132 if (count($field_primary)) {
133 $fields = array();
134 foreach ($field_primary as $field_nr) {
135 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
137 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
138 unset($fields);
141 // Builds the indexes statements and updates the table
142 if (count($field_index)) {
143 $fields = array();
144 foreach ($field_index as $field_nr) {
145 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
147 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
148 unset($fields);
151 // Builds the uniques statements and updates the table
152 if (count($field_unique)) {
153 $fields = array();
154 foreach ($field_unique as $field_nr) {
155 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
157 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
158 unset($fields);
161 // Builds the fulltext statements and updates the table
162 if (count($field_fulltext)) {
163 $fields = array();
164 foreach ($field_fulltext as $field_nr) {
165 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
167 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
168 unset($fields);
171 // To allow replication, we first select the db to use and then run queries
172 // on this db.
173 PMA_DBI_selectDb($db)
174 or PMA_Util::mysqlDie(
175 PMA_DBI_getError(), 'USE ' . PMA_Util::backquote($db), '', $err_url
177 $sql_query = 'ALTER TABLE ' .
178 PMA_Util::backquote($table) . ' ' . implode(', ', $definitions) . ';';
179 $result = PMA_DBI_tryQuery($sql_query);
181 if ($result === true) {
182 // If comments were sent, enable relation stuff
183 include_once 'libraries/transformations.lib.php';
185 // Update comment table for mime types [MIME]
186 if (isset($_REQUEST['field_mimetype'])
187 && is_array($_REQUEST['field_mimetype'])
188 && $cfg['BrowseMIME']
190 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
191 if (isset($_REQUEST['field_name'][$fieldindex])
192 && strlen($_REQUEST['field_name'][$fieldindex])
194 PMA_setMIME(
195 $db, $table,
196 $_REQUEST['field_name'][$fieldindex],
197 $mimetype,
198 $_REQUEST['field_transformation'][$fieldindex],
199 $_REQUEST['field_transformation_options'][$fieldindex]
205 // Go back to the structure sub-page
206 $message = PMA_Message::success(
207 __('Table %1$s has been altered successfully')
209 $message->addParam($table);
211 if ($GLOBALS['is_ajax_request'] == true) {
212 $response->addJSON('message', $message);
213 $response->addJSON(
214 'sql_query',
215 PMA_Util::getMessage(null, $sql_query)
217 exit;
220 $active_page = 'tbl_structure.php';
221 $abort = true;
222 include 'tbl_structure.php';
223 } else {
224 $error_message_html = PMA_Util::mysqlDie('', '', '', $err_url, false);
225 $response->addHTML($error_message_html);
226 if ($GLOBALS['is_ajax_request'] == true) {
227 exit;
229 // An error happened while inserting/updating a table definition.
230 // to prevent total loss of that data, we embed the form once again.
231 // The variable $regenerate will be used to restore data in libraries/
232 // tbl_columns_definition_form.inc.php
233 $num_fields = $_REQUEST['orig_num_fields'];
234 if (isset($_REQUEST['orig_after_field'])) {
235 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
237 if (isset($_REQUEST['orig_field_where'])) {
238 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
240 $regenerate = true;
242 } // end do alter table
245 * Displays the form used to define the new field
247 if ($abort == false) {
249 * Gets tables informations
251 include_once 'libraries/tbl_common.inc.php';
252 include_once 'libraries/tbl_info.inc.php';
254 $active_page = 'tbl_structure.php';
256 * Display the form
258 $action = 'tbl_addfield.php';
259 include_once 'libraries/tbl_columns_definition_form.inc.php';