<div class="controls">
  <?= $this->render('show/components/header.phtml'); ?>
  <h1><?= $this->translate('This Object\'s Event History'); ?></h1>
  <?= $this->widget('limiter', array('url' => $url, 'max' => $history->count())); ?>
  <?= $this->paginationControl($history, null, null, array('preserve' => $this->preserve)); ?>
</div>

<div class="content">
<?php if (empty($history)): ?>
  <?= $this->translate('No history available for this object'); ?>
</div>
<?php return; endif ?>

<?php
$self = $this;
function contactsLink($match, $view) {
    $links = array();
    foreach (preg_split('/,\s/', $match[1]) as $contact) {
        $links[] = $view->qlink($contact, 'monitoring/show/contact', array('contact' => $contact));
    }
    return '[' . implode(', ', $links) . ']';
}
?>

  <table data-base-target="_next" class="action objecthistory">
    <tbody>
    <?php foreach ($history as $event): ?>
    <?php
      $stateClass = 'invalid';
      $isService = isset($event->service_description);
      switch ($event->type) {
        case 'notify':
          $icon = 'notification';
          $title = $this->translate('Notification');
          $stateClass = (
            $isService
              ? strtolower($this->util()->getServiceStateName($event->state))
              : strtolower($this->util()->getHostStateName($event->state))
          );

          $msg = preg_replace_callback(
              '/^\[([^\]]+)\]/',
              function($match) use ($self) { return contactsLink($match, $self); },
              $this->escape($event->output)
          );
          break;
        case 'comment':
          $icon = 'comment';
          $title = $this->translate('Comment');
          $msg = $this->escape($event->output);
          break;
        case 'comment_deleted':
          $icon = 'remove';
          $title = $this->translate('Comment deleted');
          $msg = $this->escape($event->output);
          break;
        case 'ack':
          $icon = 'acknowledgement';
          $title = $this->translate('Acknowledge');
          $msg = $this->escape($event->output);
          break;
        case 'ack_deleted':
          $icon = 'remove';
          $title = $this->translate('Ack removed');
          $msg = $this->escape($event->output);
          break;
        case 'dt_comment':
          $icon = 'in_downtime';
          $title = $this->translate('In Downtime');
          $msg = $this->escape($event->output);
          break;
        case 'dt_comment_deleted':
          $icon = 'remove';
          $title = $this->translate('Downtime removed');
          $msg = $this->escape($event->output);
          break;
        case 'flapping':
          $icon = 'flapping';
          $title = $this->translate('Flapping');
          $msg = $this->escape($event->output);
          break;
        case 'flapping_deleted':
          $icon = 'remove';
          $title = $this->translate('Flapping stopped');
          $msg = $this->escape($event->output);
          break;
        case 'hard_state':
          $icon = $isService ? 'service' : 'host';
          $msg = '[ ' . $event->attempt . '/' . $event->max_attempts . ' ] ' . $this->escape($event->output);
          $stateClass = (
            $isService
              ? strtolower($this->util()->getServiceStateName($event->state))
              : strtolower($this->util()->getHostStateName($event->state))
          );
          $title = strtoupper($stateClass); // TODO: Should be translatable!
          break;
        case 'soft_state':
          $icon = 'softstate';
          $msg = '[ ' . $event->attempt . '/' . $event->max_attempts . ' ] ' . $this->escape($event->output);
          $stateClass = (
            $isService
              ? strtolower($this->util()->getServiceStateName($event->state))
              : strtolower($this->util()->getHostStateName($event->state))
          );
          $title = strtoupper($stateClass); // TODO: Should be translatable!
          break;
        case 'dt_start':
          $icon = 'downtime_start';
          $title = $this->translate('Downtime Start');
          $msg = $this->escape($event->output);
          break;
        case 'dt_end':
          $icon = 'downtime_end';
          $title = $this->translate('Downtime End');
          $msg = $this->escape($event->output);
          break;
      }
    ?>
      <tr class="state <?= $stateClass; ?>">
        <td class="state">
          <strong><?= $this->escape($title); ?></strong>
          <br>
          <?= date('d.m. H:i', $event->timestamp); ?>
        </td>
        <td><?php

$output = $this->tickets ? preg_replace_callback(
    $this->tickets->getPattern(),
    array($this->tickets, 'createLink'),
    $msg
) : $msg;

?>
        <?php if ($isService): ?>
          <?= $this->escape($event->service_description) . ' ' . $this->translate('on') . ' ' . $this->escape($event->host_name); ?>
        <?php else: ?>
          <?= $this->escape($event->host_name); ?>
        <?php endif ?>
          <br>
          <div>
            <?= $this->icon($icon . '.png', $title); ?> <?= empty($msg) ? '' : $msg; ?>
          </div>
        </td>
      </tr>
<?php endforeach; ?>
    </tbody>
  </table>
</div>
