-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schedule detail: Introduce Timescale
- Loading branch information
1 parent
41595b9
commit 48cb522
Showing
3 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* Icinga Notifications Web | (c) 2025 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Notifications\Widget\TimeGrid; | ||
|
||
use DateTime; | ||
use IntlDateFormatter; | ||
use ipl\Html\Attributes; | ||
use ipl\Html\BaseHtmlElement; | ||
use ipl\Html\HtmlElement; | ||
use ipl\Html\Text; | ||
use ipl\I18n\Translation; | ||
use ipl\Web\Style; | ||
use Locale; | ||
|
||
/** | ||
* Creates a localized timescale for the TimeGrid | ||
*/ | ||
class Timescale extends BaseHtmlElement | ||
{ | ||
use Translation; | ||
|
||
protected $tag = 'div'; | ||
|
||
protected $defaultAttributes = ['class' => 'timescale']; | ||
|
||
/** @var int The number of days shown */ | ||
protected $days; | ||
|
||
/** @var Style */ | ||
protected $style; | ||
|
||
/** | ||
* Create a new Timescale | ||
* | ||
* @param int $days | ||
* @param Style $style | ||
*/ | ||
public function __construct(int $days, Style $style) | ||
{ | ||
$this->days = $days; | ||
$this->style = $style; | ||
} | ||
|
||
public function assemble(): void | ||
{ | ||
switch (true) { | ||
case $this->days === 1: | ||
$timestampPerDay = 24; | ||
break; | ||
case $this->days <= 7: | ||
$timestampPerDay = 3; | ||
break; | ||
case $this->days <= 14: | ||
$timestampPerDay = 2; | ||
break; | ||
default: | ||
$timestampPerDay = 1; | ||
} | ||
|
||
$this->style->addFor($this, ['--timestampsPerDay' => $timestampPerDay * 2]); // *2 for .ticks | ||
|
||
$dateFormatter = new IntlDateFormatter( | ||
Locale::getDefault(), | ||
IntlDateFormatter::NONE, | ||
IntlDateFormatter::SHORT | ||
); | ||
|
||
$timeIntervals = 24 / $timestampPerDay; | ||
|
||
$time = new DateTime(); | ||
$dayTimestamps = []; | ||
for ($i = 0; $i < $timestampPerDay; $i++) { | ||
// am-pm is separated by non-breaking whitespace | ||
$parts = preg_split('/\s/u', $dateFormatter->format($time->setTime($i * $timeIntervals, 0))); | ||
|
||
$stamp = [new HtmlElement('span', null, new Text($parts[0]))]; | ||
if (isset($parts[1])) { | ||
$stamp[] = new HtmlElement('span', null, new Text($parts[1])); | ||
} | ||
|
||
$dayTimestamps[] = new HtmlElement('span', new Attributes(['class' => 'timestamp']), ...$stamp); | ||
$dayTimestamps[] = new HtmlElement('span', new Attributes(['class' => 'ticks'])); | ||
} | ||
|
||
$allTimestamps = array_merge(...array_fill(0, $this->days, $dayTimestamps)); | ||
// clone is required because $allTimestamps contains references of same object | ||
$allTimestamps[] = (clone $allTimestamps[0])->addAttributes(['class' => 'midnight']); // extra stamp of 12AM | ||
|
||
$this->addHtml(...$allTimestamps); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters