-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
157 lines (124 loc) · 3.25 KB
/
controller.php
File metadata and controls
157 lines (124 loc) · 3.25 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php namespace timetable;
class Controller
{
const TPL_LOGIN = "login";
const TPL_OVERVIEW = "overview";
protected $dataForView = array();
/**
* @var Model $model
*/
private static $model;
/**
* Controller constructor.
*/
public function __construct()
{
if (self::$model == null)
self::$model = new Model();
}
/**
* @param array $input
*/
public function handleInput($input)
{ //TODO: LOGIN!!
$this->showOverview();
die(); //TODO let the magic happen
if(!isset($input['type']))
{
$this->display(self::TPL_LOGIN);
return;
}
$type = $input['type'];
switch ($type)
{
case 'login':
break;
default:
$this->display(self::TPL_LOGIN);
break;
}
}
public function showOverview()
{
$lessons = self::$model->getLessons();
$this->sortByLesson($lessons);
$this->compactDuplicate($lessons);
$lessonsByDate = array();
/** @var Lesson $lesson */
foreach ($lessons as $lesson)
$lessonsByDate[$this->getDayDescription($lesson->getDate())][] = $lesson;
$this->dataForView['lessons'] = $lessonsByDate;
$this->display(self::TPL_OVERVIEW);
}
/**
* @param string $day
* @return string
*/
public function getDayDescription($day)
{
$day = explode("den ", $day)[1]; //dd.mm.yyy
$dateTime = \DateTime::createFromFormat("d.m.Y", $day);
$day = $dateTime->format("D. d.m.y");
return $day;
}
/**
* @param string $template Template to be displayed
*/
public function display($template)
{
ChromePhp::info("Displaying $template with data " . json_encode($this->dataForView));
$view = View::getInstance();
$view->setDataForView($this->dataForView);
$view->loadTemplate($template);
}
/**
* @param $array
*/
public function sortByLesson(&$array)
{
usort($array, function ($a, $b)
{
/**
* @var Lesson $a
* @var Lesson $b
*/
return strcmp($a->getHour(), $b->getHour());
});
}
/**
* @param $array
*/
public function compactDuplicate(&$array)
{
$removed = array();
/**
* @var Lesson $a
* @var Lesson $b
*/
foreach ($array as $a)
{
foreach ($array as $b)
{
if(strpos($a->getHour(), "-") !== false || strpos($b->getHour(), "-") !== false)
continue;
if($a->isConnectedClass($b))
{
$a->setHour($a->getHour() . "-" . $b->getHour());
$this->unsetValue($array, $b);
}
}
}
}
/**
* Removes a value from an array
* @param array $array value should be removed from
* @param mixed $value value to be removed
*/
public function unsetValue(&$array, $value)
{
if(($key = array_search($value, $array)) !== false) {
unset($array[$key]);
}
}
}
?>