Calendar.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Calendar extends Component
  5. {
  6. public $records;
  7. public $names = [];
  8. public $course_types = [];
  9. public $course_durations = [];
  10. public $course_frequencies = [];
  11. public $course_levels = [];
  12. public $courts = [];
  13. public $instructors = [];
  14. public $motivations = [];
  15. public $name_filter = null;
  16. public $name = null;
  17. public $from = null;
  18. public $to = null;
  19. public $court_id = null;
  20. public $instructor_id = null;
  21. public $note = null;
  22. public $course_type_id = null;
  23. public $course_duration_id = null;
  24. public $course_frequency_id = null;
  25. public $course_level_id = null;
  26. public $motivation_id = null;
  27. public function mount()
  28. {
  29. $this->names = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  30. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  31. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  32. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  33. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  34. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  35. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  36. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->get();
  37. if (isset($_GET["name_filter"]))
  38. $this->name_filter = $_GET["name_filter"];
  39. }
  40. public function render()
  41. {
  42. $reload = false;
  43. $this->records = [];
  44. if ($this->name_filter != null && $this->name_filter != "")
  45. {
  46. $calendars = \App\Models\Calendar::where('name', $this->name_filter)->get();
  47. $reload = true;
  48. }
  49. else
  50. $calendars = \App\Models\Calendar::get();
  51. foreach($calendars as $c)
  52. {
  53. $data = array('id' => $c->id, 'title' => $c->course ? $c->course->name : $c->name . ($c->status == 99 ? ' (annullata)' : ''), 'start' => $c->from, 'end' => $c->to);
  54. if ($c->course && $c->course->color != '')
  55. $data['color'] = $c->course->color;
  56. $this->records[] = $data;
  57. }
  58. if ($reload)
  59. $this->emit('reload-calendar', ["'" . json_encode($this->records) . "'"]);
  60. return view('livewire.calendar');
  61. }
  62. public function createCalendar()
  63. {
  64. $calendar = new \App\Models\Calendar();
  65. $calendar->course_id = null;
  66. $calendar->court_id = $this->court_id;
  67. $calendar->name = $this->name;
  68. $calendar->course_type_id = $this->course_type_id != '' ? $this->course_type_id : null;
  69. $calendar->course_duration_id = $this->course_duration_id != '' ? $this->course_duration_id : null;
  70. $calendar->course_frequency_id = $this->course_frequency_id != '' ? $this->course_frequency_id : null;
  71. $calendar->course_level_id = $this->course_level_id != '' ? $this->course_level_id : null;
  72. $calendar->instructor_id = $this->instructor_id;
  73. $calendar->from = $this->from;
  74. $calendar->to = $this->to;
  75. $calendar->note = $this->note;
  76. $calendar->status = 0;
  77. $calendar->manual = 1;
  78. $calendar->save();
  79. return redirect()->to('/presences?calendarId=' . $calendar->id);
  80. }
  81. public function cancelCalendar($id, $motivation_id)
  82. {
  83. $calendar = \App\Models\Calendar::findOrFail($id);
  84. $calendar->motivation_id = $motivation_id;
  85. $calendar->status = 99;
  86. $calendar->save();
  87. return redirect()->to('/calendar');
  88. }
  89. }