2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 * Get some core libraries
11 require_once 'libraries/common.inc.php';
13 require_once 'libraries/header.inc.php';
16 PMA_checkParameters(array('db', 'table'));
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);
25 * The form used to define the field to add has been submitted
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'];
39 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
40 $num_fields = (int) $_REQUEST['num_fields'];
45 if (isset($_REQUEST['do_save_data'])) {
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])
59 if ($_REQUEST['field_key'][$i] == 'primary_' . $i) {
60 $field_primary[] = $i;
62 if ($_REQUEST['field_key'][$i] == 'index_' . $i) {
65 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
68 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
69 $field_fulltext[] = $i;
74 // Builds the field creation statement and alters the table
75 for ($i = 0; $i < $field_cnt; ++
$i) {
76 // '0' is also empty for php :-(
77 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
81 $definition = ' ADD ' . PMA_Table
::generateFieldSpec(
82 $_REQUEST['field_name'][$i],
83 $_REQUEST['field_type'][$i],
84 $_REQUEST['field_length'][$i],
85 $_REQUEST['field_attribute'][$i],
86 isset($_REQUEST['field_collation'][$i])
87 ?
$_REQUEST['field_collation'][$i]
89 isset($_REQUEST['field_null'][$i])
90 ?
$_REQUEST['field_null'][$i]
92 $_REQUEST['field_default_type'][$i],
93 $_REQUEST['field_default_value'][$i],
94 isset($_REQUEST['field_extra'][$i])
95 ?
$_REQUEST['field_extra'][$i]
97 isset($_REQUEST['field_comments'][$i])
98 ?
$_REQUEST['field_comments'][$i]
104 if ($_REQUEST['field_where'] != 'last') {
105 // Only the first field can be added somewhere other than at the end
107 if ($_REQUEST['field_where'] == 'first') {
108 $definition .= ' FIRST';
110 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
113 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
116 $definitions[] = $definition;
119 // Builds the primary keys statements and updates the table
120 if (count($field_primary)) {
122 foreach ($field_primary as $field_nr) {
123 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
125 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
129 // Builds the indexes statements and updates the table
130 if (count($field_index)) {
132 foreach ($field_index as $field_nr) {
133 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
135 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
139 // Builds the uniques statements and updates the table
140 if (count($field_unique)) {
142 foreach ($field_unique as $field_nr) {
143 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
145 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
149 // Builds the fulltext statements and updates the table
150 if (count($field_fulltext)) {
152 foreach ($field_fulltext as $field_nr) {
153 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
155 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
159 // To allow replication, we first select the db to use and then run queries
161 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
162 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions) . ';';
163 $result = PMA_DBI_try_query($sql_query);
165 if ($result === true) {
166 // If comments were sent, enable relation stuff
167 include_once 'libraries/transformations.lib.php';
169 // Update comment table for mime types [MIME]
170 if (isset($_REQUEST['field_mimetype'])
171 && is_array($_REQUEST['field_mimetype'])
172 && $cfg['BrowseMIME']
174 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
175 if (isset($_REQUEST['field_name'][$fieldindex])
176 && strlen($_REQUEST['field_name'][$fieldindex])
180 $_REQUEST['field_name'][$fieldindex],
182 $_REQUEST['field_transformation'][$fieldindex],
183 $_REQUEST['field_transformation_options'][$fieldindex]
189 // Go back to the structure sub-page
190 $message = PMA_Message
::success(__('Table %1$s has been altered successfully'));
191 $message->addParam($table);
193 if ( $GLOBALS['is_ajax_request'] == true) {
194 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
195 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
198 $active_page = 'tbl_structure.php';
199 include 'tbl_structure.php';
201 PMA_mysqlDie('', '', '', $err_url, false);
202 // An error happened while inserting/updating a table definition.
203 // to prevent total loss of that data, we embed the form once again.
204 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
205 $num_fields = $_REQUEST['orig_num_fields'];
206 if (isset($_REQUEST['orig_after_field'])) {
207 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
209 if (isset($_REQUEST['orig_field_where'])) {
210 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
214 } // end do alter table
217 * Displays the form used to define the new field
219 if ($abort == false) {
221 * Gets tables informations
223 include_once 'libraries/tbl_common.php';
224 include_once 'libraries/tbl_info.inc.php';
226 * Displays top menu links
228 $active_page = 'tbl_structure.php';
229 if ($GLOBALS['is_ajax_request'] != true) {
230 include_once 'libraries/tbl_links.inc.php';
235 $action = 'tbl_addfield.php';
236 include_once 'libraries/tbl_properties.inc.php';
238 // Diplays the footer
239 include 'libraries/footer.inc.php';