Course.php 7.3 KB

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