Skip to content

Added category and category type #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions app/Http/Controllers/Admin/CategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use BalajiDharma\LaravelAdminCore\Requests\StoreCategoryRequest;
use BalajiDharma\LaravelAdminCore\Requests\UpdateCategoryRequest;
use BalajiDharma\LaravelCategory\Models\CategoryType;
use BalajiDharma\LaravelCategory\Models\Category;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class CategoryController extends Controller
{
public function __construct()
{
$this->middleware('can:category list', ['only' => ['index', 'show']]);
$this->middleware('can:category create', ['only' => ['create', 'store']]);
$this->middleware('can:category edit', ['only' => ['edit', 'update']]);
$this->middleware('can:category delete', ['only' => ['destroy']]);
}

/**
* Display a listing of the resource.
*
* @return \Inertia\Response
*/
public function index(CategoryType $type)
{
$items = (new Category)->toTree($type->id, true);

return Inertia::render('Admin/Category/Item/Index', [
'categoryType' => $type,
'items' => $items,
'can' => [
'create' => Auth::user()->can('category create'),
'edit' => Auth::user()->can('category edit'),
'delete' => Auth::user()->can('category delete'),
]
]);
}

/**
* Show the form for creating a new resource.
*
* @return \Inertia\Response
*/
public function create(CategoryType $type)
{
$itemOptions = Category::selectOptions($type->id, null, true);
return Inertia::render('Admin/Category/Item/Create', [
'categoryType' => $type,
'itemOptions' => $itemOptions
]);
}

/**
* Store a newly created resource in storage.
*
* @param StoreCategoryRequest $request
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreCategoryRequest $request, CategoryType $type)
{
if(!$request->has('enabled')) {
$request['enabled'] = false;
}

$type->categories()->create($request->all());

return redirect()->route('category.type.item.index', $type->id)
->with('message', 'Category created successfully.');
}

/**
* Show the form for editing the specified resource.
*
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
* @return \Inertia\Response
*/
public function edit(CategoryType $type, Category $item)
{
$itemOptions = Category::selectOptions($type->id, $item->parent_id ?? $item->id);
return Inertia::render('Admin/Category/Item/Edit', [
'categoryType' => $type,
'item' => $item,
'itemOptions' => $itemOptions
]);
}

/**
* Update the specified resource in storage.
*
* @param UpdateCategoryRequest $request
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
* @param \BalajiDharma\LaravelCategory\Models\Category $item
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateCategoryRequest $request, CategoryType $type, Category $item)
{
if(!$request->has('enabled')) {
$request['enabled'] = false;
}

$item->update($request->all());

return redirect()->route('category.type.item.index', $type->id)
->with('message', 'Category updated successfully.');
}

/**
* Remove the specified resource from storage.
*
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $type
* @param \BalajiDharma\LaravelCategory\Models\Category $typeItem
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(CategoryType $type, Category $item)
{
$item->delete();

return redirect()->route('category.type.item.index', $type->id)
->with('message', __('Category deleted successfully'));
}
}
139 changes: 139 additions & 0 deletions app/Http/Controllers/Admin/CategoryTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use BalajiDharma\LaravelCategory\Models\CategoryType;
use BalajiDharma\LaravelAdminCore\Requests\StoreCategoryTypeRequest;
use BalajiDharma\LaravelAdminCore\Requests\UpdateCategoryTypeRequest;
use Illuminate\Support\Facades\Auth;
use Inertia\Inertia;

class CategoryTypeController extends Controller
{
public function __construct()
{
$this->middleware('can:category.type list', ['only' => ['index']]);
$this->middleware('can:category.type create', ['only' => ['create', 'store']]);
$this->middleware('can:category.type edit', ['only' => ['edit', 'update']]);
$this->middleware('can:category.type delete', ['only' => ['destroy']]);
}

/**
* Display a listing of the resource.
*
* @return \Inertia\Response
*/
public function index()
{
$categoryTypes = (new CategoryType)->newQuery();

if (request()->has('search')) {
$categoryTypes->where('name', 'Like', '%'.request()->input('search').'%');
}

if (request()->query('sort')) {
$attribute = request()->query('sort');
$sort_order = 'ASC';
if (strncmp($attribute, '-', 1) === 0) {
$sort_order = 'DESC';
$attribute = substr($attribute, 1);
}
$categoryTypes->orderBy($attribute, $sort_order);
} else {
$categoryTypes->latest();
}

$categoryTypes = $categoryTypes->paginate(5)->onEachSide(2);

return Inertia::render('Admin/Category/Type/Index', [
'categoryTypes' => $categoryTypes,
'filters' => request()->all('search'),
'can' => [
'create' => Auth::user()->can('category.type create'),
'edit' => Auth::user()->can('category.type edit'),
'delete' => Auth::user()->can('category.type delete'),
'manage' => Auth::user()->can('category index'),
]
]);
}

/**
* Show the form for creating a new resource.
*
* @return \Inertia\Response
*/
public function create()
{
return Inertia::render('Admin/Category/Type/Create');
}

/**
* Store a newly created resource in storage.
*
* @param StoreCategoryTypeRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreCategoryTypeRequest $request)
{
if(!$request->has('is_flat')) {
$request['is_flat'] = false;
}

CategoryType::create([
'name' => $request->name,
'machine_name' => $request->machine_name,
'description' => $request->description,
'is_flat' => $request->is_flat,
]);

return redirect()->route('category.type.index')
->with('message', 'Category type created successfully.');
}

/**
* Show the form for editing the specified resource.
*
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $categoryType
* @return \Inertia\Response
*/
public function edit(CategoryType $type)
{
return Inertia::render('Admin/Category/Type/Edit', [
'categoryType' => $type,
]);
}

/**
* Update the specified resource in storage.
*
* @param UpdateCategoryTypeRequest $request
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $categoryType
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateCategoryTypeRequest $request, CategoryType $type)
{
if(!$request->has('is_flat')) {
$request['is_flat'] = false;
}

$type->update($request->all());

return redirect()->route('category.type.index')
->with('message', 'Category type updated successfully.');
}

/**
* Remove the specified resource from storage.
*
* @param \BalajiDharma\LaravelCategory\Models\CategoryType $categoryType
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(CategoryType $type)
{
$type->delete();

return redirect()->route('category.type.index')
->with('message', __('Category type deleted successfully'));
}
}
66 changes: 66 additions & 0 deletions resources/js/Components/Admin/CategoryItemList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<script setup>
import { Link } from "@inertiajs/vue3"
import BaseButton from "@/Components/BaseButton.vue"
import BaseButtons from "@/Components/BaseButtons.vue"
import {
mdiSquareEditOutline,
mdiTrashCan,
} from "@mdi/js"

const props = defineProps({
item: {
type: Object,
default: () => ({}),
},
categoryType: {
type: Object,
default: () => ({}),
},
can: {
type: Object,
default: () => ({}),
},
level: {
type: Number,
default: 0
},
})
</script>

<template>
<tr :key="item.id">
<td data-label="Name">
<div :style="{ 'margin-left': level*20 + 'px' }">{{ item.name }}</div>
</td>
<td data-label="Description">
{{ item.description }}
</td>
<td data-label="Enabled">
{{ item.enabled }}
</td>
<td
v-if="can.edit || can.delete"
class="before:hidden lg:w-1 whitespace-nowrap"
>
<BaseButtons type="justify-start lg:justify-end" no-wrap>
<BaseButton
v-if="can.edit"
:route-name="route('category.type.item.edit', {type: categoryType.id, item: item.id})"
color="info"
:icon="mdiSquareEditOutline"
small
/>
<BaseButton
v-if="can.delete"
color="danger"
:icon="mdiTrashCan"
small
@click="destroy(item.id)"
/>
</BaseButtons>
</td>
</tr>
<template v-for="item in item.children">
<CategoryItemList :item="item" :categoryType="categoryType" :can="can" :level="level + 1" />
</template>
</template>
Loading