|
| 1 | +<?php |
| 2 | + |
| 3 | +/* Icinga Notifications Web | (c) 2025 Icinga GmbH | GPLv2 */ |
| 4 | + |
| 5 | +namespace Icinga\Module\Notifications\Widget\TimeGrid; |
| 6 | + |
| 7 | +use DateTime; |
| 8 | +use IntlDateFormatter; |
| 9 | +use ipl\Html\Attributes; |
| 10 | +use ipl\Html\BaseHtmlElement; |
| 11 | +use ipl\Html\HtmlElement; |
| 12 | +use ipl\Html\Text; |
| 13 | +use ipl\I18n\Translation; |
| 14 | +use ipl\Web\Style; |
| 15 | +use Locale; |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a localized timescale for the TimeGrid |
| 19 | + */ |
| 20 | +class Timescale extends BaseHtmlElement |
| 21 | +{ |
| 22 | + use Translation; |
| 23 | + |
| 24 | + protected $tag = 'div'; |
| 25 | + |
| 26 | + protected $defaultAttributes = ['class' => 'timescale']; |
| 27 | + |
| 28 | + /** @var int The number of days shown */ |
| 29 | + protected $days; |
| 30 | + |
| 31 | + /** @var Style */ |
| 32 | + protected $style; |
| 33 | + |
| 34 | + /** |
| 35 | + * Create a new Timescale |
| 36 | + * |
| 37 | + * @param int $days |
| 38 | + * @param Style $style |
| 39 | + */ |
| 40 | + public function __construct(int $days, Style $style) |
| 41 | + { |
| 42 | + $this->days = $days; |
| 43 | + $this->style = $style; |
| 44 | + } |
| 45 | + |
| 46 | + public function assemble(): void |
| 47 | + { |
| 48 | + if ($this->days === 1) { |
| 49 | + $timestampPerDay = 12; |
| 50 | + } elseif ($this->days <= 7) { |
| 51 | + $timestampPerDay = 2; |
| 52 | + } else { |
| 53 | + $timestampPerDay = 1; |
| 54 | + } |
| 55 | + |
| 56 | + $this->style->addFor($this, ['--timestampsPerDay' => $timestampPerDay * 2]); // *2 for .ticks |
| 57 | + |
| 58 | + $dateFormatter = new IntlDateFormatter( |
| 59 | + Locale::getDefault(), |
| 60 | + IntlDateFormatter::NONE, |
| 61 | + IntlDateFormatter::SHORT |
| 62 | + ); |
| 63 | + |
| 64 | + $timeIntervals = 24 / $timestampPerDay; |
| 65 | + |
| 66 | + $time = new DateTime(); |
| 67 | + $dayTimestamps = []; |
| 68 | + for ($i = 0; $i < $timestampPerDay; $i++) { |
| 69 | + // am-pm is separated by non-breaking whitespace |
| 70 | + $parts = preg_split('/\s/u', $dateFormatter->format($time->setTime($i * $timeIntervals, 0))); |
| 71 | + |
| 72 | + $stamp = [new HtmlElement('span', null, new Text($parts[0]))]; |
| 73 | + if (isset($parts[1])) { |
| 74 | + $stamp[] = new HtmlElement('span', null, new Text($parts[1])); |
| 75 | + } |
| 76 | + |
| 77 | + $dayTimestamps[] = new HtmlElement('span', new Attributes(['class' => 'timestamp']), ...$stamp); |
| 78 | + $dayTimestamps[] = new HtmlElement('span', new Attributes(['class' => 'ticks'])); |
| 79 | + } |
| 80 | + |
| 81 | + $allTimestamps = array_merge(...array_fill(0, $this->days, $dayTimestamps)); |
| 82 | + // clone is required because $allTimestamps contains references of same object |
| 83 | + $allTimestamps[] = (clone $allTimestamps[0])->addAttributes(['class' => 'midnight']); // extra stamp of 12AM |
| 84 | + |
| 85 | + $this->addHtml(...$allTimestamps); |
| 86 | + } |
| 87 | +} |
0 commit comments