master_template/app/Http/Controllers/MenuitemsController.php

238 lines
11 KiB
PHP
Raw Normal View History

2024-06-10 12:21:58 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Menuitems;
use App\Service\CommonModelService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Log;
class MenuitemsController extends Controller
{
protected $modelService;
protected static $menuTypes;
public function __construct(Menuitems $model)
{
$this->modelService = new CommonModelService($model);
self::initializeController();
}
public function initializeController()
{
$menuTypes = [
['display' => "Articles", 'value' => "tbl_articles"],
['display' => "Country", 'value' => "tbl_countries"],
['display' => "Country Articles", 'value' => "tbl_countryarticles"],
['display' => "Events", 'value' => "tbl_events"],
['display' => "Faqs", 'value' => "tbl_faqs"],
['display' => "Institutions", 'value' => "tbl_institutions"],
['display' => "News/Blogs", 'value' => "tbl_news"],
['display' => "Preparation class", 'value' => "tbl_preparationclasses"],
['display' => "Testimonial", 'value' => "tbl_testimonials"],
['display' => "Custom", 'value' => ""],
];
foreach ($menuTypes as &$menuType) {
switch ($menuType['value']) {
case 'tbl_articles':
$menuType['values'] = json_encode(DB::select("select article_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_countries':
$menuType['values'] = json_encode(DB::select("select country_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_countryarticles':
$menuType['values'] = json_encode(DB::select("select article_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_events':
$menuType['values'] = json_encode(DB::select("select event_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_faqs':
$menuType['values'] = json_encode(DB::select("select faq_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_institutions':
$menuType['values'] = json_encode(DB::select("select institution_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_news':
$menuType['values'] = json_encode(DB::select("select news_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_preparationclasses':
$menuType['values'] = json_encode(DB::select("select preparationclass_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_testimonials':
$menuType['values'] = json_encode(DB::select("select testimonial_id as value, `by` as display from " . $menuType['value'] . " where status=1 order by `by`"));
break;
default:
$menuType['values'] = "";
}
}
self::$menuTypes = $menuTypes;
}
public function index(Request $request)
{
createActivityLog(MenuitemsController::class, 'index', ' Menuitems index');
$data = Menuitems::where('status', '<>', -1);
$menulocation = $request->query("menulocation");
if (null != $menulocation) {
$data = $data->where('menulocations_id', $menulocation);
}
$data = $data->orderBy('display_order')->get();
return view("crud.generated.menuitems.index", compact('data', 'menulocation'));
}
public function create(Request $request)
{
createActivityLog(MenuitemsController::class, 'create', ' Menuitems create');
$TableData = Menuitems::where('status', '<>', -1);
$menulocation = $request->query("menulocation");
if (null != $menulocation) {
$TableData = $TableData->where('menulocations_id', $menulocation);
}
$TableData = $TableData->orderBy('display_order')->get();
$menuTypes = self::$menuTypes;
return view("crud.generated.menuitems.create", compact('TableData', 'menuTypes', 'menulocation'));
}
public function store(Request $request)
{
createActivityLog(MenuitemsController::class, 'store', ' Menuitems store');
$validator = Validator::make($request->all(), [
//ADD REQUIRED FIELDS FOR VALIDATION
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors(),
], 500);
}
$request->request->add(['alias' => slugify($request->title)]);
$request->request->add(['display_order' => getDisplayOrder('tbl_menuitems')]);
$requestData = $request->all();
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL') . '/', '', $value);
});
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL'), '', $value);
});
DB::beginTransaction();
try {
$operationNumber = getOperationNumber();
$this->modelService->create($operationNumber, $operationNumber, null, $requestData);
} catch (\Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(MenuitemsController::class, 'store', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Menuitems Created Successfully.'], 200);
}
return redirect()->back()->with('success', 'The Menuitems Added successfully.');
// return redirect()->route('menuitems.index')->with('success', 'The Menuitems created Successfully.');
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = Menuitems::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 = Menuitems::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(MenuitemsController::class, 'show', ' Menuitems show');
$data = Menuitems::findOrFail($id);
return view("crud.generated.menuitems.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(MenuitemsController::class, 'edit', ' Menuitems edit');
$TableData = Menuitems::where('status', '<>', -1);
$menulocation = $request->query("menulocation");
if (null != $menulocation) {
$TableData = $TableData->where('menulocations_id', $menulocation);
}
$TableData = $TableData->orderBy('display_order')->get();
$menuTypes = self::$menuTypes;
$data = Menuitems::findOrFail($id);
return view("crud.generated.menuitems.edit", compact('data', 'TableData', 'menulocation', 'menuTypes'));
}
public function update(Request $request, $id)
{
createActivityLog(MenuitemsController::class, 'update', ' Menuitems update');
$validator = Validator::make($request->all(), [
//ADD VALIDATION FOR REQIRED FIELDS
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors(),
], 500);
}
$requestData = $request->all();
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL') . '/', '', $value);
});
array_walk_recursive($requestData, function (&$value) {
$value = str_replace(env('APP_URL'), '', $value);
});
// dd($requestData);
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->update($OperationNumber, $OperationNumber, null, $requestData, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(MenuitemsController::class, 'update', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The Menuitems updated Successfully.'], 200);
}
return redirect()->route('menuitems.index')->with('success', 'The Menuitems updated Successfully.');
// return redirect()->back()->with('success', 'The Menuitems updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(MenuitemsController::class, 'destroy', ' Menuitems destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(MenuitemsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Menuitems Deleted Successfully.'], 200);
}
}