TanchoToplineCargo/app/Http/Controllers/MenuitemsController.php

258 lines
12 KiB
PHP
Raw Normal View History

2024-05-05 04:47:49 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Menuitems;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Service\CommonModelService;
use Log;
use Exception;
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' => "Company Related Articles", 'value' => "tbl_companyarticles"],
['display' => "Franchise Related Articles", 'value' => "tbl_franchises"],
['display' => "News Related Articles", 'value' => "tbl_news"],
['display' => "Teams Related Articles", 'value' => "tbl_teams"],
['display' => "Testimonial Related Articles", 'value' => "tbl_testimonials"],
['display' => "Services", 'value' => "tbl_services"],
// ['display' => "Recruitment Related Articles", 'value' => "tbl_recruitmentarticles"],
// ['display' => "Other Articles", 'value' => "tbl_otherarticles"],
// ['display' => "Companies", 'value' => "tbl_companies"],
// ['display' => "Job Categories", 'value' => "tbl_job_categories"],
// ['display' => "News", 'value' => "tbl_news"],
// ['display' => "Country", 'value' => "tbl_countries"],
// ['display' => "Contacts", 'value' => "tbl_contacts"],
// ['display' => "Galleries", 'value' => "tbl_galleries"],
// ['display' => "Forms", 'value' => "tbl_forms"],
['display' => "Custom", 'value' => ""]
];
foreach ($menuTypes as &$menuType) {
switch ($menuType['value']) {
case 'tbl_companyarticles':
$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_franchises':
$menuType['values'] = json_encode(DB::select("select franchise_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_teams':
$menuType['values'] = json_encode(DB::select("select team_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,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
case 'tbl_services':
$menuType['values'] = json_encode(DB::select("select service_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
break;
// case 'tbl_otherarticles':
// $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_companies':
// $menuType['values'] = json_encode(DB::select("select company_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
// break;
// case 'tbl_job_categories':
// $menuType['values'] = json_encode(DB::select("select category_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_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_contacts':
// $menuType['values'] = json_encode(DB::select("select contact_id as value,name as display from " . $menuType['value'] . " where status=1 Order by name"));
// break;
// case 'tbl_galleries':
// $menuType['values'] = json_encode(DB::select("select gallery_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
// break;
// case 'tbl_forms':
// $menuType['values'] = json_encode(DB::select("select form_id as value,title as display from " . $menuType['value'] . " where status=1 Order by title"));
// 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 Companyarticles 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, $request->input('menu_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);
}
}