<?php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\VCard;
use App\Form\VCardType;
use App\Entity\VCardStudent;
use App\Form\VCardStudentType;
use App\Repository\VCardRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\HttpClient\CurlHttpClient;
use JeroenDesloovere\VCard\VCard as VCardFile;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use phpCAS;
use App\Controller\VCardController;
use DateTime;
use DateTimeZone;
class VStudentCardController extends CustomController // AbstractController
{
private $applicationSender = 'no-reply';
// private $uploadsDIR = __DIR__.'/../../public/uploads/';
private $qrcodeOutDIR = __DIR__.'/../../public/uploads/';
private $studentsFileURL = 'https://am-auriontbo-1/servlet/Dispatcher?action=transformFavori&data=<transformFavori><favori><id>132012446</id></favori><database>ensam</database><xslFile>JSON_json.xsl</xslFile></transformFavori>';
// private $studentsCacheFile = __DIR__.'/../../public/uploads/'.'students.json';
private $CACHE_DURATION = 120;
private $SOCIAL_NETS = array(
'linkedin' => 'https://www.linkedin.com/in/',
'facebook' => 'https://www.facebook.com/',
'twitter' => '',
'tiktok' => 'https://www.tiktok.com/',
'instagram' => 'https://www.instagram.com/',
'other' => '',
);
private $pendingMailDatas = array(
'à valider' => array(
'subject' => '[Rappel] Carte de visite',
'content' => 'La carte de {PRENOM} {NOM} attend votre validation, cliquez sur le lien pour accéder à la demande de validation.<br/>'
. '<a href="{URI}/v/card/manager">'
. 'Voir...'
. '</a><br/>'
. '<i>Ce message a été créé automatiquement par le gestionnaire de carte de visites des Arts et Métiers, merci de ne pas y répondre.</i>'
),
'enregistrée' => array('subject' => '[Rappel] Carte de visite',
'content' => "N'oubliez-pas de soumettre votre carte de visite pour sa publication...<br/>"
.'Cliquez sur le lien pour la soumettre.<br/>'
. '<a href="{URI}/v/card/">'
. 'Voir...'
. '</a><br/>'
. '<i>Ce message a été créé automatiquement par le gestionnaire de carte de visites des Arts et Métiers, merci de ne pas y répondre.</i>'
),
'rejetée' => array('subject' => '[Rappel] Carte de visite',
'content' => 'Votre carte de visite a été rejetée.<br/>'
.'Vous pouvez la modifier à cette adresse:<br/>'
. '<a href="{URI}/v/card/{ID_VCARD}">'
. 'Voir...'
. '</a><br/>'
. '<i>Ce message a été créé automatiquement par le gestionnaire de carte de visites des Arts et Métiers, merci de ne pas y répondre.</i>'
// "VALIDATE_CONTENT"
),
);
protected function getNewStudentVersion($id) {
$out = -1;
$query = 'select max(version)+1 as NEW_VERSION from vcard_student where id_apprenant='.$id;
$result = $this->selectRaw($query);
if (isset($result[0]['NEW_VERSION'])) {
$out = $result[0]['NEW_VERSION'];
}
return $out;
}
protected function getNewStudentVCardId($TEST_MODE = false) {
$out = -1;
if ($TEST_MODE === false) {
$query = 'select max(id_vcard) + 1 as NEW_ID from VCARD_STUDENT';
$result = $this->selectRaw($query);
if (isset($result[0]['NEW_ID'])) {
$out = $result[0]['NEW_ID'];
}
} else {
$out = date_timestamp_get(date_create());
}
return $out;
}
protected function getParamType($param) : String{
$out = 's';
if (ctype_digit((string) $param)) {
$out = $param <= PHP_INT_MAX ? 'i' : 's';
}
if (is_numeric($param)) {
$out = 'd';
}
return $out;
}
protected function getBoundTypes(array $values) : String {
$out = "";
foreach($values as $k => $v) {
$out .= $this->getParamType($values[$k]);
}
return $out;
}
protected function writeRaw($SQL, array $values, $DBG = false) {
$out = false;
$nb_values = count($values);
if (isset($SQL)
&& strpos($SQL, ':') !== false
&& $nb_values > 0
) {
$wc_count = substr_count($SQL, ':');
if ($wc_count == $nb_values) {
// $bound_types = $this-> getBoundTypes($values);
// echo(__METHOD__. ': SQL:' . $SQL . ', '. $bound_types);
$em = $this->getDoctrine()->getManager();
// $statement = $em->getConnection()->prepare($SQL);
$db = $em->getConnection(); // Doctrine_Manager::getInstance()->getCurrentConnection();
$query = $db->prepare($SQL);
$res = $query->execute($values);
/*
$results = $res->fetchAllAssociative();
echo(__METHOD__.' results: '.var_export($results, true));
*/
/*
// Set parameters
foreach($values as $item => $item_value) {
$statement->bindValue($item, $item_value);
}
$res = $statement->execute();
*/
if ( TRUE || // TODO: remove Debug
isset($_GET['DBG'])
&& $_GET['DBG'] == 1
) {
echo(__METHOD__.' values: '.var_export($values, true));
}
$out = true;
} // END if count
}
return $out;
}
/**
* @Route("/v/student/card/", name="app_v_card_student_index", methods={"GET", "POST"})
*/
public function index(Request $request, VCardRepository $vCardRepository, LoggerInterface $logger): Response {
return new Response(null, 302, array(
'Location' => 'show',
));
}
/**
* @Route("/v/student/card/auth", name="app_v_card_student_auth", methods={"GET", "POST"})
*/
public function auth(Request $request): Response {
return new Response(null, 302, array(
'Location' => "https://{$_SERVER['HTTP_HOST']}",
));
}
/**
* @Route("/v/student/card/testauth", name="app_v_card_student_testauth", methods={"GET", "POST"})
*/
public function testauth(Request $request): Response {
$session = $request->getSession();
$this->checkAuth($session, 'app_v_card_student_testauth');
$content = __METHOD__.': authentified';
return new Response($content, 302, array(
'Location' => 'auth',
));
}
/**
* @Route("/v/student/card/edit", name="app_v_card_student_edit", methods={"GET", "POST"})
*/
public function edit(Request $request): Response {
$session = $request->getSession();
$this->checkAuth($session, 'app_v_card_student_edit');
$user_email = $session->get('user_email'); // $_GET['email'];
$is_auth = $session->get('is_auth'); // $_GET['email'];
$url = "https://{$_SERVER['HTTP_HOST']}/v/student/card/last?";
$DBG = isset($_GET['DBG']) && $_GET['DBG'] == '1';
$student_datas = array(
'avecphoto' => 'non',
'tel2' => '',
);
if (isset($user_email)
&& $is_auth
) {
/*
$found = $this->getStudentDatasFromMail($user_email);
if (isset($found)) {
$student_datas['titre'] = $found['Fonction'];
$student_datas['tel'] = $found['Portable'];
$student_datas['emailPerso'] = $found['emailPerso'];
*/
$student_datas = $this->getAllStudentDatas($user_email, $DBG);
// echo(__METHOD__.' student_datas:'.var_export($student_datas, true)); exit();
if (!isset($student_datas['id.Apprenant'])) {
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
} else if (!isset($student_datas['ID_VCARD'])) {
$args = array();
if (isset($_GET['email'])) {
$args['email'] = $_GET['email'];
}
return $this->redirectToRoute('app_v_card_student_new', $args, Response::HTTP_SEE_OTHER);
}
$ide_dossier = 'E-'.$student_datas['id.Apprenant'];
$custom_photo_key = '';
$delete_custom_key = '';
$file_path = $this->uploadsDIR.('photo_'.$ide_dossier.'.jpg');
$is_custom_photo = (file_exists($file_path)); // NOTE: user needs to delete to disable custom
if ($is_custom_photo) {
$custom_photo_key = $this->storeInSession($session, $file_path);
$delete_custom_key = $this->storeInSession($session, $file_path);
}
$comm_datas = json_decode($student_datas['COMMENTAIRE'], true); // NOTE: commentaire used for keeping opt fields
$comm_datas['login'] = $student_datas['login.Individu'];
if (isset($comm_datas['role'])) {
$student_datas['role'] = $comm_datas['role'];
}
if (!isset($comm_datas['date_expiration'])
&& isset($student_datas['date_expiration'])
) {
$comm_datas['date_expiration'] = $student_datas['date_expiration'];
}
/* TODO: enable social networks
foreach($this->SOCIAL_NETS as $net_item => $val) {
if (isset($comm_datas[$net_item])
) {
$student_datas[$net_item] = $comm_datas[$net_item];
}
}
// */
$vCardStudent = new VCardStudent();
$vCardStudent->setcommentaire($student_datas['COMMENTAIRE']);
$form = $this->createForm(VCardStudentType::class, $vCardStudent);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$datas = $form->getData();
$titre_datas = json_decode($datas->gettitre(), true);
if (isset($titre_datas[1])) {
$comm_datas['role'] = $titre_datas[1];
} else {
$comm_datas['role'] = '';
}
$form_comment = $datas->getcommentaire();
if (isset($form_comment)) {
$tmp_comms = json_decode($form_comment, true);
$comm_datas = array_merge($comm_datas, $tmp_comms);
}
$datas->setid_apprenant($student_datas['id.Apprenant']);
$datas->setformation($student_datas['Formation']);
$datas->setVERSION(printf("%d", $student_datas['VERSION']) + 1);
$datas->setcommentaire(json_encode($comm_datas));
$this->addVCardStudent($datas); // , true); // TODO: REMOVE true !!!!!
$this->generateQrCodeFile($url, $user_email);
if (isset($_GET['DBG'])) {
echo(__METHOD__.': '. var_export($datas->gettitre(), true));
echo(__METHOD__.': '. var_export($datas, true));
} else {
return $this->redirectToRoute('app_v_card_student_show', [], Response::HTTP_SEE_OTHER);
}
}
//} // END if found
return $this->renderForm('v_card_student/new.html.twig', [
// 'v_card' => $vCard,
'user_email' => $user_email,
// 'personnel' => $personnel_datas,
'student' => $student_datas,
'is_new' => true,
'current_card' => true,
'disp_form_student' => true,
// 'is_manager' => $session->get('is_manager'),
'custom_photo_key' => $custom_photo_key,
'delete_custom_key' => $delete_custom_key,
'form' => $form,
]);
} else { // END if user_email
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
}
/**
* @Route("/v/student/card/{id}/vcardinfo", name="app_v_card_student_vcardinfo", methods={"GET", "POST"})
*/
public function vcardinfo(Request $request, $id): Response {
$datas = array();
$found = $this->selectVcardStudentWhere('ID_VCARD='.sprintf("%d", $id));
if (isset($found)) {
$datas = $found;
}
$content = __METHOD__.' datas: '.var_export($datas, true);
return new Response($content, 200, array(
)
);
}
/**
* @Route("/v/student/card/notfound", name="app_v_card_student_notfound", methods={"GET"})
*/
public function notfound(Request $request): Response {
// echo(__METHOD__); exit();
$v_datas = array();
$tpl_name = 'v_card_student/message.html.twig';
$needs_create = false;
$v_datas[ 'disp_message'] = true;
$v_datas[ 'is_student'] = true;
$v_datas['message'] = ($needs_create ? 'Aucune carte' : 'Carte non').' trouvée....';
if (isset($_GET['embedded'])) {
$v_datas['embedded'] = true;
// $v_datas['message'] = 'Carte non trouvée.<br/>Créer votre carte de visite <a href="/v/card">ici</a>';
if (isset($_GET['needs_create']) && $_GET['needs_create'] == 1) {
$v_datas['needs_create'] = $needs_create = true;
}
$tpl_name = 'v_card_student/notfound_embedded.html.twig';
}
return $this->render($tpl_name, $v_datas);
}
/**
* @Route("/v/student/card/show", name="app_v_card_student_show", methods={"GET", "POST"})
*/
public function show(Request $request): Response {
$session = $request->getSession();
$this->checkAuth($session, 'app_v_card_student_show');
$email = $session->get('user_email'); // $_GET['email'];
$is_auth = $session->get('is_auth');
$student_datas = $this->getAllStudentDatas($email, isset($_GET['DBG']) && $_GET['DBG'] == 1); // $this->getStudentDatasFromMail($email);
if (!isset($student_datas['id.Apprenant'])) {
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
if (!isset($student_datas['ID_VCARD'])) {
return $this->redirectToRoute('app_v_card_student_new', [], Response::HTTP_SEE_OTHER);
}
$ide_dossier = 'E-'.$student_datas['id.Apprenant'];
// echo(__METHOD__.': student_datas: '. var_export($student_datas, true));
$vcard_datas = array();
$vcard_datas['user_email'] = $email;
$vcard_datas['disp_vcard_student'] = true;
$vcard_datas['current_card'] = true;
$vcard_datas['can_validate'] = false;
$vcard_datas['can_duplicate'] = false;
$vcard_datas['validated'] = true;
$vcard_datas['new_version'] = true;
$vcard_datas['is_manager'] = false;
// $vcard_datas['published'] = true;
// $vcard_datas['published_card'] = true;
$vcard_datas['needs_submit'] = false;
$vcard_datas['personnel_datas'] = array(
'PRENOM'=> $student_datas['Prénom'],
'NOM' => $student_datas['Nom'],
'TEL_PRO' => $student_datas['Portable'],
'EMAIL_PRO' => $email,
);
$comm_datas = json_decode($student_datas['COMMENTAIRE'], true);
if (isset($comm_datas['role'])) {
$vcard_datas['personnel_datas']['ROLE'] = $comm_datas['role'];
}
$vcard_datas['v_datas_card'] = array(
'SITE_RATTACHEMENT' => $student_datas['Campus'],
'ADRESSE_PRO' => $student_datas['Campus'].' - '. $student_datas['ADRESSE_PRO'],
// 'ADRESSE_PRO' => $student_datas['ADRESSE_PRO'],
);
$vcard_datas['v_card'] = array(
'titre' => '["'.$student_datas['Fonction'].'"]',
'tel' => $student_datas['TEL'], // '["'.$student_datas['Portable'].'"]',
'email' => '["'.$student_datas['email'].'","'.$student_datas['emailPerso'].'"]'
);
//* // TODO: enable social links
foreach ($this->SOCIAL_NETS as $net_item => $net_value) {
if (isset($comm_datas[$net_item])
&& $comm_datas[$net_item] != ''
) {
$vcard_datas['v_card'][$net_item] = // $net_value.
$comm_datas[$net_item];
$vcard_datas['v_card'][$net_item.'_url'] = $net_value.$comm_datas[$net_item];
}
}
// */
$photo_file = '/mnt/recadre/'.$ide_dossier.'.jpg';
if ($student_datas['AVEC_PHOTO'] == 'oui'
|| (file_exists($this->uploadsDIR.('photo_'.$ide_dossier.'.jpg')))
) {
if ($student_datas['AVEC_PHOTO'] != 'oui') {
$photo_file = $this->uploadsDIR.('photo_'.$ide_dossier.'.jpg');
}
$key = $this->storeInSession($session, $photo_file);
if (!empty($key)) {
$vcard_datas['PHOTO_K'] = $key;
}
}
return $this->renderForm('v_card_student/show.html.twig', $vcard_datas);
/*
[
'v_card' => $vCard,
'vcard_datas' => $vcard_datas,
'v_datas_card' => $v_datas_card,
'personnel_datas' => $personnel_datas,
'infos_carte' => $infos_carte,
'can_validate' => $this->isCardManager($vCard),
]
);
*/
}
public function selectMailLike($search, $DBG = false) {
$out = array();
if (!empty($search)) {
$RAW_QUERY = "SELECT EMAIL FROM VCARD_STUDENT where EMAIL like '%".$search."%' order by VERSION desc FETCH FIRST 1 ROWS ONLY"; // "SELECT EMAIL FROM VCARD where STATUT='validé' and LOWER(EMAIL) like '%".$search."%' order by VERSION desc FETCH FIRST 1 ROWS ONLY"; // "SELECT EMAIL FROM VCARD where LOWER(EMAIL) like '%". $search."%'"; // ' where IDE_DOSSIER='.$ide_dossier;
$found = $this->selectRaw($RAW_QUERY, $DBG);
if (count($found) > 0) {
$out = $found;
}
}
return $out;
}
/**
* @Route("/v/student/card/search", name="app_v_card_student_search", methods={"GET", "POST"})
*/
public function search(Request $request): Response {
// echo(__METHOD__.' _POST: '.var_export($_POST, true)); exit();
$session = $request->getSession();
$is_auth = $session->get('is_auth');
$datas = array();
$datas['is_manager'] = $session->get('is_manager');
if (!$is_auth) {
$datas['needs_auth'] = true;
}
$datas['disp_search_student'] = true;
if (isset($_POST['email'])
&& $_POST['email'] != ''
) {
$ans = array('msg' => 'NOT_FOUND');
$found = $this->selectMailLike($_POST['email']); // getVCardFromEmail($_POST['email'], $get_like = true);
// echo(__METHOD__.' found: '.var_export($found, true));
/*
if (isset($found['EMAIL_PRO'])) {
$ans['msg'] = 'ok';
$ans['EMAIL_PRO'] = $found['EMAIL_PRO'];
}
//*/
//
if (count($found) > 0) {
$ans = $found;
}
$content = json_encode($ans);
return new Response($content, 200, array(
'Content-type' => 'text/json',
)
);
} else {
return $this->render('v_card_student/search.html.twig', $datas);
}
}
protected function addOptVcardFields($vcard, array $datas, $DBG = false) {
if (isset($vcard)) {
if ($DBG !== false) {
echo(__METHOD__.': datas: '.var_export($datas, true));
}
$vcard->addRole('Elève ingénieur Arts et Métiers');
if (isset($datas['personnel_datas']['ROLE'])) {
$vcard->addNote($datas['personnel_datas']['ROLE']);
}
foreach ($this->SOCIAL_NETS as $field_type => $values) {
if (isset($datas['v_datas_card'][$field_type])) {
$vcard->addURL($datas['v_datas_card'][$field_type], $field_type);
}
}
}
}
/**
* @Route("/v/student/card/last", name="app_v_card_student_last", methods={"GET"})
*/
public function last(Request $request, LoggerInterface $logger): Response {
$session = $request->getSession();
$is_auth = $session->get('is_auth');
$user_email = isset($_GET['email']) ? $_GET['email'] : $session->get('user_email');
$student_datas = array(
'avec_photo' => 'non',
'tel2' => '',
);
$vcard_datas['can_validate'] = false;
$vcard_datas['needs_submit'] = false;
$vcard_datas['can_duplicate'] = false;
$vcard_datas['disp_vcard_student'] = true;
$vcard_datas['is_last'] = true;
$ide_dossier = null;
$tpl_name = 'v_card_student/last.html.twig';
$is_download = (
(isset($_GET['download']) && $_GET['download'] == 1)
|| (isset($_POST['download']) && $_POST['download'] == 1)
);
if (!$is_download) {
$vcard_datas['is_search'] = true;
if (isset($_GET['back'])
&& !empty($_GET['back'])
) {
$vcard_datas['back_url'] = '/v/card/search';
}
if (isset($_GET['embedded'])) {
$vcard_datas['embedded'] = true;
}
$vcard_datas['is_manager'] = false;
$vcard_datas['published'] = true;
$vcard_datas['published_card'] = true;
if (isset($_GET['back'])
&& $_GET['back'] == 'search'
) {
$vcard_datas['from_search'] = true;
}
}
if (isset($user_email)) {
$found = $this->getStudentDatasFromMail($user_email);
if (isset($found)) {
/*
$student_datas['email'] = $user_email;
$student_datas['Fonction'] = $found['Fonction'];
$student_datas['Portable'] = $found['Portable'];
$student_datas['emailPerso'] = $found['emailPerso'];
$student_datas['Campus'] = $found['Campus'];
//*/
//
$student_datas = $this->getAllStudentDatas($user_email, isset($_GET['DBG']) && $_GET['DBG'] == 1);
if (!isset($student_datas['ID_VCARD'])) {
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
$ide_dossier = 'E-'.$student_datas['id.Apprenant'];
/*
$tmp_vcard = new VCard();
$tmp_vcard->setid_vcard(123);
$tmp_vcard->settitre($student_datas['titre']);
$tmp_vcard->settel($student_datas['tel']);
$tmp_vcard->setemail($user_email);
$tmp_vcard->setavec_photo($student_datas['avec_photo']);
*/
$vcard_datas['v_card'] = array(
'titre' => '["'.$student_datas['Fonction'].'"]',
'tel' => $student_datas['TEL'], // '["'.$student_datas['Portable'].'"]',
'email' => '["'.$student_datas['email'].'","'.$student_datas['emailPerso'].'"]',
);
$tmp_tel_datas = json_decode($student_datas['TEL'], true);
$d_creation_datas = explode(' ', $student_datas['D_CREATION']);
$d_creation_label = $d_creation_datas[1];
$d_creation_datas = explode('-', $d_creation_datas[0]);
$d_creation_label = $d_creation_datas[2].'/'. $d_creation_datas[1].'/'.$d_creation_datas[0].' '.$d_creation_label;
$vcard_datas['v_datas_card'] = array(
'IDE_DOSSIER' => $ide_dossier,
'SITE_RATTACHEMENT' => $student_datas['Campus'],
'ID_VCARD' => $student_datas['ID_VCARD'],
'EMAIL_PRO' => $user_email,
'ADRESSE_PRO' => $student_datas['Campus'].' - '. $student_datas['ADRESSE_PRO'],
'AVEC_PHOTO' => $student_datas['AVEC_PHOTO'],
'PRENOM' => $student_datas['Prénom'],
'NOM' => $student_datas['Nom'],
'TITRE' => '["'.$student_datas['Fonction'].'"]',
'TEL_PRO' => (isset($tmp_tel_datas[0]) ? $tmp_tel_datas[0] : ''), // $student_datas['Portable'],
'TEL' => $student_datas['TEL'],
'EMAIL' => '["'.$student_datas['email'].'","'.$student_datas['emailPerso'].'"]',
// TODO: comment social links for now
/*
'facebook' => 'test_facebook',
'linkedin' => 'test_linkedin',
'tikTok' => 'test_tiktok',
'instagram' => 'test_insta',
// */
'D_CREATION' => $d_creation_label
);
// echo(__METHOD__.' adding v_card....');
} else { // ENDIF isset found
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
$comm_datas = json_decode($student_datas['COMMENTAIRE'], true);
//* // TODO: enable social networks support
foreach ($this->SOCIAL_NETS as $net_item => $net_value) {
if (isset($comm_datas[$net_item])
&& $comm_datas[$net_item] != ''
) {
$vcard_datas['v_card'][$net_item] = $net_value.$comm_datas[$net_item];
$vcard_datas['v_datas_card'][$net_item] = $net_value.$comm_datas[$net_item];
$vcard_datas['v_card'][$net_item.'_url'] = $net_value.$comm_datas[$net_item];
}
}
//*/
$vcard_datas['personnel_datas'] = array(
'PRENOM'=> $found['Prénom'],
'NOM' => $found['Nom'],
'TEL_PRO' => $found['Portable'],
'EMAIL_PRO' => $user_email,
);
if (isset($comm_datas['role'])) {
$vcard_datas['personnel_datas']['ROLE'] = $comm_datas['role'];
}
$uri = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$uri .=(isset($_GET['email']) || isset($_GET['embedded']) ? '&' : '?');
$dwnl = $uri . 'download=1'.(isset($user_email) ? '&email='.$user_email : '&email='.$personnel_datas['EMAIL_PRO']);
$vcard_datas['download_url'] = $dwnl;
$vcard_datas['qr_code_url'] = $this->generateQrCode($vcard_datas);
$vcard_datas['disp_vcard_student'] = true;
$vcard_datas['is_last'] = true;
$is_download = (
(isset($_GET['download']) && $_GET['download'] == 1)
|| (isset($_POST['download']) && $_POST['download'] == 1)
);
//* // TODO: enable social links
foreach($this->SOCIAL_NETS as $net_item => $val) {
if (isset($comm_datas[$net_item])
&& $comm_datas[$net_item] != ''
) {
$vcard_datas['v_card'][$net_item] = $comm_datas[$net_item];
}
}
//*/
$tpl_name = isset($_GET['embedded']) // ($_GET['v2'])
? 'v_card_student/embedded.html.twig'
: 'v_card_student/last.html.twig';
if (isset($_GET['embedded'])
&& isset($_GET['v4'])
) {
$tpl_name = 'v_card_student/embedded_v4.html.twig';
}
$photo_file = '/mnt/recadre/'.$ide_dossier.'.jpg';
// $photo_file = '/mnt/recadre/'.$ide_dossier.'.jpg';
if ($student_datas['AVEC_PHOTO'] == 'oui'
|| (file_exists($this->uploadsDIR.('photo_'.$ide_dossier.'.jpg')))
) {
if ($student_datas['AVEC_PHOTO'] != 'oui') {
$photo_file = $this->uploadsDIR.('photo_'.$ide_dossier.'.jpg');
}
$key = $this->storeInSession($session, $photo_file);
if (!empty($key)) {
$vcard_datas['PHOTO_K'] = $key;
}
}
/*
// echo(__METHOD__.': '. var_export($last_vcard, true)); exit();
if (isset($student_datas['AVEC_PHOTO']) && $student_datas['AVEC_PHOTO'] == 'oui'
|| (file_exists($this->uploadsDIR.('photo_'.$ide_dossier.'.jpg')))
) {
if (!isset($last_vcard['AVEC_PHOTO']) || $last_vcard['AVEC_PHOTO'] != 'oui') {
$photo_file = $this->uploadsDIR.('photo_'.$ide_dossier.'.jpg');
}
$key = $this->storeInSession($session, $photo_file);
if (!empty($key)) {
$vcard_datas['PHOTO_K'] = $key;
}
}
//*/
if (isset($_GET['DBG'])) {
echo('<br/>'.__METHOD__.': user_email: '.$user_email);
echo('<br/>'.__METHOD__.': download_url: '.$vcard_datas['download_url']);
}
if ($is_download) {
$vcard_struct_datas = $this->buildVCardFileStructure($user_email);
$vcard_datas = array_merge($vcard_struct_datas, $vcard_datas);
// echo(__METHOD__.': vcard_datas: '.var_export($vcard_datas, true));
$content = $this->getVCardFileContent($vcard_datas, isset($_GET['DBG']));
$filename = 'vcard_'.$ide_dossier.'.vcf';
// echo(__METHOD__.' content:'.$content); exit();
$logger->debug('Downloading QRCODE', [
// 'user_email' => $user_email,
'IP_ADDR' => $_SERVER['REMOTE_ADDR'],
]);
return new Response($content, 200, array(
//*
'X-Sendfile' => $filename,
'Content-type' => 'application/octet-stream',
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename)
//*/
)
);
}
} else { // END if user_email
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
if (isset($_GET['DBG'])
&& $tpl_name == 'v_card_student/last.html.twig'
) {
echo(__METHOD__.': '.var_export($vcard_datas, true));
}
return $this->renderForm($tpl_name, $vcard_datas);
}
/**
* @Route("/v/student/card/lastcardinfo", name="app_v_card_student_lastcardinfo", methods={"GET"})
*/
public function lastcardinfo(Request $request, VCardRepository $vCardRepository): Response {
$content = 'NOT FOUND';
$session = $request->getSession();
$user_email = $session->get('user_email');
$student_datas = $this->getAllStudentDatas($user_email);
$found = $this->selectLastVcardStudent($student_datas['id.Apprenant']);
$content = __METHOD__.'<br/>'.var_export($found, true);
$content .= '<br/>'.var_export($student_datas, true);
return new Response($content, 200, array(
)
);
}
/**
* @Route("/v/student/card/testvcardstruct", name="app_v_card_student_testvcardstruct", methods={"GET"})
*/
public function testvcardstruct(Request $request, VCardRepository $vCardRepository): Response {
$datas = $this->buildVCardFileStructure($_GET['email'], true);
$content = __METHOD__.' datas: '.var_export($datas, true);
return new Response($content, 200, array(
)
);
}
protected function DEP_selectVcardStudentWhere($where = '') {
$out = null;
// $id_apprenant = isset($datas['id.Apprenant']) ? $datas['id.Apprenant'] : null;
if (isset($where)) {
$where = ' where '.$where;
}
$query = 'select * from vcard_student'. $where;
$found = $this->selectRaw($query);
echo(__METHOD__.': query:'.var_export($query, true));
if (count($found) > 0) {
// $found = $found[0];
if (isset($found[0]['ID_VCARD'])) {
$out = $found;
}
}
return $out;
}
protected function buildVCardFileStructure($user_email, $DBG = false) {
$out = array();
$student_datas = $this->getStudentDatasFromMail($user_email);
$out['v_datas_card'] = array(
'NOM' => $student_datas['Nom'],
'PRENOM'=> $student_datas['Prénom'],
'TITRE' => '["'.$student_datas['Fonction'].'"]',
'EMAIL_PRO' => $user_email,
'EMAIL' => $student_datas['emailPerso'],
'TEL_PRO' => '',
'TEL' => '["'.$student_datas['Portable'].'"]',
'ADRESSE_PRO' => $this->getFromSite($student_datas['Campus']),
'IDE_DOSSIER' => $student_datas['id.Apprenant'],
);
$out['personnel_datas'] = array(
'SERVICE' => $student_datas['Formation'],
);
$out = array_merge($out, $this->getAllStudentDatas($user_email));
if ($DBG !== false) {
echo(__METHOD__.' out: '. var_export($out, true));
}
return $out;
}
protected function selectLastVcardStudent($id_apprenant) {
$out = null;
// $id_apprenant = isset($datas['id.Apprenant']) ? $datas['id.Apprenant'] : null;
if (isset($id_apprenant)) {
$query = 'select * from vcard_student where id_apprenant = '. $id_apprenant .' order by id_vcard desc fetch first 1 rows only';
$found = $this->selectRaw($query);
// echo(__METHOD__.': found:'.var_export($found, true));
if (count($found) > 0) {
$found = $found[0];
if (isset($found['ID_VCARD'])) {
$out = $found;
}
}
}
return $out;
}
protected function addStudentOptFields($datas) {
$out = $datas;
$add_fields = array(
'Formation'
);
$add_fields = array_merge($add_fields, array_keys($this->SOCIAL_NETS));
if (isset($datas['COMMENTAIRE'])) {
$tmp_val = json_decode($datas['COMMENTAIRE'], true);
foreach($add_fields as $k => $item) {
if (isset($tmp_val[$item])) {
$out[$item] = $tmp_val[$item];
}
}
}
return $out;
}
protected function getAllStudentDatas($user_email, $DBG = false) {
$out = array();
if (isset($user_email)) {
$found = $this->getStudentDatasFromMail($user_email);
if (isset($found)) {
$student_datas = array(
'avecphoto' => 'non',
'tel2' => '',
);
$student_datas = array_merge($found, $student_datas);
$student_datas['titre'] = $found['Fonction'];
$student_datas['tel'] = $found['Portable'];
$student_datas['emailPerso'] = $found['emailPerso'];
$student_datas['ADRESSE_PRO'] = $this->getFromSite($found['Campus']);
$db_datas = $this->selectLastVcardStudent($found['id.Apprenant']);
if (isset($db_datas['ID_VCARD'])
) {
$tmp_val = json_decode($db_datas['EMAIL'], true);
if (count($tmp_val) > 1) {
$student_datas['emailPerso'] = $tmp_val[1];
}
$tmp_val = json_decode($db_datas['TEL'], true);
if (count($tmp_val) > 0) {
$student_datas['tel'] = '0' . substr($tmp_val[0], 3);
if (isset($tmp_val[1])) {
$student_datas['tel2'] = '0' . substr($tmp_val[1], 3);
}
}
if ($db_datas['AVEC_PHOTO'] == 'oui') {
$student_datas['avecphoto'] = $db_datas['AVEC_PHOTO'];
}
$student_datas = array_merge($student_datas, $this->addStudentOptFields($db_datas));
}
$student_datas['avec_photo'] = $student_datas['avecphoto'];
$out = $student_datas;
if ($DBG !== false) {
echo(__METHOD__.' out:'. var_export($out, true));
}
}
}
return $out;
}
/**
* @Route("/v/student/card/new", name="app_v_card_student_new", methods={"GET", "POST"})
*/
public function new(Request $request, VCardRepository $vCardRepository): Response {
$session = $request->getSession();
$this->checkAuth($session, 'app_v_card_student_new');
$user_email = $session->get('user_email'); // $_GET['email'];
$is_auth = $session->get('is_auth');
$DBG = // true || // TODO: remove true
isset($_GET['DBG']) && $_GET['DBG'] == '1';
$student_datas = array(
'avecphoto' => 'non',
'tel2' => '',
);
$comm_datas = array();
$url = "https://{$_SERVER['HTTP_HOST']}/v/student/card/last?";
if (!isset($user_email)) {
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
if (isset($user_email)
&& $is_auth
) {
$db_datas = $this->getAllStudentDatas($user_email);
if (isset($db_datas['ID_VCARD'])) { // NOTE: card exists in DB
return $this->redirectToRoute('app_v_card_student_edit', [], Response::HTTP_SEE_OTHER);
}
//*
$found = $this->getStudentDatasFromMail($user_email, $DBG);
if (isset($found)) {
$student_datas['titre'] = $found['Fonction'];
$student_datas['tel'] = $found['Portable'];
$student_datas['Nom'] = $found['Nom'];
$student_datas['Prénom'] = $found['Prénom'];
$student_datas['Campus'] = $found['Campus'];
$student_datas['ADRESSE_PRO'] = $this->getFromSite($student_datas['Campus']);
$student_datas['emailPerso'] = $found['emailPerso'];
$student_datas['id.Apprenant'] = $found['id.Apprenant'];
$student_datas['Formation'] = $found['Formation'];
$student_datas['VERSION'] = 0;
//*/
// $student_datas = $this->getAllStudentDatas($user_email, $DBG);
$vCardStudent = new VCardStudent();
$form = $this->createForm(VCardStudentType::class, $vCardStudent);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$datas = $form->getData();
$tmp_comms = array();
$comm_datas['date_expiration'] = $found['date_expiration'];
$comm_datas['login'] = $found['login.Individu'];
$datas->setid_apprenant($student_datas['id.Apprenant']);
$datas->setformation($student_datas['Formation']);
$datas->setVERSION(sprintf("%d", $student_datas['VERSION']) + 1);
// NOTE: getting titre opt used for storing role
$titre_datas = json_decode($datas->gettitre(), true);
if (isset($titre_datas[1])) {
$comm_datas['role'] = $titre_datas[1];
}
$form_comment = $datas->getcommentaire();
if (isset($form_comment)) {
$tmp_comms = json_decode($form_comment, true);
$comm_datas = array_merge($comm_datas, $tmp_comms);
}
if (isset($found['date_expiration'])) {
$datas->setcommentaire(json_encode($comm_datas));
}
// echo(__METHOD__.': '.var_export($datas->gettitre(), true));
// echo(__METHOD__.': '. var_export($datas, true));
if (false !== $this->addVCardStudent($datas)) { // , true)) { // TODO: REMOVE true !!!!!
$this->generateQrCodeFile($url, $user_email);
return $this->redirectToRoute('app_v_card_student_show', [], Response::HTTP_SEE_OTHER);
} else {
echo(__METHOD__. ' addVCardStudent FAILURE: datas: '.var_export($datas, true)); exit();
}
}
} else { // END if found
return $this->redirectToRoute('app_v_card_student_notfound', [], Response::HTTP_SEE_OTHER);
}
} // END if user_email
return $this->renderForm('v_card_student/new.html.twig', [
// 'v_card' => $vCard,
'user_email' => $user_email,
// 'personnel' => $personnel_datas,
'student' => $student_datas,
'is_new' => true,
'disp_form_student' => true,
// 'is_manager' => $session->get('is_manager'),
'form' => $form,
]);
}
protected function addVCardStudent(VCardStudent $datas, $TEST_MODE = false) {
// INSERT INTO "EASYID"."VCARD_STUDENT" (ID_VCARD, ID_APPRENANT, FORMATION, EMAIL, TEL, STATUT, COMMENTAIRE, VERSION, AVEC_PHOTO) VALUES ('1', '1234', 'TEST_FORMATION', 'email@test.com', '[""]', 'enregistrée', '{}', '1', 'non')
$out = false;
if (isset($datas)) {
$query = 'INSERT INTO "EASYID"."VCARD_STUDENT" (ID_VCARD, ID_APPRENANT, FORMATION, EMAIL, TEL, STATUT, COMMENTAIRE, VERSION, AVEC_PHOTO, D_CREATION) VALUES (:ID_VCARD, :ID_APPRENANT, :FORMATION, :EMAIL, :TEL, :STATUT, :COMMENTAIRE, :VERSION, :AVEC_PHOTO, CURRENT_DATE)';
$tmp_id = $this->getNewStudentVCardId(); // $datas->getId();
if (!isset($tmp_id)) {
$tmp_id = $this->getNewStudentVCardId($TEST_MODE);
$datas->setid_vcard($tmp_id);
}
$version = $datas->getversion();
$version = ((int) $version) + 1;
$version = $this->getNewStudentVersion($datas->getid_apprenant());
$commentaire_datas = $datas->getcommentaire();
if (!isset($commentaire_datas)) {
$commentaire_datas = '{}';
};
$avec_photo = $datas->getavecphoto();
if ($avec_photo != 'oui') {
$avec_photo = 'non';
}
$values = array(
'ID_VCARD' => $tmp_id,
'ID_APPRENANT' => $datas->getid_apprenant(),
'FORMATION' => $datas->getformation(),
'EMAIL' => $datas->getemail(),
'TEL' => $datas->gettel(),
'STATUT' => $datas->getstatut(),
'COMMENTAIRE' => $commentaire_datas,
'VERSION' => $version,
'AVEC_PHOTO' => $avec_photo
);
// $tmp_datas[] = $datas[];
if ($TEST_MODE) {
echo(__METHOD__.': SQL:' . $query . '<br/>' . var_export($values, true));
}
$out = $this->writeRaw($query, $values, $TEST_MODE);
}
return $out;
}
/**
* @Route("/v/student/card/setuser", name="app_v_card_student_setuser", methods={"GET", "POST"})
*/
public function setuser(Request $request): Response {
$session = $request->getSession();
$session->set('is_auth', true);
$session->set('user_email', $_GET['email']);
$content = __METHOD__.': session:'.var_export($session, true);
return new Response($content, 200, array(
'Content-type' => 'text/html'
)
);
}
/**
* @Route("/v/student/card/logout", name="app_v_card_student_logout", methods={"GET", "POST"})
*/
public function logout(Request $request): Response {
$session = $request->getSession();
$session->set('is_auth', false);
$session->set('user_email', null);
$content = __METHOD__.': LOGGED_OUT';
return new Response($content, 200, array(
'Content-type' => 'text/html'
)
);
}
/**
* @Route("/v/student/card/userinfo", name="app_v_card_student_userinfo", methods={"GET", "POST"})
*/
public function userinfo(Request $request): Response {
$session = $request->getSession();
$user = $session->get('user_email');
$content = __METHOD__.': session:'.var_export($session, true);
$content .= ' user: ' . $user;
$content .= '<br/>'.var_export($this->getStudentDatasFromMail($user), true);
return new Response($content, 200, array(
'Content-type' => 'text/html'
)
);
}
/**
* @Route("/v/student/card/uploadres", name="app_v_card_student_uploadres", methods={"GET", "POST"})
*/
public function uploadres(Request $request, VCardRepository $vCardRepository): Response
{
$session = $request->getSession();
/*
$is_auth = $session->get('is_auth');
//*/
$is_auth = true;
if (!$is_auth) {
$session->set('previous', 'app_v_card_student_show');
// $session->set('prev_id', $vCard->getId());
return new Response(null, 302, array(
'Location' => 'auth',
));
} else {
$user_email = $session->get('user_email');
$personnel_datas = $this->getAllStudentDatas($user_email); // getPersonnelFromEnv($session);
$ide_dossier = 'E-'.$personnel_datas['id.Apprenant'];
/*
$filename = '/mnt/recadre/E-94824323.jpg';
$target_dir = 'public/uploads/';
if (!isset($_GET['key'])) {
$key = $this->storeInSession($session, $filename);
// echo(__METHOD__.': key: '.$key);
} else {
$val = $session->get($_GET['key']);
echo(__METHOD__.': val: '.$val);
}
// exit();
//
//
*/
if (isset($_FILES['fileToUpload']['name'])) { // (isset($_POST['submit']))
$file_out = $this->uploadFile('photo_'.$ide_dossier.'.jpg');
$file_path = __DIR__.'/../../public'.$file_out;
// echo(__METHOD__.': file_out: '.$file_path);
$key = $this->storeInSession($session, $file_path);
$del_key = $this->storeInSession($session, $file_path);
$data_out = array(
'src' => '/v/card/photo?rel='.$key,
'del_key' => $del_key,
);
$content = json_encode($data_out);
// $content = 'Done.';
return new Response($content, 200, array(
'Content-type' => 'text/html'
)
);
} else if (!empty($_GET['remove'])){
$datas = array();
$datas['status'] = $this->deleteUploadedFile($_GET['remove']);
$content = json_encode($datas);
return new Response($content, 200, array(
'Content-type' => 'text/html'
));
} else {
return $this->render('v_card/upload.html.twig', []);
}
} // END else not auth
}
/**
* @Route("/v/student/card/photo", name="app_v_card_student_photo", methods={"GET", "POST"})
*/
public function photo(Request $request, VCardRepository $vCardRepository): Response
{
$params = array_merge($_GET, $_POST);
$session = $request->getSession();
// $is_auth = $session->get('is_auth');
$img_filename = $this->retrieveFromSession($session, $params['rel']); // $session->get($params['rel']);
$content = (isset($params['rel']) && !empty($params['rel']) && isset($img_filename)) ? file_get_contents($img_filename) : '';
return new Response($content, 200, array(
'Content-type' => 'image/jpeg'
)
);
}
/**
* @Route("/v/student/card/testbound", name="app_v_studentcard_testbound")
*/
public function testbound(): Response {
echo(__METHOD__.': '. $this->writeRaw('insert into ma_table values(?, ?, ?, ?)',['un', 'deuz', '3', 4])); exit();
}
/**
* @Route("/v/student/card/sonsbeurk", name="app_v_card_student_sonsbeurk")
*/
public function sonsbeurk(): Response {
$tmp_card = (new VCardFile());
$test_datas = array(
'v_datas_card' => array(
'linkedin' => 'https://my_linked_in_URL'
)
);
$this->addVcardOptionalFields($tmp_card, $test_datas);
echo(__METHOD__.'tmp_vard: ' . $tmp_card->getOutput());
$this->beurk(); exit();
}
protected function addVcardSocialFields(VCardFile $vcard, $datas) {
$out = $vcard;
// $lastname = $datas['v_datas_card']['NOM'];
if (isset($datas)
&& isset($datas['v_datas_card'])
) {
$field_type = 'linkedin';
if (isset($datas['v_datas_card'][$field_type])) {
$vcard->addURL($datas['v_datas_card'][$field_type], $field_type);
}
}
echo(__METHOD__.' vcard: '.var_export($vcard, true). '<br/>datas:' . var_export($datas, true));
return $out;
}
public function generateQrCodeFile($uri, $email = '', $prefix = 'QRC') {
// $uri = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
// $uri .=(isset($_GET['email']) || isset($_GET['embedded']) ? '&' : '?');
$dwnl = $uri . 'download=1&email='.$email;
$DBG = isset($_GET['DBG']);
if (!is_null($uri)
&& !is_null($email)
) {
$student_datas = $this->getAllStudentDatas($email);
// TODO: write to file
/*
$vcard = $this->getVCardFromEmail($email);
if ($DBG) {
echo(__METHOD__.': vcard: '. var_export($vcard, true));
}
// */
if (isset($student_datas['id.Apprenant'])) {
$ide_dossier = 'E-'.$student_datas['id.Apprenant'];
$filename = $ide_dossier.'.JPG';
$filesystem = new Filesystem();
try {
$filename = Path::normalize('/mnt/qrcode/'
// .'TEST__' // TEST ONLY
.$prefix.$filename
); // sys_get_temp_dir().'/'. $filename);
if (!$this->isNewQRCode($filename)) {
if (file_exists($filename)) {
rename($filename, $filename.'.OLD'); // NOTE: backup old version
}
$options = new QROptions([
'version' => 5,
'eccLevel' => QRCode::ECC_L,
'scale' => 6, // 275x275
'outputType' => QRCode::OUTPUT_IMAGE_JPG,
'imageTransparent' => false,
]);
// echo(strpos(file('/mnt/qrcode/TEST__QRC5474.jpg')[0], 'CREATOR'));
$content = (new QRCode($options))->render($dwnl, $filename);
/* $filesystem->appendToFile(
$filename, $content, true
);
*/
if ($DBG) {
echo(__METHOD__.': dwnl:'.$dwnl.'<br/>File: '.$filename);
}
} else {
if ($DBG) {
echo(__METHOD__.': filename: '.$filename.' already updated....');
}
}
} catch (IOExceptionInterface $exception) {
echo "An error occurred while creating your directory at ".$exception->getPath();
}
} // END if IDE_DOSSIER
}
}
/**
* @Route("/v/student/card/testcreate", name="app_v_card_student_testcreate", methods={"GET", "POST"})
*/
public function testcreate(Request $request, VCardRepository $vCardRepository): Response {
$content = 'NOT_FOUND';
/*
$new_id = $this->getNewStudentVCardId();
$content = __METHOD__.': new_id: '. $new_id;
//*/
echo(__METHOD__.': POST: '.var_export($_POST, true));
if ( isset($_POST['v_card']['id_etudiant'])
// && isset($_POST['v_card']['email'])
// && isset($_POST['v_card']['titre'])
// && isset($_POST['v_card']['tel'])
// && isset($_POST['v_card']['commentaire'])
// && isset($_POST['v_card']['avecphoto'])
) {
$content = __METHOD__.' datas: '.$_POST['v_card']['id_etudiant'];
}
return new Response($content, 200, array(
)
);
}
/**
* @Route("/v/student/card/getcachedate", name="app_v_card_student__getcachedate", methods={"GET"})
*/
public function getcachedate(Request $request, VCardRepository $vCardRepository): Response {
$content = 'NOT_FOUND';
$js_datas = array();
$found = $this->getStudentDatasById('CREATED');
$content = isset($found) ? $found : 'NOT_FOUND';
return new Response($content, 200, array(
)
);
}
/**
* @Route("/v/student/card/getdatasbymail", name="app_v_card_student__getdatasbymail", methods={"GET"})
*/
public function getdatasbymail(Request $request, VCardRepository $vCardRepository): Response {
$content = 'NOT_FOUND';
$js_datas = array();
/*
if (isset($_GET['email'])) {
$file_datas = file_get_contents($this->studentsCacheFile);
if (isset($file_datas)) {
$js_datas = json_decode($file_datas, true);
if (count($js_datas) > 0
) {
$js_datas = $js_datas[$_GET['email']];
if (isset($js_datas['email'])) {
$content = json_encode($js_datas);
}
}
}
}
*/
$found = $this->getStudentDatasFromMail($_GET['email']);
$content = isset($found) ? json_encode($found) : 'NOT_FOUND';
return new Response($content, 200, array(
)
);
}
/**
* @Route("/v/student/card/refreshstudentsfile", name="app_v_studentcard_refreshstudentsfile", methods={"GET"})
*/
public function refreshstudentsfile(Request $request, VCardRepository $vCardRepository): Response {
$content = 'KO';
$cache_date = (file_exists($this->studentsCacheFile)) ? $this->getStudentDatasById('CREATED') : 0;
if (isset($cache_date)) {
$content = $cache_date;
$timestamp_now = date_timestamp_get(date_create());
$duration = $timestamp_now - ((int) $cache_date);
if ($duration > $this->CACHE_DURATION) {
$datas = $this->getStudentDatasFromServer();
if (isset($datas['CREATED'])) {
$content = $datas['CREATED'];
}
}
if (isset($_GET['DBG'])) {
$content .= ' delta:' .$duration;
}
}
return new Response($content, 200, array(
)
);
}
public function getStudentDatasFromServer($DBG = false) {
$out = false;
$client = new CurlHttpClient();
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// $client->AllowSelfSignedCertificate();
$response = $client->request('GET', $this->studentsFileURL, [
// ...
"verify_peer"=>false,
"verify_host"=>false,
'extra' => [
'curl' => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
],
],
]);
$content = $response->getContent();
$datas = json_decode($content, true);
$students_datas = array();
foreach($datas as $k => $data_line) {
if (isset($data_line['email'])) {
$students_datas[$data_line['email']] = $data_line;
}
}
if (count($students_datas) > 1) {
$students_datas['CREATED'] = date_timestamp_get(date_create());
file_put_contents($this->studentsCacheFile, json_encode($students_datas));
$out = $students_datas['CREATED'];
}
if ($DBG !== false) {
echo(__METHOD__. ' Response: ' . var_export($students_datas, true)); exit();
}
return $out;
}
/**
* @Route("/v/student/card/testSQL", name="app_v_card_student_testSQL", methods={"GET"})
*/
public function testSQL(Request $request, VCardRepository $vCardRepository): Response {
//*
$query = "INSERT INTO \"EASYID\".\"VCARD_STUDENT\" (ID_VCARD, ID_APPRENANT, FORMATION, EMAIL, TEL, STATUT, COMMENTAIRE, VERSION, AVEC_PHOTO) VALUES (:ID_VCARD, :ID_APPRENANT, :FORMATION, :EMAIL, :TEL, :STATUT, :COMMENTAIRE, :VERSION, :AVEC_PHOTO)";
$result = $this->writeRaw($query, array(
'ID_VCARD' => 2,
'ID_APPRENANT' => 1234,
'FORMATION' => 'TEST_FORMATION',
'EMAIL' => 'email@test.com',
'TEL' => '[\"\"]',
'STATUT' => 'enregistrée',
'COMMENTAIRE' => '{}',
'VERSION' => '1',
'AVEC_PHOTO' => 'non'
)
);
// */
/*
$query = 'select id_vcard,email from vcard_student where id_apprenant = :id_apprenant';
$result = $this->writeRaw($query, array(
'id_apprenant' => 1234
)
);
*/
$content = __METHOD__.' result: '.$result;
return new Response($content, 200, array(
)
);
}
/**
* @Route("/v/student/card/testcurl", name="app_v_card_student_testcurl", methods={"GET"})
*/
public function testcurl(Request $request, VCardRepository $vCardRepository): Response {
$client = new CurlHttpClient();
$response = $client->request('GET', 'https://www.google.com', [
// ...
'extra' => [
'curl' => [
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
],
],
]);
$content = $response->getContent();
echo(__METHOD__. ' Response: ' .$content); exit();
}
/**
* @Route("/studentindex", name="app_v_card_student_index", methods={"GET"})
public function index(Request $request, VCardRepository $vCardRepository): Response
{
$session = $request->getSession();
$is_auth = $session->get('is_auth');
$user_email = $session->get('user_email');
print(__METHOD__.' session: '.var_export($session, true).'<br/>'); exit();
if (!$is_auth) {
$session->set('previous', 'app_v_card_index');
return new Response(null, 302, array(
'Location' => '/v/card/auth',
));
} else if (isset($_GET['admin']) && $_GET['admin'] == 1) {
// echo(__METHOD__.': user_mail:'.var_export($user_email, true)); exit();
return $this->render('v_card/index.html.twig', [
'v_cards' => $vCardRepository->findAll(),
]);
} else {
$content = __METHOD__.': user not found...';
$personnel_datas = $this->getPersonnelFromEnv($session);
$is_manager = $session->get('is_manager'); // $this->isManager($personnel_datas, $session);
// echo(__METHOD__.': personnel_datas: '.var_export($personnel_datas, true)); exit();
if (isset($personnel_datas['IDE_DOSSIER'])) {
$ide_dossier = $personnel_datas['IDE_DOSSIER'];
$last_card = $this->getRecentVCardByIdeDossier($ide_dossier);
// $content = var_export($last_card, true);
if (isset($last_card['IDE_DOSSIER'])) {
return $this->redirectToRoute('app_v_card_student_show', ['id_vcard' => $last_card['ID_VCARD']], Response::HTTP_SEE_OTHER);
} else {
return $this->redirectToRoute('app_v_card_student_new', [], Response::HTTP_SEE_OTHER);
}
}
return new Response($content, 200, array(
)
);
}
}
*/
}