master_template/app/Http/Controllers/NewsController.php
2024-06-15 22:23:54 +05:45

214 lines
7.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Repositories\NewsRepository;
use Illuminate\Http\Request;
use App\Models\News;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Service\CommonModelService;
use Illuminate\Support\Str;
use Log;
use Exception;
class NewsController extends Controller
{
protected $modelService;
protected $newsRepository;
public function __construct(News $model, NewsRepository $newsRepository)
{
$this->modelService = new CommonModelService($model);
$this->newsRepository = $newsRepository;
}
public function index(Request $request)
{
createActivityLog(NewsController::class, 'index', ' News index');
$data = News::where('status', '<>', -1)->orderBy('display_order')->get();
return view("crud.generated.news.index", compact('data'));
}
public function create(Request $request)
{
createActivityLog(NewsController::class, 'create', ' News create');
$TableData = News::where('status', '<>', -1)->orderBy('display_order')->get();
return view("crud.generated.news.create", compact('TableData'));
}
public function store(Request $request)
{
// dd($request->all());
$isFeatured = $request->input('featured_news');
createActivityLog(NewsController::class, 'store', ' News store');
$validator = Validator::make($request->all(), [
//ADD REQUIRED FIELDS FOR VALIDATION
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors(),
], 500);
}
$request->mergeIfMissing([
'alias' => Str::slug($request->title),
]);
$request->request->add(['display_order' => getDisplayOrder('tbl_news')]);
$requestData = $request->all();
$requestData['createdBy'] = Auth::user()->id;
$requestData['updatedBy'] = Auth::user()->id;
if ($isFeatured) {
News::where('featured_news', 'True')->update(['featured_news' => 'False']);
$requestData['featured_news'] = 'True';
} else {
$requestData['featured_news'] = 'False';
}
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);
});
$this->newsRepository->create($requestData);
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The News Created Successfully.'], 200);
}
return redirect()->route('news.index')->with('success', 'The News created Successfully.');
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = News::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 = News::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(NewsController::class, 'show', ' News show');
$data = News::findOrFail($id);
return view("crud.generated.news.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(NewsController::class, 'edit', ' News edit');
$TableData = News::where('status', '<>', -1)->orderBy('display_order')->get();
$data = News::findOrFail($id);
if ($request->ajax()) {
$html = view("crud.generated.news.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.news.edit", compact('data', 'TableData'));
}
public function update(Request $request, $id)
{
$isFeatured = $request->input('featured_news');
createActivityLog(NewsController::class, 'update', ' News update');
$validator = Validator::make($request->all(), [
//ADD VALIDATION FOR REQIRED FIELDS
]);
if ($validator->fails()) {
return response()->json([
'error' => $validator->errors(),
], 500);
}
$request->mergeIfMissing([
'alias' => Str::slug($request->title),
]);
$filterData = $request->except('_method', '_token');
if ($isFeatured) {
News::where('featured_news', 'True')->update(['featured_news' => 'False']);
$filterData['featured_news'] = 'True';
} else {
$filterData['featured_news'] = 'False';
}
array_walk_recursive($filterData, function (&$value) {
$value = str_replace(env('APP_URL') . '/', '', $value);
});
array_walk_recursive($filterData, function (&$value) {
$value = str_replace(env('APP_URL'), '', $value);
});
$this->newsRepository->update($id, $filterData);
if ($request->ajax()) {
return response()->json(['status' => true, 'message' => 'The News updated Successfully.'], 200);
}
// return redirect()->route('news.index')->with('success','The News updated Successfully.');
return redirect()->route('news.index')->with('success', 'The News updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(NewsController::class, 'destroy', ' News destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(NewsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The News Deleted Successfully.'], 200);
}
public function toggle(Request $request, $id)
{
createActivityLog(NewsController::class, 'destroy', ' News destroy');
$data = News::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(NewsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The News Deleted Successfully.'], 200);
}
}