CalendarRemove.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class CalendarRemove extends Component
  5. {
  6. public $records;
  7. public $groups = [];
  8. public function render()
  9. {
  10. $this->records = [];
  11. $this->groups = [];
  12. $calendars = \App\Models\Calendar::orderBy('from')->get();
  13. foreach($calendars as $calendar)
  14. {
  15. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  16. $dow = date('w', strtotime($calendar->from));
  17. $d = $days[$dow];
  18. $h = date('H:i', strtotime($calendar->from));
  19. // Elenco corsi per tipologia in base al calendario
  20. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to)->pluck('id')->toArray();
  21. $months = date("n", strtotime($calendar->from));
  22. $members_count = \App\Models\MemberCourse::query()
  23. ->whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])
  24. ->whereRaw("JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED)), '$')", [$months])
  25. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', 2), '$')", [$months])
  26. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', '2'), '$')", [$months])
  27. ->whereIn('course_id', $courses)
  28. ->count();
  29. if ($members_count == 0)
  30. {
  31. $hour = date("H:i", strtotime($calendar->from));
  32. $x = ($calendar->course ? ($calendar->course->name . " ") : "") . $d . " " . $hour;
  33. if (!in_array($x, $this->groups))
  34. $this->groups[] = $x;
  35. $this->records[] = array('id' => $calendar->id, 'date' => date("d/m/Y", strtotime($calendar->from)), "name" => $calendar->name, "hour" => $hour, "day" => $d);
  36. }
  37. }
  38. return view('livewire.calendar_remove');
  39. }
  40. public function removeSelected($ids)
  41. {
  42. foreach($ids as $id)
  43. {
  44. \App\Models\Calendar::findOrFail($id)->delete();
  45. }
  46. return redirect()->to('/calendar_remove');
  47. }
  48. }