summaryrefslogtreecommitdiff
path: root/app/frontend/dto/GridEventGroupDTO.php
blob: 58450760ee3a81677197713a3c10e9f87623d806 (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
<?php

Prado::using('Application.dto.GridEventDTO');
Prado::using('Application.controls.grid.IGridElementDTO');

class GridEventGroupDTO implements IGridElementDTO {

    public $Header;
    public $Events;
    public $Calendar;
    public $GridPosition;

    public function __construct(array $events) {
        $this->Events = $events;
        if ($this->Events) {
            $this->Calendar = $this->Events[0]->Calendar;
        }
        usort($this->Events, ['GridEventDTO', '__compare']);
        $this->Header = $this->_determineCommonHeader($this->Events);
    }

    public static function __compare(GridEventGroupDTO $ev1, GridEventGroupDTO $ev2) {
        if ($ev1->GridPosition === NULL || $ev2->GridPosition === NULL) {
            if (count($ev1->Events) && count($ev2->Events)) {
                return EventDTO::__compare($ev1->Events[0], $ev2->Events[0]);
            }
        }
        return $ev1->GridPosition - $ev2->GridPosition;
    }

    private function _determineCommonHeader(array $events) {
        if (!$events) {
            return NULL;
        }
        $names = array_map(
            function(EventDTO $event) {
                return $event->Name;
            },
            $events
        );
        $prefix = $names[0];
        $passed = FALSE;
        while (!$passed && $prefix) {
            $passed = TRUE;
            $prefixLength = strlen($prefix);
            foreach ($names as $name) {
                if (substr($name, 0, $prefixLength) != $prefix) {
                    $passed = FALSE;
                    break;
                }
            }
            if ($passed) {
                foreach ($names as $name) {
                    if (is_numeric(trim(substr($name, $prefixLength)))) {
                        $passed = FALSE;
                        break;
                    }
                }
            }
            if (!$passed) {
                $prefix = substr($prefix, 0, strrpos($prefix, ' '));
            }
        }
        return $prefix ?: $events[0]->Calendar->Name;
    }

    public function getRendererClass() {
        return 'Group';
    }

}

?>