CalendarRemove.php 2.0 KB

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