| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Presence extends Component
- {
- public $calendar;
- public $records;
- public $court_id, $instructor_id, $motivation_id, $note;
- public $courts = [];
- public $instructors = [];
- public $motivations = [];
- public $members = [];
- public $newMembers = [];
- public $ids = [];
- public function mount()
- {
-
- $this->calendar = \App\Models\Calendar::findOrFail($_GET["calendarId"]);
- $this->court_id = $this->calendar->court_id;
- $this->instructor_id = $this->calendar->instructor_id;
- $this->note = $this->calendar->note;
- $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
- $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
- $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->get();
- $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
- }
- public function render()
- {
-
- $this->records = [];
-
- // Carco tutti gli iscritti a quel corso
- $members_courses = \App\Models\MemberCourse::where('course_id', $this->calendar->course_id)->pluck('member_id')->toArray();
- $members = \App\Models\Member::whereIn('id', $members_courses)->get();
- $presenceMembers = [];
- // $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->pluck('member_id')->toArray();
- // $my_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->pluck('member_id')->toArray();
- foreach($members as $member)
- {
- $presenceMembers[] = $member->id;
- $this->records[] = $this->getMember($member);
- }
- // Aggiungo i membri iscritti
- $members_presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereNotIn('member_id', $presenceMembers)->pluck('member_id')->toArray();
- $members = \App\Models\Member::whereIn('id', $members_presences)->get();
- foreach($members as $member)
- {
- $this->records[] = $this->getMember($member);
- }
- foreach($this->newMembers as $m)
- {
- $member = \App\Models\Member::findOrFail($m);
- $this->records[] = $this->getMember($member);
- }
- /*$calendars = \App\Models\Calendar::get();
- foreach($calendars as $c)
- {
- $this->records[] = array('id' => $c->id, 'title' => $c->course->name, 'start' => $c->from, 'end' => $c->to);
- }*/
- return view('livewire.presence');
- }
- public function getMember($member)
- {
- $latestCert = \App\Models\MemberCertificate::where('member_id', $member->id)
- ->orderBy('expire_date', 'desc')
- ->first();
- $certificate = '';
- if ($latestCert)
- {
- $latest_date = $latestCert->expire_date;
- $status = '';
- if ($latest_date < date("Y-m-d")) {
- $status = "0"; // Expired
- } else if ($latest_date <= date("Y-m-d", strtotime("+1 month"))) {
- $status = "1"; // Expiring soon
- } else {
- $status = "2"; // Valid
- }
- $y = $status . "|" . date("d/m/Y", strtotime($latest_date));
- }
- $presence = false;
- $my_presence = false;
- $status = 0;
- $has_presence = \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('member_id', $member->id)->first();
- if ($has_presence)
- {
- $presence = true;
- $my_presence = $has_presence->user_id == \Auth::user()->id;
- $status = $has_presence->status;
- }
- return array('id' => $member->id, 'first_name' => $member->first_name, 'last_name' => $member->last_name, 'certificate' => $y, 'presence' => $presence, 'my_presence' => $my_presence, 'status' => $status);
- }
- public function save($ids)
- {
- $this->calendar->court_id = $this->court_id;
- $this->calendar->instructor_id = $this->instructor_id;
- $this->calendar->note = $this->note;
- if ($this->motivation_id != "" && $this->motivation_id != null)
- $this->calendar->motivation_id = $this->motivation_id;
- $this->calendar->save();
- \App\Models\Presence::where('calendar_id', $this->calendar->id)->where('user_id', \Auth::user()->id)->delete();
- foreach($ids as $id)
- {
- $p = new \App\Models\Presence();
- $p->member_id = $id;
- $p->calendar_id = $this->calendar->id;
- $p->user_id = \Auth::user()->id;
- $p->status = 0;
- $p->save();
- }
- return redirect()->to('/calendar');
- }
- public function cancel($ids)
- {
- $presences = \App\Models\Presence::where('calendar_id', $this->calendar->id)->whereIn('user_id', $ids)->get();
- foreach($presences as $presence)
- {
- $presence->status = 99;
- $presence->save();
- }
- }
- public function addMember($id)
- {
- if (!in_array($id, $this->newMembers))
- $this->newMembers[] = $id;
- $this->emit('reload');
- }
- public function cancelCalendar()
- {
- $this->calendar->motivation_id = $this->motivation_id;
- $this->calendar->status = 99;
- $this->calendar->save();
- return redirect()->to('/calendar');
- }
- }
|