FEATURE: add category edit and overview
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Modules\Categories\Entities;
|
||||
|
||||
use App\Models\Module;
|
||||
use Franzose\ClosureTable\Models\Entity;
|
||||
|
||||
class Category extends Entity implements CategoryInterface
|
||||
@@ -20,4 +21,8 @@ class Category extends Entity implements CategoryInterface
|
||||
* @var categoryClosure
|
||||
*/
|
||||
protected $closure = 'Modules\Categories\Entities\CategoryClosure';
|
||||
|
||||
public function module() {
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php namespace Modules\Categories\Http\Controllers;
|
||||
|
||||
use App\Enumerations\ModuleNamesWithIds;
|
||||
use App\Models\Module;
|
||||
use Modules\Categories\Entities\Category;
|
||||
use Pingpong\Modules\Routing\Controller;
|
||||
|
||||
class CategoriesOverview extends Controller
|
||||
{
|
||||
public function getOverview()
|
||||
{
|
||||
// get list of all categories
|
||||
$categories = Category::getTree()
|
||||
->toArray();
|
||||
|
||||
$modules = ModuleNamesWithIds::getWithTranslations();
|
||||
|
||||
return view('categories::admin.overview', [
|
||||
'categories' => $categories,
|
||||
'modules' => $modules
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -16,15 +16,17 @@ class CreateNewCategory extends Controller
|
||||
*/
|
||||
public function getNew()
|
||||
{
|
||||
$category = new Category();
|
||||
$categories = Category::getTree()->toArray();
|
||||
// get list of all categories
|
||||
$categories = Category::getTree()
|
||||
->toArray();
|
||||
|
||||
// get list of modules which supports categories
|
||||
$modules = Module::where('enabled', true)
|
||||
->where('support_categories', true)
|
||||
->get();
|
||||
|
||||
|
||||
return view('categories::admin.new', [
|
||||
'category' => $category,
|
||||
'category' => new Category(),
|
||||
'categories' => $categories,
|
||||
'modules' => $modules
|
||||
]);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php namespace Modules\Categories\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Module;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Categories\Entities\Category;
|
||||
use Pingpong\Modules\Routing\Controller;
|
||||
|
||||
class EditCategory extends Controller
|
||||
{
|
||||
/**
|
||||
* Display form for editing category
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||
*/
|
||||
public function getEdit($id)
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
// get list of all categories
|
||||
$categories = Category::getTree()
|
||||
->toArray();
|
||||
|
||||
// get list of modules which supports categories
|
||||
$modules = Module::where('enabled', true)
|
||||
->where('support_categories', true)
|
||||
->get();
|
||||
|
||||
return view('categories::admin.edit', [
|
||||
'category' => $category,
|
||||
'categories' => $categories,
|
||||
'modules' => $modules
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new category
|
||||
*
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function postEdit(Request $request, $id)
|
||||
{
|
||||
$category = Category::findOrFail($id);
|
||||
|
||||
$category->name = $request->name;
|
||||
$category->url = Str::slug($request->url);
|
||||
$category->description = $request->description;
|
||||
|
||||
$category->save();
|
||||
|
||||
|
||||
if ($category->parent_id != $request->parentCategory) {
|
||||
$category->parent_id = empty($request->parentCategory) ? null : $request->parentCategory;
|
||||
$category->save();
|
||||
|
||||
if (empty($request->parentCategory)) {
|
||||
$category->makeRoot(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return redirect()
|
||||
->route('categories.edit', ['id' => $id])
|
||||
->with('success', trans('categories::categories.saveSuccessfull'));
|
||||
}
|
||||
}
|
||||
@@ -12,4 +12,6 @@ Route::group([
|
||||
|
||||
Route::get('/edit/{id}', 'EditCategory@getEdit')->name('categories.edit');
|
||||
Route::post('/edit/{id}', 'EditCategory@postEdit')->name('categories.postEdit');
|
||||
|
||||
Route::get('/delete/{id}', 'EditCategory@delete')->name('categories.delete');
|
||||
});
|
||||
@@ -20,16 +20,17 @@
|
||||
<textarea name="description">{{old('description', $category->description)}}</textarea>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
@lang('categories::categories.fields.module')
|
||||
<select name="moduleId">
|
||||
@foreach($modules as $module)
|
||||
<option @if($module->id == $category->module_id) selected
|
||||
@endif value="{{$module->id}}">{{trans($module->name)}}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</label>
|
||||
|
||||
@if(!$category->id)
|
||||
<label>
|
||||
@lang('categories::categories.fields.module')
|
||||
<select name="moduleId">
|
||||
@foreach($modules as $module)
|
||||
<option @if($module->id == $category->module_id) selected
|
||||
@endif value="{{$module->id}}">{{trans($module->name)}}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</label>
|
||||
@endif
|
||||
<label>
|
||||
@lang('categories::categories.fields.parentCategory')
|
||||
<select name="parentCategory">
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
@extends('categories::layouts.master')
|
||||
@extends('admin.layout')
|
||||
|
||||
@section('content')
|
||||
|
||||
<h1>Hello World</h1>
|
||||
|
||||
<p>
|
||||
This view is loaded from module: {!! config('categories.name') !!}
|
||||
</p>
|
||||
<h2>@lang('categories::categories.overview')</h2>
|
||||
|
||||
<table class="full-width">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>@lang('categories::categories.name')</th>
|
||||
<th>@lang('categories::categories.module')</th>
|
||||
<th>@lang('categories::categories.actions')</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach($categories as $category)
|
||||
@include('categories::admin.snippets.overviewRow', ['level' => 0, 'currentCategory' => $category])
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
@stop
|
||||
@@ -0,0 +1,17 @@
|
||||
<tr>
|
||||
<td>
|
||||
{{str_repeat('-', $level)}} {{$currentCategory['name']}}
|
||||
</td>
|
||||
<td>@lang($modules[$currentCategory['module_id']])</td>
|
||||
<td>
|
||||
<a href="{{route('categories.edit', ['id' => $currentCategory['id']])}}"><i class="fa fa-fw fa-edit"></i></a>
|
||||
<a class="confirm" href="{{route('categories.delete', ['id' => $currentCategory['id']])}}"><i class="fa fa-fw fa-remove"></i></a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
@if(isset($currentCategory['children']))
|
||||
@foreach($currentCategory['children'] as $category)
|
||||
@include('categories::admin.snippets.overviewRow', ['currentCategory' => $category, 'level' => $level+1])
|
||||
@endforeach
|
||||
@endif
|
||||
Reference in New Issue
Block a user