<?php
$js_msg = '';
$this_params = null;
if (isset($one_key['constraint'])) {
    $drop_fk_query = 'ALTER TABLE ' . PMA_Util::backquote($GLOBALS['table'])
        . ' DROP FOREIGN KEY '
        . PMA_Util::backquote($one_key['constraint']) . ';';
    $this_params = $GLOBALS['url_params'];
    $this_params['goto'] = 'tbl_relation.php';
    $this_params['back'] = 'tbl_relation.php';
    $this_params['sql_query'] = $drop_fk_query;
    $this_params['message_to_show'] = sprintf(
        __('Foreign key constraint %s has been dropped'),
        $one_key['constraint']
    );
    $js_msg = PMA_jsFormat(
        'ALTER TABLE ' . $GLOBALS['table']
        . ' DROP FOREIGN KEY '
        . $one_key['constraint'] . ';'
    );
}

// For ON DELETE and ON UPDATE, the default action
// is RESTRICT as per MySQL doc; however, a SHOW CREATE TABLE
// won't display the clause if it's set as RESTRICT.
$on_delete = isset($one_key['on_delete'])
    ? $one_key['on_delete'] : 'RESTRICT';
$on_update = isset($one_key['on_update'])
    ? $one_key['on_update'] : 'RESTRICT';

$column_array = array();
$column_array[''] = '';
foreach ($columns as $column) {
    if (! empty($column['Key'])) {
        $column_array[$column['Field']] = $column['Field'];
    }
}

$foreign_table = false;
// foreign database dropdown
$foreign_db = (isset($one_key['ref_db_name'])) ? $one_key['ref_db_name'] : $db;

$tables = array();
if ($foreign_db) {
    $foreign_table = isset($one_key['ref_table_name'])
        ? $one_key['ref_table_name'] : '';

    // In Drizzle, 'SHOW TABLE STATUS' will show status only for the tables
    // which are currently in the table cache. Hence we have to use
    // 'SHOW TABLES' and manualy retrieve table engine values.
    if (PMA_DRIZZLE) {
        $tables_rs = $GLOBALS['dbi']->query(
            'SHOW TABLES FROM ' . PMA_Util::backquote($foreign_db),
            null,
            PMA_DatabaseInterface::QUERY_STORE
        );
        while ($row = $GLOBALS['dbi']->fetchArray($tables_rs)) {
            $engine = $GLOBALS['dbi']->getTable(
                $foreign_db,
                $row[0]
            )->getStatusInfo('Engine');
            if (isset($engine)
                && /*overload*/mb_strtoupper($engine) == $tbl_storage_engine
            ) {
                $tables[] = $row[0];
            }
        }
    } else {
        $tables_rs = $GLOBALS['dbi']->query(
            'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($foreign_db),
            null,
            PMA_DatabaseInterface::QUERY_STORE
        );
        while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
            if (isset($row[1])
                && /*overload*/mb_strtoupper($row[1]) == $tbl_storage_engine
            ) {
                $tables[] = $row[0];
            }
        }
    }
}
?>
<tr class="<?php echo ($odd_row ? 'odd' : 'even'); ?>">
    <!-- Drop key anchor -->
    <td>
        <?php if (isset($one_key['constraint'])): ?>
            <input type="hidden"
                   class="drop_foreign_key_msg"
                   value="<?php echo $js_msg; ?>" />
            <a class="drop_foreign_key_anchor ajax"
               href="sql.php<?php echo PMA_URL_getCommon($this_params); ?>" >
                <?php echo PMA_Util::getIcon('b_drop.png', __('Drop')); ?>
            </a>
        <?php endif; ?>
    </td>
    <td>
        <span class="formelement clearfloat">
            <input type="text" name="constraint_name[<?php echo $i; ?>]"
                   value="<?php if (isset($one_key['constraint']))
                       echo htmlspecialchars($one_key['constraint']); ?>"
                   placeholder="<?php echo __('Constraint name'); ?>"
                   maxlength="64" />
        </span>
        <div class="floatleft">
            <span class="formelement">
                <?php echo PMA\Template::get('table/relation/dropdown_generate')->render(
                    array(
                        'dropdown_question' => 'ON DELETE',
                        'select_name' => 'on_delete[' . $i . ']',
                        'choices' => $options_array,
                        'selected_value' => $on_delete
                    )
                ); ?>
            </span>
            <span class="formelement">
                <?php echo PMA\Template::get('table/relation/dropdown_generate')->render(
                    array(
                        'dropdown_question' => 'ON UPDATE',
                        'select_name' => 'on_update[' . $i . ']',
                        'choices' => $options_array,
                        'selected_value' => $on_update
                    )
                ); ?>
            </span>
        </div>
    </td>
    <td>
        <?php if (isset($one_key['index_list'])): ?>
            <?php foreach ($one_key['index_list'] as $key => $column): ?>
                <span class="formelement clearfloat">
                    <?php echo PMA\Template::get('table/relation/dropdown_generate')->render(
                        array(
                            'dropdown_question' => '',
                            'select_name' => 'foreign_key_fields_name[' . $i . '][]',
                            'choices' => $column_array,
                            'selected_value' => $column
                        )
                    ); ?>
                </span>
            <?php endforeach; ?>
        <?php else: ?>
            <span class="formelement clearfloat">
                <?php echo PMA\Template::get('table/relation/dropdown_generate')->render(
                    array(
                        'dropdown_question' => '',
                        'select_name' => 'foreign_key_fields_name[' . $i . '][]',
                        'choices' => $column_array,
                        'selected_value' => ''
                    )
                ); ?>
            </span>
        <?php endif; ?>
        <a class="formelement clearfloat add_foreign_key_field"
           href=""
           data-index="<?php echo $i; ?>">
            <?php echo __('+ Add column'); ?>
        </a>
    </td>
    <td>
        <span class="formelement clearfloat">
            <?php echo PMA\Template::get('table/relation/relational_dropdown')->render(
                array(
                    'name' => 'destination_foreign_db[' . $i . ']',
                    'title' => __('Database'),
                    'values' => $GLOBALS['pma']->databases,
                    'foreign' => $foreign_db
                )
            ); ?>
    </td>
    <td>
        <span class="formelement clearfloat">
            <?php echo PMA\Template::get('table/relation/relational_dropdown')->render(
                array(
                    'name' => 'destination_foreign_table[' . $i . ']',
                    'title' => __('Table'),
                    'values' => $tables,
                    'foreign' => $foreign_table
                )
            ); ?>
        </span>
    </td>
    <td>
        <?php if ($foreign_db && $foreign_table): ?>
            <?php foreach ($one_key['ref_index_list'] as $foreign_column): ?>
                <?php
                $table_obj = new PMA_Table($foreign_table, $foreign_db);
                $columns = $table_obj->getUniqueColumns(false, false);
                ?>
                <span class="formelement clearfloat">
                    <?php echo PMA\Template::get('table/relation/relational_dropdown')->render(
                        array(
                            'name' => 'destination_foreign_column[' . $i . '][]',
                            'title' => __('Column'),
                            'values' => $columns,
                            'foreign' => $foreign_column
                        )
                    ); ?>
                </span>
            <?php endforeach; ?>
        <?php else: ?>
            <span class="formelement clearfloat">
                <?php echo PMA\Template::get('table/relation/relational_dropdown')->render(
                    array(
                        'name' => 'destination_foreign_column[' . $i . '][]',
                        'title' => __('Column'),
                        'values' => array(),
                        'foreign' => ''
                    )
                ); ?>
            </span>
        <?php endif; ?>
    </td>
</tr>