<?php
namespace App\Controller;
use App\Service\AppHelper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AppController extends AbstractController
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
// Accessing the session in the constructor is *NOT* recommended, since
// it might not be accessible yet or lead to unwanted side-effects
// $this->session = $requestStack->getSession();
}
/**
* @Route("/", name="app")
*/
public function index(String $accountToken): Response
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->getParameter('map_url', '').'/api/gp_type.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-i42AccountToken: '.$accountToken
)
));
$response = curl_exec($curl);
curl_close($curl);
$resp_type = json_decode($response, true);
$postFields = '{
"data" : {
"language": "de",
}
}';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->getParameter('map_url', '').'/api/specializationlist.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-i42AccountToken: '.$accountToken
)
));
$response = curl_exec($curl);
curl_close($curl);
$resp = json_decode($response, true);
return $this->render('app/index.html.twig', ['types' => $resp_type, 'specializations' => $resp]);
}
/**
* @Route("/fdrdr/{page}", name="filter_doctor_doctor", defaults={"page"=0})
*/
public function fdrdr(Request $request, AppHelper $appHelper, String $accountToken, $page): Response
{
$html = '<div>Keine Einträge gefunden</div>';
$arrFilter = $request->get('filter', array());
$session = $this->requestStack->getSession();
$offset = intval($page);
if ($page==0){
$offset=1;
$session->set('location', '');
$session->set('results', array());
if (
isset($arrFilter['radius']) && ctype_digit($arrFilter['radius']) &&
isset($arrFilter['zipcity']) && !empty($arrFilter['zipcity'])
)
{
if (is_numeric($arrFilter['zipcity'])){
$location = $appHelper->getPositionByZipcodeFromGoogle($this->getParameter('gmap_apikey', ''), $arrFilter['zipcity']);
} else {
$location = $appHelper->getPositionByAddressFromGoogle($this->getParameter('gmap_apikey', ''), $arrFilter['zipcity']);
}
if (count($location) && isset($location['lat']) && isset($location['lng']))
{
$postFields = '{ "type": "'.$arrFilter['type'].'", "lat": '.$location['lat'].', "lng": '.$location['lng'].', "distance": '.$arrFilter['radius'];
if (isset($arrFilter['specialist']) && $arrFilter['specialist']==='on'){
$postFields .= ', "specialist": true';
}
if (isset($arrFilter['gender']) && !empty($arrFilter['gender'])){
$postFields .= ', "gender": "' . $arrFilter['gender'] . '" ';
}
if (isset($_POST['filter_specialization']) && !empty($_POST['filter_specialization']) && $_POST['filter_specialization'][0]!==''){
$postFields .= ', "specialization": {';
$i = 0;
foreach ($_POST['filter_specialization'] as $specialization) {
if ($i > 0) {
$postFields .= ', ' ;
}
$postFields .= '"'.$i.'": '.$specialization;
$i++;
}
$postFields .= '}';
}
$postFields .= '}';
//var_dump($postFields) ;
//die;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $this->getParameter('map_url', '').'/api/places.json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-i42AccountToken: '.$accountToken
)
));
$response = curl_exec($curl);
curl_close($curl);
$resp = json_decode($response, true);
if (is_array($resp) && isset($resp['results']) && count($resp['results']))
{
$session->set('location', $location);
$session->set('results', $resp['results']);
}
}
} else {
$html = '<div>PLZ / Ort und Umkreis muss eingetragen sein!</div>';
}
}
if (count($session->get('results'))){
$limit=10;
$results=array();
$resultsSession=$session->get('results');
$maxOffset=ceil(count($resultsSession)/$limit);
if ($offset*$limit>=count($resultsSession)){
$loopLength = intval(count($resultsSession));
} else {
$loopLength = $offset*$limit;
}
for ($i=($offset-1)*$limit;$i<$loopLength;$i++){
$results[$i] = $resultsSession[$i];
}
$html = $this->renderView('app/__data_detail.html.twig', array(
'data' => $results,
'location' => $session->get('location'),
'maxOffset' => $maxOffset,
'page' => $offset
));
}
return new JsonResponse(array('html' => $html));
}
}