| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Card extends Component
- {
- public $records, $name, $enabled, $use_for_user_check, $one_year_expire, $next_day_expire, $next_month_expire, $dataId, $update = false, $add = false;
- protected $rules = [
- 'name' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio'
- ];
- public $sortField ='name';
- public $sortAsc = true;
- public function sortBy($field)
- {
- if($this->sortField === $field)
- {
- $this->sortAsc = ! $this->sortAsc;
- } else {
- $this->sortAsc = true;
- }
- $this->sortField = $field;
- }
- public function resetFields(){
- $this->name = '';
- $this->next_day_expire = null;
- $this->next_month_expire = null;
- $this->enabled = true;
- $this->use_for_user_check = true;
- $this->one_year_expire = false;
- }
- public function render()
- {
- $this->records = \App\Models\Card::orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
- return view('livewire.card');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- $this->validate();
- if ($this->one_year_expire)
- {
- $this->next_day_expire = 0;
- $this->next_month_expire = 0;
- }
- try {
- \App\Models\Card::create([
- 'name' => $this->name,
- 'next_day_expire' => $this->next_day_expire,
- 'next_month_expire' => $this->next_month_expire,
- 'enabled' => $this->enabled,
- 'use_for_user_check' => $this->use_for_user_check,
- 'one_year_expire' => $this->one_year_expire
- ]);
- session()->flash('success','Tessera creata');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id){
- try {
- $card = \App\Models\Card::findOrFail($id);
- if( !$card) {
- session()->flash('error','Tessera non trovata');
- } else {
- $this->name = $card->name;
- $this->enabled = $card->enabled;
- $this->use_for_user_check = $card->use_for_user_check;
- $this->one_year_expire = $card->one_year_expire;
- $this->next_day_expire = $card->next_day_expire;
- $this->next_month_expire = $card->next_month_expire;
- $this->dataId = $card->id;
- $this->update = true;
- $this->add = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- if ($this->one_year_expire)
- {
- $this->next_day_expire = 0;
- $this->next_month_expire = 0;
- }
- try {
- \App\Models\Card::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'next_day_expire' => $this->next_day_expire,
- 'next_month_expire' => $this->next_month_expire,
- 'enabled' => $this->enabled,
- 'use_for_user_check' => $this->use_for_user_check,
- 'one_year_expire' => $this->one_year_expire
- ]);
- session()->flash('success','Tessera aggiornata');
- $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\Card::find($id)->delete();
- session()->flash('success',"Tessera eliminata");
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- }
|