Course.php 11 KB

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