PMA\libraries\Index::getHtmlForIndexes PHP Method

getHtmlForIndexes() public static method

Show index data
public static getHtmlForIndexes ( string $table, string $schema, boolean $print_mode = false ) : string
$table string The table name
$schema string The schema name
$print_mode boolean Whether the output is for the print mode
return string HTML for showing index
    public static function getHtmlForIndexes($table, $schema, $print_mode = false)
    {
        $indexes = Index::getFromTable($table, $schema);
        $no_indexes_class = count($indexes) > 0 ? ' hide' : '';
        $no_indexes = "<div class='no_indexes_defined{$no_indexes_class}'>";
        $no_indexes .= Message::notice(__('No index defined!'))->getDisplay();
        $no_indexes .= '</div>';
        if (!$print_mode) {
            $r = '<fieldset class="index_info">';
            $r .= '<legend id="index_header">' . __('Indexes');
            $r .= Util::showMySQLDocu('optimizing-database-structure');
            $r .= '</legend>';
            $r .= $no_indexes;
            if (count($indexes) < 1) {
                $r .= '</fieldset>';
                return $r;
            }
            $r .= Index::findDuplicates($table, $schema);
        } else {
            $r = '<h3>' . __('Indexes') . '</h3>';
            $r .= $no_indexes;
            if (count($indexes) < 1) {
                return $r;
            }
        }
        $r .= '<table id="table_index">';
        $r .= '<thead>';
        $r .= '<tr>';
        if (!$print_mode) {
            $r .= '<th colspan="2" class="print_ignore">' . __('Action') . '</th>';
        }
        $r .= '<th>' . __('Keyname') . '</th>';
        $r .= '<th>' . __('Type') . '</th>';
        $r .= '<th>' . __('Unique') . '</th>';
        $r .= '<th>' . __('Packed') . '</th>';
        $r .= '<th>' . __('Column') . '</th>';
        $r .= '<th>' . __('Cardinality') . '</th>';
        $r .= '<th>' . __('Collation') . '</th>';
        $r .= '<th>' . __('Null') . '</th>';
        $r .= '<th>' . __('Comment') . '</th>';
        $r .= '</tr>';
        $r .= '</thead>';
        $r .= '<tbody>';
        foreach ($indexes as $index) {
            $row_span = ' rowspan="' . $index->getColumnCount() . '" ';
            $r .= '<tr class="noclick" >';
            if (!$print_mode) {
                $this_params = $GLOBALS['url_params'];
                $this_params['index'] = $index->getName();
                $r .= '<td class="edit_index print_ignore';
                $r .= ' ajax';
                $r .= '" ' . $row_span . '>' . '    <a class="';
                $r .= 'ajax';
                $r .= '" href="tbl_indexes.php' . URL::getCommon($this_params) . '">' . Util::getIcon('b_edit.png', __('Edit')) . '</a>' . '</td>' . "\n";
                $this_params = $GLOBALS['url_params'];
                if ($index->getName() == 'PRIMARY') {
                    $this_params['sql_query'] = 'ALTER TABLE ' . Util::backquote($table) . ' DROP PRIMARY KEY;';
                    $this_params['message_to_show'] = __('The primary key has been dropped.');
                    $js_msg = Sanitize::jsFormat('ALTER TABLE ' . $table . ' DROP PRIMARY KEY');
                } else {
                    $this_params['sql_query'] = 'ALTER TABLE ' . Util::backquote($table) . ' DROP INDEX ' . Util::backquote($index->getName()) . ';';
                    $this_params['message_to_show'] = sprintf(__('Index %s has been dropped.'), htmlspecialchars($index->getName()));
                    $js_msg = Sanitize::jsFormat('ALTER TABLE ' . $table . ' DROP INDEX ' . $index->getName() . ';');
                }
                $r .= '<td ' . $row_span . ' class="print_ignore">';
                $r .= '<input type="hidden" class="drop_primary_key_index_msg"' . ' value="' . $js_msg . '" />';
                $r .= '    <a class="drop_primary_key_index_anchor';
                $r .= ' ajax';
                $r .= '" href="sql.php' . URL::getCommon($this_params) . '" >' . Util::getIcon('b_drop.png', __('Drop')) . '</a>' . '</td>' . "\n";
            }
            if (!$print_mode) {
                $r .= '<th ' . $row_span . '>' . htmlspecialchars($index->getName()) . '</th>';
            } else {
                $r .= '<td ' . $row_span . '>' . htmlspecialchars($index->getName()) . '</td>';
            }
            $r .= '<td ' . $row_span . '>';
            $type = $index->getType();
            if (!empty($type)) {
                $r .= htmlspecialchars($type);
            } else {
                $r .= htmlspecialchars($index->getChoice());
            }
            $r .= '</td>';
            $r .= '<td ' . $row_span . '>' . $index->isUnique(true) . '</td>';
            $r .= '<td ' . $row_span . '>' . $index->isPacked() . '</td>';
            foreach ($index->getColumns() as $column) {
                if ($column->getSeqInIndex() > 1) {
                    $r .= '<tr class="noclick" >';
                }
                $r .= '<td>' . htmlspecialchars($column->getName());
                if ($column->getSubPart()) {
                    $r .= ' (' . htmlspecialchars($column->getSubPart()) . ')';
                }
                $r .= '</td>';
                $r .= '<td>' . htmlspecialchars($column->getCardinality()) . '</td>';
                $r .= '<td>' . htmlspecialchars($column->getCollation()) . '</td>';
                $r .= '<td>' . htmlspecialchars($column->getNull(true)) . '</td>';
                if ($column->getSeqInIndex() == 1) {
                    $r .= '<td ' . $row_span . '>' . htmlspecialchars($index->getComments()) . '</td>';
                }
                $r .= '</tr>';
            }
            // end foreach $index['Sequences']
        }
        // end while
        $r .= '</tbody>';
        $r .= '</table>';
        if (!$print_mode) {
            $r .= '</fieldset>';
        }
        return $r;
    }

Usage Example

 /**
  * Process the data from the edit/create index form,
  * run the query to build the new index
  * and moves back to "tbl_sql.php"
  *
  * @return void
  */
 public function doSaveDataAction()
 {
     $error = false;
     $sql_query = $this->dbi->getTable($this->db, $this->table)->getSqlQueryForIndexCreateOrEdit($this->index, $error);
     // If there is a request for SQL previewing.
     if (isset($_REQUEST['preview_sql'])) {
         $this->response->addJSON('sql_data', Template::get('preview_sql')->render(array('query_data' => $sql_query)));
     } elseif (!$error) {
         $this->dbi->query($sql_query);
         if ($GLOBALS['is_ajax_request'] == true) {
             $message = Message::success(__('Table %1$s has been altered successfully.'));
             $message->addParam($this->table);
             $this->response->addJSON('message', Util::getMessage($message, $sql_query, 'success'));
             $this->response->addJSON('index_table', Index::getHtmlForIndexes($this->table, $this->db));
         } else {
             include 'tbl_structure.php';
         }
     } else {
         $this->response->setRequestStatus(false);
         $this->response->addJSON('message', $error);
     }
 }