Course.php 11 KB

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