<div class="controls">
<?= $this->tabs ?>
</div>

<div class="content">
<?php if ($this->errorMessage): ?>
<p class="error"><?= $this->errorMessage ?></p>
<?php endif ?>
<?php if (! $this->stats): ?>
<?= $this->form ?>
</div>
<?php return; endif ?>
<?php

function statSummary($self, $type) {
    $stat = $self->stats[$type];

    if ((int) $stat->cnt_total === 0) {
        return $self->translate('No object has been defined yet');
    }

    if ((int) $stat->cnt_total === 1) {
        if ($stat->cnt_template > 0) {
            $msg = $self->translate('One template has been defined');
        } elseif ($stat->cnt_external > 0) {
            $msg = $self->translate('One external object has been defined, it will not be deployed');
        } else {
            $msg = $self->translate('One object has been defined');
        }

    } else {
        $msg = sprintf(
            $self->translate('%d objects have been defined'),
            $stat->cnt_total
        );
    }

    $extra = array();
    if ($stat->cnt_total !== $stat->cnt_object) {

    if ($stat->cnt_template > 0) {
        $extra[] = sprintf(
            $self->translate('%d of them are templates'),
            $stat->cnt_template
        );
    }
    if ($stat->cnt_external > 0) {
        $extra[] = sprintf(
            $self->translate('%d have been externally defined and will not be deployed'),
            $stat->cnt_external
        );
    }
    }

    if (array_key_exists($type . 'group', $self->stats)) {
        $groupstat = $self->stats[$type . 'group'];
        if ((int) $groupstat->cnt_total === 0) {
            $extra[] = $self->translate('no related group exists');
        } elseif ((int) $groupstat->cnt_total === 1) {
            $extra[] = $self->translate('one related group exists');
        } else {
            $extra[] = sprintf(
                $self->translate('%s related group objects have been created'),
                $groupstat->cnt_total
            );
        }
    }

    if (empty($extra)) {
        return $msg;
    }

    return $msg . ', ' . implode(', ', $extra);
}

function pendingDeployments($self) {
    if ($self->undeployedActivities === 0) return '';
    return '. <span class="error">' . sprintf(
        $self->translate('A total of %d config changes happened since your last deployed config has been rendered'),
        $self->undeployedActivities
    ) . '</span>';
}
if (!$this->hasDeploymentEndpoint) {
    echo $this->form;
}

if ($this->migrationsForm) {
    echo '<h1>' . $this->translate('There are pending database schema migrations') . "</h2>\n";
    echo $this->migrationsForm;
}

$all = array(
    $this->translate('Define whatever you want to be monitored') => array(
        array('host',     $this->translate('Host objects'), 'director/hosts', statSummary($this, 'host')),
        array('services', $this->translate('Monitored Services'), 'director/services', statSummary($this, 'service')),
        array('wrench',   $this->translate('Commands'), 'director/commands', statSummary($this, 'command')),
        array('users',    $this->translate('Users / Contacts'), 'director/users', statSummary($this, 'user')),
        array('megaphone', $this->translate('Notifications'), 'director/notifications', $this->translate('Schedule your notifications.') . ' ' . statSummary($this, 'notification')),
        array('calendar', $this->translate('Timeperiods'), 'director/timeperiods', statSummary($this, 'timeperiod')),
    ),
    $this->translate('Deploy configuration to your Icinga nodes') => array(
        array('wrench',   $this->translate('Deployment'), 'director/config/deployments', $this->translate('Config deployment') . pendingDeployments($this), $this->undeployedActivities ? 'warning' : 'ok'),
        array('book',   $this->translate('Activity Log'), 'director/config/activities', $this->translate('Wondering about what changed why? Track you changes!'), 'ok'),
        array('lock-open-alt', $this->translate('Api users'), 'director/apiusers', statSummary($this, 'apiuser')),
        array('cloud',    $this->translate('Endpoints'), 'director/endpoints', statSummary($this, 'endpoint') . ( $this->hasDeploymentEndpoint ? '' : '. ' . $this->translate('None could be used for deployments right now')), $this->hasDeploymentEndpoint ? '' : 'critical'),
        array('globe',    $this->translate('Zones'), 'director/zones', statSummary($this, 'zone')),
    ),
    $this->translate('Do more with your data') => array(
        array('database',     $this->translate('Import data sources'), 'director/list/importsource', $this->translate('Define and manage imports from various data sources'), $this->importState),
        array('flapping',     $this->translate('Synchronize'), 'director/list/syncrule', $this->translate('Define how imported data should be synchronized with Icinga'), $this->syncState),
        array('clock',     $this->translate('Jobs'), 'director/jobs', $this->translate('Schedule and automate Import, Syncronization, Config Deployment, Housekeeping and more'), $this->jobState),
        array('sort-name-up', $this->translate('Provide data lists'), 'director/data/lists', $this->translate('Provide data lists to make life easier for your users')),
        array('edit',         $this->translate('Define data fields'), 'director/data/fields', $this->translate('Data fields make sure that configuration fits your rules')),
    )
);
?>

<?php foreach ($all as $title => $actions): ?>
<?= $this->partial(
    'index/actions.phtml',
    array(
        'actions' => $actions,
        'title'   => $title,
    )
) ?>
<?php endforeach ?>
</div>
