324 lines
13 KiB
PHP
324 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use App\Http\Requests\Creation;
|
|
use App\Models\Session;
|
|
use App\Services\SessionCommunicationService;
|
|
use DB;
|
|
|
|
use PDF;
|
|
use Storage;
|
|
use Response;
|
|
use Auth;
|
|
|
|
class SessionController extends LocalController
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->_service = 'mag';
|
|
$this->_module = 'magazine';
|
|
$this->_viewname = 'session';
|
|
$this->_title = 'Session';
|
|
$this->_base_url = '/session';
|
|
$this->_basemodel = new Session;
|
|
}
|
|
|
|
protected function get_list_data(Request $request) {
|
|
$sessions = !empty(Auth::user()->sessions) ? json_decode(Auth::user()->sessions) : [];
|
|
// $roles = json_decode(Auth::user()->roles);
|
|
// foreach($roles as $role) {
|
|
// $role = LambdaRequest::signedrequest('get', 'ums', 'roles/get_one', ['id' => $role]);
|
|
// //echo '<pre>'; print_r($role); exit;
|
|
// }
|
|
if(Auth::user()->id > 2) {
|
|
$data = $this->_basemodel->wherein('code', $sessions)->orderby('id', 'desc')->get();
|
|
} else {
|
|
$data = $this->_basemodel->orderby('id', 'desc')->get();
|
|
}
|
|
//echo '<pre>'; print_r($data); exit;
|
|
$modeLabels = Session::communicationModes();
|
|
$dd = [];
|
|
foreach($data->toArray() as $d) {
|
|
$mode = $d['communication_mode'] ?? Session::MODE_WHATSAPP;
|
|
$d['communication_mode'] = $modeLabels[$mode] ?? 'WhatsApp';
|
|
$dd[] = (object)$d;
|
|
}
|
|
return $dd;
|
|
}
|
|
|
|
protected function get_list_buttons() {
|
|
return [ ['title' => 'Add New', 'link' => $this->_base_url.'/addnew' ] ];
|
|
}
|
|
|
|
protected function get_list_fields(){
|
|
return [
|
|
'actions' => ['title' => 'Actions', 'buttons' => [ 'edit', 'delete', 'agenda' => ['icon' => 'format-list-text', 'querystring' => 'id={ID}'], 'visit' => ['icon' => 'eye', 'querystring' => 'id={ID}', 'target' => '_blank'], 'brochure' => ['icon' => 'page-layout-body', 'querystring' => 'id={ID}', 'target' => '_blank'], 'attendance' => ['icon' => 'account', 'querystring' => 'id={ID}', 'target' => '_blank'] ] ],
|
|
'type' => ['title' => 'Session Type', 'type' => 'text'],
|
|
'number' => ['title' => 'Session Title', 'type' => 'text'],
|
|
'start_date' => ['title' => 'Date', 'type' => 'date'],
|
|
'venue' => ['title' => 'Venue', 'type' => 'text'],
|
|
'status' => ['title' => 'Status', 'type' => 'select'],
|
|
'communication_mode' => ['title' => 'Communication Mode', 'type' => 'text'],
|
|
];
|
|
}
|
|
|
|
protected function get_form_fields($type, $values){
|
|
$faculties = DB::table('faculty')->where('status', 'active')->select('id', 'name', 'type')->get();
|
|
$faculty_bb = [];
|
|
$faculty_types = [];
|
|
foreach($faculties as $faculty){
|
|
$types = explode(',', $faculty->type);
|
|
foreach($types as $type) {
|
|
$faculty_types[$type][] = $faculty->id;
|
|
}
|
|
$faculty_bb[$faculty->id] = $faculty->name;
|
|
}
|
|
//echo '<pre>'; print_r($faculty_types); exit;
|
|
$fields = [
|
|
'type' => ['title' => 'Session Type', 'type' => 'select', 'values' => ['road' => 'Road', 'casper' => 'Casper', 'casper_clinic' => 'Casper Clinic', 'APFT' => 'APFT', 'spirometry_simplified' => 'Spirometry Simplified', 'casper_clinic_new' => 'Casper Clinic New', 'casper_plus' => 'Casper Plus', 'ecampus' => 'E Campus'], 'required' => true],
|
|
'number' => ['title' => 'Title', 'type' => 'text', 'required' => true ],
|
|
'venue' => ['title' => 'Venue', 'type' => 'text', 'required' => true, 'fullwidth' => true],
|
|
'address' => ['title' => 'Address', 'type' => 'textarea', 'fullwidth' => true],
|
|
'faculty' => ['title' => 'Faculty (multiple)', 'type' => 'multiselect', 'required' => true, 'values' => $faculty_bb, 'types' => $faculty_types ],
|
|
'status' => ['title' => 'Status', 'type' => 'select', 'required' => true, 'values' => ['active' => 'Active', 'disabled' => 'Disabled'] ],
|
|
'join_link' => ['title' => 'Join Link', 'type' => 'text', 'fullwidth' => true],
|
|
'feedback_link' => ['title' => 'Feedback Link', 'type' => 'text', 'fullwidth' => true],
|
|
'survey_link' => ['title' => 'Survey Link', 'type' => 'text', 'fullwidth' => true],
|
|
'communication_mode' => [
|
|
'title' => 'Communication Mode',
|
|
'type' => 'select',
|
|
'values' => Session::communicationModes(),
|
|
],
|
|
];
|
|
return $fields;
|
|
}
|
|
|
|
protected function _beforesave($type, $request) {
|
|
$data = $request->except('_token');
|
|
if ($type == 'new') {
|
|
$data['code'] = \Str::random(20);
|
|
}
|
|
//echo '<pre>'; print_r($data); exit;
|
|
$data['start_date'] = $data['dates']['date'][0];
|
|
$data['dates'] = json_encode($data['dates']);
|
|
$data['faculty'] = '|'.implode('|',$data['faculty']).'|';
|
|
$comm = app(SessionCommunicationService::class);
|
|
$data['communication_mode'] = $comm->normalizeModeForSave($data['communication_mode'] ?? null);
|
|
return $data;
|
|
}
|
|
|
|
protected function _beforeform($type, $values) {
|
|
//$values->library_ids = explode(',', $values->library_ids);
|
|
if($type == 'edit') {
|
|
$values->dates = json_decode($values->dates);
|
|
$values->faculty = explode('|', $values->faculty);
|
|
$values->faculty = array_filter($values->faculty, function($value) { return !is_null($value) && $value !== ''; });
|
|
if (empty($values->communication_mode)) {
|
|
$values->communication_mode = Session::MODE_WHATSAPP;
|
|
}
|
|
}
|
|
//echo '<pre>'; print_r($values); exit;
|
|
return $values;
|
|
}
|
|
|
|
public function visit(Request $request) {
|
|
$id = $request->input('id');
|
|
$values = $this->_basemodel->find($id)->toArray();
|
|
return redirect('/microsite/'.$values['type'].'-'.$values['code']);
|
|
}
|
|
|
|
public function attendance(Request $request) {
|
|
$id = $request->input('id');
|
|
$session = $this->_basemodel->find($id);
|
|
$response = LambdaRequest::getrequest('contact', 'contact/get_all', ['source'=>'dynamodb', 'search' => json_encode(['session_code' => $session->code]), 'fields' => json_encode(['session_code', 'present', 'date_of_birth', 'gender', 'alternate_phone', 'city', 'state', 'highlight_qualification', 'specialization', 'type_of_setup', 'designation', 'clinic_institute_name', 'feedback']) ], env('LAMBDA_ACCESS_TOKEN', 'default_value') );
|
|
$docs = [];
|
|
//echo '<pre>'; print_r($response); print_r($session); exit;
|
|
foreach($response->data->items as $doc) {
|
|
$doc->type_of_setup = is_array($doc->type_of_setup) ? implode(', ', $doc->type_of_setup) : $doc->type_of_setup;
|
|
$docs[] = $doc;
|
|
}
|
|
//
|
|
return view('session.attendance', ['session' => $session, 'doctors' => $docs]);
|
|
}
|
|
|
|
public function certificate(Request $request) {
|
|
$code = $request->input('code');
|
|
$session = $this->_basemodel->where('code', $code)->first();
|
|
$doctor = $request->input('doctor');
|
|
$response = LambdaRequest::getrequest('contact', 'contact/get_one', ['id'=>$doctor], env('LAMBDA_ACCESS_TOKEN', 'default_value') );
|
|
$doctor = $response->data->item;
|
|
//echo '<pre>'; print_r($doctor); exit;
|
|
$output = Creation::certificate($session, $doctor);
|
|
$filename = $output['filename'];
|
|
$folder = $output['folder'];
|
|
|
|
$headers = array('Content-Type: image/png',);
|
|
return Response::download($folder.'/'.$filename, 'certificate.png',$headers);
|
|
}
|
|
|
|
public function certificatepreview(Request $request) {
|
|
$type = $request->input('type', 'casper_plus');
|
|
$session = (object) [
|
|
'type' => $type,
|
|
'code' => 'preview',
|
|
'start_date' => $request->input('date', date('Y-m-d')),
|
|
];
|
|
$doctor = (object) [
|
|
'name' => $request->input('name', 'Sample Doctor Name'),
|
|
'id' => 'preview-' . time(),
|
|
];
|
|
$output = Creation::certificate($session, $doctor);
|
|
|
|
return response()->file($output['folder'] . '/' . $output['filename'], [
|
|
'Content-Type' => 'image/png',
|
|
]);
|
|
}
|
|
|
|
public function agenda(Request $request) {
|
|
$id = $request->input('id', 0);
|
|
if($id === 0) {
|
|
return redirect('/sessions')->withError('Session not found!');
|
|
} else {
|
|
$request->session()->put('session_id', $id);
|
|
return redirect('/sessionagendas');
|
|
}
|
|
}
|
|
|
|
public function brochure(Request $request) {
|
|
$id = $request->input('id', 0);
|
|
if($id === 0) {
|
|
return redirect('/sessions')->withError('Session not found!');
|
|
} else {
|
|
$session = $this->_basemodel->find($id);
|
|
return redirect('/brochure/'.$session->code.'?fresh=1');
|
|
}
|
|
}
|
|
|
|
protected function retimage($image) {
|
|
$size = getimagesize($image);
|
|
switch ($size['mime']) {
|
|
case 'image/jpeg':
|
|
$photoimg = imagecreatefromjpeg($image);
|
|
break;
|
|
case 'image/png':
|
|
$photoimg = imagecreatefrompng($image);
|
|
break;
|
|
case 'image/jpeg':
|
|
$photoimg = imagecreatefrompng($image);
|
|
break;
|
|
default:
|
|
# code...
|
|
break;
|
|
}
|
|
return $photoimg;
|
|
}
|
|
|
|
public function sendnotification(Request $request)
|
|
{
|
|
$session_id = $request->input('session_id');
|
|
if (!$session_id) {
|
|
return response()->json(['status' => 'error', 'message' => 'Session ID is required.'], 400);
|
|
}
|
|
|
|
$session = Session::find($session_id);
|
|
if (!$session) {
|
|
return response()->json(['status' => 'error', 'message' => 'Session not found.'], 404);
|
|
}
|
|
|
|
$comm = app(SessionCommunicationService::class);
|
|
$mode = $comm->resolveMode($session);
|
|
|
|
$response = null;
|
|
if ($comm->shouldSendWhatsApp($mode)) {
|
|
$response = LambdaRequest::signedrequest(
|
|
'get',
|
|
'mag',
|
|
'sessions/sendnotification',
|
|
[
|
|
'session_id' => $session_id,
|
|
'communication_mode' => $mode,
|
|
]
|
|
);
|
|
}
|
|
|
|
$emailQueued = 0;
|
|
if ($comm->shouldSendEmail($mode)) {
|
|
$emailQueued = $this->sendnotificationEmail($session);
|
|
}
|
|
|
|
$waOk = !$comm->shouldSendWhatsApp($mode)
|
|
|| (isset($response->status) && $response->status == 'success');
|
|
|
|
if ($waOk || $emailQueued > 0) {
|
|
return response()->json(['status' => 'success', 'message' => 'Notification sent successfully.']);
|
|
}
|
|
|
|
$errorMsg = isset($response->message) ? $response->message : 'Failed to send notification.';
|
|
return response()->json(['status' => 'error', 'message' => $errorMsg], 500);
|
|
}
|
|
|
|
protected function sendnotificationEmail(Session $session): int
|
|
{
|
|
$comm = app(SessionCommunicationService::class);
|
|
$response = LambdaRequest::getrequest(
|
|
'contact',
|
|
'contact/get_all',
|
|
[
|
|
'source' => 'dynamodb',
|
|
'search' => json_encode(['session_code' => $session->code]),
|
|
'fields' => json_encode(['session_code', 'email', 'name', 'phone', 'id']),
|
|
],
|
|
env('LAMBDA_ACCESS_TOKEN', 'default_value')
|
|
);
|
|
|
|
if (empty($response->data->items)) {
|
|
return 0;
|
|
}
|
|
|
|
$message = $this->buildSessionNotificationText($session);
|
|
$queued = 0;
|
|
|
|
foreach ($response->data->items as $contact) {
|
|
if ($comm->queueEmailFromText($session, $contact, $message, 'session_notification')) {
|
|
$queued++;
|
|
}
|
|
}
|
|
|
|
return $queued;
|
|
}
|
|
|
|
protected function buildSessionNotificationText(Session $session): string
|
|
{
|
|
$lines = [
|
|
'Greetings,',
|
|
'',
|
|
'This is a notification regarding your Crest session "' . $session->number . ' (' . $session->type . ')".',
|
|
'',
|
|
'Venue: ' . $session->venue,
|
|
'Address: ' . ($session->address ?? ''),
|
|
'',
|
|
'Timings:',
|
|
];
|
|
|
|
$dates = json_decode($session->dates);
|
|
if ($dates && isset($dates->date)) {
|
|
foreach ($dates->date as $i => $date) {
|
|
$lines[] = date('d M, Y', strtotime($date))
|
|
. ' at ' . date('h:iA', strtotime($dates->start[$i]))
|
|
. ' - ' . date('h:iA', strtotime($dates->end[$i]));
|
|
}
|
|
}
|
|
|
|
$lines[] = '';
|
|
$lines[] = 'Session website: https://road.xltr.in/microsite/' . $session->type . '-' . $session->code;
|
|
$lines[] = '';
|
|
$lines[] = 'Regards,';
|
|
$lines[] = 'CREST Team';
|
|
|
|
return implode("\n", $lines);
|
|
}
|
|
}
|