1: <?php
2: namespace Budovy;
3:
4: use Nette\Application\Responses\JsonResponse;
5:
6: 7: 8:
9: class HomepagePresenter extends BasePresenter {
10:
11: public $periodicTaskRepository;
12:
13:
14: public $scheduledTaskRepository;
15:
16:
17: public $breakdownRepository;
18:
19:
20: public $httpRequest;
21:
22: function renderDefault() {
23:
24: $enddate = new \DateTime();
25: $enddate->modify('+1 month');
26:
27:
28:
29:
30: $periodicTasks = [];
31: $allPeriodicTasks = $this->periodicTaskRepository->getTasksNotice();
32:
33: foreach ($allPeriodicTasks as $task) {
34: if ($this->periodicTaskRepository->checkPeriod($task->period, $task->date)) {
35: $periodicTasks[] = $task;
36: }
37: }
38:
39: $this->template->scheduledTasks = $this->scheduledTaskRepository->getTasksNotice();
40: $this->template->periodicTasks = $periodicTasks;
41: }
42:
43: function actionCalendar() {
44: $startdate = new \DateTime();
45: $enddate = new \DateTime();
46:
47: $params = $this->getHttpRequest()->getQuery();
48:
49: if (isset($params['start'])) {
50: $startdate->setTimestamp($params['start']);
51: }
52:
53: if (isset($params['end'])) {
54: $enddate->setTimestamp($params['end']);
55: }
56:
57: $obj = array();
58:
59:
60: $scheduledtasks = $this->scheduledTaskRepository->getTasksByDateRange($startdate, $enddate);
61: $breakdowns = $this->breakdownRepository->getBreakdownsByDateRange($startdate, $enddate);
62: $taskArray = $this->getPeriodicTasks($startdate, $enddate);
63:
64: foreach ($taskArray as $timestamp => $tasks) {
65: foreach ($tasks as $task) {
66: $ukol = new \stdClass();
67: $ukol->title = $task->name;
68: $ukol->start = date('Y-m-d', $timestamp);
69: $ukol->url = $this->link('PeriodicTask:preview', $task->id);
70: $ukol->color = $task->ref('priority')->color;
71:
72: $obj[] = $ukol;
73: }
74: }
75:
76: 77: 78:
79: foreach ($scheduledtasks as $task) {
80: $ukol = new \stdClass();
81: $ukol->title = $task->name;
82: $ukol->start = (string)$task->start;
83: $ukol->end = (string)$task->end;
84: $ukol->color = $task->ref('priority')->color;
85: $ukol->url = $this->link('ScheduledTask:preview', $task->id);
86:
87: $obj[] = $ukol;
88: }
89:
90: 91: 92:
93: foreach ($breakdowns as $task) {
94: $ukol = new \stdClass();
95: $ukol->title = $task->name;
96: $ukol->start = (string)$task->date;
97: $ukol->end = (string)$task->date;
98: $ukol->color = 'red';
99: $ukol->url = $this->link('Breakdown:preview', $task->id);
100:
101: $obj[] = $ukol;
102: }
103:
104: $this->sendResponse(new JsonResponse($obj));
105: }
106:
107: protected function getPeriodicTasks(\DateTime $startdate, \DateTime $enddate) {
108: $tasksArray = [];
109:
110: $tasks = $this->periodicTaskRepository->getTasksByDateRange($startdate, $enddate);
111: $taskmap = $this->createTaskMask($tasks);
112:
113: 114: 115:
116: for (;$startdate <= $enddate; $startdate->modify('+1 day')) {
117: $month = $startdate->format('m') - 1;
118: $timestamp = $startdate->getTimestamp();
119:
120: for ($n = 0; $n <= 10; $n++) {
121: if ($this->periodicTaskRepository->checkPeriod($n, $startdate) && isset($taskmap[$n])) {
122: foreach ($taskmap[$n] as $task) {
123: if ($task->months & pow(2, $month)) {
124: if (!isset($tasksArray[$timestamp])) {
125: $tasksArray[$timestamp] = [];
126: }
127:
128: $tasksArray[$timestamp][] = $task;
129: }
130: }
131: }
132: }
133: }
134:
135: return $tasksArray;
136: }
137:
138: protected function createTaskMask($tasks) {
139: $taskmap = [];
140:
141: foreach ($tasks as $task) {
142: if (!isset($taskmap[$task->period])) {
143: $taskmap[$task->period] = array();
144: }
145:
146: $taskmap[$task->period][] = $task;
147: }
148:
149: return $taskmap;
150: }
151: }