Calendar.php 8.5 KB

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