2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 * Get some core libraries
11 require_once './libraries/common.inc.php';
12 require_once './libraries/Table.class.php';
14 $GLOBALS['js_include'][] = 'functions.js';
15 require_once './libraries/header.inc.php';
18 PMA_checkParameters(array('db', 'table'));
22 * Defines the url to return to in case of error in a sql statement
24 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
27 * The form used to define the field to add has been submitted
31 // check number of fields to be created
32 if (isset($_REQUEST['submit_num_fields'])) {
33 if (isset($_REQUEST['orig_after_field'])) {
34 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
36 if (isset($_REQUEST['orig_field_where'])) {
37 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
39 $num_fields = $_REQUEST['orig_num_fields'] +
$_REQUEST['added_fields'];
41 } elseif (isset($_REQUEST['num_fields']) && intval($_REQUEST['num_fields']) > 0) {
42 $num_fields = (int) $_REQUEST['num_fields'];
47 if (isset($_REQUEST['do_save_data'])) {
49 $definitions = array();
51 // Transforms the radio button field_key into 3 arrays
52 $field_cnt = count($_REQUEST['field_name']);
53 $field_primary = array();
54 $field_index = array();
55 $field_unique = array();
56 $field_fulltext = 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) {
66 if ($_REQUEST['field_key'][$i] == 'unique_' . $i) {
69 if ($_REQUEST['field_key'][$i] == 'fulltext_' . $i) {
70 $field_fulltext[] = $i;
75 // Builds the field creation statement and alters the table
76 for ($i = 0; $i < $field_cnt; ++
$i) {
77 // '0' is also empty for php :-(
78 if (empty($_REQUEST['field_name'][$i]) && $_REQUEST['field_name'][$i] != '0') {
82 $definition = ' ADD ' . PMA_Table
::generateFieldSpec(
83 $_REQUEST['field_name'][$i],
84 $_REQUEST['field_type'][$i],
85 $_REQUEST['field_length'][$i],
86 $_REQUEST['field_attribute'][$i],
87 isset($_REQUEST['field_collation'][$i])
88 ?
$_REQUEST['field_collation'][$i]
90 isset($_REQUEST['field_null'][$i])
91 ?
$_REQUEST['field_null'][$i]
93 $_REQUEST['field_default_type'][$i],
94 $_REQUEST['field_default_value'][$i],
95 isset($_REQUEST['field_extra'][$i])
96 ?
$_REQUEST['field_extra'][$i]
98 isset($_REQUEST['field_comments'][$i])
99 ?
$_REQUEST['field_comments'][$i]
105 if ($_REQUEST['field_where'] != 'last') {
106 // Only the first field can be added somewhere other than at the end
108 if ($_REQUEST['field_where'] == 'first') {
109 $definition .= ' FIRST';
111 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['after_field']);
114 $definition .= ' AFTER ' . PMA_backquote($_REQUEST['field_name'][$i-1]);
117 $definitions[] = $definition;
120 // Builds the primary keys statements and updates the table
121 if (count($field_primary)) {
123 foreach ($field_primary as $field_nr) {
124 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
126 $definitions[] = ' ADD PRIMARY KEY (' . implode(', ', $fields) . ') ';
130 // Builds the indexes statements and updates the table
131 if (count($field_index)) {
133 foreach ($field_index as $field_nr) {
134 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
136 $definitions[] = ' ADD INDEX (' . implode(', ', $fields) . ') ';
140 // Builds the uniques statements and updates the table
141 if (count($field_unique)) {
143 foreach ($field_unique as $field_nr) {
144 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
146 $definitions[] = ' ADD UNIQUE (' . implode(', ', $fields) . ') ';
150 // Builds the fulltext statements and updates the table
151 if (count($field_fulltext)) {
153 foreach ($field_fulltext as $field_nr) {
154 $fields[] = PMA_backquote($_REQUEST['field_name'][$field_nr]);
156 $definitions[] = ' ADD FULLTEXT (' . implode(', ', $fields) . ') ';
160 // To allow replication, we first select the db to use and then run queries
162 PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_getError(), 'USE ' . PMA_backquotes($db), '', $err_url);
163 $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' ' . implode(', ', $definitions);
164 $result = PMA_DBI_try_query($sql_query);
166 if ($result === true) {
167 // If comments were sent, enable relation stuff
168 require_once './libraries/relation.lib.php';
169 require_once './libraries/transformations.lib.php';
171 // Update comment table for mime types [MIME]
172 if (isset($_REQUEST['field_mimetype'])
173 && is_array($_REQUEST['field_mimetype'])
174 && $cfg['BrowseMIME']) {
175 foreach ($_REQUEST['field_mimetype'] as $fieldindex => $mimetype) {
176 if (isset($_REQUEST['field_name'][$fieldindex])
177 && strlen($_REQUEST['field_name'][$fieldindex])) {
178 PMA_setMIME($db, $table,
179 $_REQUEST['field_name'][$fieldindex],
181 $_REQUEST['field_transformation'][$fieldindex],
182 $_REQUEST['field_transformation_options'][$fieldindex]);
187 // Go back to the structure sub-page
188 $message = PMA_Message
::success(__('Table %1$s has been altered successfully'));
189 $message->addParam($table);
190 $active_page = 'tbl_structure.php';
191 require './tbl_structure.php';
193 PMA_mysqlDie('', '', '', $err_url, false);
194 // An error happened while inserting/updating a table definition.
195 // to prevent total loss of that data, we embed the form once again.
196 // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
197 $num_fields = $_REQUEST['orig_num_fields'];
198 if (isset($_REQUEST['orig_after_field'])) {
199 $_REQUEST['after_field'] = $_REQUEST['orig_after_field'];
201 if (isset($_REQUEST['orig_field_where'])) {
202 $_REQUEST['field_where'] = $_REQUEST['orig_field_where'];
206 } // end do alter table
209 * Displays the form used to define the new field
211 if ($abort == false) {
213 * Gets tables informations
215 require_once './libraries/tbl_common.php';
216 require_once './libraries/tbl_info.inc.php';
218 * Displays top menu links
220 $active_page = 'tbl_structure.php';
221 require_once './libraries/tbl_links.inc.php';
225 $action = 'tbl_addfield.php';
226 require_once './libraries/tbl_properties.inc.php';
228 // Diplays the footer
229 require_once './libraries/footer.inc.php';