Skip to content

Commit

Permalink
Preview schedules in the overview
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Jun 25, 2024
1 parent ba627b4 commit 9d402c7
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 88 deletions.
5 changes: 5 additions & 0 deletions application/controllers/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function addRotationAction(): void
});
$form->on(RotationConfigForm::ON_SUCCESS, function (RotationConfigForm $form) use ($scheduleId) {
$form->addRotation();
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
});

Expand All @@ -154,14 +155,17 @@ public function editRotationAction(): void
$form->setSuggestionUrl(Url::fromPath('notifications/schedule/suggest-recipient'));
$form->on(RotationConfigForm::ON_SUCCESS, function (RotationConfigForm $form) use ($id, $scheduleId) {
$form->editRotation($id);
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
});
$form->on(RotationConfigForm::ON_SENT, function (RotationConfigForm $form) use ($id, $scheduleId) {
if ($form->hasBeenRemoved()) {
$form->removeRotation($id);
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
} elseif ($form->hasBeenWiped()) {
$form->wipeRotation();
$this->sendExtraUpdates(['#col1']);
$this->closeModalAndRefreshRelatedView(Links::schedule($scheduleId));
} elseif (! $form->hasBeenSubmitted()) {
foreach ($form->getPartUpdates() as $update) {
Expand All @@ -188,6 +192,7 @@ public function moveRotationAction(): void

$form = new MoveRotationForm(Database::get());
$form->on(MoveRotationForm::ON_SUCCESS, function (MoveRotationForm $form) {
$this->sendExtraUpdates(['#col1']);
$this->redirectNow(Links::schedule($form->getScheduleId()));
});

Expand Down
12 changes: 12 additions & 0 deletions application/controllers/SchedulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace Icinga\Module\Notifications\Controllers;

use DateTime;
use Icinga\Module\Notifications\Common\Database;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Model\Schedule;
use Icinga\Module\Notifications\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Notifications\Widget\ItemList\ScheduleList;
use Icinga\Module\Notifications\Widget\TimeGrid\DaysHeader;
use ipl\Html\Attributes;
use ipl\Html\HtmlElement;
use ipl\Stdlib\Filter;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\SearchControls;
Expand Down Expand Up @@ -70,6 +74,14 @@ public function indexAction(): void
))->openInModal()
);

$this->addContent(
HtmlElement::create(
'div',
Attributes::create(['class' => ['timeline', 'schedules-header']]),
new DaysHeader((new DateTime())->setTime(0, 0), 7)
)
);

$this->addContent(new ScheduleList($schedules));

if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
Expand Down
26 changes: 25 additions & 1 deletion library/Notifications/Widget/ItemList/ScheduleListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

namespace Icinga\Module\Notifications\Widget\ItemList;

use DateTime;
use Icinga\Module\Notifications\Common\Links;
use Icinga\Module\Notifications\Model\Schedule;
use Icinga\Module\Notifications\Widget\Timeline;
use Icinga\Module\Notifications\Widget\Timeline\Rotation;
use Icinga\Util\Csp;
use ipl\Html\BaseHtmlElement;
use ipl\Web\Common\BaseListItem;
use ipl\Web\Style;
use ipl\Web\Widget\Link;

/**
Expand Down Expand Up @@ -41,8 +46,27 @@ protected function assembleHeader(BaseHtmlElement $header): void
$header->addHtml($this->createTitle());
}

protected function assembleCaption(BaseHtmlElement $caption): void
{
// Number of days is set to 7, since default mode for schedule is week
// and the start day should be the current day
$timeline = (new Timeline((new DateTime())->setTime(0, 0), 7))
->minimalLayout()
->setStyle(
(new Style())
->setNonce(Csp::getStyleNonce())
->setModule('notifications')
);

foreach ($this->item->rotation->with('timeperiod')->orderBy('first_handoff', SORT_DESC) as $rotation) {
$timeline->addRotation(new Rotation($rotation));
}

$caption->addHtml($timeline);
}

protected function assembleMain(BaseHtmlElement $main): void
{
$main->addHtml($this->createHeader());
$main->addHtml($this->createHeader(), $this->createCaption());
}
}
90 changes: 90 additions & 0 deletions library/Notifications/Widget/TimeGrid/DaysHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/** Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Widget\TimeGrid;

use DateInterval;
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 Locale;

class DaysHeader extends BaseHtmlElement
{
use Translation;

/** @var int The number of days to show */
protected $days;

protected $tag = 'div';

protected $defaultAttributes = ['class' => 'header'];

/** @var DateTime Starting day */
protected $startDay;

public function __construct(DateTime $startDay, int $days)
{
$this->startDay = $startDay;
$this->days = $days;
}

public function assemble(): void
{
$dayNames = [
$this->translate('Mon', 'monday'),
$this->translate('Tue', 'tuesday'),
$this->translate('Wed', 'wednesday'),
$this->translate('Thu', 'thursday'),
$this->translate('Fri', 'friday'),
$this->translate('Sat', 'saturday'),
$this->translate('Sun', 'sunday')
];

$interval = new DateInterval('P1D');
$today = (new DateTime())->setTime(0, 0);
$time = clone $this->startDay;
$dateFormatter = new IntlDateFormatter(
Locale::getDefault(),
IntlDateFormatter::MEDIUM,
IntlDateFormatter::NONE
);

for ($i = 0; $i < $this->days; $i++) {
if ($time == $today) {
$title = [new HtmlElement(
'span',
Attributes::create(['class' => 'day-name']),
Text::create($this->translate('Today'))
)];
} else {
$title = [
new HtmlElement(
'span',
Attributes::create(['class' => 'date']),
Text::create($time->format($this->translate('d/m', 'day-name, time')))
),
Text::create(' '),
new HtmlElement(
'span',
Attributes::create(['class' => 'day-name']),
Text::create($dayNames[$time->format('N') - 1])
)
];
}

$this->addHtml(new HtmlElement(
'div',
Attributes::create(['class' => 'column-title', 'title' => $dateFormatter->format($time)]),
...$title
));

$time->add($interval);
}
}
}
57 changes: 1 addition & 56 deletions library/Notifications/Widget/TimeGrid/DynamicGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

