Course.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Course extends Component
  5. {
  6. protected $listeners = ['setCausal' => 'setCausal'];
  7. public $records, $parent_id, $name, $enabled, $dataId, $update = false, $add = false;
  8. public $course_type_id,
  9. $course_duration_id,
  10. $course_frequency_id,
  11. $course_level_id,
  12. $causal_id,
  13. $max_members,
  14. $instructor,
  15. $year,
  16. $price,
  17. $months,
  18. $date_from, $date_to;
  19. public $course_types = [];
  20. public $course_durations = [];
  21. public $course_frequencies = [];
  22. public $course_levels = [];
  23. public $causals = [];
  24. public $monthList = [];
  25. public $typeIN = 'IN';
  26. // public $selectedMonthList = [];
  27. protected $rules = [
  28. 'name' => 'required',
  29. 'course_type_id' => 'required',
  30. 'course_duration_id' => 'required',
  31. 'course_frequency_id' => 'required',
  32. 'course_level_id' => 'required',
  33. 'causal_id' => 'required'
  34. ];
  35. protected $messages = [
  36. 'name.required' => 'Il nome è obbligatorio'
  37. ];
  38. public function resetFields(){
  39. $this->name = '';
  40. $this->parent_id = null;
  41. $this->course_type_id = null;
  42. $this->course_duration_id = null;
  43. $this->course_frequency_id = null;
  44. $this->course_level_id = null;
  45. $this->causal_id = null;
  46. $this->max_members = 0;
  47. $this->instructor = '';
  48. $this->year = date("Y");
  49. $this->price = 0;
  50. $this->date_from = null;
  51. $this->date_to = null;
  52. $this->months = array();
  53. $this->enabled = true;
  54. }
  55. public function mount()
  56. {
  57. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  58. {
  59. $this->monthList[$i][1] = "Gennaio";
  60. $this->monthList[$i][2] = "Febbraio";
  61. $this->monthList[$i][3] = "Marzo";
  62. $this->monthList[$i][4] = "Aprile";
  63. $this->monthList[$i][5] = "Maggio";
  64. $this->monthList[$i][6] = "Giugno";
  65. $this->monthList[$i][7] = "Luglio";
  66. $this->monthList[$i][8] = "Agosto";
  67. $this->monthList[$i][9] = "Settembre";
  68. $this->monthList[$i][10] = "Ottobre";
  69. $this->monthList[$i][11] = "Novembre";
  70. $this->monthList[$i][12] = "Dicembre";
  71. }
  72. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  73. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  74. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  75. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  76. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  77. }
  78. public function render()
  79. {
  80. $this->records = \App\Models\Course::where('parent_id', null)->with('type', 'duration')->get();
  81. return view('livewire.course');
  82. }
  83. public function add()
  84. {
  85. $this->resetFields();
  86. $this->add = true;
  87. $this->update = false;
  88. }
  89. /*
  90. public function addLevel($parent_id)
  91. {
  92. $this->resetFields();
  93. $this->parent_id = $parent_id;
  94. $this->add = true;
  95. $this->update = false;
  96. }
  97. */
  98. public function store()
  99. {
  100. $this->validate();
  101. try {
  102. \App\Models\Course::create([
  103. 'name' => $this->name,
  104. 'parent_id' => $this->parent_id,
  105. 'course_type_id' => $this->course_type_id,
  106. 'course_duration_id' => $this->course_duration_id,
  107. 'course_frequency_id' => $this->course_frequency_id,
  108. 'course_level_id' => $this->course_level_id,
  109. 'date_from' => $this->date_from,
  110. 'date_to' => $this->date_to,
  111. 'causal_id' => $this->causal_id,
  112. 'max_members' => $this->max_members,
  113. 'instructor' => $this->instructor,
  114. 'year' => $this->year,
  115. 'price' => currencyToDouble($this->price),
  116. 'months' => json_encode($this->months),
  117. 'enabled' => $this->enabled
  118. ]);
  119. session()->flash('success','Corso creato');
  120. $this->resetFields();
  121. $this->add = false;
  122. } catch (\Exception $ex) {
  123. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  124. }
  125. }
  126. public function edit($id){
  127. try {
  128. $course = \App\Models\Course::findOrFail($id);
  129. if( !$course) {
  130. session()->flash('error','Corso non trovato');
  131. } else {
  132. $this->name = $course->name;
  133. $this->enabled = $course->enabled;
  134. $this->parent_id = $course->parent_id;
  135. $this->course_type_id = $course->course_type_id;
  136. $this->course_duration_id = $course->course_duration_id;
  137. $this->course_frequency_id = $course->course_frequency_id;
  138. $this->course_level_id = $course->course_level_id;
  139. $this->date_from = $course->date_from;
  140. $this->date_to = $course->date_to;
  141. $this->causal_id = $course->causal_id;
  142. $this->max_members = $course->max_members;
  143. $this->instructor = $course->instructor;
  144. $this->year = $course->year;
  145. $this->price = formatPrice($course->price);
  146. $this->months = json_decode($course->months);
  147. $this->dataId = $course->id;
  148. $this->update = true;
  149. $this->add = false;
  150. }
  151. } catch (\Exception $ex) {
  152. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  153. }
  154. }
  155. public function update()
  156. {
  157. $this->validate();
  158. try {
  159. \App\Models\Course::whereId($this->dataId)->update([
  160. 'name' => $this->name,
  161. 'parent_id' => $this->parent_id,
  162. 'course_type_id' => $this->course_type_id,
  163. 'course_duration_id' => $this->course_duration_id,
  164. 'course_frequency_id' => $this->course_frequency_id,
  165. 'course_level_id' => $this->course_level_id,
  166. 'date_from' => $this->date_from,
  167. 'date_to' => $this->date_to,
  168. 'causal_id' => $this->causal_id,
  169. 'max_members' => $this->max_members,
  170. 'instructor' => $this->instructor,
  171. 'year' => $this->year,
  172. 'price' => currencyToDouble($this->price),
  173. 'months' => json_encode($this->months),
  174. 'enabled' => $this->enabled
  175. ]);
  176. session()->flash('success','Corso aggiornato');
  177. $this->resetFields();
  178. $this->update = false;
  179. } catch (\Exception $ex) {
  180. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  181. }
  182. }
  183. public function cancel()
  184. {
  185. $this->add = false;
  186. $this->update = false;
  187. $this->resetFields();
  188. }
  189. public function delete($id)
  190. {
  191. try{
  192. \App\Models\Course::find($id)->delete();
  193. session()->flash('success',"Corso eliminato");
  194. }catch(\Exception $e){
  195. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  196. }
  197. }
  198. public function setCausal($id, $idx)
  199. {
  200. $this->causal_id = $id;
  201. }
  202. }