<?php
$requested_sort = 'table';
$requested_sort_order = $future_sort_order = $initial_sort_order;
// If the user requested a sort
if (isset($_REQUEST['sort'])) {
    $requested_sort = $_REQUEST['sort'];
    if (isset($_REQUEST['sort_order'])) {
        $requested_sort_order = $_REQUEST['sort_order'];
    }
}
$order_img = '';
$order_link_params = array();
$order_link_params['title'] = __('Sort');
// If this column was requested to be sorted.
if ($requested_sort == $sort) {
    if ($requested_sort_order == 'ASC') {
        $future_sort_order = 'DESC';
        // current sort order is ASC
        $order_img  = ' ' . PMA\libraries\Util::getImage(
            's_asc.png',
            __('Ascending'),
            array('class' => 'sort_arrow', 'title' => '')
        );
        $order_img .= ' ' . PMA\libraries\Util::getImage(
            's_desc.png',
             __('Descending'),
            array('class' => 'sort_arrow hide', 'title' => '')
        );
        // but on mouse over, show the reverse order (DESC)
        $order_link_params['onmouseover'] = "$('.sort_arrow').toggle();";
        // on mouse out, show current sort order (ASC)
        $order_link_params['onmouseout'] = "$('.sort_arrow').toggle();";
    } else {
        $future_sort_order = 'ASC';
        // current sort order is DESC
        $order_img  = ' ' . PMA\libraries\Util::getImage(
            's_asc.png',
            __('Ascending'),
            array('class' => 'sort_arrow hide', 'title' => '')
        );
        $order_img .= ' ' . PMA\libraries\Util::getImage(
            's_desc.png',
            __('Descending'),
            array('class' => 'sort_arrow', 'title' => '')
        );
        // but on mouse over, show the reverse order (ASC)
        $order_link_params['onmouseover'] = "$('.sort_arrow').toggle();";
        // on mouse out, show current sort order (DESC)
        $order_link_params['onmouseout'] = "$('.sort_arrow').toggle();";
    }
}
$_url_params = array(
    'db' => $_REQUEST['db'],
    'pos' => 0, // We set the position back to 0 every time they sort.
    'sort' => $sort,
    'sort_order' => $future_sort_order,
);

if (PMA_isValid($_REQUEST['tbl_type'], array('view', 'table'))) {
     $_url_params['tbl_type'] = $_REQUEST['tbl_type'];
}
if (! empty($_REQUEST['tbl_group'])) {
    $_url_params['tbl_group'] = $_REQUEST['tbl_group'];
}

$url = 'db_structure.php' . PMA\libraries\URL::getCommon($_url_params);

echo PMA\libraries\Util::linkOrButton(
    $url, $title . $order_img, $order_link_params
);
