<?php $rownum = 0; $odd_row = true; ?>
<form method="post" action="tbl_structure.php" name="fieldsForm" id="fieldsForm" class="ajax<?php echo $HideStructureActions; ?>">
    <?php echo PMA_URL_getHiddenInputs($db, $table); ?>
    <input type="hidden" name="table_type" value=
    <?php if ($db_is_system_schema): ?>
        "information_schema" />
    <?php elseif ($tbl_is_view): ?>
        "view" />
    <?php else: ?>
        "table" />
    <?php endif; ?>
    <table id="tablestructure" class="data topmargin">
        <!-- table header -->
        <?php echo PMA\Template::get('table/structure/table_structure_header')->render(
            array(
                'db_is_system_schema' => $db_is_system_schema,
                'tbl_is_view' => $tbl_is_view
            )
        ); ?>
        <tbody>
        <!-- table body -->
        <?php foreach($fields as $row): ?>
            <?php
                $rownum++;
                $columns_list[] = $row['Field'];
                $extracted_columnspec = PMA_Util::extractColumnSpec($row['Type']);
                $attribute = $extracted_columnspec['attribute'];

                // prepare a common variable to reuse below; however,
                // in case of a VIEW, $create_table_fields is empty
                if (isset($create_table_fields[$row['Field']])) {
                    $tempField = $create_table_fields[$row['Field']];
                } else {
                    $tempField = array();
                }

                // MySQL 4.1.2+ TIMESTAMP options
                // (if on_update_current_timestamp is set, then it's TRUE)
                if (isset($tempField['on_update_current_timestamp'])) {
                    $attribute = 'on update CURRENT_TIMESTAMP';
                }

                // here, we have a TIMESTAMP that SHOW FULL COLUMNS reports as having the
                // NULL attribute, but SHOW CREATE TABLE says the contrary. Believe
                // the latter.
                if (! empty($tempField['type'])
                    && $tempField['type'] == 'TIMESTAMP'
                    && $tempField['timestamp_not_null']
                ) {
                    $row['Null'] = '';
                }

                if (! isset($row['Default'])) {
                    if ($row['Null'] == 'YES') {
                        $row['Default'] = '<i>NULL</i>';
                    }
                } else {
                    $row['Default'] = htmlspecialchars($row['Default']);
                }

                $field_name    = htmlspecialchars($row['Field']);
                $displayed_field_name = $field_name;

                // underline commented fields and display a hover-title (CSS only)

                if (isset($comments_map[$row['Field']])) {
                    $displayed_field_name = '<span class="commented_column" title="'
                        . htmlspecialchars($comments_map[$row['Field']]) . '">'
                        . $field_name . '</span>';
                }

                if ($primary && $primary->hasColumn($field_name)) {
                    $displayed_field_name .= PMA_Util::getImage(
                        'b_primary.png', __('Primary')
                    );
                }
                if (in_array($field_name, $columns_with_index)) {
                    $displayed_field_name .= PMA_Util::getImage(
                        'bd_primary.png', __('Index')
                    );
                }
            ?>
        <tr class="<?php echo ($odd_row ? 'odd': 'even'); ?>">
            <?php $odd_row = !$odd_row; ?>
            <?php echo PMA\Template::get('table/structure/table_structure_row')->render(
                array(
                    'row' => $row,
                    'rownum' => $rownum,
                    'displayed_field_name' => $displayed_field_name,
                    'type_nowrap' => PMA_Util::getClassForType($extracted_columnspec['type']),
                    'extracted_columnspec' => $extracted_columnspec,
                    'attribute' => $attribute,
                    'tbl_is_view' => $tbl_is_view,
                    'db_is_system_schema' => $db_is_system_schema,
                    'url_query' => $url_query,
                    'field_encoded' => urlencode($row['Field']),
                    'titles' => $titles,
                    'table' => $table,
                    'tbl_storage_engine' => $tbl_storage_engine
                )
            ); ?>
            <?php if (! $tbl_is_view && ! $db_is_system_schema): ?>
                <?php echo PMA\Template::get('table/structure/actions_in_table_structure')->render(
                    array(
                        'extracted_columnspec' => $extracted_columnspec,
                        'type' => (!empty($extracted_columnspec['print_type'])) ? $extracted_columnspec['print_type'] : ' ',
                        'tbl_storage_engine' => $tbl_storage_engine,
                        'primary' => $primary,
                        'field_name' => $field_name,
                        'url_query' => $url_query,
                        'titles' => $titles,
                        'row' => $row,
                        'rownum' => $rownum,
                        'columns_with_unique_index' => $columns_with_unique_index,
                        'isInCentralColumns' => in_array($row['Field'], $central_list) ? true : false
                    )
                ); ?>
            <?php endif; ?>
        </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php echo PMA\Template::get('table/structure/check_all_table_column')->render(
        array(
            'pmaThemeImage' => $GLOBALS['pmaThemeImage'],
            'text_dir' => $GLOBALS['text_dir'],
            'tbl_is_view' => $tbl_is_view,
            'db_is_system_schema' => $db_is_system_schema,
            'tbl_storage_engine' => $tbl_storage_engine
        )
    ); ?>
