GlossaryVariants   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 47
c 1
b 0
f 1
dl 0
loc 140
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A reorder() 0 20 2
A closeModal() 0 3 1
A delete() 0 14 1
A saveGlossaryVariant() 0 21 2
A mount() 0 7 1
A openModal() 0 13 3
A render() 0 3 1
1
<?php
2
3
namespace App\Http\Livewire;
4
5
use App\Models\Glossary;
6
use App\Models\GlossaryVariant;
7
use App\Repositories\GlossaryVariantRepository;
8
use App\Services\GlossaryService;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\App;
11
use Livewire\Component;
12
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
13
14
/**
15
 * Class GlossaryVariants
16
 *
17
 * Inspired by this talk of Caleb
18
 * https://laravel-livewire.com/screencasts/s8-dragging-list
19
 *
20
 * @package App\Http\Livewire
21
 * */
22
23
class GlossaryVariants extends Component
24
{
25
    public $glossaryTerm;
26
    public $variants; //Collection
27
    public $newVariant;
28
    public $showModal = false;
29
    public $locales;
30
31
    /*protected $rules = [
32
        'newVariant.term' => ['required', 'string'],
33
    ];*/
34
35
    protected $listeners = ['variantsRefresh' => 'mount'];
36
37
    /**
38
     * The component constructor
39
     *
40
     * @param int $glossaryId
41
     */
42
    public function mount(int $glossaryId)
43
    {
44
        $glossaryService = App::make(GlossaryService::class);
45
        $this->glossaryTerm = $glossaryService->getById($glossaryId);
46
47
        $this->variants = $this->glossaryTerm->variants->sortBy('order');
48
        $this->locales = LaravelLocalization::getSupportedLocales();
49
    }
50
51
    /**
52
     * Default component render method
53
     */
54
    public function render()
55
    {
56
        return view('livewire.glossary-variants');
57
    }
58
59
    /**
60
     * Reorder variants with Drag and Drop
61
     *
62
     * @param array $orderedIds
63
     */
64
    public function reorder(array $orderedIds)
65
    {
66
        // Array with itemId as key and order as value
67
        $orderedIds = collect($orderedIds)->mapWithKeys(
68
            function ($item) {
69
                return [$item['value'] => $item['order']];
70
            }
71
        )->toArray();
72
73
        // Assign the order to the variants
74
        $this->variants = $this->variants->map(
75
            function ($item) use ($orderedIds) {
76
                $item->order = $orderedIds[$item->id];
77
                return $item;
78
            }
79
        )->sortBy('order');
80
81
        // Update the variants on DB
82
        foreach ($this->variants as $variant) {
83
            $variant->update();
84
        }
85
    }
86
87
    /**
88
     * Store a newly created variant in storage.
89
     */
90
    public function saveGlossaryVariant(): void
91
    {
92
        $glossaryVariantRepository = App::make(
93
            GlossaryVariantRepository::class
94
        );
95
96
        //$this->validate();
97
98
        $this->newVariant['glossary_id'] = $this->glossaryTerm->id;
99
100
        if (array_key_exists('id', $this->newVariant)) {
101
            $variant = $glossaryVariantRepository->update($this->newVariant, $this->newVariant['id']);
0 ignored issues
show
Unused Code introduced by
The assignment to $variant is dead and can be removed.
Loading history...
102
        } else {
103
            $variant = $glossaryVariantRepository->store($this->newVariant);
104
        }
105
106
        // Reload variants
107
        $this->emit('variantsRefresh', $this->glossaryTerm->id);
108
109
        $this->showModal = false;
110
        $this->newVariant = [];
111
    }
112
113
114
    /**
115
     * Delete a variant
116
     *
117
     * @param int $variantId
118
     */
119
    public function delete(int $variantId)
120
    {
121
        $glossaryVariantRepository = App::make(
122
            GlossaryVariantRepository::class
123
        );
124
125
        /*$this->variants->reject(function ($item) use ($variantId) {
126
            return $item->id == $variantId;
127
        });*/
128
129
        $glossaryVariantRepository->delete($variantId);
130
131
        // Reload variants
132
        $this->emit('variantsRefresh', $this->glossaryTerm->id);
133
    }
134
135
    /**
136
     * Open the modal
137
     *
138
     * @param int|null $variantId
139
     */
140
    public function openModal(int $variantId = null): void
141
    {
142
        $this->showModal = true;
143
144
        if (!is_null($variantId)) {
145
            $glossaryVariantRepository = App::make(
146
                GlossaryVariantRepository::class
147
            );
148
            $variant = $glossaryVariantRepository->getById($variantId);
149
            $this->newVariant['id'] = $variant->id;
150
            $this->newVariant['glossary_id'] = $this->glossaryTerm->id;
151
            foreach ($this->locales as $key => $locale) {
152
                $this->newVariant['lang'][$key] = $variant->getTranslation('term', $key);
153
            }
154
        }
155
    }
156
157
    /**
158
     * Close the modal
159
     */
160
    public function closeModal(): void
161
    {
162
        $this->showModal = false;
163
    }
164
165
166
167
}
168