Translated using Weblate (Polish)
[phpmyadmin.git] / tbl_addfield.php
blob3d5f08545aca791704b50b77207bdbc697efcdd0
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 $response = PMA_Response::getInstance();
14 $header = $response->getHeader();
15 $scripts = $header->getScripts();
16 $scripts->addFile('tbl_structure.js');
18 // Check parameters
19 PMA_Util::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 //avoid an incorrect calling of PMA_updateColumns() via
50 //tbl_structure.php below
51 unset($_REQUEST['do_save_data']);
53 $query = '';
54 $definitions = array();
56 // Transforms the radio button field_key into 3 arrays
57 $field_cnt = count($_REQUEST['field_name']);
58 $field_primary = array();
59 $field_index = array();
60 $field_unique = array();
61 $field_fulltext = array();
62 for ($i = 0; $i < $field_cnt; ++$i) {
63 if (isset($_REQUEST['field_key'][$i])
64 && strlen($_REQUEST['field_name'][$i])
65 ) {
66 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
67 $field_primary[] = $i;
69 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
70 $field_index[] = $i;
72 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
73 $field_unique[] = $i;
75 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
76 $field_fulltext[] = $i;
78 } // end if
79 } // end for
81 // Builds the field creation statement and alters the table
82 for ($i = 0; $i < $field_cnt; ++$i) {
83 // '0' is also empty for php :-(
84 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
85 continue;
88 $definition = ' ADD ' . PMA_Table::generateFieldSpec(
89 $_REQUEST['field_name'][$i],
90 $_REQUEST['field_type'][$i],
91 $i,
92 $_REQUEST['field_length'][$i],
93 $_REQUEST['field_attribute'][$i],
94 isset($_REQUEST['field_collation'][$i])
95 ? $_REQUEST['field_collation'][$i]
96 : '',
97 isset($_REQUEST['field_null'][$i])
98 ? $_REQUEST['field_null'][$i]
99 : 'NOT NULL',
100 $_REQUEST['field_default_type'][$i],
101 $_REQUEST['field_default_value'][$i],
102 isset($_REQUEST['field_extra'][$i])
103 ? $_REQUEST['field_extra'][$i]
104 : false,
105 isset($_REQUEST['field_comments'][$i])
106 ? $_REQUEST['field_comments'][$i]
107 : '',
108 $field_primary
111 if ($_REQUEST['field_where'] != 'last') {
112 // Only the first field can be added somewhere other than at the end
113 if ($i == 0) {
114 if ($_REQUEST['field_where'] == 'first') {
115 $definition .= ' FIRST';
116 } else {
117 $definition .= ' AFTER ' . PMA_Util::backquote($_REQUEST['after_field']);
119 } else {
120 $definition .= ' AFTER ' . PMA_Util::backquote($_REQUEST['field_name'][$i-1]);
123 $definitions[] = $definition;
124 } // end for
126 // Builds the primary keys statements and updates the table
127 if (count($field_primary)) {
128 $fields = array();
129 foreach ($field_primary as $field_nr) {
130 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
132 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
133 unset($fields);
136 // Builds the indexes statements and updates the table
137 if (count($field_index)) {
138 $fields = array();
139 foreach ($field_index as $field_nr) {
140 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
142 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
143 unset($fields);
146 // Builds the uniques statements and updates the table
147 if (count($field_unique)) {
148 $fields = array();
149 foreach ($field_unique as $field_nr) {
150 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
152 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
153 unset($fields);
156 // Builds the fulltext statements and updates the table
157 if (count($field_fulltext)) {
158 $fields = array();
159 foreach ($field_fulltext as $field_nr) {
160 $fields[] = PMA_Util::backquote($_REQUEST['field_name'][$field_nr]);
162 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
163 unset($fields);
166 // To allow replication, we first select the db to use and then run queries
167 // on this db.
168 PMA_DBI_select_db($db) or PMA_Util::mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_Util::backquote($db), '', $err_url);
169 $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($table) . ' ' . implode(', ', $definitions) . ';';
170 $result = PMA_DBI_try_query($sql_query);
172 if ($result === true) {
173 // If comments were sent, enable relation stuff
174 include_once 'libraries/transformations.lib.php';
176 // Update comment table for mime types [MIME]
177 if (isset($_REQUEST['field_mimetype'])
178 && is_array($_REQUEST['field_mimetype'])
179 && $cfg['BrowseMIME']
181 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
182 if (isset($_REQUEST['field_name'][$fieldindex])
183 && strlen($_REQUEST['field_name'][$fieldindex])
185 PMA_setMIME(
186 $db, $table,
187 $_REQUEST['field_name'][$fieldindex],
188 $mimetype,
189 $_REQUEST['field_transformation'][$fieldindex],
190 $_REQUEST['field_transformation_options'][$fieldindex]
196 // Go back to the structure sub-page
197 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
198 $message->addParam($table);
200 if ($GLOBALS['is_ajax_request'] == true) {
201 $response->addJSON('message', $message);
202 $response->addJSON(
203 'sql_query',
204 PMA_Util::getMessage(null, $sql_query)
206 exit;
209 $active_page = 'tbl_structure.php';
210 $abort = true;
211 include 'tbl_structure.php';
212 } else {
213 $error_message_html = PMA_Util::mysqlDie('', '', '', $err_url, false);
214 $response->addHTML($error_message_html);
215 if ($GLOBALS['is_ajax_request'] == true) {
216 exit;
218 // An error happened while inserting/updating a table definition.
219 // to prevent total loss of that data, we embed the form once again.
220 // The variable $regenerate will be used to restore data in libraries/tbl_columns_definition_form.inc.php
221 $num_fields = $_REQUEST['orig_num_fields'];
222 if (isset($_REQUEST['orig_after_field'])) {
223 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
225 if (isset($_REQUEST['orig_field_where'])) {
226 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
228 $regenerate = true;
230 } // end do alter table
233 * Displays the form used to define the new field
235 if ($abort == false) {
237 * Gets tables informations
239 include_once 'libraries/tbl_common.inc.php';
240 include_once 'libraries/tbl_info.inc.php';
242 $active_page = 'tbl_structure.php';
244 * Display the form
246 $action = 'tbl_addfield.php';
247 include_once 'libraries/tbl_columns_definition_form.inc.php';