Skip to content

Commit e794668

Browse files
committed
Added category and category type
1 parent 2b7450d commit e794668

File tree

10 files changed

+1144
-0
lines changed

10 files changed

+1144
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use BalajiDharma\LaravelAdminCore\Requests\StoreCategoryRequest;
7+
use BalajiDharma\LaravelAdminCore\Requests\UpdateCategoryRequest;
8+
use BalajiDharma\LaravelCategory\Models\CategoryType;
9+
use BalajiDharma\LaravelCategory\Models\Category;
10+
use Illuminate\Support\Facades\Auth;
11+
use Inertia\Inertia;
12+
13+
class CategoryController extends Controller
14+
{
15+
public function __construct()
16+
{
17+
$this->middleware('can:category list', ['only' => ['index', 'show']]);
18+
$this->middleware('can:category create', ['only' => ['create', 'store']]);
19+
$this->middleware('can:category edit', ['only' => ['edit', 'update']]);
20+
$this->middleware('can:category delete', ['only' => ['destroy']]);
21+
}
22+
23+
/**
24+
* Display a listing of the resource.
25+
*
26+
* @return \Inertia\Response
27+
*/
28+
public function index(CategoryType $type)
29+
{
30+
$items = (new Category)->toTree($type->id, true);
31+
32+
return Inertia::render('Admin/Category/Item/Index', [
33+
'categoryType' => $type,
34+
'items' => $items,
35+
'can' => [
36+
'create' => Auth::user()->can('category create'),
37+
'edit' => Auth::user()->can('category edit'),
38+
'delete' => Auth::user()->can('category delete'),
39+
]
40+
]);
41+
}
42+
43+
/**
44+
* Show the form for creating a new resource.
45+
*
46+
* @return \Inertia\Response
47+
*/
48+
public function create(CategoryType $type)
49+
{
50+
$itemOptions = Category::selectOptions($type->id, null, true);
51+
return Inertia::render('Admin/Category/Item/Create', [
52+
'categoryType' => $type,
53+
'itemOptions' => $itemOptions
54+
]);
55+
}
56+
57+
/**
58+
* Store a newly created resource in storage.
59+
*
60+
* @param StoreCategoryRequest $request
61+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
62+
* @return \Illuminate\Http\RedirectResponse
63+
*/
64+
public function store(StoreCategoryRequest $request, CategoryType $type)
65+
{
66+
if(!$request->has('enabled')) {
67+
$request['enabled'] = false;
68+
}
69+
70+
$type->categories()->create($request->all());
71+
72+
return redirect()->route('category.type.item.index', $type->id)
73+
->with('message', 'Category created successfully.');
74+
}
75+
76+
/**
77+
* Show the form for editing the specified resource.
78+
*
79+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
80+
* @return \Inertia\Response
81+
*/
82+
public function edit(CategoryType $type, Category $item)
83+
{
84+
$itemOptions = Category::selectOptions($type->id, $item->parent_id ?? $item->id);
85+
return Inertia::render('Admin/Category/Item/Edit', [
86+
'categoryType' => $type,
87+
'item' => $item,
88+
'itemOptions' => $itemOptions
89+
]);
90+
}
91+
92+
/**
93+
* Update the specified resource in storage.
94+
*
95+
* @param UpdateCategoryRequest $request
96+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
97+
* @param \BalajiDharma\LaravelCategory\Models\Category $item
98+
* @return \Illuminate\Http\RedirectResponse
99+
*/
100+
public function update(UpdateCategoryRequest $request, CategoryType $type, Category $item)
101+
{
102+
if(!$request->has('enabled')) {
103+
$request['enabled'] = false;
104+
}
105+
106+
$item->update($request->all());
107+
108+
return redirect()->route('category.type.item.index', $type->id)
109+
->with('message', 'Category updated successfully.');
110+
}
111+
112+
/**
113+
* Remove the specified resource from storage.
114+
*
115+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
116+
* @param \BalajiDharma\LaravelCategory\Models\Category $typeItem
117+
* @return \Illuminate\Http\RedirectResponse
118+
*/
119+
public function destroy(CategoryType $type, Category $item)
120+
{
121+
$item->delete();
122+
123+
return redirect()->route('category.type.item.index', $type->id)
124+
->with('message', __('Category deleted successfully'));
125+
}
126+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use BalajiDharma\LaravelCategory\Models\CategoryType;
7+
use BalajiDharma\LaravelAdminCore\Requests\StoreCategoryTypeRequest;
8+
use BalajiDharma\LaravelAdminCore\Requests\UpdateCategoryTypeRequest;
9+
use Illuminate\Support\Facades\Auth;
10+
use Inertia\Inertia;
11+
12+
class CategoryTypeController extends Controller
13+
{
14+
public function __construct()
15+
{
16+
$this->middleware('can:category.type list', ['only' => ['index']]);
17+
$this->middleware('can:category.type create', ['only' => ['create', 'store']]);
18+
$this->middleware('can:category.type edit', ['only' => ['edit', 'update']]);
19+
$this->middleware('can:category.type delete', ['only' => ['destroy']]);
20+
}
21+
22+
/**
23+
* Display a listing of the resource.
24+
*
25+
* @return \Inertia\Response
26+
*/
27+
public function index()
28+
{
29+
$categoryTypes = (new CategoryType)->newQuery();
30+
31+
if (request()->has('search')) {
32+
$categoryTypes->where('name', 'Like', '%'.request()->input('search').'%');
33+
}
34+
35+
if (request()->query('sort')) {
36+
$attribute = request()->query('sort');
37+
$sort_order = 'ASC';
38+
if (strncmp($attribute, '-', 1) === 0) {
39+
$sort_order = 'DESC';
40+
$attribute = substr($attribute, 1);
41+
}
42+
$categoryTypes->orderBy($attribute, $sort_order);
43+
} else {
44+
$categoryTypes->latest();
45+
}
46+
47+
$categoryTypes = $categoryTypes->paginate(5)->onEachSide(2);
48+
49+
return Inertia::render('Admin/Category/Type/Index', [
50+
'categoryTypes' => $categoryTypes,
51+
'filters' => request()->all('search'),
52+
'can' => [
53+
'create' => Auth::user()->can('category.type create'),
54+
'edit' => Auth::user()->can('category.type edit'),
55+
'delete' => Auth::user()->can('category.type delete'),
56+
'manage' => Auth::user()->can('category index'),
57+
]
58+
]);
59+
}
60+
61+
/**
62+
* Show the form for creating a new resource.
63+
*
64+
* @return \Inertia\Response
65+
*/
66+
public function create()
67+
{
68+
return Inertia::render('Admin/Category/Type/Create');
69+
}
70+
71+
/**
72+
* Store a newly created resource in storage.
73+
*
74+
* @param StoreCategoryTypeRequest $request
75+
* @return \Illuminate\Http\RedirectResponse
76+
*/
77+
public function store(StoreCategoryTypeRequest $request)
78+
{
79+
if(!$request->has('is_flat')) {
80+
$request['is_flat'] = false;
81+
}
82+
83+
CategoryType::create([
84+
'name' => $request->name,
85+
'machine_name' => $request->machine_name,
86+
'description' => $request->description,
87+
'is_flat' => $request->is_flat,
88+
]);
89+
90+
return redirect()->route('category.type.index')
91+
->with('message', 'Category type created successfully.');
92+
}
93+
94+
/**
95+
* Show the form for editing the specified resource.
96+
*
97+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $categoryType
98+
* @return \Inertia\Response
99+
*/
100+
public function edit(CategoryType $type)
101+
{
102+
return Inertia::render('Admin/Category/Type/Edit', [
103+
'categoryType' => $type,
104+
]);
105+
}
106+
107+
/**
108+
* Update the specified resource in storage.
109+
*
110+
* @param UpdateCategoryTypeRequest $request
111+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $categoryType
112+
* @return \Illuminate\Http\RedirectResponse
113+
*/
114+
public function update(UpdateCategoryTypeRequest $request, CategoryType $type)
115+
{
116+
if(!$request->has('is_flat')) {
117+
$request['is_flat'] = false;
118+
}
119+
120+
$type->update($request->all());
121+
122+
return redirect()->route('category.type.index')
123+
->with('message', 'Category type updated successfully.');
124+
}
125+
126+
/**
127+
* Remove the specified resource from storage.
128+
*
129+
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $categoryType
130+
* @return \Illuminate\Http\RedirectResponse
131+
*/
132+
public function destroy(CategoryType $type)
133+
{
134+
$type->delete();
135+
136+
return redirect()->route('category.type.index')
137+
->with('message', __('Category type deleted successfully'));
138+
}
139+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<script setup>
2+
import { Link } from "@inertiajs/vue3"
3+
import BaseButton from "@/Components/BaseButton.vue"
4+
import BaseButtons from "@/Components/BaseButtons.vue"
5+
import {
6+
mdiSquareEditOutline,
7+
mdiTrashCan,
8+
} from "@mdi/js"
9+
10+
const props = defineProps({
11+
item: {
12+
type: Object,
13+
default: () => ({}),
14+
},
15+
categoryType: {
16+
type: Object,
17+
default: () => ({}),
18+
},
19+
can: {
20+
type: Object,
21+
default: () => ({}),
22+
},
23+
level: {
24+
type: Number,
25+
default: 0
26+
},
27+
})
28+
</script>
29+
30+
<template>
31+
<tr :key="item.id">
32+
<td data-label="Name">
33+
<div :style="{ 'margin-left': level*20 + 'px' }">{{ item.name }}</div>
34+
</td>
35+
<td data-label="Description">
36+
{{ item.description }}
37+
</td>
38+
<td data-label="Enabled">
39+
{{ item.enabled }}
40+
</td>
41+
<td
42+
v-if="can.edit || can.delete"
43+
class="before:hidden lg:w-1 whitespace-nowrap"
44+
>
45+
<BaseButtons type="justify-start lg:justify-end" no-wrap>
46+
<BaseButton
47+
v-if="can.edit"
48+
:route-name="route('category.type.item.edit', {type: categoryType.id, item: item.id})"
49+
color="info"
50+
:icon="mdiSquareEditOutline"
51+
small
52+
/>
53+
<BaseButton
54+
v-if="can.delete"
55+
color="danger"
56+
:icon="mdiTrashCan"
57+
small
58+
@click="destroy(item.id)"
59+
/>
60+
</BaseButtons>
61+
</td>
62+
</tr>
63+
<template v-for="item in item.children">
64+
<CategoryItemList :item="item" :categoryType="categoryType" :can="can" :level="level + 1" />
65+
</template>
66+
</template>

0 commit comments

Comments
 (0)