Calendar.php 8.3 KB

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