Raffales-LMS/app/Http/Controllers/OptionsController.php

260 lines
10 KiB
PHP
Raw Normal View History

2024-04-16 09:58:24 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Options;
use App\Service\CommonModelService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class OptionsController extends Controller
{
protected $modelService;
public function __construct(Options $model)
{
$this->modelService = new CommonModelService($model);
}
public function index(Request $request)
{
createActivityLog(OptionsController::class, 'index', ' Options index');
$data = Options::where('status', '<>', -1)->orderBy('display_order')->get();
return view("crud.generated.options.index", compact('data'));
}
public function create(Request $request)
{
createActivityLog(OptionsController::class, 'create', ' Options create');
$programs = DB::table('programs')
->where('status', '<>', -1)
->orderBy('title') // Sort by title in alphabetical order
->get();
return view("crud.generated.options.create", compact('programs'));
}
public function issued_create(Request $request)
{
createActivityLog(OptionsController::class, 'create', ' Options create');
$programs = DB::table('programs')
->where('status', '<>', -1)
->orderBy('title') // Sort by title in alphabetical order
->get();
return view("crud.generated.options.issued_create", compact('programs'));
}
public function store(Request $request)
{
createActivityLog(OptionsController::class, 'store', 'Options store');
// Validate the request if needed
$validator = Validator::make($request->all(), [
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors(),
], 500);
}
// Get the JavaScript arrays from hidden fields
$programsData = json_decode($request->input('programsData'));
$advicesData = json_decode($request->input('advicesData'));
$descriptionsData = json_decode($request->input('descriptionsData'));
// Initialize an array to store the created option records
$createdOptions = [];
$valid = true;
if (is_int($programsData)) {
$valid = false;
}
// Loop through the JavaScript arrays and save data to the database
if ($valid) {
foreach ($programsData as $key => $programData) {
$programId = $programData;
$advice = isset($advicesData[$key]) ? $advicesData[$key] : '';
$description = isset($descriptionsData[$key]) ? $descriptionsData[$key] : '';
// Your code to save data for each row
$newOption = [
'alias' => slugify($request->title),
'display_order' => getDisplayOrder('tbl_options'),
'created_at' => now(),
'updated_at' => now(),
'students_id' => $request->input('students_id'),
'programs_id' => $programId,
'advice' => $advice,
'description' => $description,
];
// Add the new option to the createdOptions array
$createdOptions[] = $newOption;
}
}
if (!empty($request->input('advices')) && !empty($request->input('descriptions'))) {
$newOption = [
'alias' => slugify($request->title),
'display_order' => getDisplayOrder('tbl_options'),
'created_at' => now(),
'updated_at' => now(),
'students_id' => $request->input('students_id'),
'programs_id' => $request->input('programs_id'),
'advice' => $request->input('advices'),
'description' => $request->input('descriptions'),
];
$createdOptions[] = $newOption;
}
// Now you have an array of options to save
// You can use Eloquent or any other method to save the data to the database
DB::beginTransaction();
try {
$operationNumber = getOperationNumber();
// Create option records in a loop
foreach ($createdOptions as $optionData) {
$this->modelService->create($operationNumber, $operationNumber, null, $optionData);
}
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(OptionsController::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 Options Created Successfully.'], 200);
}
return redirect()->route('options.index')->with('success', 'The Options created Successfully.');
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = Options::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 = Options::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(OptionsController::class, 'show', ' Options show');
$data = Options::where('students_id', $id)->get();
$student_id = $id;
return view("crud.generated.options.show", compact('data', 'student_id'));
}
public function edit(Request $request, $id)
{
createActivityLog(OptionsController::class, 'edit', ' Options edit');
$TableData = Options::where('status', '<>', -1)->orderBy('display_order')->get();
$data = Options::findOrFail($id);
if ($request->ajax()) {
$html = view("crud.generated.options.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.options.edit", compact('data', 'TableData'));
}
public function update(Request $request, $id)
{
createActivityLog(OptionsController::class, 'update', ' Options 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);
});
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->update($OperationNumber, $OperationNumber, null, $requestData, $request->input('option_id'));
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(OptionsController::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 Options updated Successfully.'], 200);
}
// return redirect()->route('options.index')->with('success','The Options updated Successfully.');
return redirect()->back()->with('success', 'The Options updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(OptionsController::class, 'destroy', ' Options destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(OptionsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Options Deleted Successfully.'], 200);
}
public function toggle(Request $request, $id)
{
createActivityLog(OptionsController::class, 'destroy', ' Options destroy');
$data = Options::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(OptionsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Options Deleted Successfully.'], 200);
}
}