Course.php 12 KB

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