Course.php 10 KB

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