|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SET\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
7
|
|
|
use Illuminate\Support\Facades\Auth; |
|
8
|
|
|
use Illuminate\Support\Facades\Gate; |
|
9
|
|
|
use Illuminate\Support\Facades\Request; |
|
10
|
|
|
use SET\Duty; |
|
11
|
|
|
use SET\Handlers\Calendar\Calendar; |
|
12
|
|
|
use SET\Handlers\Duty\DutyGroups; |
|
13
|
|
|
use SET\Handlers\Duty\DutyUsers; |
|
14
|
|
|
use SET\Training; |
|
15
|
|
|
use SET\TrainingUser; |
|
16
|
|
|
use SET\User; |
|
17
|
|
|
|
|
18
|
|
|
class HomeController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Display a listing of the resource. |
|
22
|
|
|
* Return notes with due dates 4 weeks from now. |
|
23
|
|
|
* Return last 20 recent notes. |
|
24
|
|
|
* |
|
25
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View |
|
26
|
|
|
*/ |
|
27
|
|
|
public function index() |
|
28
|
|
|
{ |
|
29
|
|
|
//if you don't have view ability, redirect to your own page. |
|
30
|
|
|
if (Gate::denies('view')) { |
|
31
|
|
|
return redirect()->action('UserController@show', Auth::user()->id); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$trainingUser = TrainingUser::with('user', 'training') |
|
35
|
|
|
->where('completed_date', '>=', Carbon::today()->subWeek(1)) |
|
36
|
|
|
->orderBy('updated_at', 'DESC') |
|
37
|
|
|
->get(); |
|
38
|
|
|
|
|
39
|
|
|
$activityLog = (new User())->getUserLog(); |
|
40
|
|
|
|
|
41
|
|
|
$calendar = (new Calendar())->getCalendar(); |
|
42
|
|
|
|
|
43
|
|
|
$duties = $this->getDuties(); |
|
44
|
|
|
|
|
45
|
|
|
return view('home.index', compact('trainingUser', 'activityLog', 'calendar', 'duties')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Return a list of users & trainings that will be used for our ajax search bar in the headers. |
|
50
|
|
|
* |
|
51
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
52
|
|
|
*/ |
|
53
|
|
|
public function search() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->authorize('view'); |
|
56
|
|
|
$qInput = Request::input('q'); |
|
57
|
|
|
$status = true; |
|
58
|
|
|
$dbUsers = User::skipSystem()->searchUsers($qInput) |
|
59
|
|
|
->get(['id', 'first_name', 'last_name', 'status', 'emp_num']); |
|
60
|
|
|
$dbTraining = Training::searchTraining($qInput)->get(['id', 'name']); |
|
61
|
|
|
|
|
62
|
|
|
// Means no result were found |
|
63
|
|
|
if (count($dbUsers) <= 0 && count($dbTraining) <= 0) { |
|
64
|
|
|
$status = false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return response()->json([ |
|
68
|
|
|
'status' => $status, |
|
69
|
|
|
'error' => null, |
|
70
|
|
|
'data' => [ |
|
71
|
|
|
'user' => $dbUsers, |
|
72
|
|
|
'training' => $dbTraining, |
|
73
|
|
|
], |
|
74
|
|
|
]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function getDuties() |
|
78
|
|
|
{ |
|
79
|
|
|
$newCollection = new Collection(); |
|
80
|
|
|
$allDuties = Duty::all(); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($allDuties as $duty) { |
|
83
|
|
|
if ($duty->has_groups) { |
|
84
|
|
|
$userList = (new DutyGroups($duty))->getList()->first()['group']; |
|
85
|
|
|
$groupUsers = $this->getHtmlUserOutput($userList); |
|
86
|
|
|
$newCollection->push([ |
|
87
|
|
|
'duty' => $duty->name, |
|
88
|
|
|
'user' => implode('; ', $groupUsers), |
|
89
|
|
|
]); |
|
90
|
|
|
} else { |
|
91
|
|
|
$user = (new DutyUsers($duty))->getList()->first()['user']; |
|
92
|
|
|
$newCollection->push([ |
|
93
|
|
|
'duty' => $duty->name, |
|
94
|
|
|
'user' => "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a>', |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $newCollection; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function getHtmlUserOutput($userList) |
|
103
|
|
|
{ |
|
104
|
|
|
foreach ($userList as $user) { |
|
105
|
|
|
$groupUsers[] = "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a>'; |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $groupUsers; |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.