287 lines
14 KiB
PHP
287 lines
14 KiB
PHP
|
<?php
|
||
|
namespace App\Http\Controllers;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Models\Agents;
|
||
|
use App\Models\Campaigns;
|
||
|
use App\Models\Countries;
|
||
|
use App\Models\Leadcategories;
|
||
|
use App\Models\Sources;
|
||
|
use Illuminate\Http\Request;
|
||
|
use App\Models\Students;
|
||
|
use Illuminate\Support\Facades\DB;
|
||
|
use Illuminate\Support\Facades\Validator;
|
||
|
use App\Service\CommonModelService;
|
||
|
use Log;
|
||
|
use Exception;
|
||
|
|
||
|
class StudentsController extends Controller
|
||
|
{
|
||
|
protected $modelService;
|
||
|
public function __construct(Students $model)
|
||
|
{
|
||
|
$this->modelService = new CommonModelService($model);
|
||
|
}
|
||
|
public function index(Request $request)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'index', ' Students index');
|
||
|
$data = Students::where('status','<>',-1)->orderBy('display_order')->get();
|
||
|
|
||
|
return view("crud.generated.students.index", compact('data'));
|
||
|
}
|
||
|
public function bycampaign(Request $request, $CampaignAlias)
|
||
|
{
|
||
|
$Campaign = Campaigns::where("status", 1)->where("alias", $CampaignAlias)->first();
|
||
|
$title = "Students By Campaign : " . $Campaign->title;
|
||
|
$data = $this->filterStudents($request, $Campaign);
|
||
|
return view("crud.generated.students.index", compact('data', 'title', 'request'));
|
||
|
}
|
||
|
public function bycategory(Request $request, $CategoryAlias)
|
||
|
{
|
||
|
$LeadCategory = Leadcategories::where("alias", $CategoryAlias)->first();
|
||
|
$title = "Registrations By Lead Category : " . $LeadCategory->title;
|
||
|
$data = $this->filterStudents($request, null, $LeadCategory);
|
||
|
$reques = $request->all();
|
||
|
return view("crud.generated.students.index", compact('data', 'title', 'request'));
|
||
|
}
|
||
|
public function bysource(Request $request, $SourceAlias)
|
||
|
{
|
||
|
$Source = Sources::where("alias", $SourceAlias)->first();
|
||
|
$title = "Registrations By Source : " . $Source->title;
|
||
|
$data = $this->filterStudents($request, null, null, $Source);
|
||
|
$reques = $request->all();
|
||
|
return view("crud.generated.students.index", compact('data', 'title', 'request'));
|
||
|
}
|
||
|
public function bycountry(Request $request, $CountryAlias)
|
||
|
{
|
||
|
$Country = Countries::where("alias", $CountryAlias)->first();
|
||
|
$title = "Registrations By Country : " . $Country->title;
|
||
|
$data = $this->filterStudents($request, null, null, null, $Country);
|
||
|
$reques = $request->all();
|
||
|
return view("crud.generated.students.index", compact('data', 'title', 'request'));
|
||
|
}
|
||
|
public function byagent(Request $request, $AgentAlias)
|
||
|
{
|
||
|
$Agent = Agents::where("alias", $AgentAlias)->first();
|
||
|
$title = "Registrations By Agency : " . $Agent->title;
|
||
|
$data = $this->filterStudents($request, null, null, null, null, $Agent);
|
||
|
$reques = $request->all();
|
||
|
return view("crud.generated.students.index", compact('data', 'title', 'request'));
|
||
|
}
|
||
|
private function filterStudents($request, $Campaign = null, $LeadCategory = null, $Source = null, $Country = null, $Agent = null)
|
||
|
{
|
||
|
DB::enableQueryLog();
|
||
|
$requestData = $request->all();
|
||
|
// dd($requestData);
|
||
|
$data = Students::where('status', '<>', -1);
|
||
|
if (isset($requestData['from']) && $requestData['from']) {
|
||
|
$data = $data->whereDate("created_at", ">=", $requestData['from']);
|
||
|
}
|
||
|
if (isset($requestData['to'])) {
|
||
|
$data = $data->whereDate("created_at", "<", $requestData['to']);
|
||
|
}
|
||
|
if (isset($requestData['source_id']) && $requestData['source_id'] != 0) {
|
||
|
$data = $data->where("sources_id", "=", $requestData['source_id']);
|
||
|
}
|
||
|
if (isset($requestData['country_id']) && $requestData['country_id'] != 0) {
|
||
|
$data = $data->where("countries_id", "=", $requestData['country_id']);
|
||
|
}
|
||
|
if (isset($requestData['leadcategory_id']) && $requestData['leadcategory_id']) {
|
||
|
$data = $data->where("leadcategories_id", "=", $requestData['leadcategory_id']);
|
||
|
}
|
||
|
if ($Campaign !== null) {
|
||
|
$data = $data->where("campaigns_id", $Campaign->campaign_id);
|
||
|
}
|
||
|
if ($LeadCategory !== null) {
|
||
|
$data = $data->where("leadcategories_id", $LeadCategory->leadcategory_id);
|
||
|
}
|
||
|
if ($Source !== null) {
|
||
|
$data = $data->where("sources_id", $Source->source_id);
|
||
|
}
|
||
|
if ($Country !== null) {
|
||
|
$data = $data->where("countries_id", $Country->country_id);
|
||
|
}
|
||
|
if ($Agent !== null) {
|
||
|
$data = $data->where("agents_id", $Agent->agent_id);
|
||
|
}
|
||
|
$data = $data->orderBy('display_order');
|
||
|
$sqlQuery = $data->toSql();
|
||
|
$data = $data->get();
|
||
|
$data->RawQuery = DB::getQueryLog();
|
||
|
return $data;
|
||
|
}
|
||
|
|
||
|
|
||
|
public function create(Request $request)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'create', ' Students create');
|
||
|
$TableData = Students::where('status','<>',-1)->orderBy('display_order')->get();
|
||
|
return view("crud.generated.students.create",compact('TableData'));
|
||
|
}
|
||
|
|
||
|
public function store(Request $request)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'store', ' Students 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_students')]);
|
||
|
$request->request->add(['created_at' => date("Y-m-d h:i:s")]);
|
||
|
$request->request->add(['updated_at' => date("Y-m-d h:i:s")]);
|
||
|
$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(StudentsController::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 Students Created Successfully.'], 200);
|
||
|
}
|
||
|
return redirect()->route('students.index')->with('success','The Students created Successfully.');
|
||
|
}
|
||
|
|
||
|
public function sort(Request $request)
|
||
|
{
|
||
|
$idOrder = $request->input('id_order');
|
||
|
|
||
|
foreach ($idOrder as $index => $id) {
|
||
|
$companyArticle = Students::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 = Students::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(StudentsController::class, 'show', ' Students show');
|
||
|
$data = Students::findOrFail($id);
|
||
|
|
||
|
return view("crud.generated.students.show", compact('data'));
|
||
|
}
|
||
|
|
||
|
|
||
|
public function edit(Request $request, $id)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'edit', ' Students edit');
|
||
|
$TableData = Students::where('status','<>',-1)->orderBy('display_order')->get();
|
||
|
$data = Students::findOrFail($id);
|
||
|
if ($request->ajax()) {
|
||
|
$html = view("crud.generated.students.ajax.edit", compact('data'))->render();
|
||
|
return response()->json(['status' => true, 'content' => $html], 200);
|
||
|
}
|
||
|
return view("crud.generated.students.edit", compact('data','TableData'));
|
||
|
}
|
||
|
|
||
|
|
||
|
public function update(Request $request, $id)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'update', ' Students 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('student_id'));
|
||
|
} catch (Exception $e) {
|
||
|
DB::rollBack();
|
||
|
Log::info($e->getMessage());
|
||
|
createErrorLog(StudentsController::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 Students updated Successfully.'], 200);
|
||
|
}
|
||
|
// return redirect()->route('students.index')->with('success','The Students updated Successfully.');
|
||
|
return redirect()->back()->with('success', 'The Students updated successfully.');
|
||
|
}
|
||
|
|
||
|
public function destroy(Request $request,$id)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'destroy', ' Students destroy');
|
||
|
DB::beginTransaction();
|
||
|
try {
|
||
|
$OperationNumber = getOperationNumber();
|
||
|
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
|
||
|
} catch (Exception $e) {
|
||
|
DB::rollBack();
|
||
|
Log::info($e->getMessage());
|
||
|
createErrorLog(StudentsController::class, 'destroy', $e->getMessage());
|
||
|
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
|
||
|
}
|
||
|
DB::commit();
|
||
|
return response()->json(['status'=>true,'message'=>'The Students Deleted Successfully.'],200);
|
||
|
}
|
||
|
public function toggle(Request $request,$id)
|
||
|
{
|
||
|
createActivityLog(StudentsController::class, 'destroy', ' Students destroy');
|
||
|
$data = Students::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(StudentsController::class, 'destroy', $e->getMessage());
|
||
|
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
|
||
|
}
|
||
|
DB::commit();
|
||
|
return response()->json(['status'=>true,'message'=>'The Students Deleted Successfully.'],200);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|