| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Course extends Component
- {
- protected $listeners = ['setCausal' => 'setCausal'];
- public $records, $parent_id, $name, $enabled, $dataId, $update = false, $add = false;
- public $course_type_id,
- $course_duration_id,
- $course_frequency_id,
- $course_level_id,
- $causal_id,
- $max_members,
- $instructor,
- $year,
- $price,
- $months,
- $date_from, $date_to;
- public $course_types = [];
- public $course_durations = [];
- public $course_frequencies = [];
- public $course_levels = [];
- public $causals = [];
- public $monthList = [];
- public $typeIN = 'IN';
- // public $selectedMonthList = [];
- protected $rules = [
- 'name' => 'required',
- 'course_type_id' => 'required',
- 'course_duration_id' => 'required',
- 'course_frequency_id' => 'required',
- 'course_level_id' => 'required',
- 'causal_id' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio'
- ];
- public function resetFields(){
- $this->name = '';
- $this->parent_id = null;
- $this->course_type_id = null;
- $this->course_duration_id = null;
- $this->course_frequency_id = null;
- $this->course_level_id = null;
- $this->causal_id = null;
- $this->max_members = 0;
- $this->instructor = '';
- $this->year = date("Y");
- $this->price = 0;
- $this->date_from = null;
- $this->date_to = null;
- $this->months = array();
- $this->enabled = true;
- }
- public function mount()
- {
- for($i=date("Y"); $i<=date("Y") + 1; $i++)
- {
- $this->monthList[$i][1] = "Gennaio";
- $this->monthList[$i][2] = "Febbraio";
- $this->monthList[$i][3] = "Marzo";
- $this->monthList[$i][4] = "Aprile";
- $this->monthList[$i][5] = "Maggio";
- $this->monthList[$i][6] = "Giugno";
- $this->monthList[$i][7] = "Luglio";
- $this->monthList[$i][8] = "Agosto";
- $this->monthList[$i][9] = "Settembre";
- $this->monthList[$i][10] = "Ottobre";
- $this->monthList[$i][11] = "Novembre";
- $this->monthList[$i][12] = "Dicembre";
- }
- $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
- $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
- $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
- $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
- $this->causals = \App\Models\Causal::select('*')->where('type', 'IN')->where('enabled', true)->get();
- }
- public function render()
- {
- $this->records = \App\Models\Course::where('parent_id', null)->with('type', 'duration')->get();
- return view('livewire.course');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- /*
- public function addLevel($parent_id)
- {
- $this->resetFields();
- $this->parent_id = $parent_id;
- $this->add = true;
- $this->update = false;
- }
- */
- public function store()
- {
- $this->validate();
- try {
- \App\Models\Course::create([
- 'name' => $this->name,
- 'parent_id' => $this->parent_id,
- 'course_type_id' => $this->course_type_id,
- 'course_duration_id' => $this->course_duration_id,
- 'course_frequency_id' => $this->course_frequency_id,
- 'course_level_id' => $this->course_level_id,
- 'date_from' => $this->date_from,
- 'date_to' => $this->date_to,
- 'causal_id' => $this->causal_id,
- 'max_members' => $this->max_members,
- 'instructor' => $this->instructor,
- 'year' => $this->year,
- 'price' => currencyToDouble($this->price),
- 'months' => json_encode($this->months),
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Corso creato');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id){
- try {
- $course = \App\Models\Course::findOrFail($id);
- if( !$course) {
- session()->flash('error','Corso non trovato');
- } else {
- $this->name = $course->name;
- $this->enabled = $course->enabled;
- $this->parent_id = $course->parent_id;
- $this->course_type_id = $course->course_type_id;
- $this->course_duration_id = $course->course_duration_id;
- $this->course_frequency_id = $course->course_frequency_id;
- $this->course_level_id = $course->course_level_id;
- $this->date_from = $course->date_from;
- $this->date_to = $course->date_to;
- $this->causal_id = $course->causal_id;
- $this->max_members = $course->max_members;
- $this->instructor = $course->instructor;
- $this->year = $course->year;
- $this->price = formatPrice($course->price);
- $this->months = json_decode($course->months);
- $this->dataId = $course->id;
- $this->update = true;
- $this->add = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- \App\Models\Course::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'parent_id' => $this->parent_id,
- 'course_type_id' => $this->course_type_id,
- 'course_duration_id' => $this->course_duration_id,
- 'course_frequency_id' => $this->course_frequency_id,
- 'course_level_id' => $this->course_level_id,
- 'date_from' => $this->date_from,
- 'date_to' => $this->date_to,
- 'causal_id' => $this->causal_id,
- 'max_members' => $this->max_members,
- 'instructor' => $this->instructor,
- 'year' => $this->year,
- 'price' => currencyToDouble($this->price),
- 'months' => json_encode($this->months),
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Corso aggiornato');
- $this->resetFields();
- $this->update = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->add = false;
- $this->update = false;
- $this->resetFields();
- }
- public function delete($id)
- {
- try{
- \App\Models\Course::find($id)->delete();
- session()->flash('success',"Corso eliminato");
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function setCausal($id, $idx)
- {
- $this->causal_id = $id;
- }
- }
|