Calendar.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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)->where('type', 'del')->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. $s = $c->motivation ? $c->motivation->name : '';
  54. $data = array('id' => $c->id, 'title' => $c->course ? $c->course->name : $c->name . ($c->status == 99 ? ' (annullata - ' . $s . ')' : ''), 'start' => $c->from, 'end' => $c->to);
  55. if ($c->course && $c->course->color != '')
  56. $data['color'] = $c->course->color;
  57. if ($c->status == 99)
  58. $data['color'] = "#808080";
  59. $this->records[] = $data;
  60. }
  61. for ($anno = 2025; $anno <= 2040; $anno++) {
  62. $color = "grey";
  63. $this->records[] = array('id' => 0, 'title' => 'Capodanno', 'start' => "$anno-01-01 00:00:00", 'end' => "$anno-01-01 23:59:59", 'color' => $color);
  64. $this->records[] = array('id' => 0, 'title' => 'Epifania', 'start' => "$anno-01-06 00:00:00", 'end' => "$anno-01-06 23:59:59", 'color' => $color);
  65. $this->records[] = array('id' => 0, 'title' => 'Festa della Liberazione', 'start' => "$anno-04-25 00:00:00", 'end' => "$anno-04-25 23:59:59", 'color' => $color);
  66. $this->records[] = array('id' => 0, 'title' => 'Festa del Lavoro', 'start' => "$anno-05-01 00:00:00", 'end' => "$anno-05-01 23:59:59", 'color' => $color);
  67. $this->records[] = array('id' => 0, 'title' => 'Festa della Repubblica', 'start' => "$anno-06-02 00:00:00", 'end' => "$anno-06-02 23:59:59", 'color' => $color);
  68. $this->records[] = array('id' => 0, 'title' => 'Ferragosto', 'start' => "$anno-08-15 00:00:00", 'end' => "$anno-08-15 23:59:59", 'color' => $color);
  69. $this->records[] = array('id' => 0, 'title' => 'Ognissanti', 'start' => "$anno-11-01 00:00:00", 'end' => "$anno-11-01 23:59:59", 'color' => $color);
  70. $this->records[] = array('id' => 0, 'title' => 'Immacolata Concezione', 'start' => "$anno-12-08 00:00:00", 'end' => "$anno-12-08 23:59:59", 'color' => $color);
  71. $this->records[] = array('id' => 0, 'title' => 'Natale', 'start' => "$anno-12-25 00:00:00", 'end' => "$anno-12-25 23:59:59", 'color' => $color);
  72. $this->records[] = array('id' => 0, 'title' => 'Santo Stefano', 'start' => "$anno-12-26 00:00:00", 'end' => "$anno-12-26 23:59:59", 'color' => $color);
  73. $pasqua = date("Y-m-d", easter_date($anno));
  74. $this->records[] = array('id' => 0, 'title' => 'Pasqua', 'start' => "$pasqua 00:00:00", 'end' => "$pasqua 23:59:59", 'color' => $color);
  75. $pasquetta = date("Y-m-d", strtotime("$pasqua +1 day"));
  76. $this->records[] = array('id' => 0, 'title' => 'Pasquetta', 'start' => "$pasquetta 00:00:00", 'end' => "$pasquetta 23:59:59", 'color' => $color);
  77. }
  78. if ($reload)
  79. $this->emit('reload-calendar', ["'" . json_encode($this->records) . "'"]);
  80. return view('livewire.calendar');
  81. }
  82. public function createCalendar()
  83. {
  84. $calendar = new \App\Models\Calendar();
  85. $calendar->course_id = null;
  86. $calendar->court_id = $this->court_id != '' ? $this->court_id : null;
  87. $calendar->name = $this->name;
  88. $calendar->course_type_id = $this->course_type_id != '' ? $this->course_type_id : null;
  89. $calendar->course_duration_id = $this->course_duration_id != '' ? $this->course_duration_id : null;
  90. $calendar->course_frequency_id = $this->course_frequency_id != '' ? $this->course_frequency_id : null;
  91. $calendar->course_level_id = $this->course_level_id != '' ? $this->course_level_id : null;
  92. $calendar->instructor_id = $this->instructor_id != '' ? $this->instructor_id : null;
  93. $calendar->from = $this->from;
  94. $calendar->to = $this->to;
  95. $calendar->note = $this->note;
  96. $calendar->status = 0;
  97. $calendar->manual = 1;
  98. $calendar->save();
  99. return redirect()->to('/presences?calendarId=' . $calendar->id);
  100. }
  101. public function cancelCalendar($id, $motivation_id)
  102. {
  103. $calendar = \App\Models\Calendar::findOrFail($id);
  104. $calendar->motivation_id = $motivation_id;
  105. $calendar->status = 99;
  106. $calendar->save();
  107. return redirect()->to('/calendar');
  108. }
  109. }