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.inc.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)) {
21 preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
23 $fields[$row['Field']] = $tmp[1] . '('
24 . str_replace(',', ', ', $tmp[2]) . ')';
26 $fields[$row['Field']] = $row['Type'];
30 // Prepares the form values
31 if (isset($_REQUEST['index'])) {
32 if (is_array($_REQUEST['index'])) {
33 // coming already from form
34 $index = new PMA_Index($_REQUEST['index']);
36 $index = PMA_Index
::singleton($db, $table, $_REQUEST['index']);
39 $index = new PMA_Index
;
43 * Process the data from the edit/create index form,
44 * run the query to build the new index
45 * and moves back to "tbl_sql.php"
47 if (isset($_REQUEST['do_save_data'])) {
50 // $sql_query is the one displayed in the query box
51 $sql_query = 'ALTER TABLE ' . PMA_Util
::backquote($db)
52 . '.' . PMA_Util
::backquote($table);
54 // Drops the old index
55 if (! empty($_REQUEST['old_index'])) {
56 if ($_REQUEST['old_index'] == 'PRIMARY') {
57 $sql_query .= ' DROP PRIMARY KEY,';
59 $sql_query .= ' DROP INDEX '
60 . PMA_Util
::backquote($_REQUEST['old_index']) . ',';
65 switch ($index->getType()) {
67 if ($index->getName() == '') {
68 $index->setName('PRIMARY');
69 } elseif ($index->getName() != 'PRIMARY') {
70 $error = PMA_Message
::error(
71 __('The name of the primary key must be "PRIMARY"!')
74 $sql_query .= ' ADD PRIMARY KEY';
80 if ($index->getName() == 'PRIMARY') {
81 $error = PMA_Message
::error(__('Can\'t rename index to PRIMARY!'));
83 $sql_query .= ' ADD ' . $index->getType() . ' '
84 . ($index->getName() ? PMA_Util
::backquote($index->getName()) : '');
88 $index_fields = array();
89 foreach ($index->getColumns() as $key => $column) {
90 $index_fields[$key] = PMA_Util
::backquote($column->getName());
91 if ($column->getSubPart()) {
92 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
96 if (empty($index_fields)) {
97 $error = PMA_Message
::error(__('No index parts defined!'));
99 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
102 if (PMA_MYSQL_INT_VERSION
> 50500) {
103 $sql_query .= "COMMENT '"
104 . PMA_Util
::sqlAddSlashes($index->getComment())
110 PMA_DBI_query($sql_query);
111 $message = PMA_Message
::success(
112 __('Table %1$s has been altered successfully')
114 $message->addParam($table);
116 if ($GLOBALS['is_ajax_request'] == true) {
117 $response = PMA_Response
::getInstance();
118 $response->addJSON('message', $message);
119 $response->addJSON('index_table', PMA_Index
::getView($table, $db));
122 PMA_Util
::getMessage(null, $sql_query)
125 $active_page = 'tbl_structure.php';
126 include 'tbl_structure.php';
130 if ($GLOBALS['is_ajax_request'] == true) {
131 $response = PMA_Response
::getInstance();
132 $response->isSuccess(false);
133 $response->addJSON('message', $error);
139 } // end builds the new index
143 * Display the form to edit/create an index
146 // Displays headers (if needed)
147 $response = PMA_Response
::getInstance();
148 $header = $response->getHeader();
149 $scripts = $header->getScripts();
150 $scripts->addFile('indexes.js');
151 require_once 'libraries/tbl_info.inc.php';
153 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
154 // coming already from form
156 = count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
157 if (isset($_REQUEST['add_fields'])) {
158 $add_fields +
= $_REQUEST['added_fields'];
160 } elseif (isset($_REQUEST['create_index'])) {
161 $add_fields = $_REQUEST['added_fields'];
166 // end preparing form values
169 <form action
="tbl_indexes.php" method
="post" name
="index_frm" id
="index_frm" class="ajax"
170 onsubmit
="if (typeof(this.elements['index[Key_name]'].disabled) != 'undefined') {
171 this.elements['index[Key_name]'].disabled = false}">
173 $form_params = array(
178 if (isset($_REQUEST['create_index'])) {
179 $form_params['create_index'] = 1;
180 } elseif (isset($_REQUEST['old_index'])) {
181 $form_params['old_index'] = $_REQUEST['old_index'];
182 } elseif (isset($_REQUEST['index'])) {
183 $form_params['old_index'] = $_REQUEST['index'];
186 echo PMA_generate_common_hidden_inputs($form_params);
188 <fieldset id
="index_edit_fields">
190 if ($GLOBALS['is_ajax_request'] != true) {
194 if (isset($_REQUEST['create_index'])) {
195 echo __('Add index');
197 echo __('Edit index');
204 <div
class='index_info'>
208 <label
for="input_index_name">
209 <?php
echo __('Index name:'); ?
>
211 echo PMA_Util
::showHint(
214 '("PRIMARY" <b>must</b> be the name of'
215 . ' and <b>only of</b> a primary key!)'
223 <input type
="text" name
="index[Key_name]" id
="input_index_name" size
="25"
224 value
="<?php echo htmlspecialchars($index->getName()); ?>"
225 onfocus
="this.select()" />
228 if (PMA_MYSQL_INT_VERSION
> 50500) {
233 <label
for="input_index_comment">
234 <?php
echo __('Comment:'); ?
>
238 <input type
="text" name
="index[Index_comment]" id
="input_index_comment" size
="30"
239 value
="<?php echo htmlspecialchars($index->getComment()); ?>"
240 onfocus
="this.select()" />
248 <label
for="select_index_type">
249 <?php
echo __('Index type:'); ?
>
250 <?php
echo PMA_Util
::showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?
>
254 <select name
="index[Index_type]" id
="select_index_type" >
255 <?php
echo $index->generateIndexSelector(); ?
>
258 <div
class="clearfloat"></div
>
261 <table id
="index_columns">
263 <tr
><th
><?php
echo __('Column'); ?
></th
>
264 <th
><?php
echo __('Size'); ?
></th
>
270 $spatial_types = array(
271 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
272 'multilinestring', 'multipolygon', 'geomtrycollection'
274 foreach ($index->getColumns() as $column) {
276 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
278 <select name
="index[columns][names][]">
279 <option value
="">-- <?php
echo __('Ignore'); ?
> --</option
>
281 foreach ($fields as $field_name => $field_type) {
282 if (($index->getType() != 'FULLTEXT'
283 ||
preg_match('/(char|text)/i', $field_type))
284 && ($index->getType() != 'SPATIAL'
285 ||
in_array($field_type, $spatial_types))
287 echo '<option value="' . htmlspecialchars($field_name) . '"'
288 . (($field_name == $column->getName())
289 ?
' selected="selected"'
291 . htmlspecialchars($field_name) . ' ['
292 . htmlspecialchars($field_type) . ']'
293 . '</option>' . "\n";
295 } // end foreach $fields
300 <input type
="text" size
="5" onfocus
="this.select()"
301 name
="index[columns][sub_parts][]"
303 if ($index->getType() != 'SPATIAL') {
304 echo $column->getSubPart();
310 $odd_row = !$odd_row;
311 } // end foreach $edited_index_info['Sequences']
312 for ($i = 0; $i < $add_fields; $i++
) {
314 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
316 <select name
="index[columns][names][]">
317 <option value
="">-- <?php
echo __('Ignore'); ?
> --</option
>
319 foreach ($fields as $field_name => $field_type) {
320 echo '<option value="' . htmlspecialchars($field_name) . '">'
321 . htmlspecialchars($field_name) . ' ['
322 . htmlspecialchars($field_type) . ']'
323 . '</option>' . "\n";
324 } // end foreach $fields
329 <input type
="text" size
="5" onfocus
="this.select()"
330 name
="index[columns][sub_parts][]" value
="" />
334 $odd_row = !$odd_row;
335 } // end foreach $edited_index_info['Sequences']
340 <fieldset
class="tblFooters">
342 if ($GLOBALS['is_ajax_request'] != true ||
! empty($_REQUEST['ajax_page_request'])) {
344 <input type
="submit" name
="do_save_data" value
="<?php echo __('Save'); ?>" />
345 <span id
="addMoreColumns">
349 __('Add %s column(s) to index') . "\n",
350 '<input type="text" name="added_fields" size="2" value="1" />'
352 echo '<input type="submit" name="add_fields" value="' . __('Go') . '" />' . "\n";
357 $btn_value = sprintf(__('Add %s column(s) to index'), 1);
358 echo '<div class="slider"></div>';
359 echo '<div class="add_fields">';
360 echo '<input type="submit" value="' . $btn_value . '" />';