Raffales-LMS/app/Http/Controllers/FormsController.php
2024-04-16 15:43:24 +05:45

132 lines
5.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Mail\CustomMailer;
use App\Models\Registrations;
use App\Service\CommonModelService;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Mail;
class FormsController extends Controller
{
protected $modelService;
public function __construct(Registrations $model)
{
$this->modelService = new CommonModelService($model);
}
function saveregistration(Request $r)
{
$requestData=$r->all();
DB::beginTransaction();
try {
$operationNumber = getOperationNumber();
$this->modelService->create($operationNumber, $operationNumber, null, $requestData);
} catch (\Exception $e) {
DB::rollBack();
Log::info($e->getMessage());
createErrorLog(ProvincesController::class, 'store', $e->getMessage());
return response()->json(['status' => false, 'message' => $e->getMessage()], 500);
}
DB::commit();
// $FormData = array(
// "name" => isset($r['name']) ? $r['name'] : "",
// "email" => isset($r['email']) ? $r['email'] : "",
// "phone" => isset($r['phone']) ? $r['phone'] : "",
// "preferred_destination" => isset($r['preferred_destination']) ? $r['preferred_destination'] : "",
// "mode_of_counselling" => isset($r['mode_of_counselling']) ? $r['mode_of_counselling'] : "",
// "nearest_branch" => isset($r['nearest_branch']) ? $r['nearest_branch'] : "",
// "message" => isset($r['message']) ? $r['message'] : ""
// );
// DB::table('enquiries')->insert($FormData);
// $mailer = new CustomMailer($FormData);
// Mail::to("prajwalbro@hotmail.com")->send($mailer->enquiryform());
// Mail::to($r['email'])->send($mailer->enquiryresponse());
// Respond back to the customer
return response()->json(['success' => true, 'message' => 'Form submitted successfully!']);
}
function submitenquiry(Request $r)
{
$r = $r->all();
if (!Schema::hasTable('enquiries')) {
try {
Schema::create('enquiries', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('phone');
$table->string('preferred_destination');
$table->string('mode_of_counselling');
$table->string('nearest_branch');
$table->text('message');
$table->timestamps();
});
} catch (QueryException $e) {
return response()->json(['error' => 'Error creating the table: ' . $e->getMessage()], 500);
}
}
$FormData = array(
"name" => isset($r['name']) ? $r['name'] : "",
"email" => isset($r['email']) ? $r['email'] : "",
"phone" => isset($r['phone']) ? $r['phone'] : "",
"preferred_destination" => isset($r['preferred_destination']) ? $r['preferred_destination'] : "",
"mode_of_counselling" => isset($r['mode_of_counselling']) ? $r['mode_of_counselling'] : "",
"nearest_branch" => isset($r['nearest_branch']) ? $r['nearest_branch'] : "",
"message" => isset($r['message']) ? $r['message'] : ""
);
DB::table('enquiries')->insert($FormData);
$mailer = new CustomMailer($FormData);
Mail::to("prajwalbro@hotmail.com")->send($mailer->enquiryform());
Mail::to($r['email'])->send($mailer->enquiryresponse());
// Respond back to the customer
return response()->json(['success' => true, 'message' => 'Form submitted successfully!']);
}
function handleFormSubmission(Request $r)
{
$r = $r->all();
$FormID = $r['form_id'];
$FormFields = Forms::where("form_id", $FormID)->pluck('form_fields')[0];
$tableName = 'formsubmissions';
if (!Schema::hasTable($tableName)) {
Schema::create($tableName, function (Blueprint $table) {
$table->increments('formsubmission_id');
$table->integer('forms_id')->unsigned();
$table->json('submitted_values');
$table->integer('display_order')->default(0);
$table->integer('status')->default(1);
$table->timestamps();
$table->integer('createdby')->nullable();
$table->integer('updatedby')->nullable();
});
}
$FormFields = json_decode($FormFields);
$FilledForm = array();
foreach ($FormFields as $FormField) {
$fieldData = new \stdClass;
$fieldData->fieldName = $FormField->fieldName;
$fieldData->fieldType = $FormField->fieldType;
$fieldData->fieldValue = isset($r[strtolower($FormField->fieldAlias)]) ? $r[strtolower($FormField->fieldAlias)] : "";
$FilledForm[] = $fieldData;
}
$FilledForm = json_encode($FilledForm);
$formSubmission = new FormSubmissions();
$formSubmission->forms_id = $FormID;
$formSubmission->submitted_values = $FilledForm;
$formSubmission->save();
return redirect()->back()->with('success', 'Form submitted successfully!');
}
}