Course.php 13 KB

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