CalendarRemove.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 $data = [];
  9. public function render()
  10. {
  11. $this->records = [];
  12. $this->groups = [];
  13. $calendars = \App\Models\Calendar::orderBy('from')->get();
  14. foreach($calendars as $calendar)
  15. {
  16. $days = ['dom', 'lun', 'mar', 'mer', 'gio', 'ven', 'sab'];
  17. $dow = date('w', strtotime($calendar->from));
  18. $d = $days[$dow];
  19. $h = date('H:i', strtotime($calendar->from));
  20. // Elenco corsi per tipologia in base al calendario
  21. $courses = \App\Models\Course::where('name', $calendar->name)->where('date_from', '<=', $calendar->from)->where('date_to', '>=', $calendar->to)->pluck('id')->toArray();
  22. $months = date("n", strtotime($calendar->from));
  23. $members_count = \App\Models\MemberCourse::query()
  24. ->whereRaw("JSON_CONTAINS(`when`, JSON_OBJECT('day', JSON_ARRAY(?), 'from', ?), '$')", [$d, $h])
  25. ->whereRaw("JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED)), '$')", [$months])
  26. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', 2), '$')", [$months])
  27. ->whereRaw("NOT JSON_CONTAINS(months, JSON_OBJECT('m', CAST(? AS UNSIGNED), 'status', '2'), '$')", [$months])
  28. ->whereIn('course_id', $courses)
  29. ->count();
  30. if ($members_count == 0)
  31. {
  32. $hour = date("H:i", strtotime($calendar->from));
  33. $x = ($calendar->course ? ($calendar->course->name . " ") : "") . $d . " " . $hour;
  34. if (!in_array($x, $this->groups)) {
  35. $this->groups[] = $x;
  36. if ($calendar->course) {
  37. if (!isset($this->data[$calendar->course->name])) {
  38. $this->data[$calendar->course->name] = [];
  39. }
  40. $this->data[$calendar->course->name][$dow][] = [
  41. 'calendar_id' => $calendar->id,
  42. 'grp' => $x,
  43. 'hour' => $hour,
  44. ];
  45. }
  46. }
  47. $this->records[] = array('id' => $calendar->id, 'date' => date("d/m/Y", strtotime($calendar->from)), "name" => $calendar->name, "hour" => $hour, "day" => $d);
  48. }
  49. }
  50. // ordinamento corsi/giorni/ore
  51. ksort($this->data, SORT_STRING);
  52. foreach ($this->data as &$days) {
  53. ksort($days, SORT_NUMERIC);
  54. foreach($days as &$times) {
  55. usort($times, function ($a, $b) {
  56. return strtotime($a['hour']) <=> strtotime($b['hour']);
  57. });
  58. }
  59. }
  60. unset($days, $times);
  61. return view('livewire.calendar_remove');
  62. }
  63. public function removeSelected($ids)
  64. {
  65. foreach($ids as $id)
  66. {
  67. \App\Models\Calendar::findOrFail($id)->delete();
  68. }
  69. return redirect()->to('/calendar_remove');
  70. }
  71. }