first upload all files
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Http\Controllers\Admin;
|
||||
|
||||
use Modules\Category\Entities\Category;
|
||||
use Modules\Admin\Traits\HasCrudActions;
|
||||
use Modules\Category\Http\Requests\SaveCategoryRequest;
|
||||
|
||||
class CategoryController
|
||||
{
|
||||
use HasCrudActions;
|
||||
|
||||
/**
|
||||
* Model for the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $model = Category::class;
|
||||
|
||||
/**
|
||||
* Label of the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $label = 'category::categories.category';
|
||||
|
||||
/**
|
||||
* View path of the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $viewPath = 'category::admin.categories';
|
||||
|
||||
/**
|
||||
* Form requests for the resource.
|
||||
*
|
||||
* @var array|string
|
||||
*/
|
||||
protected $validation = SaveCategoryRequest::class;
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return Category::with('files')->withoutGlobalScope('active')->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy resources by given ids.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
Category::withoutGlobalScope('active')
|
||||
->findOrFail($id)
|
||||
->delete();
|
||||
|
||||
return back()->withSuccess(trans('admin::messages.resource_deleted', ['resource' => $this->getLabel()]));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Http\Controllers\Admin;
|
||||
|
||||
use Modules\Category\Entities\Category;
|
||||
use Modules\Category\Services\CategoryTreeUpdater;
|
||||
use Modules\Category\Http\Responses\CategoryTreeResponse;
|
||||
|
||||
class CategoryTreeController
|
||||
{
|
||||
/**
|
||||
* Display category tree in json.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$categories = Category::withoutGlobalScope('active')
|
||||
->orderByRaw('-position DESC')
|
||||
->get();
|
||||
|
||||
return new CategoryTreeResponse($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update category tree in storage.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
CategoryTreeUpdater::update(request('category_tree'));
|
||||
|
||||
return trans('category::messages.category_order_saved');
|
||||
}
|
||||
}
|
||||
20
Modules/Category/Http/Controllers/CategoryController.php
Normal file
20
Modules/Category/Http/Controllers/CategoryController.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Http\Controllers;
|
||||
|
||||
use Modules\Category\Entities\Category;
|
||||
|
||||
class CategoryController
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('public.categories.index', [
|
||||
'categories' => Category::all()->nest(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Http\Controllers;
|
||||
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Category\Entities\Category;
|
||||
use Modules\Product\Filters\ProductFilter;
|
||||
use Modules\Product\Http\Controllers\ProductSearch;
|
||||
|
||||
class CategoryProductController
|
||||
{
|
||||
use ProductSearch;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param string $slug
|
||||
* @param \Modules\Product\Entities\Product $model
|
||||
* @param \Modules\Product\Filters\ProductFilter $productFilter
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index($slug, Product $model, ProductFilter $productFilter)
|
||||
{
|
||||
request()->merge(['category' => $slug]);
|
||||
|
||||
if (request()->expectsJson()) {
|
||||
return $this->searchProducts($model, $productFilter);
|
||||
}
|
||||
|
||||
$category = Category::findBySlug($slug);
|
||||
|
||||
return view('public.products.index', [
|
||||
'categoryName' => $category->name,
|
||||
'categoryBanner' => $category->banner->path,
|
||||
]);
|
||||
}
|
||||
}
|
||||
45
Modules/Category/Http/Requests/SaveCategoryRequest.php
Normal file
45
Modules/Category/Http/Requests/SaveCategoryRequest.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Http\Requests;
|
||||
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Category\Entities\Category;
|
||||
use Modules\Core\Http\Requests\Request;
|
||||
|
||||
class SaveCategoryRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Available attributes.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $availableAttributes = 'category::attributes';
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'name' => 'required',
|
||||
'slug' => $this->getSlugRules(),
|
||||
'is_active' => 'required|boolean',
|
||||
'is_active' => 'required|boolean',
|
||||
];
|
||||
}
|
||||
|
||||
private function getSlugRules()
|
||||
{
|
||||
$rules = $this->route()->getName() === 'admin.categories.update'
|
||||
? ['required']
|
||||
: ['nullable'];
|
||||
|
||||
$slug = Category::withoutGlobalScope('active')->where('id', $this->id)->value('slug');
|
||||
|
||||
$rules[] = Rule::unique('categories', 'slug')->ignore($slug, 'slug');
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
55
Modules/Category/Http/Responses/CategoryTreeResponse.php
Normal file
55
Modules/Category/Http/Responses/CategoryTreeResponse.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Http\Responses;
|
||||
|
||||
use Illuminate\Contracts\Support\Responsable;
|
||||
|
||||
class CategoryTreeResponse implements Responsable
|
||||
{
|
||||
/**
|
||||
* Categories collection.
|
||||
*
|
||||
* @var \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
private $categories;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Collection $categories
|
||||
*/
|
||||
public function __construct($categories)
|
||||
{
|
||||
$this->categories = $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an HTTP response that represents the object.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function toResponse($request)
|
||||
{
|
||||
return response()->json($this->transform());
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the categories.
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
private function transform()
|
||||
{
|
||||
return $this->categories->map(function ($category) {
|
||||
return [
|
||||
'id' => $category->id,
|
||||
'parent' => $category->parent_id ?: '#',
|
||||
'text' => $category->name,
|
||||
'data' => [
|
||||
'position' => $category->position,
|
||||
],
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user