Raffales-LMS/app/Http/Controllers/RegistrationsController.php
2024-04-24 10:17:18 +05:45

406 lines
18 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Mail\CustomMailer;
use App\Models\Agents;
use App\Models\Campaigns;
use App\Models\Campaignarticles;
use App\Models\Testimonials;
use App\Models\Countries;
use App\Models\Leadcategories;
use App\Models\Logos;
use Illuminate\Http\Request;
use App\Models\Registrations;
use App\Models\Sources;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Log;
use App\Service\CommonModelService;
use Exception;
use Illuminate\Support\Facades\Mail;
use LMS;
use Illuminate\Support\Facades\View;
use NewRegistrationAdminNotification;
class RegistrationsController extends Controller
{
protected $modelService;
public function __construct(Registrations $model)
{
$this->modelService = new CommonModelService($model);
$articles = Campaignarticles::where('status', 1)->latest()->get();
$testimonials = Testimonials::where('status', 1)->latest()->get();
$logos = Logos::where('status',1)->latest()->get();
View::share([
'articles'=> $articles,
'testimonials'=>$testimonials,
'logos'=>$logos,
]);
}
public function index(Request $request)
{
$data = $this->filterRegistrations($request);
return view("crud.generated.registrations.index", compact('data', 'request'));
}
public function bycampaign(Request $request, $CampaignAlias)
{
$Campaign = Campaigns::where("status", 1)->where("alias", $CampaignAlias)->first();
$title = "Registrations By Campaign : " . $Campaign->title;
$data = $this->filterRegistrations($request, $Campaign);
return view("crud.generated.registrations.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->filterRegistrations($request, null, $LeadCategory);
$reques = $request->all();
return view("crud.generated.registrations.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->filterRegistrations($request, null, null, $Source);
$reques = $request->all();
return view("crud.generated.registrations.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->filterRegistrations($request, null, null, null, $Country);
$reques = $request->all();
return view("crud.generated.registrations.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->filterRegistrations($request, null, null, null, null, $Agent);
$reques = $request->all();
return view("crud.generated.registrations.index", compact('data', 'title', 'request'));
}
private function filterRegistrations($request, $Campaign = null, $LeadCategory = null, $Source = null, $Country = null, $Agent = null)
{
DB::enableQueryLog();
$requestData = $request->all();
// dd($requestData);
$data = Registrations::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(RegistrationsController::class, 'create', ' Registrations create');
$TableData = Registrations::where('status', '<>', -1)->orderBy('display_order')->get();
return view("crud.generated.registrations.create", compact('TableData'));
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'mobile' => 'required|numeric|unique:registrations,mobile', // Assuming 'users' is your table name and 'mobile' is the column
]);
$Campaign = Campaigns::findOrFail($request->campaigns_id);
if ($validator->fails()) {
$existingRegistration = Registrations::where('mobile', $request->mobile)->first();
if ($existingRegistration) {
return response()->json([
'status' => true,
'message' => 'Mobile number already exists. Returning existing data.',
'registration' => $existingRegistration,
'registration_id' => $existingRegistration->registration_id,
], 200);
}
return response()->json([
'status' => false,
'errors' => $validator->errors(),
'message' => 'Form validation failed.',
], 200); // 422 Unprocessable Entity
}
$request->request->add(['alias' => slugify($request->title)]);
$request->request->add(['display_order' => getDisplayOrder('tbl_registrations')]);
$request->request->add(['intrested_for_country' => $request->preferred_destination]);
$request->request->add(['sources_id' => ($request->sources_id) ? $request->sources_id : 2]);
$request->request->add(['campaigns_id' => ($request->campaigns_id) ? $request->campaigns_id : SITEVARS->Campaigns[0]->campaign_id]);
$request->request->add(['countries_id' => $request->countries_id]);
$request->request->add(['remarks' => $request->message]);
$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();
$registration = $this->modelService->create($operationNumber, $operationNumber, null, $requestData);
if (env("SEND_EMAIL")) {
$mailer = new CustomMailer($requestData);
Mail::bcc("prajwalbro@hotmail.com")->send($mailer->build());
Mail::to($request['email'])->send($mailer->build());
$notify_emails = isset($Campaign->notify_emails) ? explode(",", $Campaign->notify_emails) : "prajwalbro@hotmail.com";
Mail::cc($notify_emails)->send($mailer->build());
}
$msg = $Campaign->sms_message;
$success_messsage = $Campaign->success_messsage;
if (env("SEND_SMS")) {
$success_messsage = str_replace('%name%', $request['name'], $success_messsage);
$msg = str_replace('%name%', $request['name'], $msg);
LMS::oldsendSMSWithCurl($requestData['mobile'], $msg);
}
LMS::SaveInGoogleSheet($requestData);
} catch (\Exception $e) {
dd($e->getMessage());
DB::rollBack();
}
DB::commit();
return response()->json([
'status' => true,
'message' => ($success_messsage) ? $success_messsage : 'The Registration Created Successfully.',
'registration_id' => $registration->registration_id,
], 200);
}
function sendbulksms(Request $request)
{
$validator = Validator::make($request->all(), [
'message' => 'required', // Assuming 'users' is your table name and 'mobile' is the column
]);
if ($validator->fails()) {
return response()->json([
'status' => false,
'errors' => $validator->errors(),
'message' => 'Form validation failed.',
], 200); // 422 Unprocessable Entity
}
$mobile_nos = explode(",", $request->mobile);
//dd($mobile_nos);
$msg = $request->message;
foreach ($mobile_nos as $mobile) {
LMS::sendSMSWithCurl($mobile, $msg);
}
}
public function home(Request $request, $campaign_alias = null)
{
switch ($campaign_alias) {
case 'office': // Student/Visitor office visit gareko bela, MOBILE bata QR Scan garera fill garne
return view(env("CLIENT_PATH") . ".home");
case 'spin_the_wheel': // Student/Visitor office visit gareko bela, MOBILE bata QR Scan garera fill garne
return view("games.spin_the_wheel.game");
case 'direct': //Office ko Reception Bata directly fill-in garne form
return view("backend.reception");
case 'online': //Adv etc run garda online default landing bata fill-in garne form
return view(env("CLIENT_PATH") . ".home");
default:
if ($campaign_alias != "") {
$Campaign = LMS::getCampaignByAlias($campaign_alias);
} else {
$Campaign = LMS::getActiveCampaign();
}
if ($Campaign != null) {
$viewPath = env("CLIENT_PATH") . ".landing";
if (view()->exists($viewPath)) {
return view($viewPath, compact("Campaign"));
} else {
// dd($article);
return view(env("CLIENT_PATH") . '.home', compact("Campaign"));
}
} else {
$Campaign = LMS::getActiveCampaign();
$viewPath = env("CLIENT_PATH") . ".landing";
if (view()->exists($viewPath)) {
return view($viewPath, compact("Campaign"));
} else {
return view(env("CLIENT_PATH") . '.home', compact("Campaign"));
}
}
}
}
public function getTestimonial()
{
$testimonials = Testimonials::where('status', 1)->latest()->get();
// dd($testimonials);
return response()->json($testimonials);
}
public function reception(Request $request)
{
//Office ko Reception Bata directly fill-in garne form
return view("backend.reception");
}
public function reception_registrations(Request $request)
{
$data = $this->filterRegistrations($request);
//Office ko Reception Bata directly fill-in garne form
return view("backend.reception.registrations", compact('data'));
}
public function sort(Request $request)
{
$idOrder = $request->input('id_order');
foreach ($idOrder as $index => $id) {
$companyArticle = Registrations::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 = Registrations::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 view($id)
{
$data = '';
$data = Registrations::where(DB::raw('md5(registration_id)'), $id)->firstOrFail();
return view("lms.view", compact('data'));
}
public function confirmation($id)
{
$data = '';
$data = Registrations::where(DB::raw('md5(registration_id)'), $id)->firstOrFail();
return view("lms.confirmation", compact('data'));
}
public function show(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'show', ' Registrations show');
$data = Registrations::findOrFail($id);
return view("crud.generated.registrations.show", compact('data'));
}
public function edit(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'edit', ' Registrations edit');
$TableData = Registrations::where('status', '<>', -1)->orderBy('display_order')->get();
$data = Registrations::findOrFail($id);
if ($request->ajax()) {
$html = view("crud.generated.registrations.ajax.edit", compact('data'))->render();
return response()->json(['status' => true, 'content' => $html], 200);
}
return view("crud.generated.registrations.edit", compact('data', 'TableData'));
}
public function ajaxedit(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'edit', ' Registrations edit');
$TableData = Registrations::where('status', '<>', -1)->orderBy('display_order')->get();
$data = Registrations::findOrFail($id);
return view("crud.generated.registrations.ajaxedit", compact('data', 'TableData'));
}
public function update(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'update', ' Registrations 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('registration_id'));
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(RegistrationsController::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 Registrations updated Successfully.'], 200);
}
// return redirect()->route('registrations.index')->with('success','The Registrations updated Successfully.');
return redirect()->back()->with('success', 'The Registrations updated successfully.');
}
public function destroy(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'destroy', ' Registrations destroy');
DB::beginTransaction();
try {
$OperationNumber = getOperationNumber();
$this->modelService->destroy($OperationNumber, $OperationNumber, $id);
} catch (Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(RegistrationsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Registrations Deleted Successfully.'], 200);
}
public function toggle(Request $request, $id)
{
createActivityLog(RegistrationsController::class, 'destroy', ' Registrations destroy');
$data = Registrations::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(RegistrationsController::class, 'destroy', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
return response()->json(['status' => true, 'message' => 'The Registrations Deleted Successfully.'], 200);
}
}