Course.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. {
  116. if (date("m") >= env('FISCAL_YEAR_MONTH_FROM', 1))
  117. $this->selectedYear = date("Y") . "-" . (date("Y") + 1);
  118. else
  119. $this->selectedYear = (date("Y") - 1) . "-" . date("Y");
  120. //$this->selectedYear = $this->course_years[0];
  121. }
  122. //$this->selectedYear = date("Y") . "-" . (date("Y") + 1);
  123. $this->records = \App\Models\Course::where('parent_id', null)->where('year', $this->selectedYear)->with('type', 'duration')->get();
  124. return view('livewire.course');
  125. }
  126. public function add()
  127. {
  128. $this->resetFields();
  129. $this->add = true;
  130. $this->update = false;
  131. $this->emit('setEdit', true);
  132. }
  133. /*
  134. public function addLevel($parent_id)
  135. {
  136. $this->resetFields();
  137. $this->parent_id = $parent_id;
  138. $this->add = true;
  139. $this->update = false;
  140. }
  141. */
  142. public function store()
  143. {
  144. $this->validate();
  145. try {
  146. \App\Models\Course::create([
  147. 'name' => $this->name,
  148. 'parent_id' => $this->parent_id,
  149. 'course_type_id' => $this->course_type_id,
  150. 'course_duration_id' => $this->course_duration_id,
  151. 'course_frequency_id' => $this->course_frequency_id,
  152. 'course_level_id' => $this->course_level_id,
  153. 'date_from' => $this->date_from,
  154. 'date_to' => $this->date_to,
  155. 'category_id' => $this->category_id,
  156. 'causal_id' => $this->causal_id,
  157. 'sub_causal_id' => $this->sub_causal_id,
  158. 'max_members' => $this->max_members,
  159. 'instructor_id' => $this->instructor_id,
  160. 'year' => $this->year,
  161. 'price' => currencyToDouble($this->price),
  162. 'subscription_price' => currencyToDouble($this->subscription_price),
  163. 'months' => json_encode($this->months),
  164. 'enabled' => $this->enabled
  165. ]);
  166. session()->flash('success','Corso creato');
  167. $this->resetFields();
  168. $this->add = false;
  169. $this->emit('setEdit', false);
  170. } catch (\Exception $ex) {
  171. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  172. }
  173. }
  174. public function edit($id){
  175. $this->resetFields();
  176. try {
  177. $course = \App\Models\Course::findOrFail($id);
  178. if( !$course) {
  179. session()->flash('error','Corso non trovato');
  180. } else {
  181. $this->name = $course->name;
  182. $this->enabled = $course->enabled;
  183. $this->parent_id = $course->parent_id;
  184. $this->course_type_id = $course->course_type_id;
  185. $this->course_duration_id = $course->course_duration_id;
  186. $this->course_frequency_id = $course->course_frequency_id;
  187. $this->course_level_id = $course->course_level_id;
  188. $this->date_from = $course->date_from;
  189. $this->date_to = $course->date_to;
  190. $this->category_id = $course->category_id;
  191. $this->causal_id = $course->causal_id;
  192. $this->sub_causal_id = $course->sub_causal_id;
  193. $this->max_members = $course->max_members;
  194. $this->instructor_id = $course->instructor_id;
  195. $this->year = $course->year;
  196. $this->price = formatPrice($course->price);
  197. $this->subscription_price = formatPrice($course->subscription_price);
  198. $this->months = json_decode($course->months);
  199. $this->dataId = $course->id;
  200. $this->update = true;
  201. $this->add = false;
  202. }
  203. $this->emit('setEdit', true);
  204. } catch (\Exception $ex) {
  205. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  206. }
  207. }
  208. public function update()
  209. {
  210. $this->validate();
  211. try {
  212. \App\Models\Course::whereId($this->dataId)->update([
  213. 'name' => $this->name,
  214. 'parent_id' => $this->parent_id,
  215. 'course_type_id' => $this->course_type_id,
  216. 'course_duration_id' => $this->course_duration_id,
  217. 'course_frequency_id' => $this->course_frequency_id,
  218. 'course_level_id' => $this->course_level_id,
  219. 'date_from' => $this->date_from,
  220. 'date_to' => $this->date_to,
  221. 'category_id' => $this->category_id,
  222. 'causal_id' => $this->causal_id,
  223. 'sub_causal_id' => $this->sub_causal_id,
  224. 'max_members' => $this->max_members,
  225. 'instructor_id' => $this->instructor_id,
  226. 'year' => $this->year,
  227. 'price' => currencyToDouble($this->price),
  228. 'subscription_price' => currencyToDouble($this->subscription_price),
  229. 'months' => json_encode($this->months),
  230. 'enabled' => $this->enabled
  231. ]);
  232. session()->flash('success','Corso aggiornato');
  233. $this->resetFields();
  234. $this->update = false;
  235. $this->emit('setEdit', false);
  236. } catch (\Exception $ex) {
  237. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  238. }
  239. }
  240. public function cancel()
  241. {
  242. $this->add = false;
  243. $this->update = false;
  244. $this->resetFields();
  245. $this->emit('setEdit', false);
  246. }
  247. public function delete($id)
  248. {
  249. try{
  250. \App\Models\Course::find($id)->delete();
  251. session()->flash('success',"Corso eliminato");
  252. return redirect(request()->header('Referer'));
  253. }catch(\Exception $e){
  254. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  255. }
  256. }
  257. public function setCausal($id, $idx)
  258. {
  259. $this->causal_id = $id;
  260. }
  261. public function setSubscriptionCausal($id, $idx)
  262. {
  263. $this->sub_causal_id = $id;
  264. }
  265. public function setCategory($id)
  266. {
  267. $this->category_id = $id;
  268. }
  269. public function duplicate($id, $isMultiple){
  270. $course = \App\Models\Course::findOrFail($id);
  271. $newCourse = $course->replicate();
  272. $newYear = date("Y") . "-" . (date("Y") + 1);
  273. if ($course->year != '')
  274. {
  275. list($u, $y) = explode("-", $course->year);
  276. $newYear = ($u + 1) . "-" . ($u + 2);
  277. }
  278. $newCourse->year = $newYear;
  279. $newCourse->save();
  280. if (!$isMultiple)
  281. $this->edit($newCourse->id);
  282. }
  283. public function duplicateMultiple($ids){
  284. foreach($ids as $id)
  285. {
  286. $this->duplicate($id, true);
  287. }
  288. return redirect()->to('/courses');
  289. }
  290. }