<?php
$systemDatabases = $this->config->get('config.systemDatabases');
$formUrl = $this->url(array('controller'=>'sql','action'=>'index'));
$cycleHelper = $this->getHelper('cycle')->cycle(array('row-even', 'row-odd'));
?>
<div id="content">
<h2><?php echo $this->lang->L_DATABASES_OF_USER;?> '<?php echo $this->config->get('config.dbuser.user').'\'@\''.$this->config->get('config.dbuser.host');?>'</h2>
<?php echo $this->sqlHeadNavi(); ?>
<?php
if (isset($this->actionResults)) {
?>
<h4><?php echo $this->lang->L_ACTION . ' - ' . $this->lang->getTranslator()->translate($this->executedAction); ?>:</h4>

<table class="bdr" summary="Table shows formatted result of action">
<tr class="thead nowrap">
    <td class="right">#</td>
    <th class="left"><?php echo $this->lang->L_DB; ?></th>
    <th class="small"><?php echo $this->lang->L_QUERY; ?></th>
    <th colspan="2" class="left"><?php echo $this->lang->L_SQL_OUTPUT; ?></th>
</tr>
<?php
    $row = 0;
    foreach ($this->actionResults as $dbName => $dbResults) {
        foreach ($dbResults as $dbResult) {
            $row++;
            if ($dbResult['result']) {
?>
<tr class="nowrap <?php echo $cycleHelper->next();?>">
    <td class="small right"><?php echo $row; ?>.</td>
    <td class="small"><?php echo $dbName; ?></td>
    <td class="small"><?php echo $dbResult['query']; ?></td>
    <td class="small right"><?php echo $this->getIcon('Ok', '', 16); ?></td>
</tr>
<?php
            } else {
?>
<tr class="error">
    <td class="right"><?php echo $row; ?>.</td>
    <td class="small"><?php echo $dbName; ?></td>
    <td><?php echo $dbResult['query']; ?></td>
    <td class="error"><?php echo $dbResult['errorInfo']['code'] . ': ' . $dbResult['errorInfo']['message']; ?></td>
    <td class="right"><?php echo $this->getIcon('Attention', '', 16); ?></td>
</tr>
<?php
            }
        }
    }
    if (isset($this->actionResults) && empty($this->actionResults)) {
        ?>
        <tr class="nowrap <?php echo $cycleHelper->next();?>">
            <td colspan="4">
                <?php echo $this->lang->L_NOTHING_TO_DO; ?>
            </td>
        </tr>
        <?php
    }
?>
</table><br/>
<?php
}
?>
<form method="post" action="<?php echo $formUrl;?>" class="dbForm" id="dbForm">
<table class="bdr" summary="Table lists all databases">
    <tr class="thead">
        <th colspan="10">
            <button class="Formbutton" type="button" onclick="window.location.href='<?php echo $this->url(array('controller' => 'sql', 'action' => 'create.database')); ?>';">
                <?php
                    echo $this->getIcon('Database', '', 16);
                    echo $this->lang->L_CREATE_DATABASE;
                ?>
            </button>
            <button id="buttonTruncate" disabled="disabled" class="Formbutton" type="button"
                onclick="if (!hasCheckedElements('.dbCheckbox')) return false;$('#confirmTruncateDb').dialog('open');">
                <?php
                    echo $this->getIcon('DeleteTables', '', 16);
                    echo $this->lang->L_SQL_EMPTYDB;
                ?>
            </button>
            <button id="buttonDrop" disabled="disabled" class="Formbutton" type="button"
                onclick="if (!hasCheckedElements('.dbCheckbox')) return false;$('#confirmDropDb').dialog('open');">
                <?php
                    echo $this->getIcon('DeleteDatabase', '', 16);
                    echo $this->lang->L_DELETE_DATABASE;
                ?>
            </button>
        </th>
    </tr>
    <?php
    echo $this->partial('sql/databases/database-header.phtml',
            array('lang' => $this->lang));
    $i=1;
    $iconShowTable = $this->getIcon('Tables', '', 16);
    foreach($this->dbInfos as $dbName => $data):
        $dbNameEncoded = base64_encode($dbName);
        $viewUrl = $this->url(array(
                                   'controller'=>'sql',
                                   'action'=>'show.tables',
                                   'database'=> $dbNameEncoded)
        );
        ?>
        <tr class="nowrap
        <?php
            if ($dbName == $this->config->get('dynamic.dbActual')) {
                echo 'row-highlight';
            } else {
                echo $this->cycle(array('row-even', 'row-odd'))->next();
            }
            ?>">
            <td class="small right">
                <a href="<?php echo $this->url(array('controller' => 'sql', 'action' => 'show.tables', 'dbName' => $dbNameEncoded)); ?>"
                    title="<?php echo $this->lang->L_SHOW_TABLES . ': ' . $dbName;?>" class="tooltip">
                    <?php echo $iconShowTable; ?>
                </a>
            </td>
            <td class="small right">
                <?php
                    if (!in_array($dbName, $systemDatabases)) {
                ?>
                    <input id="<?php echo $this->out($dbName);?>" type="checkbox" name="dbNames[]" class="dbCheckbox checkbox" value="<?php echo $dbNameEncoded; ?>"/>
                <?php } else { ?>
                    &nbsp;
                <?php } ?>
            </td>
            <td class="small right"><?php echo $i;?>.</td>
            <td class="small">
                <?php
                    if (!in_array($dbName, $systemDatabases)) {
                ?>
                    <label class="block tooltip" for="<?php echo $this->out($dbName);?>" title="<?php echo $this->lang->L_SELECT . ': ' . $this->out($dbName);?>">
                        <?php echo $dbName;?>
                    </label>
                <?php } else {
                        echo $dbName;
                    }
                 ?>
            </td>
            <td class="small right">
                <?php echo $this->numberFormat($data['tables']);?>
            </td>
            <td class="small right">
                <?php echo $this->byteOutput($data['size']);?>
            </td>
            <td class="small right">
                <?php echo $this->numberFormat($data['views']);?>
            </td>
            <td class="small right">
                <?php echo $this->numberFormat($data['routines']);?>
            </td>
            <td class="small">
                <?php echo $data['DEFAULT_CHARACTER_SET_NAME'];?>
            </td>
            <td class="small">
                <?php echo $data['DEFAULT_COLLATION_NAME'];?>
            </td>
        </tr>
        <?php
            $i++;
    endforeach;
    echo $this->partial('sql/databases/database-header.phtml',
            array('lang' => $this->lang));
    ?>
