summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/IntlZfQueryBasedTable.php
blob: 81ef14c0d5bd7a97fac3aae74383c7e99938ba6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace Icinga\Module\Director\Web\Table;

use DateTime;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use IntlDateFormatter;
use Locale;

abstract class IntlZfQueryBasedTable extends ZfQueryBasedTable
{
    protected function getDateFormatter()
    {
        return (new IntlDateFormatter(
            Locale::getDefault(),
            IntlDateFormatter::FULL,
            IntlDateFormatter::NONE
        ));
    }

    /**
     * @param  int $timestamp
     */
    protected function renderDayIfNew($timestamp)
    {
        $day = $this->getDateFormatter()->format((new DateTime())->setTimestamp($timestamp));

        if ($this->lastDay !== $day) {
            $this->nextHeader()->add(
                $this::th($day, [
                    'colspan' => 2,
                    'class'   => 'table-header-day'
                ])
            );

            $this->lastDay = $day;
            $this->nextBody();
        }
    }

    protected function getTime(int $timeStamp)
    {
        $timeFormatter = $this->getDateFormatter();

        $timeFormatter->setPattern(
            in_array(Locale::getDefault(), ['en_US', 'en_US.UTF-8']) ? 'h:mm:ss a': 'H:mm:ss'
        );
        
        return $timeFormatter->format((new DateTime())->setTimestamp($timeStamp));
    }
}