Course.php 9.5 KB

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