Calendar.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 $festivities = [];
  28. public $css_festivities = '';
  29. public $lastDate = null;
  30. public $course_colors = [];
  31. public function mount()
  32. {
  33. $this->names = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
  34. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  35. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  36. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  37. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  38. $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
  39. $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
  40. $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
  41. $this->course_colors = \App\Models\CourseColor::pluck('hex', 'name')->toArray();
  42. if (isset($_GET["name_filter"]))
  43. $this->name_filter = $_GET["name_filter"];
  44. }
  45. public function render()
  46. {
  47. $reload = false;
  48. $this->records = [];
  49. if ($this->name_filter != null && $this->name_filter != "")
  50. {
  51. $calendars = \App\Models\Calendar::where('name', $this->name_filter)->get();
  52. $reload = true;
  53. }
  54. else
  55. $calendars = \App\Models\Calendar::get();
  56. foreach($calendars as $c)
  57. {
  58. $s = $c->motivation ? $c->motivation->name : '';
  59. $data = array('id' => $c->id, 'title' => ($c->course ? $c->course->name : $c->name) . ($c->status == 99 ? ' (annullata - ' . $s . ')' : ''), 'start' => $c->from, 'end' => $c->to);
  60. // if ($c->course && $c->course->color != '')
  61. // $data['color'] = $c->course->color;
  62. if (isset($this->course_colors[$c->name]))
  63. $data['color'] = $this->course_colors[$c->name];
  64. if ($c->status == 99)
  65. $data['color'] = "#808080";
  66. $this->records[] = $data;
  67. }
  68. for ($anno = 2025; $anno <= 2036; $anno++) {
  69. // $background_color = "transparent";
  70. // $color = "black";
  71. $this->records[] = array('id' => 0, 'title' => 'Capodanno', 'allDay' => true, 'start' => "$anno-01-01 00:00:00", 'end' => "$anno-01-01 23:59:59", 'classNames' => ["festivity"]);
  72. $this->records[] = array('id' => 0, 'title' => 'Epifania', 'allDay' => true, 'start' => "$anno-01-06 00:00:00", 'end' => "$anno-01-06 23:59:59", 'classNames' => ["festivity"]);
  73. $this->records[] = array('id' => 0, 'title' => 'Festa della Liberazione', 'allDay' => true, 'start' => "$anno-04-25 00:00:00", 'end' => "$anno-04-25 23:59:59", 'classNames' => ["festivity"]);
  74. $this->records[] = array('id' => 0, 'title' => 'Festa del Lavoro', 'allDay' => true, 'start' => "$anno-05-01 00:00:00", 'end' => "$anno-05-01 23:59:59", 'classNames' => ["festivity"]);
  75. $this->records[] = array('id' => 0, 'title' => 'Festa della Repubblica', 'allDay' => true, 'start' => "$anno-06-02 00:00:00", 'end' => "$anno-06-02 23:59:59", 'classNames' => ["festivity"]);
  76. $this->records[] = array('id' => 0, 'title' => 'Ferragosto', 'allDay' => true, 'start' => "$anno-08-15 00:00:00", 'end' => "$anno-08-15 23:59:59", 'classNames' => ["festivity"]);
  77. $this->records[] = array('id' => 0, 'title' => 'Ognissanti', 'allDay' => true, 'start' => "$anno-11-01 00:00:00", 'end' => "$anno-11-01 23:59:59", 'classNames' => ["festivity"]);
  78. $this->records[] = array('id' => 0, 'title' => 'Immacolata Concezione', 'allDay' => true, 'start' => "$anno-12-08 00:00:00", 'end' => "$anno-12-08 23:59:59", 'classNames' => ["festivity"]);
  79. $this->records[] = array('id' => 0, 'title' => 'Natale', 'allDay' => true, 'start' => "$anno-12-25 00:00:00", 'end' => "$anno-12-25 23:59:59", 'classNames' => ["festivity"]);
  80. $this->records[] = array('id' => 0, 'title' => 'Santo Stefano', 'allDay' => true, 'start' => "$anno-12-26 00:00:00", 'end' => "$anno-12-26 23:59:59", 'classNames' => ["festivity"]);
  81. $pasqua = date("Y-m-d", easter_date($anno));
  82. $this->records[] = array('id' => 0, 'title' => 'Pasqua', 'allDay' => true, 'start' => "$pasqua 00:00:00", 'end' => "$pasqua 23:59:59", 'classNames' => ["festivity"]);
  83. $pasquetta = date("Y-m-d", strtotime("$pasqua +1 day"));
  84. $this->records[] = array('id' => 0, 'title' => 'Pasquetta', 'allDay' => true, 'start' => "$pasquetta 00:00:00", 'end' => "$pasquetta 23:59:59", 'classNames' => ["festivity"]);
  85. $this->festivities[] = "$anno-01-01";
  86. $this->festivities[] = "$anno-01-06";
  87. $this->festivities[] = "$anno-04-25";
  88. $this->festivities[] = "$anno-05-01";
  89. $this->festivities[] = "$anno-06-02";
  90. $this->festivities[] = "$anno-08-15";
  91. $this->festivities[] = "$anno-11-01";
  92. $this->festivities[] = "$anno-12-08";
  93. $this->festivities[] = "$anno-12-25";
  94. $this->festivities[] = "$anno-12-26";
  95. $this->festivities[] = "$pasqua";
  96. $this->festivities[] = "$pasquetta";
  97. }
  98. $this->festivities = array_map(function($date) {
  99. return "[data-date='$date']";
  100. }, $this->festivities);
  101. $this->css_festivities = implode(", ", $this->festivities);
  102. $this->lastDate = null;
  103. if (isset($_GET['last_date'])) {
  104. try {
  105. $this->lastDate = \Illuminate\Support\Facades\Date::createFromFormat('Y-m-d', $_GET['last_date']);
  106. } catch (\Throwable $e) {
  107. $this->lastDate = null;
  108. }
  109. if (!$this->lastDate) {
  110. $this->lastDate = null;
  111. } else {
  112. $this->lastDate = $this->lastDate->format("Y-m-d");
  113. }
  114. }
  115. if ($reload)
  116. $this->emit('reload-calendar', ["'" . json_encode($this->records) . "'"]);
  117. return view('livewire.calendar');
  118. }
  119. public function createCalendar()
  120. {
  121. $calendar = new \App\Models\Calendar();
  122. $calendar->course_id = null;
  123. $calendar->court_id = $this->court_id != '' ? $this->court_id : null;
  124. $calendar->name = $this->name;
  125. $calendar->course_type_id = $this->course_type_id != '' ? $this->course_type_id : null;
  126. $calendar->course_duration_id = $this->course_duration_id != '' ? $this->course_duration_id : null;
  127. $calendar->course_frequency_id = $this->course_frequency_id != '' ? $this->course_frequency_id : null;
  128. $calendar->course_level_id = $this->course_level_id != '' ? $this->course_level_id : null;
  129. $calendar->instructor_id = $this->instructor_id != '' ? $this->instructor_id : null;
  130. $calendar->from = $this->from;
  131. $calendar->to = $this->to;
  132. $calendar->note = $this->note;
  133. $calendar->status = 0;
  134. $calendar->manual = 1;
  135. $calendar->save();
  136. return redirect()->to('/presences?calendarId=' . $calendar->id);
  137. }
  138. public function cancelCalendar($id, $motivation_id)
  139. {
  140. $calendar = \App\Models\Calendar::findOrFail($id);
  141. $calendar->motivation_id = $motivation_id;
  142. $calendar->status = 99;
  143. $calendar->save();
  144. return redirect()->to('/calendar');
  145. }
  146. public function revertCalendarDeletion($id)
  147. {
  148. $calendar = \App\Models\Calendar::findOrFail($id);
  149. $calendar->motivation_id = null;
  150. $calendar->status = 0;
  151. $calendar->save();
  152. return redirect()->to('/calendar');
  153. }
  154. }