</table>
</form>
</div>
<?php
$dropDatabaseUrl = $this->url(array('controller' => 'sql', 'action' => 'drop.database'));
$this->popUpMessage()->addMessage(
    'confirmDropDb',
    'L_DELETE_DATABASE',
    'L_CONFIRM_DROP_DATABASES',
    array(
        'modal' => true,
        'autoOpen' => false,
        'buttons' => array(
            'L_YES' => "function() { changeFormAction('.dbForm', '" . $dropDatabaseUrl . "');$('#dbForm').submit();}",
            'L_CANCEL' => 'function(){$(this).dialog("close"); return false;}',
        )
    )
);

$truncateDatabaseUrl = $this->url(array('controller' => 'sql', 'action' => 'truncate.database'));
$this->popUpMessage()->addMessage(
    'confirmTruncateDb',
    'L_TRUNCATE_DATABASE',
    'L_CONFIRM_TRUNCATE_DATABASES',
    array(
        'modal' => true,
        'autoOpen' => false,
        'buttons' => array(
            'L_YES' => "function() { changeFormAction('.dbForm', '" . $truncateDatabaseUrl . "');$('#dbForm').submit();}",
            'L_CANCEL' => 'function(){$(this).dialog("close"); return false;}',
        )
    )
);
$this->jQuery()->onLoadCaptureStart();
?>
$('.dbCheckbox').change(function() {
    checkButtonState();
});
<?php
$this->jQuery()->onLoadCaptureEnd();
?>
<script type="text/javascript">
function checkButtonState() {
    var objs = ['#buttonTruncate', '#buttonDrop'];
    if ($('.dbCheckbox:checked').size() > 0) {
        objs_enable(objs);
    } else {
        objs_disable(objs);
    }
}
</script>
