4
4
5
5
namespace Icinga \Module \Notifications \Widget ;
6
6
7
- use DateTimeZone ;
8
- use Icinga \Module \Notifications \Common \Database ;
9
- use Icinga \Module \Notifications \Model \ScheduleMember ;
10
- use Icinga \Module \Notifications \Model \TimeperiodEntry ;
11
- use Icinga \Module \Notifications \Widget \Calendar \Attendee ;
12
- use Icinga \Module \Notifications \Widget \Calendar \Controls ;
13
- use Icinga \Module \Notifications \Widget \Calendar \Entry ;
7
+ use Icinga \Module \Notifications \Widget \Schedule \Controls ;
8
+ use Icinga \Module \Notifications \Widget \Timeline \Rotation ;
14
9
use Icinga \Util \Csp ;
15
10
use ipl \Html \Attributes ;
16
11
use ipl \Html \BaseHtmlElement ;
17
12
use ipl \Html \HtmlElement ;
18
- use ipl \Stdlib \Filter ;
19
13
use ipl \Web \Common \BaseTarget ;
20
14
use ipl \Web \Style ;
21
- use ipl \Web \Url ;
22
- use ipl \Web \Widget \Icon ;
23
- use ipl \Web \Widget \Link ;
24
15
25
16
class Schedule extends BaseHtmlElement
26
17
{
@@ -36,122 +27,54 @@ class Schedule extends BaseHtmlElement
36
27
/** @var Controls */
37
28
protected $ controls ;
38
29
39
- public function __construct (Controls $ controls , ?\Icinga \Module \Notifications \Model \Schedule $ schedule )
30
+ /**
31
+ * Create a new Schedule
32
+ *
33
+ * @param \Icinga\Module\Notifications\Model\Schedule $schedule
34
+ * @param Controls $controls
35
+ */
36
+ public function __construct (\Icinga \Module \Notifications \Model \Schedule $ schedule , Controls $ controls )
40
37
{
41
38
$ this ->schedule = $ schedule ;
42
39
$ this ->controls = $ controls ;
43
40
}
44
41
45
- protected function assembleCalendar (Calendar $ calendar ): void
42
+ /**
43
+ * Assemble the timeline
44
+ *
45
+ * @param Timeline $timeline
46
+ */
47
+ protected function assembleTimeline (Timeline $ timeline ): void
46
48
{
47
- $ calendar ->setAddEntryUrl (Url::fromPath (
48
- 'notifications/schedule/add-entry ' ,
49
- ['schedule ' => $ this ->schedule ->id ]
50
- ));
51
-
52
- $ calendar ->setUrl (Url::fromPath (
53
- 'notifications/schedules ' ,
54
- ['schedule ' => $ this ->schedule ->id ]
55
- ));
56
-
57
- $ db = Database::get ();
58
- $ entries = TimeperiodEntry::on ($ db )
59
- ->filter (Filter::equal ('timeperiod.schedule.id ' , $ this ->schedule ->id ))
60
- ->orderBy (['start_time ' , 'timeperiod_id ' ]);
61
-
62
- $ entryFilter = Filter::any (
63
- Filter::all ( // all entries that start in the shown range
64
- Filter::greaterThanOrEqual ('start_time ' , $ calendar ->getGrid ()->getGridStart ()->getTimestamp ()),
65
- Filter::lessThanOrEqual ('start_time ' , $ calendar ->getGrid ()->getGridEnd ()->getTimestamp ())
66
- ),
67
- Filter::all ( // all entries that end in the shown range
68
- Filter::greaterThanOrEqual ('end_time ' , $ calendar ->getGrid ()->getGridStart ()->getTimestamp ()),
69
- Filter::lessThanOrEqual ('end_time ' , $ calendar ->getGrid ()->getGridEnd ()->getTimestamp ())
70
- ),
71
- Filter::all ( // all entries that start before and end after the shown range
72
- Filter::lessThanOrEqual ('start_time ' , $ calendar ->getGrid ()->getGridStart ()->getTimestamp ()),
73
- Filter::greaterThanOrEqual ('end_time ' , $ calendar ->getGrid ()->getGridEnd ()->getTimestamp ())
74
- ),
75
- Filter::none ( // all entries that are repeated and may still occur in the shown range
76
- Filter::lessThanOrEqual ('until_time ' , $ calendar ->getGrid ()->getGridStart ()->getTimestamp ())
77
- ),
78
- Filter::all ( // all entries that are repeated endlessly and already started in the past
79
- Filter::unlike ('until_time ' , '* ' ),
80
- Filter::like ('rrule ' , '* ' ),
81
- Filter::lessThanOrEqual ('start_time ' , $ calendar ->getGrid ()->getGridStart ()->getTimestamp ())
82
- )
83
- );
84
-
85
- foreach ($ entries ->filter ($ entryFilter ) as $ entry ) {
86
- $ members = ScheduleMember::on ($ db )
87
- ->with (['timeperiod ' , 'contact ' , 'contactgroup ' ])
88
- ->filter (Filter::equal ('timeperiod_id ' , $ entry ->timeperiod_id ))
89
- ->orderBy (['contact_id ' , 'contactgroup_id ' ]);
90
-
91
- foreach ($ members as $ member ) {
92
- if ($ member ->contact_id !== null ) {
93
- $ attendee = new Attendee ($ member ->contact ->full_name );
94
- } else { // $member->contactgroup_id !== null
95
- $ attendee = new Attendee ($ member ->contactgroup ->name );
96
- $ attendee ->setIcon ('users ' );
97
- }
98
-
99
- $ calendar ->addEntry (
100
- (new Entry ($ entry ->id ))
101
- ->setDescription ($ entry ->description )
102
- ->setRecurrencyRule ($ entry ->rrule )
103
- ->setStart ((clone $ entry ->start_time )->setTimezone (new DateTimeZone ($ entry ->timezone )))
104
- ->setEnd ((clone $ entry ->end_time )->setTimezone (new DateTimeZone ($ entry ->timezone )))
105
- ->setUrl (Url::fromPath ('notifications/schedule/edit-entry ' , [
106
- 'id ' => $ entry ->id ,
107
- 'schedule ' => $ this ->schedule ->id
108
- ]))
109
- ->setAttendee ($ attendee )
110
- );
111
- }
49
+ foreach ($ this ->schedule ->rotation ->with ('timeperiod ' ) as $ rotation ) {
50
+ $ timeline ->addRotation (new Rotation ($ rotation ));
112
51
}
113
52
}
114
53
115
- public function assemble ()
54
+ /**
55
+ * Create the timeline
56
+ *
57
+ * @return Timeline
58
+ */
59
+ protected function createTimeline (): Timeline
116
60
{
117
- $ calendar = (new Calendar ())
118
- ->setControls ($ this ->controls )
119
- ->setStyle (
120
- (new Style ())
121
- ->setNonce (Csp::getStyleNonce ())
122
- ->setModule ('notifications ' )
123
- );
124
-
125
- $ this ->setBaseTarget ('entry-form ' );
126
- if ($ this ->controls ->getBaseTarget () === null ) {
127
- $ this ->controls ->setBaseTarget ('_self ' );
128
- }
61
+ $ timeline = new Timeline ($ this ->controls ->getStartDate (), $ this ->controls ->getNumberOfDays ());
62
+ $ timeline ->setStyle (
63
+ (new Style ())
64
+ ->setNonce (Csp::getStyleNonce ())
65
+ ->setModule ('notifications ' )
66
+ );
129
67
130
- $ scheduleHeader = new HtmlElement ('div ' , Attributes::create (['class ' => 'schedule-header ' ]));
131
- if ($ this ->schedule !== null ) {
132
- $ this ->assembleCalendar ($ calendar );
133
- $ scheduleHeader ->addHtml (
134
- new Link (
135
- [
136
- new Icon ('plus ' ),
137
- t ('Add new entry ' )
138
- ],
139
- Url::fromPath ('notifications/schedule/add-entry ' , ['schedule ' => $ this ->schedule ->id ]),
140
- ['class ' => 'button-link ' ]
141
- )
142
- );
143
- }
68
+ $ this ->assembleTimeline ($ timeline );
144
69
145
- $ scheduleContainer = new HtmlElement ('div ' , Attributes::create (['class ' => 'schedule-container ' ]));
146
- $ scheduleContainer ->addHtml ($ calendar );
147
- $ scheduleContainer ->addHtml (new HtmlElement (
148
- 'div ' ,
149
- Attributes::create ([
150
- 'id ' => 'entry-form ' ,
151
- 'class ' => 'entry-form container '
152
- ])
153
- ));
70
+ return $ timeline ;
71
+ }
154
72
155
- $ this ->addHtml ($ scheduleHeader , $ scheduleContainer );
73
+ protected function assemble ()
74
+ {
75
+ $ this ->addHtml (
76
+ new HtmlElement ('div ' , Attributes::create (['class ' => 'schedule-header ' ]), $ this ->controls ),
77
+ new HtmlElement ('div ' , Attributes::create (['class ' => 'schedule-container ' ]), $ this ->createTimeline ())
78
+ );
156
79
}
157
80
}
0 commit comments