<?php
namespace App\Controller\Pages;
use App\Controller\Objects\AquaReg;
use App\Controller\Objects\AquaResInfo;
use App\Controller\Objects\AquaSettings;
use App\Controller\System;
use App\Controller\Objects\HomeFacebookItem;
use App\Kernel;
use App\Controller\AbstractKasController;
use App\Controller\AbstractKasModel;
use App\Controller\Database;
use App\Controller\PageInfo;
use Doctrine\DBAL\Exception;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\VarDumper;
use PHPMailer\PHPMailer\PHPMailer;
use Doctrine\DBAL\ParameterType;
class AquaController extends AbstractKasController
{
private AquaSettings $settings;
public function __construct(RequestStack $requestStack, Database $db, PageInfo $pageInfo)
{
parent::__construct($requestStack, $db, $pageInfo);
$this->request = $requestStack->getCurrentRequest();
$this->model = new AquaModel($db);
$this->settings = $this->model->getAquaSettings();
}
/**
* @Route("/aquasvet/kurzy-plavani")
* @return Response
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public function kurzy_plavani(): Response
{
//$n = $this->pageInfo->getNavbar();
//dump($n);
$link = $this->request->getRequestUri();
$link = str_replace('/kurzy-plavani', '', $link);
$model = new ObjectModel($this->db);
$objectinfo = $model->getObjectInfo($link);
//pokud nenalezeno, tak 404
if (!$objectinfo->isFound()) return $this->render('/pages/404.html.twig', [
'pageinfo' => $this->pageInfo
]);
$model = new HomeModel($this->db);
$top_events = $model->getTopEvents(); //dump($top_events);
$facebook_feed = $model->getFacebookFeed();
$courses = $this->model->getCourses();
return $this->render('/pages/aqua_courses.html.twig', [
'courses' => $courses,
'pageinfo' => $this->pageInfo,
'objectinfo' => $objectinfo,
'top_events' => $top_events,
'html_info' => $this->settings->getHtmlInfo(),
'facebook_feed' => $facebook_feed
]);
}
/**
* @Route("/aquasvet/prehled-terminu")
* @Route("/aquasvet/prehled-nasledujicich-terminu")
* @return Response
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public function prehled_terminu(): Response
{
//$n = $this->pageInfo->getNavbar();
//dump($n);
$link = $this->request->getRequestUri();
$model = new ObjectModel($this->db);
$objectinfo = $model->getObjectInfo('/aquasvet');
//pokud nenalezeno, tak 404
if (!$objectinfo->isFound()) return $this->render('/pages/404.html.twig', [
'pageinfo' => $this->pageInfo
]);
$model = new HomeModel($this->db);
$top_events = $model->getTopEvents(); //dump($top_events);
$facebook_feed = $model->getFacebookFeed();
if ($link == '/aquasvet/prehled-terminu') $filename = 'prehled_terminu.html';
else $filename = 'prehled_nasledujicich_terminu.html';
$html = $this->getHTML($filename);
$objectinfo->setTitle('Přehled termínů');
$objectinfo->setSubtitle('Plavecných kurzů ve dvouměsíčních cyklech');
$objectinfo->setButtons([]);
return $this->render('/pages/aqua_termines.html.twig', [
'html' => $html,
'pageinfo' => $this->pageInfo,
'objectinfo' => $objectinfo,
'top_events' => $top_events,
'facebook_feed' => $facebook_feed
]);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz")
* @return Response
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public function prihlaska_na_kurz(): Response
{
$model = new ObjectModel($this->db);
$objectinfo = $model->getObjectInfo('/aquasvet');
$objectinfo->setTitle('Přihláška na kurz');
$objectinfo->setButtons([]);
//pokud nenalezeno, tak 404
if (!$objectinfo->isFound()) return $this->render('/pages/404.html.twig', [
'pageinfo' => $this->pageInfo
]);
$model = new HomeModel($this->db);
$top_events = $model->getTopEvents(); //dump($top_events);
$facebook_feed = $model->getFacebookFeed();
$active_months = $this->model->getActiveMonths();
//nejsou aktivni kurzy pro prihlaseni
if ($active_months->getName()=='') {
$objectinfo->setSubtitle('');
return $this->render('/pages/aqua_form_prepare.html.twig', [
'settings' => $this->settings,
'pageinfo' => $this->pageInfo,
'objectinfo' => $objectinfo,
'top_events' => $top_events,
'facebook_feed' => $facebook_feed
]);
}
else {
$objectinfo->setSubtitle($active_months->getName());
return $this->render('/pages/aqua_form.html.twig', [
'settings' => $this->settings,
'pageinfo' => $this->pageInfo,
'objectinfo' => $objectinfo,
'top_events' => $top_events,
'facebook_feed' => $facebook_feed,
'g_site_key' => getenv('G_SITE_KEY')
]);
}
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/kurzy")
* @Route("/aquasvet/prihlaska-na-kurz/kurzy-{age}")
* @return Response
*/
public function prihlaska_kurzy(float $age): Response
{
$courses = $this->model->getCoursesReg($age);
return $this->render('/partials/aquareg_courses.html.twig', [
'courses' => $courses,
'age' => $age
]);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/terminy")
* @Route("/aquasvet/prihlaska-na-kurz/terminy-{course}")
* @return Response
*/
public function prihlaska_terminy(int $course): Response
{
$termines = [];
if ($course>0) {
$active_months = $this->model->getActiveMonths();
$termines = $this->model->getTerminesReg($course, $active_months->getMonthIds());
}
return $this->render('/partials/aquareg_termines.html.twig', [
'termines' => $termines
]);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/datum", methods={"POST"})
* @return Response
*/
public function prihlaska_datum(): JsonResponse
{
$date = $this->request->get('date');
$age = '0';
//validace
if ($date=='') {
$message = 'zadejte datum narození';
$focus = 'id-date';
}
elseif (!System::isDateCZ($date)) {
$message = 'špatný formát data narození';
$focus = 'id-date';
}
elseif (self::isFutureDate($date)) {
$message = 'nenarozené účastníky nebereme';
$focus = 'id-date';
}
else {
$age = self::ageFromDate($date);
$message = 'OK';
$focus = 'id-firstname';
}
$return = ['message' => $message, 'age' => $age, 'focus' => $focus];
return new JsonResponse($return);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/odeslani", methods={"POST"})
* @return Response
* @throws Exception
*/
public function prihlaska_odeslani(): JsonResponse
{
sleep(1);
$message = '';
$reload = '';
$focus = '';
$error = 1;
$age = '0';
$date = $this->request->get('date');
$firstname = $this->request->get('firstname');
$lastname = $this->request->get('lastname');
$email = $this->request->get('email');
$phone = $this->request->get('phone');
$course = $this->request->get('course');
$less = $this->request->get('less');
$subst = $this->request->get('subst');
$cond = $this->request->get('cond');
$gdpr = $this->request->get('gdpr');
$note = $this->request->get('note');
//validace
if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], getenv('APP_URL')) !==0) {
$message = 'chyba zpracování';
$focus = '';
}
elseif ($date=='') {
$message = 'zadejte datum narození';
$focus = 'id-date';
}
elseif (!System::isDateCZ($date)) {
$message = 'špatný formát data narození';
$focus = 'id-date';
}
elseif (self::isFutureDate($date)) {
$message = 'nenarozené účastníky nebereme';
$focus = 'id-date';
}
elseif ($firstname=='') {
$message = 'zadejte křestní jméno účastníka';
$focus = 'id-firstname';
}
elseif ($lastname=='') {
$message = 'zadejte příjmení účastníka';
$focus = 'id-lastname';
}
elseif ($email=='') {
$message = 'zadejte e-mail zákonného zástupce';
$focus = 'id-email';
}
elseif (!System::isEmail($email)) {
$message = 'chybný formát e-mailu';
$focus = 'id-email';
}
elseif ($phone=='') {
$message = 'zadejte telefon zákonného zástupce';
$focus = 'id-phone';
}
elseif (!System::isPhone($phone)) {
$message = 'chybný formát telefonního čísla';
$focus = 'id-phone';
}
else if ($course.'' === '' || !System::IsIntPosNum($course)) {
$message = 'vyberte kurz';
$focus = 'id-table-courses-radios';
}
elseif ($less.''==='' && $subst.''==='') {
$message = 'vyberte termín kurzu';
$focus = 'id-table-less';
}
elseif ((int)$cond!==1) {
$message = 'potvrďte souhlas s podmínkami plavecké školy';
$focus = 'id-cond';
}
elseif ((int)$gdpr!==1) {
$message = 'potvrďte souhlas se zpracováním osobních údajů';
$focus = 'id-gdpr';
}
else {
$age = self::ageFromDate($date);
$pin = self::generatePIN($date, $firstname, $lastname);
$rcnt = $this->model->getPinPerMonth($pin, $this->settings);
$reg = new AquaReg();
$reg->setAge($age);
$reg->setPin($pin);
$reg->setFirstname($firstname);
$reg->setLastname($lastname);
$reg->setEmail($email);
$reg->setPhone($phone);
$reg->setCourseId($course);
$reg->setDate($date);
$reg->setNote($note);
//zjistim pocet prihlasek na unikatni id
if ($rcnt>=$this->settings->getMaxReg()) {
$message = 'pro uvedené údaje je již definován maximální počet možných přihlášek za měsíc! ['.$rcnt.'x]';
$focus = 'id-date';
$return = ['message' => $message, 'age' => $age, 'focus' => $focus];
return new JsonResponse($return);
}
//musime jeste serverove zkontrolovat vek ... jestli si lokalne disablovane pole nepovolili
$age_from = $age_to = 0;
$this->model->getCourseAge($course, $age_from, $age_to);
if ($age<$age_from || $age>$age_to) {
$message = 'účastník věkově nespadá do vybraného kurzu';
$focus = 'id-table-courses-radios';
$reload = 'courses';
$return = ['message' => $message, 'age' => $age, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
//serverova kontrola volne kapacity
if ($less!='') {
$a = explode('|', $less);
$from = $a[0];
$day = $a[1];
$reg->setTimeFrom($from);
$reg->setDayIndex($day);
$cap = $this->model->getCapacityLess($course, $day, $from);
if ($cap===0) {
$message = 'bohužel jste nestihli rezervovat, na zvolenou lekci se můžete zaregistrovat jako náhradník';
$focus = 'id-table-less';
$reload = 'termines';
$return = ['message' => $message, 'age' => $age, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
}
elseif ($subst!='') {
$a = explode('|', $subst);
$from = $a[0];
$day = $a[1];
$reg->setTimeFrom($from);
$reg->setDayIndex($day);
$reg->setSubst(true);
$cap = $this->model->getCapacitySubst($course, $day, $from);
if ($cap===0) {
$message = 'bohužel jste nestihli rezervovat, byl naplněn také limit pro náhradníky';
$focus = 'id-table-less';
$reload = 'termines';
$return = ['message' => $message, 'age' => $age, 'focus' => $focus];
return new JsonResponse($return);
}
$less = $subst;
}
//kontrola, zda již není klient na konkrétní termín kurzu přihlášen
$already_in = $this->model->isAlreadyIn($pin, $course, $less);
if ($already_in) {
$message = 'na vybraný kurz a čas je již uživatel přihlášen(a)';
$focus = 'id-table-less';
$return = ['message' => $message, 'age' => $age, 'focus' => $focus];
return new JsonResponse($return);
}
//captcha
/*$result = file_get_contents( 'https://www.google.com/recaptcha/api/siteverify', false, stream_context_create( array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( array(
'response' => $_POST['g-recaptcha-response'],
'secret' => getenv('G_SECRET_KEY'),
'remoteip' => $_SERVER['REMOTE_ADDR']
) ),
),
) ) );
$result = json_decode($result);
if ($result->success) {*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'response' => $_POST['g-recaptcha-response'],
'secret' => getenv('G_SECRET_KEY'),
'remoteip' => $_SERVER['REMOTE_ADDR']
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$response = @json_decode($data);
if ($response && $response->success) {
//vse je OK, ulozeni
try {
$ids = [];
$pincode = '';
$this->model->saveReg($this->settings, $reg, $ids, $pincode);
$message = 'úspěšně odesláno';
$error = 0;
$id = implode('|', $ids);
$eid = System::encrypt($id);
$reg->setEid($eid);
$reg->setPincode($pincode);
$info = $this->model->getResInfo($ids);
$subject = 'Přihláška na kurz plavání';
$body = '<h2>PŘIHLÁŠKA NA KURZ PLAVÁNÍ</h2>';
$body .= '<p>děkujeme za vyplnění přihlášky na kurz plavání.</p>';
$this->sendMail($reg, $info, $subject, $body, false, true);
} catch (Exception $e) {
$message = $e->getMessage();
$focus = 'id-submit';
}
}
else {
$message = 'zaškrtněte "Nejsem robot"';
}
}
$return = ['message' => $message, 'error' => $error, 'age' => $age, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz-{eid}")
* @Route("/prihlaska-na-kurz-{eid}")
* @return Response
* @throws Exception
* @throws \Facebook\Exceptions\FacebookSDKException
*/
public function moje_prihlaska($eid): Response
{
$model = new ObjectModel($this->db);
$objectinfo = $model->getObjectInfo('/aquasvet');
$objectinfo->setTitle('Moje přihláška');
$objectinfo->setButtons([]);
//pokud nenalezeno, tak 404
if (!$objectinfo->isFound() || $eid=='' || System::decrypt($eid)=='') {
return $this->render('/pages/404.html.twig', [
'pageinfo' => $this->pageInfo
]);
}
$ids = explode('|', System::decrypt($eid));
$model = new HomeModel($this->db);
$top_events = $model->getTopEvents(); //dump($top_events);
$facebook_feed = $model->getFacebookFeed();
$reg = $this->model->getReg($ids);
$info = $this->model->getResInfo($ids);
//$terms = $this->model->getResTerms($ids);
$months = $this->model->getMonthName($ids);
$objectinfo->setSubtitle($months);
return $this->render('/pages/aqua_mycourse.html.twig', [
'settings' => $this->settings,
'pageinfo' => $this->pageInfo,
'objectinfo' => $objectinfo,
'top_events' => $top_events,
'facebook_feed' => $facebook_feed,
'reg' => $reg,
'info' => $info,
'eid' => $eid
]);
}
/**
* @param $date
* @return int
*/
private static function ageFromDate($date):float {
if(strlen($date) >= 8 && strlen($date) <= 10 && System::isDateCZ($date)) {
//$sdate = substr($date, 0,2).'.'.substr($date, 2,2).'.20'.substr($date, 4,2);
$birthday = new \DateTime($date);
$birthday->modify('first day of this month');
$diff = $birthday->diff(new \DateTime());
$months = $diff->format('%m') + 12 * $diff->format('%y');
$months++; //vzdy se prihlasujeme na nasledujici mesic, musim se jakoby narodit o mesic driv
return $months/12;
}
return 0;
}
/**
* @param $date
* @return bool
*/
private static function isFutureDate($date):bool {
$testing_date = new \DateTime($date);
$current_date = new \DateTime();
return ($testing_date > $current_date)?true:false;
}
/**
* @param string $filename
* @return string
*/
private function getHTML(string $filename): string {
$file = getenv('APP_DIR').'private/data/files/www_html/'.$filename;
if (file_exists($file)) {
return file_get_contents($file);
}
return '';
}
/**
* @param string $date
* @param string $name
* @param string $surname
* @return string
*/
public static function generatePIN(string $date, string $name,string $surname):string
{
$name = str_replace(' ', '', System::removeAccents2($name));
$surname = str_replace(' ', '', System::removeAccents2($surname));
$sourceString = substr($name, 0, 3).$surname;
$sum = 0;
for ($i = 0; $i < strlen($sourceString); $i++) {
$sum += self::letterPosInAlphabet($sourceString[$i]) * $i;
}
$sum = $sum.'';
$sum = substr($sum, 0, 4);
while(strlen($sum) < 4) {
$sum = $sum.'0';
}
$dateArray = explode('.', $date);
$dateArray[0] = sprintf("%02d", $dateArray[0]);
$dateArray[1] = sprintf("%02d", $dateArray[1]);
// odstranění prvních dvou čísel u roku ( 2017 -> 17 )
$dateArray[2] = substr($dateArray[2], 2);
return implode('', $dateArray).$sum;
}
/**
* @param string $letterOfAlphabet
* @return int
*/
private static function letterPosInAlphabet(string $letterOfAlphabet):int {
return ord(strtoupper($letterOfAlphabet)) - ord('A') + 1;
}
/**
* @param AquaReg $reg
* @param AquaResInfo $info
* @param string $subject
* @param string $body
* @param bool $internal
* @return void
* @throws \PHPMailer\PHPMailer\Exception
*/
private function sendMail(AquaReg $reg, AquaResInfo $info, string $subject, string $body, bool $internal = false, bool $mailinfo = true):void {
$mailinfo_txt = ($mailinfo)?$this->settings->getMailInfo():'';
$body .= $this->renderView('/partials/aquareg_mail.html.twig', [
'app_url' => getenv('APP_URL'),
'reg' => $reg,
'info' => $info,
'mailinfo' => $mailinfo_txt,
'sign' => true
]);
System::sendMail(1, $subject, $body, $reg->getEmail(), $internal);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/zruseni-terminu", methods={"POST"})
* @return Response
* @throws Exception
* @throws \PHPMailer\PHPMailer\Exception
*/
public function zruseni_terminu(): JsonResponse
{
sleep(1);
$message = '';
$reload = '';
$focus = '';
$error = 1;
$pincode = $this->request->get('pincode');
$eid = $this->request->get('eid');
$termid = $this->request->get('termid');
$reason = $this->request->get('reason');
$reason2 = $this->request->get('reason2');
$r_date = date('Y-m-d H:i:s');
//validace
if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], getenv('APP_URL')) !==0) {
$message = 'chyba zpracování';
}
elseif ($eid=='' || (int)System::decrypt($eid)==0 || (int)$termid==0) {
$message = 'chyba zpracování [2]';
}
elseif ($pincode=='') {
$message = 'zadejte PIN';
}
elseif (strlen($pincode)!=4 || !System::IsIntPosNum($pincode)) {
$message = 'chybný formát PIN kódu';
}
elseif ($reason=='') {
$message = 'vyberte důvod';
}
elseif ($reason=='jiný důvod' && $reason2=='') {
$message = 'zadejte jiný důvod';
}
else {
$id = System::decrypt($eid);
//$message = System::decrypt($eid);
$db_pin = $this->model->getPIN($id);
$term_date = $this->model->getTermDate($termid);
$today_time = new \DateTime(date('d.m.Y H:i'));
//mohou se odhlásit nejpozdeji ten den do 8.00
$expire_time = new \DateTime($term_date->format('d.m.Y').' 08:00');
$past = (int)$today_time->format('YmdHi') > (int)$expire_time->format('YmdHi');
if ($pincode!=$db_pin) {
$message = 'chybný PIN kód';
}
else if ($past) {
$message = 'odhlášení je možné pouze do 8.00 v den konání kurzu';
}
else {
if ($reason=='jiný důvod') {
$reason = $reason2;
}
try {
//zruseni terminu a nahradnici
$subst_rids = $this->model->cancelTerm($id, $termid, $reason, $this->settings);
$message = 'termín byl zrušen, děkujeme za informaci';
$error = 0;
$reg = $this->model->getReg([$id]);
$ids = explode('|', $reg->getIds());
$info = $this->model->getResInfo($ids);
//rusejicimu e-mail hned
$subject = 'Zrušení termínu kurzu';
$body = '<h2>ZRUŠENÝ TERMÍN KURZU PLAVÁNÍ</h2>';
$body .= '<p>Termín <b>'.$term_date->format('d.m.Y').'</b> byl zrušen z důvodu "<b>'.$reason.'</b>", děkujeme za informaci.</p>';
$this->sendMail($reg, $info, $subject, $body, true, false);
//odeslani e-mailu nahradnikum pres frontu
if (is_array($subst_rids) && count($subst_rids)>0) {
foreach ($subst_rids as $r_id) {
//mail pres frontu
$reg = $this->model->getReg([$r_id]);
$info = $this->model->getResInfo([$r_id]);
$r_email = $reg->getEmail();
$r_subject = 'Uvolnění plaveckého kurzu';
$html = '<p style="line-height: 1.2">Dobrý den,</p>';
$html .= '<p style="line-height: 1">došlo k uvolnění plaveckého kurzu. Na tento kurz jste registrovaným náhradníkem.</p>';
$html .= '<p style="line-height: 1">V případě zájmu se dostavte uhradit kurzovné na recepci Aquasvěta do '.$this->settings->getPaydate().'. dne v měsíci. V opačném případě dojde k nabídnutí kurzu dalšímu náhradníkovi v pořadí.</p>';
$html .= '<h4>Vaše přihláška</h4>';
$html .= $this->renderView('/partials/aquareg_mail.html.twig', [
'app_url' => getenv('APP_URL'),
'reg' => $reg,
'info' => $info,
'mailinfo' => '',
'sign' => false
]);
/*
1 kurzyplavani@kultura-sport.cz
2 prazdniny@kultura-sport.cz
3 prazdniny-miniskola@kultura-sport.cz
4 prazdniny-aquasvet@kultura-sport.cz
*/
$this->model->mailToQueue($r_email, $r_subject, $html, $r_date, 1);
}
}
} catch (Exception $e) {
$message = $e->getMessage();
}
}
}
$return = ['message' => $message, 'error' => $error, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/nahrada-terminu", methods={"POST"})
* @return Response
* @throws Exception
*/
public function nahrada_terminu(): JsonResponse
{
sleep(1);
$message = '';
$reload = '';
$focus = '';
$error = 1;
$pincode = $this->request->get('pincode');
$eid = $this->request->get('eid'); //id registrace (t4)
$termid = $this->request->get('termid'); //termin, ktery nahrazujeme
$substermid = $this->request->get('substermid');//nahradni termin
//validace
if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], getenv('APP_URL')) !==0) {
$message = 'chyba zpracování';
}
elseif ($eid=='' || (int)System::decrypt($eid)==0 || (int)$termid==0) {
$message = 'chyba zpracování [2]';
}
elseif ($pincode=='') {
$message = 'zadejte PIN';
}
elseif (strlen($pincode)!=4 || !System::IsIntPosNum($pincode)) {
$message = 'chybný formát PIN kódu';
}
elseif ($substermid=='') {
$message = 'vyberte náhradní termín';
}
else {
$r_id = System::decrypt($eid);
//$message = System::decrypt($eid);
$db_pin = $this->model->getPIN($r_id);
if ($pincode!=$db_pin) {
$message = 'chybný PIN kód';
}
else {
try {
// nahrazovane ID terminu, vybrane ze selectboxu, pokud je L_ID, predavame ID lekce
if (str_starts_with($substermid, 'L_ID_')) {
$l_id = substr($substermid, 5);
$substermid = 0;
}
else {
//zjisteni lekce z terminu
$l_id = $this->model->getLessonID($substermid);
}
//kontrola, zda se nejedna o nahradu z nahrady
if ($this->model->isSubterm($termid)) {
$message = 'nelze nahrazovat náhradu';
$return = ['message' => $message, 'error' => $error, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
//kontrola 2 náhrad v měsíci
if ($this->model->getSubstCnt($r_id)>=2) {
$message = 'jsou možné pouze 2 náhrady v měsíci';
$return = ['message' => $message, 'error' => $error, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
//kontrola, zda jsem nevybral lekci, kde už jsem
$l_ids = $this->model->getLessonIDs($r_id);
if (in_array($l_id, $l_ids)) {
$message = 'na zvolené lekci již jste';
$return = ['message' => $message, 'error' => $error, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
//nakonec jeste jednou overime kapacitu (stavovost)
//zkontroluji vlastne, jestli vybrany radek jiz nebyl vybran - uvolnena kapacita nahrazena
if ($substermid>0 && !$this->model->isSubtermAval($substermid)) {
$message = 've vybraný termín již není volné místo';
$return = ['message' => $message, 'error' => $error, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
$this->model->saveSubsTerm($r_id, $l_id, $termid, $substermid);
$message = 'náhradní termín byl úspěšně vybrán';
$error = 0;
$reg = $this->model->getReg([$r_id]);
$ids = explode('|', $reg->getIds());
$info = $this->model->getResInfo($ids);
$term_date = $this->model->getTermDate($termid);
$subterm_date = $this->model->getTermDate($substermid);
$subject = 'Náhrada zrušené lekce';
$body = '<h2>NAHRAZENÝ TERMÍN KURZU PLAVÁNÍ</h2>';
$body .= '<p>Zrušený termín <b>'.$term_date->format('d.m.Y').'</b> byl nahrazen <b>'.$subterm_date->format('d.m.Y').'</b>.</p>';
$this->sendMail($reg, $info, $subject, $body, true, false);
} catch (Exception $e) {
$message = $e->getMessage();
}
}
}
$return = ['message' => $message, 'error' => $error, 'focus' => $focus, 'reload' => $reload];
return new JsonResponse($return);
}
/**
* @Route("/aquasvet/prihlaska-na-kurz/nahradni-terminy-{eid}-{term_id}", methods={"GET"})
* @return Response
* @throws Exception
*/
public function nahradni_terminy(string $eid, int $term_id): Response
{
$data = [];
if ($eid!='' && $term_id>0) {
$r_id = System::decrypt($eid);
if ((int)$r_id>0) {
$data = $this->model->loadSubsTerms($r_id, $term_id, $this->settings);
}
}
return $this->render('/partials/aquareg_subterms.html.twig', [
'data' => $data
]);
}
/**
* @Route("/aquasvet/moje-prihlaska/terminy-{eid}", methods={"GET"})
* @return Response
*/
public function moje_terminy(string $eid): Response
{
$terms=[];
if ($eid!='') {
$ids = explode('|', System::decrypt($eid));
$terms = $this->model->getResTerms($ids);
}
return $this->render('/partials/aquareg_mytermines.html.twig', [
'terms' => $terms
]);
}
}
?>