Fix comments
[phpmyadmin.git] / tbl_indexes.php
blobe366047a2124ee84b4b3a3636dcc03255216bee2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays index edit/creation form and handles it
6 * @package phpMyAdmin
7 */
9 /**
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Index.class.php';
14 require_once './libraries/tbl_common.php';
16 // Get fields and stores their name/type
17 $fields = array();
18 foreach (PMA_DBI_get_columns_full($db, $table) as $row) {
19 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
20 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
21 ',' . $tmp[2]), 1);
22 $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
23 } else {
24 $fields[$row['Field']] = $row['Type'];
26 } // end while
28 // Prepares the form values
29 if (isset($_REQUEST['index'])) {
30 if (is_array($_REQUEST['index'])) {
31 // coming already from form
32 $index = new PMA_Index($_REQUEST['index']);
33 } else {
34 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
36 } else {
37 $index = new PMA_Index;
40 /**
41 * Process the data from the edit/create index form,
42 * run the query to build the new index
43 * and moves back to "tbl_sql.php"
45 if (isset($_REQUEST['do_save_data'])) {
46 $error = false;
48 // $sql_query is the one displayed in the query box
49 $sql_query = 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table);
51 // Drops the old index
52 if (! empty($_REQUEST['old_index'])) {
53 if ($_REQUEST['old_index'] == 'PRIMARY') {
54 $sql_query .= ' DROP PRIMARY KEY,';
55 } else {
56 $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ',';
58 } // end if
60 // Builds the new one
61 switch ($index->getType()) {
62 case 'PRIMARY':
63 if ($index->getName() == '') {
64 $index->setName('PRIMARY');
65 } elseif ($index->getName() != 'PRIMARY') {
66 $error = PMA_Message::error(__('The name of the primary key must be "PRIMARY"!'));
68 $sql_query .= ' ADD PRIMARY KEY';
69 break;
70 case 'FULLTEXT':
71 case 'UNIQUE':
72 case 'INDEX':
73 case 'SPATIAL':
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'][] = 'indexes.js';
117 require_once './libraries/tbl_info.inc.php';
118 require_once './libraries/tbl_links.inc.php';
120 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
121 // coming already from form
122 $add_fields =
123 count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
124 if (isset($_REQUEST['add_fields'])) {
125 $add_fields += $_REQUEST['added_fields'];
127 } elseif (isset($_REQUEST['create_index'])) {
128 $add_fields = $_REQUEST['added_fields'];
129 } else {
130 $add_fields = 1;
133 // end preparing form values
136 <form action="./tbl_indexes.php" method="post" name="index_frm"
137 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {
138 this.elements['index'].disabled = false}">
139 <?php
140 $form_params = array(
141 'db' => $db,
142 'table' => $table,
145 if (isset($_REQUEST['create_index'])) {
146 $form_params['create_index'] = 1;
147 } elseif (isset($_REQUEST['old_index'])) {
148 $form_params['old_index'] = $_REQUEST['old_index'];
149 } elseif (isset($_REQUEST['index'])) {
150 $form_params['old_index'] = $_REQUEST['index'];
153 echo PMA_generate_common_hidden_inputs($form_params);
155 <fieldset>
156 <legend>
157 <?php
158 if (isset($_REQUEST['create_index'])) {
159 echo __('Create a new index');
160 } else {
161 echo __('Modify an index');
164 </legend>
166 <div class="formelement">
167 <label for="input_index_name"><?php echo __('Index name:'); ?></label>
168 <input type="text" name="index[Key_name]" id="input_index_name" size="25"
169 value="<?php echo htmlspecialchars($index->getName()); ?>" onfocus="this.select()" />
170 </div>
172 <div class="formelement">
173 <label for="select_index_type"><?php echo __('Index type:'); ?></label>
174 <select name="index[Index_type]" id="select_index_type" onchange="return checkIndexName()">
175 <?php echo $index->generateIndexSelector(); ?>
176 </select>
177 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
178 </div>
181 <br class="clearfloat" />
182 <?php
183 PMA_Message::error(__('("PRIMARY" <b>must</b> be the name of and <b>only of</b> a primary key!)'))->display();
186 <table>
187 <thead>
188 <tr><th><?php echo __('Column'); ?></th>
189 <th><?php echo __('Size'); ?></th>
190 </tr>
191 </thead>
192 <tbody>
193 <?php
194 $odd_row = true;
195 $spatial_types = array(
196 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
197 'multilinestring', 'multipolygon', 'geomtrycollection'
199 foreach ($index->getColumns() as $column) {
201 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
202 <td><select name="index[columns][names][]">
203 <option value="">-- <?php echo __('Ignore'); ?> --</option>
204 <?php
205 foreach ($fields as $field_name => $field_type) {
206 if (($index->getType() != 'FULLTEXT' || preg_match('/(char|text)/i', $field_type))
207 && ($index->getType() != 'SPATIAL' || in_array($field_type, $spatial_types))
209 echo '<option value="' . htmlspecialchars($field_name) . '"'
210 . (($field_name == $column->getName()) ? ' selected="selected"' : '') . '>'
211 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
212 . '</option>' . "\n";
214 } // end foreach $fields
216 </select>
217 </td>
218 <td><input type="text" size="5" onfocus="this.select()"
219 name="index[columns][sub_parts][]"
220 value="<?php if ($index->getType() != 'SPATIAL') { echo $column->getSubPart(); } ?>" />
221 </td>
222 </tr>
223 <?php
224 $odd_row = !$odd_row;
225 } // end foreach $edited_index_info['Sequences']
226 for ($i = 0; $i < $add_fields; $i++) {
228 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
229 <td><select name="index[columns][names][]">
230 <option value="">-- <?php echo __('Ignore'); ?> --</option>
231 <?php
232 foreach ($fields as $field_name => $field_type) {
233 echo '<option value="' . htmlspecialchars($field_name) . '">'
234 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
235 . '</option>' . "\n";
236 } // end foreach $fields
238 </select>
239 </td>
240 <td><input type="text" size="5" onfocus="this.select()"
241 name="index[columns][sub_parts][]" value="" />
242 </td>
243 </tr>
244 <?php
245 $odd_row = !$odd_row;
246 } // end foreach $edited_index_info['Sequences']
248 </tbody>
249 </table>
250 </fieldset>
252 <fieldset class="tblFooters">
253 <input type="submit" name="do_save_data" value="<?php echo __('Save'); ?>" />
254 <span id="addMoreColumns">
255 <?php
256 echo __('Or') . ' ';
257 echo sprintf(__('Add to index &nbsp;%s&nbsp;column(s)'),
258 '<input type="text" name="added_fields" size="2" value="1"'
259 .' onfocus="this.select()" />') . "\n";
260 echo '<input type="submit" name="add_fields" value="' . __('Go') . '"'
261 .' onclick="return checkFormElementInRange(this.form,'
262 ." 'added_fields', '" . PMA_jsFormat(__('Column count has to be larger than zero.')) . "', 1"
263 .')" />' . "\n";
265 </span>
266 </fieldset>
267 </form>
268 <?php
271 * Displays the footer
273 require './libraries/footer.inc.php';