|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Mongi\Mongicommerce\Http\Controllers\admin; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Mongi\Mongicommerce\Http\Controllers\Controller; |
|
9
|
|
|
use Mongi\Mongicommerce\Models\Category; |
|
10
|
|
|
|
|
11
|
|
|
class AdminCategoryController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
public function page(){ |
|
14
|
|
|
return view('mongicommerce::admin.pages.category.new_category'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function setNewCategory(Request $r){ |
|
18
|
|
|
$r->validate([ |
|
19
|
|
|
'name' => 'required', |
|
20
|
|
|
'description' => 'max:255' |
|
21
|
|
|
]); |
|
22
|
|
|
$name = $r->get('name'); |
|
23
|
|
|
$description = $r->get('description'); |
|
24
|
|
|
$parent_id = $r->get('parent_id'); |
|
25
|
|
|
$category = new Category(); |
|
26
|
|
|
$category->name = $name; |
|
27
|
|
|
$category->description = $description; |
|
28
|
|
|
$category->parent_id = $parent_id; |
|
29
|
|
|
$category->save(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getCategories(){ |
|
33
|
|
|
$data = $this->getCategoryTree(); |
|
34
|
|
|
return Response()->json($data); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function getCategoryTree($parent_id = null, $spacing = '', $tree_array = array()) { |
|
|
|
|
|
|
38
|
|
|
$categories = Category::select('id', 'name', 'parent_id')->where('parent_id' ,'=', $parent_id)->orderBy('parent_id')->get(); |
|
39
|
|
|
foreach ($categories as $item){ |
|
40
|
|
|
$tree_array[] = ['id' => $item->id, 'name' =>$spacing . $item->name] ; |
|
41
|
|
|
$tree_array = $this->getCategoryTree($item->id, $spacing . '- ', $tree_array); |
|
42
|
|
|
} |
|
43
|
|
|
return $tree_array; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getStructureCategories(){ |
|
47
|
|
|
|
|
48
|
|
|
$categories = Category::with('children')->whereNull('parent_id')->get(); |
|
49
|
|
|
$tree = []; |
|
50
|
|
|
foreach($categories as $category){ |
|
51
|
|
|
$tree[] = [ |
|
52
|
|
|
'id' => $category->id, |
|
53
|
|
|
'text' => $category->name, |
|
54
|
|
|
'state' => ['opened'=>true], |
|
55
|
|
|
'children' => $this->recursiveChildren($category->children) |
|
56
|
|
|
]; |
|
57
|
|
|
} |
|
58
|
|
|
return response()->json($tree); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function recursiveChildren($childrens){ |
|
62
|
|
|
$childs = []; |
|
63
|
|
|
foreach ($childrens as $children){ |
|
64
|
|
|
$childs[] = [ |
|
65
|
|
|
'id' => $children->id, |
|
66
|
|
|
'text' => $children->name, |
|
67
|
|
|
'state' => ['opened'=>true], |
|
68
|
|
|
'children' => $this->recursiveChildren($children->children) |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
return $childs; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.