Course.php 11 KB

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