1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Handlers\Calendar; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use SET\TrainingUser; |
7
|
|
|
use SET\Travel; |
8
|
|
|
use SET\User; |
9
|
|
|
|
10
|
|
|
class Calendar |
11
|
|
|
{ |
12
|
|
|
private $start; |
13
|
|
|
private $end; |
14
|
|
|
private $calendarArray; |
15
|
|
|
private $test; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Name of function that generates a collection followed by an array of dates |
19
|
|
|
* we want to mark in our calendar/agenda. |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $lists = [ |
23
|
|
|
'separatedList' => ['destroyed_date'], |
24
|
|
|
'travelsList' => ['leave_date', 'return_date'], |
25
|
|
|
'trainingUsersList' => ['due_date'], |
26
|
|
|
'newUserList' => ['created_at'] |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->start = Carbon::today()->subWeeks(1); |
32
|
|
|
$this->end = Carbon::today()->addMonths(2); |
33
|
|
|
$this->calendarArray = []; |
34
|
|
|
|
35
|
|
|
$this->generateCalendarItems(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getCalendar() |
39
|
|
|
{ |
40
|
|
|
return $this->calendarArray; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function generateCalendarItems() |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
//Let's build that calendar. |
47
|
|
|
$date = $this->start; |
48
|
|
|
$i = 0; |
49
|
|
|
|
50
|
|
|
$list = $this->callFunctionsFromLookup(); |
51
|
|
|
|
52
|
|
|
while ($date <= $this->end) { |
53
|
|
|
$currentDate = $date->format('Y-m-d'); |
54
|
|
|
$this->test = false; |
55
|
|
|
|
56
|
|
|
foreach($this->lists as $functionName => $columns) { |
57
|
|
|
$list[$functionName] = $this->buildArrayByDate($list[$functionName], $columns, $currentDate); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($this->test || $currentDate == Carbon::today()->format('Y-m-d')) { |
61
|
|
|
$this->calendarArray[$i] = [ |
62
|
|
|
'date' => $currentDate, |
63
|
|
|
'separated' => $list['separatedList'], |
64
|
|
|
'travel' => $list['travelsList'], |
65
|
|
|
'trainingUser' => $this->groupUsersForTraining($list['trainingUsersList']), |
66
|
|
|
'newUser' => $list['newUserList'] |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$date->addDay(); |
71
|
|
|
$i++; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function buildArrayByDate($list, array $columnName, $date) |
76
|
|
|
{ |
77
|
|
|
$array = []; |
78
|
|
|
|
79
|
|
|
foreach ($list as $item) { |
80
|
|
|
foreach ($columnName as $column) { |
81
|
|
|
$dbDate = $this->testForCarbonObject($item[$column]); |
82
|
|
|
if ($date == $dbDate) { |
83
|
|
|
$this->test = true; |
84
|
|
|
array_push($array, $item); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $array; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* If a carbon object, convert it to our date string. Otherwise leave it alone. |
94
|
|
|
* |
95
|
|
|
* @param date |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
private function testForCarbonObject($date) |
100
|
|
|
{ |
101
|
|
|
//check if format is YYYY-MM-DD |
102
|
|
|
if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)) { |
103
|
|
|
return $date; |
104
|
|
|
} elseif (get_class($date) == 'Carbon\Carbon') { |
105
|
|
|
return $date->format('Y-m-d'); |
106
|
|
|
} else { |
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function groupUsersForTraining($trainingUsers) |
112
|
|
|
{ |
113
|
|
|
$array = []; |
114
|
|
|
foreach ($trainingUsers as $key => $item) { |
115
|
|
|
$array[$item['training_id']][$key] = $item; |
116
|
|
|
} |
117
|
|
|
ksort($array, SORT_NUMERIC); |
118
|
|
|
|
119
|
|
|
foreach ($array as $training) { |
120
|
|
|
foreach ($training as $trainingUser) { |
121
|
|
|
$trainingUser['userLink'] = "<a href='".url('user', $trainingUser->user_id)."'>".$trainingUser->user->userFullName.'</a>'; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $array; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
private function callFunctionsFromLookup() |
132
|
|
|
{ |
133
|
|
|
$array = []; |
134
|
|
|
foreach ($this->lists as $functionName => $columns) { |
135
|
|
|
$array[$functionName] = $this->$functionName(); |
136
|
|
|
} |
137
|
|
|
return $array; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
private function separatedList() |
144
|
|
|
{ |
145
|
|
|
return User::where(function ($q) { |
146
|
|
|
$q->where('status', 'separated')->orWhere('status', 'destroyed'); |
147
|
|
|
}) |
148
|
|
|
->whereBetween('destroyed_date', [$this->start, $this->end]) |
149
|
|
|
->get(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
154
|
|
|
*/ |
155
|
|
|
private function travelsList() |
156
|
|
|
{ |
157
|
|
|
return Travel::with('user') |
158
|
|
|
->where(function ($query) { |
159
|
|
|
$query->whereBetween('leave_date', [$this->start, $this->end]) |
160
|
|
|
->orWhereBetween('return_date', [$this->start, $this->end]); |
161
|
|
|
}) |
162
|
|
|
->get(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
private function trainingUsersList() |
169
|
|
|
{ |
170
|
|
|
return TrainingUser::with('user', 'training') |
171
|
|
|
->whereBetween('due_date', [$this->start, $this->end]) |
172
|
|
|
->whereNull('completed_date') |
173
|
|
|
->orderBy('training_id') |
174
|
|
|
->get(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function newUserList() |
178
|
|
|
{ |
179
|
|
|
return User::whereBetween('created_at', [$this->start, $this->end])->get(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|