2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Displays index edit/creation form and handles it
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
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\\\'',
22 $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
24 $fields[$row['Field']] = $row['Type'];
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']);
34 $index = PMA_Index
::singleton($db, $table, $_REQUEST['index']);
37 $index = new PMA_Index
;
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'])) {
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,';
56 $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ',';
61 switch ($index->getType()) {
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';
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()) : '');
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() . ')';
90 if (empty($index_fields)){
91 $error = PMA_Message
::error(__('No index parts defined!'));
93 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
97 PMA_DBI_query($sql_query);
98 $message = PMA_Message
::success(__('Table %1$s has been altered successfully'));
99 $message->addParam($table);
101 if( $GLOBALS['is_ajax_request'] == true) {
102 $extra_data['index_table'] = PMA_Index
::getView($table, $db);
103 $extra_data['sql_query'] = PMA_showMessage(NULL, $sql_query);
104 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
107 $active_page = 'tbl_structure.php';
108 require './tbl_structure.php';
113 } // end builds the new index
117 * Display the form to edit/create an index
120 // Displays headers (if needed)
121 $GLOBALS['js_include'][] = 'indexes.js';
123 require_once './libraries/tbl_info.inc.php';
124 require_once './libraries/tbl_links.inc.php';
126 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
127 // coming already from form
129 count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
130 if (isset($_REQUEST['add_fields'])) {
131 $add_fields +
= $_REQUEST['added_fields'];
133 } elseif (isset($_REQUEST['create_index'])) {
134 $add_fields = $_REQUEST['added_fields'];
139 // end preparing form values
142 <form action
="./tbl_indexes.php" method
="post" name
="index_frm" id
="index_frm" <?php
echo ($GLOBALS['cfg']['AjaxEnable'] ?
' class="ajax"' : ''); ?
>
143 onsubmit
="if (typeof(this.elements['index[Key_name]'].disabled) != 'undefined') {
144 this.elements['index[Key_name]'].disabled = false}">
146 $form_params = array(
151 if (isset($_REQUEST['create_index'])) {
152 $form_params['create_index'] = 1;
153 } elseif (isset($_REQUEST['old_index'])) {
154 $form_params['old_index'] = $_REQUEST['old_index'];
155 } elseif (isset($_REQUEST['index'])) {
156 $form_params['old_index'] = $_REQUEST['index'];
159 echo PMA_generate_common_hidden_inputs($form_params);
164 if (isset($_REQUEST['create_index'])) {
165 echo __('Create a new index');
167 echo __('Modify an index');
172 PMA_Message
::notice(__('("PRIMARY" <b>must</b> be the name of and <b>only of</b> a primary key!)'))->display();
174 <div
class="formelement">
175 <label
for="input_index_name"><?php
echo __('Index name:'); ?
></label
>
176 <input type
="text" name
="index[Key_name]" id
="input_index_name" size
="25"
177 value
="<?php echo htmlspecialchars($index->getName()); ?>" onfocus
="this.select()" />
180 <div
class="formelement">
181 <label
for="select_index_type"><?php
echo __('Index type:'); ?
></label
>
182 <select name
="index[Index_type]" id
="select_index_type" onchange
="return checkIndexName()">
183 <?php
echo $index->generateIndexSelector(); ?
>
185 <?php
echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?
>
188 <br
class="clearfloat" /><br
/>
190 <table id
="index_columns">
192 <tr
><th
><?php
echo __('Column'); ?
></th
>
193 <th
><?php
echo __('Size'); ?
></th
>
199 $spatial_types = array(
200 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
201 'multilinestring', 'multipolygon', 'geomtrycollection'
203 foreach ($index->getColumns() as $column) {
205 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
206 <td
><select name
="index[columns][names][]">
207 <option value
="">-- <?php
echo __('Ignore'); ?
> --</option
>
209 foreach ($fields as $field_name => $field_type) {
210 if (($index->getType() != 'FULLTEXT' ||
preg_match('/(char|text)/i', $field_type))
211 && ($index->getType() != 'SPATIAL' ||
in_array($field_type, $spatial_types))
213 echo '<option value="' . htmlspecialchars($field_name) . '"'
214 . (($field_name == $column->getName()) ?
' selected="selected"' : '') . '>'
215 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
216 . '</option>' . "\n";
218 } // end foreach $fields
222 <td
><input type
="text" size
="5" onfocus
="this.select()"
223 name
="index[columns][sub_parts][]"
224 value
="<?php if ($index->getType() != 'SPATIAL') { echo $column->getSubPart(); } ?>" />
228 $odd_row = !$odd_row;
229 } // end foreach $edited_index_info['Sequences']
230 for ($i = 0; $i < $add_fields; $i++
) {
232 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
233 <td
><select name
="index[columns][names][]">
234 <option value
="">-- <?php
echo __('Ignore'); ?
> --</option
>
236 foreach ($fields as $field_name => $field_type) {
237 echo '<option value="' . htmlspecialchars($field_name) . '">'
238 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
239 . '</option>' . "\n";
240 } // end foreach $fields
244 <td
><input type
="text" size
="5" onfocus
="this.select()"
245 name
="index[columns][sub_parts][]" value
="" />
249 $odd_row = !$odd_row;
250 } // end foreach $edited_index_info['Sequences']
256 <fieldset
class="tblFooters">
257 <input type
="submit" name
="do_save_data" value
="<?php echo __('Save'); ?>" />
258 <span id
="addMoreColumns">
261 echo sprintf(__('Add to index %s column(s)'),
262 '<input type="text" name="added_fields" size="2" value="1"'
263 .' onfocus="this.select()" />') . "\n";
264 echo '<input type="submit" name="add_fields" value="' . __('Go') . '"'
265 .' onclick="return checkFormElementInRange(this.form,'
266 ." 'added_fields', '" . PMA_jsFormat(__('Column count has to be larger than zero.')) . "', 1"
275 * Displays the footer
277 require './libraries/footer.inc.php';