summaryrefslogtreecommitdiff
path: root/includes/view/ShiftsFilterRenderer.php
blob: 27c6240a3417238642dba410840cf3c111ad57f9 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace Engelsystem;

class ShiftsFilterRenderer
{
    /**
     * The shiftFilter to render.
     *
     * @var ShiftsFilter
     */
    private $shiftsFilter;

    /**
     * Should the filter display a day selection.
     *
     * @var boolean
     */
    private $daySelectionEnabled = false;

    /**
     * Days that can be selected.
     * Format Y-m-d
     *
     * @var string[]
     */
    private $days = [];

    /**
     * ShiftsFilterRenderer constructor.
     *
     * @param ShiftsFilter $shiftsFilter
     */
    public function __construct(ShiftsFilter $shiftsFilter)
    {
        $this->shiftsFilter = $shiftsFilter;
    }

    /**
     * Renders the filter.
     *
     * @param string $page_link Link pointing to the actual page.
     * @return string Generated HTML
     */
    public function render($page_link)
    {
        $toolbar = [];
        if ($this->daySelectionEnabled && !empty($this->days)) {
            $selected_day = date('Y-m-d', $this->shiftsFilter->getStartTime());
            $day_dropdown_items = [];
            foreach ($this->days as $day) {
                $link = $page_link . '&shifts_filter_day=' . $day;
                $day_dropdown_items[] = toolbar_item_link($link, '', $day);
            }
            $toolbar[] = toolbar_dropdown('', $selected_day, $day_dropdown_items, 'active');
        }
        return div('form-group', [
            toolbar_pills($toolbar)
        ]);
    }

    /**
     * Should the filter display a day selection.
     *
     * @param string[] $days
     */
    public function enableDaySelection($days)
    {
        $this->daySelectionEnabled = true;
        $this->days = $days;
    }

    /**
     * Should the filter display a day selection.
     *
     * @return bool
     */
    public function isDaySelectionEnabled()
    {
        return $this->daySelectionEnabled;
    }
}