use DateInterval;
use DateTime;
use IntlDateFormatter;
use InvalidArgumentException;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use Locale;
use LogicException;
use Traversable;

Expand Down Expand Up @@ -98,59 +95,7 @@ protected function sideBar(): BaseHtmlElement

protected function createHeader(): BaseHtmlElement
{
$dayNames = [
$this->translate('Mon', 'monday'),
$this->translate('Tue', 'tuesday'),
$this->translate('Wed', 'wednesday'),
$this->translate('Thu', 'thursday'),
$this->translate('Fri', 'friday'),
$this->translate('Sat', 'saturday'),
$this->translate('Sun', 'sunday')
];

$interval = new DateInterval('P1D');
$today = (new DateTime())->setTime(0, 0);
$time = clone $this->getGridStart();
$dateFormatter = new IntlDateFormatter(
Locale::getDefault(),
IntlDateFormatter::MEDIUM,
IntlDateFormatter::NONE
);

$header = new HtmlElement('div', Attributes::create(['class' => 'header']));
for ($i = 0; $i < $this->days; $i++) {
if ($time == $today) {
$title = [new HtmlElement(
'span',
Attributes::create(['class' => 'day-name']),
Text::create($this->translate('Today'))
)];
} else {
$title = [
new HtmlElement(
'span',
Attributes::create(['class' => 'date']),
Text::create($time->format($this->translate('d/m', 'day-name, time')))
),
Text::create(' '),
new HtmlElement(
'span',
Attributes::create(['class' => 'day-name']),
Text::create($dayNames[$time->format('N') - 1])
)
];
}

$header->addHtml(new HtmlElement(
'div',
Attributes::create(['class' => 'column-title', 'title' => $dateFormatter->format($time)]),
...$title
));

$time->add($interval);
}

return $header;
return new DaysHeader($this->getGridStart(), $this->days);
}

protected function createGridSteps(): Traversable
Expand Down
42 changes: 42 additions & 0 deletions library/Notifications/Widget/TimeGrid/MinimalGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/** Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Widget\TimeGrid;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\HtmlString;

/**
* A grid without header and sidebar
*/
class MinimalGrid extends DynamicGrid
{
protected function sideBar(): BaseHtmlElement
{
return HtmlElement::create('div', Attributes::create(['class' => 'sidebar']), new HtmlString(''));
}

protected function assemble(): void
{
$this->style->addFor($this, [
'--primaryColumns' => $this->days,
'--columnsPerStep' => 48,
'--rowsPerStep' => 1
]);

$overlay = $this->createGridOverlay();
if ($overlay->isEmpty()) {
$this->style->addFor($this, [
'--primaryRows' => count($this->sideBar())
]);
}

$this->addHtml(
$this->createGrid(),
$overlay
);
}
}
Loading

0 comments on commit 9d402c7

Please sign in to comment.