Course.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $this->emit('load-data-table');
  66. }
  67. public function getCategories($records, $indentation)
  68. {
  69. foreach($records as $record)
  70. {
  71. // $this->categories[] = array('id' => $record->id, 'name' => str_repeat(" / ", $indentation) . $record->name);
  72. $this->categories[] = array('id' => $record->id, 'name' => $record->getTree(), 'indentation' => $indentation);
  73. if(count($record->childs))
  74. $this->getCategories($record->childs, $indentation + 1);
  75. }
  76. }
  77. public function mount()
  78. {
  79. $this->categories = array();
  80. $this->getCategories(\App\Models\Category::select('id', 'name')->where('parent_id', null)->get(), 0);
  81. for($i=date("Y"); $i<=date("Y") + 1; $i++)
  82. {
  83. $this->monthList[$i][1] = "Gennaio";
  84. $this->monthList[$i][2] = "Febbraio";
  85. $this->monthList[$i][3] = "Marzo";
  86. $this->monthList[$i][4] = "Aprile";
  87. $this->monthList[$i][5] = "Maggio";
  88. $this->monthList[$i][6] = "Giugno";
  89. $this->monthList[$i][7] = "Luglio";
  90. $this->monthList[$i][8] = "Agosto";
  91. $this->monthList[$i][9] = "Settembre";
  92. $this->monthList[$i][10] = "Ottobre";
  93. $this->monthList[$i][11] = "Novembre";
  94. $this->monthList[$i][12] = "Dicembre";
  95. }
  96. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  97. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  98. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  99. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  100. $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
  101. }
  102. public function render()
  103. {
  104. $this->records = \App\Models\Course::where('parent_id', null)->with('type', 'duration')->get();
  105. return view('livewire.course');
  106. }
  107. public function add()
  108. {
  109. $this->resetFields();
  110. $this->add = true;
  111. $this->update = false;
  112. $this->emit('setEdit', true);
  113. }
  114. /*
  115. public function addLevel($parent_id)
  116. {
  117. $this->resetFields();
  118. $this->parent_id = $parent_id;
  119. $this->add = true;
  120. $this->update = false;
  121. }
  122. */
  123. public function store()
  124. {
  125. $this->validate();
  126. try {
  127. \App\Models\Course::create([
  128. 'name' => $this->name,
  129. 'parent_id' => $this->parent_id,
  130. 'course_type_id' => $this->course_type_id,
  131. 'course_duration_id' => $this->course_duration_id,
  132. 'course_frequency_id' => $this->course_frequency_id,
  133. 'course_level_id' => $this->course_level_id,
  134. 'date_from' => $this->date_from,
  135. 'date_to' => $this->date_to,
  136. 'category_id' => $this->category_id,
  137. 'causal_id' => $this->causal_id,
  138. 'sub_causal_id' => $this->sub_causal_id,
  139. 'max_members' => $this->max_members,
  140. 'instructor' => $this->instructor,
  141. 'year' => $this->year,
  142. 'price' => currencyToDouble($this->price),
  143. 'subscription_price' => currencyToDouble($this->subscription_price),
  144. 'months' => json_encode($this->months),
  145. 'enabled' => $this->enabled
  146. ]);
  147. session()->flash('success','Corso creato');
  148. $this->resetFields();
  149. $this->add = false;
  150. $this->emit('setEdit', false);
  151. } catch (\Exception $ex) {
  152. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  153. }
  154. }
  155. public function edit($id){
  156. try {
  157. $course = \App\Models\Course::findOrFail($id);
  158. if( !$course) {
  159. session()->flash('error','Corso non trovato');
  160. } else {
  161. $this->name = $course->name;
  162. $this->enabled = $course->enabled;
  163. $this->parent_id = $course->parent_id;
  164. $this->course_type_id = $course->course_type_id;
  165. $this->course_duration_id = $course->course_duration_id;
  166. $this->course_frequency_id = $course->course_frequency_id;
  167. $this->course_level_id = $course->course_level_id;
  168. $this->date_from = $course->date_from;
  169. $this->date_to = $course->date_to;
  170. $this->category_id = $course->category_id;
  171. $this->causal_id = $course->causal_id;
  172. $this->sub_causal_id = $course->sub_causal_id;
  173. $this->max_members = $course->max_members;
  174. $this->instructor = $course->instructor;
  175. $this->year = $course->year;
  176. $this->price = formatPrice($course->price);
  177. $this->subscription_price = formatPrice($course->subscription_price);
  178. $this->months = json_decode($course->months);
  179. $this->dataId = $course->id;
  180. $this->update = true;
  181. $this->add = false;
  182. }
  183. $this->emit('setEdit', true);
  184. } catch (\Exception $ex) {
  185. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  186. }
  187. }
  188. public function update()
  189. {
  190. $this->validate();
  191. try {
  192. \App\Models\Course::whereId($this->dataId)->update([
  193. 'name' => $this->name,
  194. 'parent_id' => $this->parent_id,
  195. 'course_type_id' => $this->course_type_id,
  196. 'course_duration_id' => $this->course_duration_id,
  197. 'course_frequency_id' => $this->course_frequency_id,
  198. 'course_level_id' => $this->course_level_id,
  199. 'date_from' => $this->date_from,
  200. 'date_to' => $this->date_to,
  201. 'category_id' => $this->category_id,
  202. 'causal_id' => $this->causal_id,
  203. 'sub_causal_id' => $this->sub_causal_id,
  204. 'max_members' => $this->max_members,
  205. 'instructor' => $this->instructor,
  206. 'year' => $this->year,
  207. 'price' => currencyToDouble($this->price),
  208. 'subscription_price' => currencyToDouble($this->subscription_price),
  209. 'months' => json_encode($this->months),
  210. 'enabled' => $this->enabled
  211. ]);
  212. session()->flash('success','Corso aggiornato');
  213. $this->resetFields();
  214. $this->update = false;
  215. $this->emit('setEdit', false);
  216. } catch (\Exception $ex) {
  217. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  218. }
  219. }
  220. public function cancel()
  221. {
  222. $this->add = false;
  223. $this->update = false;
  224. $this->resetFields();
  225. $this->emit('setEdit', false);
  226. }
  227. public function delete($id)
  228. {
  229. try{
  230. \App\Models\Course::find($id)->delete();
  231. session()->flash('success',"Corso eliminato");
  232. }catch(\Exception $e){
  233. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  234. }
  235. }
  236. public function setCausal($id, $idx)
  237. {
  238. $this->causal_id = $id;
  239. }
  240. public function setSubscriptionCausal($id, $idx)
  241. {
  242. $this->sub_causal_id = $id;
  243. }
  244. public function setCategory($id)
  245. {
  246. $this->category_id = $id;
  247. }
  248. public function duplicate($id){
  249. $course = \App\Models\Course::findOrFail($id);
  250. $newCourse = $course->replicate();
  251. $newYear = date("Y") . "-" . (date("Y") + 1);
  252. if ($course->year != '')
  253. {
  254. list($u, $y) = explode("-", $course->year);
  255. $newYear = ($u + 1) . "-" . ($u + 2);
  256. }
  257. $newCourse->year = $newYear;
  258. $newCourse->save();
  259. $this->edit($newCourse->id);
  260. }
  261. }