Merge remote branch 'origin/master'
[phpmyadmin/dkf.git] / tbl_indexes.php
blob1d05e2092b6adac463f372c6526e94732b3e5171
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays index edit/creation form and handles it
6 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * Gets some core libraries
13 require_once './libraries/common.inc.php';
14 require_once './libraries/Index.class.php';
15 require_once './libraries/tbl_common.php';
17 // Get fields and stores their name/type
18 $fields = array();
19 foreach (PMA_DBI_get_fields($db, $table) as $row) {
20 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
21 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
22 ',' . $tmp[2]), 1);
23 $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
24 } else {
25 $fields[$row['Field']] = $row['Type'];
27 } // end while
29 // Prepares the form values
30 if (isset($_REQUEST['index'])) {
31 if (is_array($_REQUEST['index'])) {
32 // coming already from form
33 $index = new PMA_Index($_REQUEST['index']);
34 } else {
35 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
37 } else {
38 $index = new PMA_Index;
41 /**
42 * Process the data from the edit/create index form,
43 * run the query to build the new index
44 * and moves back to "tbl_sql.php"
46 if (isset($_REQUEST['do_save_data'])) {
47 $error = false;
49 // $sql_query is the one displayed in the query box
50 $sql_query = 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table);
52 // Drops the old index
53 if (! empty($_REQUEST['old_index'])) {
54 if ($_REQUEST['old_index'] == 'PRIMARY') {
55 $sql_query .= ' DROP PRIMARY KEY,';
56 } else {
57 $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ',';
59 } // end if
61 // Builds the new one
62 switch ($index->getType()) {
63 case 'PRIMARY':
64 if ($index->getName() == '') {
65 $index->setName('PRIMARY');
66 } elseif ($index->getName() != 'PRIMARY') {
67 $error = PMA_Message::error(__('The name of the primary key must be "PRIMARY"!'));
69 $sql_query .= ' ADD PRIMARY KEY';
70 break;
71 case 'FULLTEXT':
72 case 'UNIQUE':
73 case 'INDEX':
74 if ($index->getName() == 'PRIMARY') {
75 $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
77 $sql_query .= ' ADD ' . $index->getType() . ' '
78 . ($index->getName() ? PMA_backquote($index->getName()) : '');
79 break;
80 } // end switch
82 $index_fields = array();
83 foreach ($index->getColumns() as $key => $column) {
84 $index_fields[$key] = PMA_backquote($column->getName());
85 if ($column->getSubPart()) {
86 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
88 } // end while
90 if (empty($index_fields)){
91 $error = PMA_Message::error(__('No index parts defined!'));
92 } else {
93 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
96 if (! $error) {
97 PMA_DBI_query($sql_query);
98 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
99 $message->addParam($table);
101 $active_page = 'tbl_structure.php';
102 require './tbl_structure.php';
103 exit;
104 } else {
105 $error->display();
107 } // end builds the new index
111 * Display the form to edit/create an index
114 // Displays headers (if needed)
115 $GLOBALS['js_include'][] = 'functions.js';
116 $GLOBALS['js_include'][] = 'indexes.js';
118 require_once './libraries/tbl_info.inc.php';
119 require_once './libraries/tbl_links.inc.php';
121 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
122 // coming already from form
123 $add_fields =
124 count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
125 if (isset($_REQUEST['add_fields'])) {
126 $add_fields += $_REQUEST['added_fields'];
128 } elseif (isset($_REQUEST['create_index'])) {
129 $add_fields = $_REQUEST['added_fields'];
130 } else {
131 $add_fields = 1;
134 // end preparing form values
137 <form action="./tbl_indexes.php" method="post" name="index_frm"
138 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {
139 this.elements['index'].disabled = false}">
140 <?php
141 $form_params = array(
142 'db' => $db,
143 'table' => $table,
146 if (isset($_REQUEST['create_index'])) {
147 $form_params['create_index'] = 1;
148 } elseif (isset($_REQUEST['old_index'])) {
149 $form_params['old_index'] = $_REQUEST['old_index'];
150 } elseif (isset($_REQUEST['index'])) {
151 $form_params['old_index'] = $_REQUEST['index'];
154 echo PMA_generate_common_hidden_inputs($form_params);
156 <fieldset>
157 <legend>
158 <?php
159 if (isset($_REQUEST['create_index'])) {
160 echo __('Create a new index');
161 } else {
162 echo __('Modify an index');
165 </legend>
167 <div class="formelement">
168 <label for="input_index_name"><?php echo __('Index name:'); ?></label>
169 <input type="text" name="index[Key_name]" id="input_index_name" size="25"
170 value="<?php echo htmlspecialchars($index->getName()); ?>" onfocus="this.select()" />
171 </div>
173 <div class="formelement">
174 <label for="select_index_type"><?php echo __('Index type:'); ?></label>
175 <select name="index[Index_type]" id="select_index_type" onchange="return checkIndexName()">
176 <?php echo $index->generateIndexSelector(); ?>
177 </select>
178 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
179 </div>
182 <br class="clearfloat" />
183 <?php
184 PMA_Message::warning(__('("PRIMARY" <b>must</b> be the name of and <b>only of</b> a primary key!)'))->display();
187 <table>
188 <thead>
189 <tr><th><?php echo __('Field'); ?></th>
190 <th><?php echo __('Size'); ?></th>
191 </tr>
192 </thead>
193 <tbody>
194 <?php
195 $odd_row = true;
196 foreach ($index->getColumns() as $column) {
198 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
199 <td><select name="index[columns][names][]">
200 <option value="">-- <?php echo __('Ignore'); ?> --</option>
201 <?php
202 foreach ($fields as $field_name => $field_type) {
203 if ($index->getType() != 'FULLTEXT'
204 || preg_match('/(char|text)/i', $field_type)) {
205 echo '<option value="' . htmlspecialchars($field_name) . '"'
206 . (($field_name == $column->getName()) ? ' selected="selected"' : '') . '>'
207 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
208 . '</option>' . "\n";
210 } // end foreach $fields
212 </select>
213 </td>
214 <td><input type="text" size="5" onfocus="this.select()"
215 name="index[columns][sub_parts][]" value="<?php echo $column->getSubPart(); ?>" />
216 </td>
217 </tr>
218 <?php
219 $odd_row = !$odd_row;
220 } // end foreach $edited_index_info['Sequences']
221 for ($i = 0; $i < $add_fields; $i++) {
223 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
224 <td><select name="index[columns][names][]">
225 <option value="">-- <?php echo __('Ignore'); ?> --</option>
226 <?php
227 foreach ($fields as $field_name => $field_type) {
228 echo '<option value="' . htmlspecialchars($field_name) . '">'
229 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
230 . '</option>' . "\n";
231 } // end foreach $fields
233 </select>
234 </td>
235 <td><input type="text" size="5" onfocus="this.select()"
236 name="index[columns][sub_parts][]" value="" />
237 </td>
238 </tr>
239 <?php
240 $odd_row = !$odd_row;
241 } // end foreach $edited_index_info['Sequences']
243 </tbody>
244 </table>
245 </fieldset>
247 <fieldset class="tblFooters">
248 <input type="submit" name="do_save_data" value="<?php echo __('Save'); ?>" />
249 <?php
250 echo __('Or') . ' ';
251 echo sprintf(__('Add to index &nbsp;%s&nbsp;column(s)'),
252 '<input type="text" name="added_fields" size="2" value="1"'
253 .' onfocus="this.select()" />') . "\n";
254 echo '<input type="submit" name="add_fields" value="' . __('Go') . '"'
255 .' onclick="return checkFormElementInRange(this.form,'
256 ." 'added_fields', '" . PMA_jsFormat(__('Column count has to be larger than zero.')) . "', 1"
257 .')" />' . "\n";
259 </fieldset>
260 </form>
261 <?php
264 * Displays the footer
266 require_once './libraries/footer.inc.php';