Files

177 lines
5.0 KiB
PHP

<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Group\GroupRequest;
use App\Models\Group;
use App\Models\Log;
use App\Models\Type;
use App\Repositories\Group\GroupRepositoryInterface;
use App\Repositories\Log\LogRepositoryInterface;
use Illuminate\Http\Request;
use App\Support\Cache\CacheKey;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class GroupController extends Controller
{
//validate Group ID
protected function validateGrid()
{
$validate = Validator::make(\request()->all(), ['grid' => 'numeric']);
if ($validate->fails()) {
return false;
}
return \request()->grid;
}
public function __construct(
public GroupRepositoryInterface $groupRepository,
public LogRepositoryInterface $logRepository
)
{
parent::__construct();
}
//validate Group ID
/**
* Add new group
*/
public function addGroupInformation()
{
$title = 'Add New Product Group';
return view('administrator.pages.product.group', compact('title'));
}
/**
* Store information group
*/
public function storeGroupInformation(Request $request)
{
$request->validate([
'name' => 'required',
'type_id' => 'required',
'status' => 'required',
]);
try {
DB::beginTransaction();
$this->groupRepository->create($request->only(['name', 'type_id', 'status']));
// Clear all product-related caches after group creation
$this->clearGroupCaches($request->type_id);
DB::commit();
} catch (\Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('product.index')->with('success', 'Add New Product Group Success');
}
/**
* View information group
*/
public function getGroupInformation(Request $request)
{
$group_id = $this->validateGrid();
if (!$group_id) {
return redirect()->route('product.index')->withErrors('No information data found');
}
$group = Group::find($group_id);
if (!$group) {
return redirect()->route('product.index')->withErrors('No information data found');
}
$type = Type::select('id', 'name')->get();
$title = 'Action Group';
return view("administrator.pages.product.group", compact('title', 'type', 'group', 'group_id'));
}
/**
* Save information group
*/
public function updateGroupInformation(GroupRequest $request)
{
$data = $request->validated();
$group = Group::find($data['grid']);
if (!$group) {
return redirect()->route('product.index')->withErrors('No information data found');
}
try {
DB::beginTransaction();
Group::where('id', $group->id)->update($request->except(['_token', 'grid', '_method']));
// Clear all product-related caches after group update
$this->clearGroupCaches($group->type_id);
Log::create([
'agent' => $request->userAgent(),
'client_ip' => $request->getClientIp(),
'username' => $request->user('admins')->username,
'description' => $request->user('admins')->username . " modify Group Product",
'customer_id' => 0
]);
DB::commit();
} catch (\Exception $error) {
DB::rollBack();
return back()->withErrors($error->getMessage());
}
return redirect()->route('product.index')->with('success', 'Updated Product Group success');
}
public function removeGroup(Request $request)
{
$request->validate([
'group_id' => 'required'
]);
$group = Group::find($request->group_id);
// Clear caches before deletion
$this->clearGroupCaches($group->type_id);
$group->delete();
return redirect()->route('product.index')->with('success', 'Product group deleted successfully');
}
/**
* Clear all group and product-related caches
*/
private function clearGroupCaches($typeId): void
{
Cache::forget(CacheKey::build('group', 'shop-types'));
if ($typeId) {
Cache::forget(CacheKey::build('group', 'by-type', ['type_id' => $typeId]));
$groups = $this->groupRepository->filter(['type_id' => $typeId]);
foreach ($groups as $group) {
Cache::forget(CacheKey::build('group', 'products', ['type_id' => $typeId, 'group_id' => $group->id]));
if ($group->slug) {
for ($page = 1; $page <= 10; $page++) {
Cache::forget(CacheKey::build('group', 'slug-products', ['page' => $page, 'slug' => $group->slug]));
}
}
}
}
}
}