</form>
<hr class="print_ignore"/>
<?php echo PMA\Template::get('table/structure/move_columns_dialog')->render(); ?>
<!--Work on the table-->
<div id="structure-action-links">
    <?php if ($tbl_is_view): ?>
        <?php echo PMA_Util::linkOrButton(
            $edit_view_url,
            PMA_Util::getIcon('b_edit.png', __('Edit view'), true)
        ); ?>
    <?php endif; ?>
    <?php echo PMA\Template::get('table/structure/optional_action_links')->render(
        array(
            'url_query' => $url_query,
            'tbl_is_view' => $tbl_is_view,
            'db_is_system_schema' => $db_is_system_schema
        )
    ); ?>
</div>
<?php if (! $tbl_is_view && ! $db_is_system_schema): ?>
    <br />
    <?php echo PMA\Template::get('table/structure/add_column')->render(
        array('columns_list' => $columns_list)
    ); ?>
<?php endif; ?>

<!--Displays indexes-->
<?php if (! $tbl_is_view
    && ! $db_is_system_schema && 'ARCHIVE' !=  $tbl_storage_engine): ?>
    <?php echo PMA_getHtmlForDisplayIndexes(); ?>
<?php endif; ?>

<!--Display partition details-->
<?php
$partition_names = PMA_Partition::getPartitionNames($db, $table);
// detect partitioning
if (! is_null($partition_names[0])) {

    $partitions = PMA_Partition::getPartitions($db, $table);
    $firstPartition = $partitions[0];
    $rangeOrList = $firstPartition->getMethod() == 'RANGE'
        || $firstPartition->getMethod() == 'LIST';
    $subParitions = $firstPartition->getSubPartitions();
    $hasSubPartitions = $firstPartition->hasSubPartitions();
    if ($hasSubPartitions) {
        $firstSubPartition = $subParitions[0];
    }

    $actionIcons = array(
        'ANALYZE' => PMA_Util::getIcon('b_search.png', __('Analyze')),
        'CHECK' => PMA_Util::getIcon('eye.png', __('Check')),
        'OPTIMIZE' => PMA_Util::getIcon('normalize.png', __('Optimize')),
        'REBUILD' => PMA_Util::getIcon('s_tbl.png', __('Rebuild')),
        'REPAIR' => PMA_Util::getIcon('b_tblops.png', __('Repair')),
        'TRUNCATE' => PMA_Util::getIcon('b_empty.png', __('Truncate')),
    );
    if ($rangeOrList) {
        $actionIcons['DROP'] = PMA_Util::getIcon('b_drop.png', __('Drop'));
    }

    echo PMA_Util::getDivForSliderEffect(
        'partitions', __('Partitions')
    );

    $tmp_partition_description = $firstPartition->getDescription();

    echo PMA\Template::get('table/structure/display_partitions')->render(
        array(
            'table' => $table,
            'url_query'  => $url_query,
            'partitions' => $partitions,
            'partitionMethod' => $firstPartition->getMethod(),
            'partitionExpression' => $firstPartition->getExpression(),
            'hasDescription' => ! empty($tmp_partition_description),
            'hasSubPartitions' => $hasSubPartitions,
            'subPartitionMethod' => $hasSubPartitions ? $firstSubPartition->getMethod() : null,
            'subPartitionExpression' => $hasSubPartitions ? $firstSubPartition->getExpression() : null,
            'actionIcons' => $actionIcons,
            'rangeOrList' => $rangeOrList,
        )
    );
?>
<!-- For closing Slider effect div-->
</div>
<?php } ?>

<!--Displays Space usage and row statistics-->
<?php if ($GLOBALS['cfg']['ShowStats']): ?>
    <?php echo $tablestats;  ?>
<?php endif; ?>
<div class="clearfloat"></div>
