master_template/app/Http/Controllers/PermissionsController.php

151 lines
5.4 KiB
PHP
Raw Normal View History

2024-06-20 10:57:33 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\CommonModelService;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Log;
use Exception;
class PermissionsController extends Controller
{
protected $modelService;
public function __construct(Permission $model)
{
$this->modelService = new CommonModelService($model);
}
public function index(Request $request)
{
createActivityLog(PermissionsController::class, 'index', ' Permissions index');
$data = Permission::get();
return view("crud.generated.permissions.index", compact('data'));
}
public function create(Request $request)
{
createActivityLog(PermissionsController::class, 'create', ' Permissions create');
$TableData = Permission::get();
return view("crud.generated.permissions.create", compact('TableData'));
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|max:255',
]);
Permission::create($validated);
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Permissions Created Successfully.'], 200);
}
return redirect()->route('permissions.index')->with('success', 'The Permissions created Successfully.');
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = Permission::find($id);
$companyArticle->display_order = $index + 1;
$companyArticle->save();
}
return response()->json(['status' => true, 'content' => 'The articles sorted successfully.'], 200);
}
public function updatealias(Request $request)
{
$articleId = $request->input('articleId');
$newAlias = $request->input('newAlias');
$companyArticle = Permission::find($articleId);
if (!$companyArticle) {
return response()->json(['status' => false, 'content' => 'Company article not found.'], 404);
}
$companyArticle->alias = $newAlias;
$companyArticle->save();
return response()->json(['status' => true, 'content' => 'Alias updated successfully.'], 200);
}
public function show(Request $request, $id)
{
createActivityLog(PermissionsController::class, 'show', ' Permissions show');
$data = Permission::findOrFail($id);
return view("crud.generated.permissions.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(PermissionsController::class, 'edit', ' Permissions edit');
$TableData = Permission::get();
$data = Permission::findOrFail($id);
if ($request->ajax()) {
$html = view("crud.generated.permissions.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.permissions.edit", compact('data', 'TableData'));
}
public function update(Request $request, Permission $permission)
{
$validated = $request->validate(['name' => 'required']);
$permission->update($validated);
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Permissions updated Successfully.'], 200);
}
// return redirect()->route('permissions.index')->with('success','The Permissions updated Successfully.');
return redirect()->route('permissions.index')->with('success', 'The Permissions updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(PermissionsController::class, 'destroy', ' Permissions destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(PermissionsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Permissions Deleted Successfully.'], 200);
}
public function toggle(Request $request, $id)
{
createActivityLog(PermissionsController::class, 'destroy', ' Permissions destroy');
$data = Permission::findOrFail($id);
$requestData = ['status' => ($data->status == 1) ? 0 : 1];
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->update($OperationNumber, $OperationNumber, null, $requestData, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(PermissionsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Permissions Deleted Successfully.'], 200);
}
}