|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Adminetic\Website\Http\Livewire\Admin\Category; |
|
4
|
|
|
|
|
5
|
|
|
use Adminetic\Website\Models\Admin\Category; |
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Livewire\Component; |
|
9
|
|
|
|
|
10
|
|
|
class QuickCategory extends Component |
|
11
|
|
|
{ |
|
12
|
|
|
public $parent_categories; |
|
13
|
|
|
|
|
14
|
|
|
public $model; |
|
15
|
|
|
|
|
16
|
|
|
public $attribute; |
|
17
|
|
|
|
|
18
|
|
|
public $category_id; |
|
19
|
|
|
|
|
20
|
|
|
public $category; |
|
21
|
|
|
|
|
22
|
|
|
public $label; |
|
23
|
|
|
|
|
24
|
|
|
public $category_panel_toggle = false; |
|
25
|
|
|
|
|
26
|
|
|
// Attributes |
|
27
|
|
|
public $name; |
|
28
|
|
|
public $parent_id; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct($model = null, $attribute = 'category_id', $category_id = null, $label = 'Category') |
|
31
|
|
|
{ |
|
32
|
|
|
$this->parent_categories = Cache::get('parent_categories', Category::whoIsParent()->position()->get()); |
|
33
|
|
|
$this->model = $model; |
|
34
|
|
|
$this->attribute = $attribute; |
|
35
|
|
|
$this->category_id = $category_id; |
|
36
|
|
|
$this->label = $label; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function save() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->validate([ |
|
42
|
|
|
'name' => 'required|max:100', |
|
43
|
|
|
'parent_id' => 'nullable|numeric|exists:'.config('website.table_prefix', 'website').'_categories,id', |
|
44
|
|
|
|
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$category = Category::create([ |
|
48
|
|
|
'root_parent_id' => $this->getMainCategory(), |
|
49
|
|
|
'parent_id' => $this->parent_id, |
|
50
|
|
|
'slug' => Str::slug($this->name), |
|
51
|
|
|
'model' => trim($this->model), |
|
52
|
|
|
'name' => $this->name, |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
$this->category_id = $category->id; |
|
56
|
|
|
|
|
57
|
|
|
$this->category_panel_toggle = false; |
|
58
|
|
|
|
|
59
|
|
|
$this->parent_categories = Category::whoIsParent()->position()->get(); |
|
60
|
|
|
|
|
61
|
|
|
$this->name = null; |
|
62
|
|
|
$this->parent_id = null; |
|
63
|
|
|
|
|
64
|
|
|
$this->emit('quick_category_success', 'Category created successfully'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function render() |
|
68
|
|
|
{ |
|
69
|
|
|
return view('website::livewire.admin.category.quick-category'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Get Main Category |
|
73
|
|
|
private function getMainCategory($given_category_id = null) |
|
74
|
|
|
{ |
|
75
|
|
|
$parent_id = $given_category_id ?? $this->parent_id ?? null; |
|
76
|
|
|
$category = Category::find($parent_id); |
|
77
|
|
|
if (isset($category)) { |
|
78
|
|
|
while (true) { |
|
79
|
|
|
if (isset($category->parent_id)) { |
|
80
|
|
|
$category = Category::find($category->parent_id); |
|
81
|
|
|
} else { |
|
82
|
|
|
break; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $category->id; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|