<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Tracking;
use App\Entity\Mdmrdblog;
use App\Entity\Device;
use App\Entity\Gptype;
use App\Service\AppHelper;
use App\Entity\Specialization;
class ApiController extends AbstractController
{
/**
* @Route("/api/data.json")
*/
public function dataApi(Request $request, AppHelper $appHelper, String $gmapApikey, String $appquarterUrl, String $nappUrl)
{
$em = $this->getDoctrine()->getManager();
$error = $this->validateRequest($request);
$arrResp = [];
if (count($error) > 0)
{
return $this->createErrorResponse($error);
}
if ($request->headers->has('X-i42Device') && $request->headers->has('X-i42Customer'))
{
$i42Device = $request->headers->get('X-i42Device', false);
$i42Customer = $request->headers->get('X-i42Customer', false);
if (
$i42Device && $i42Customer &&
isset($this->requestData['data']) && isset($this->requestData['data']['device']) &&
is_array($this->requestData['data']['device']) && count($this->requestData['data']['device'])
)
{
foreach ($this->requestData['data']['device'] as $data)
{
$lCountry = null;
$sql = 'SELECT country FROM tracking ';
$sql .= 'WHERE device_token = "'.$i42Device.'" AND customer_token = "'.$i42Customer.'" AND ';
$sql .= 'country != "" AND country IS NOT NULL ORDER BY id DESC';
$stm = $em->getConnection()->executeQuery($sql);
$lEntry = $stm->fetch();
if (is_array($lEntry) && isset($lEntry['country']))
{
$lCountry = $lEntry['country'];
}
$address = $appHelper->getAdressByPositionFromGoogle($gmapApikey, $data['latitude'], $data['longitude']);
$country = '';
$fAdress = 'unknown address';
if (isset($address['formatted_address']))
{
$fAdress = $address['formatted_address'];
foreach ($address['address_components'] as $_component)
{
$component = (array) $_component;
foreach ($component['types'] as $type)
{
if ($type == 'country')
{
$country = $component['short_name'];
}
}
}
}
$track = new Tracking();
$track->setDeviceToken($i42Device);
$track->setCustomerToken($i42Customer);
$track->setDeviceState($data['userState']);
$track->setEmergencyState($data['emergency']);
$track->setLat($data['latitude']);
$track->setLng($data['longitude']);
$track->setAccuracy(number_format($data['accuracy'], 4));
$track->setAltitude(number_format($data['altitude'], 4));
$track->setAddress($fAdress);
$track->setCountry($country);
$track->setCreatedAt(\DateTime::createFromFormat('U', $data['timestamp']));
$em->persist($track);
$em->flush();
if (
!empty($lCountry) && (!empty($country) && ($lCountry != $country))
)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $appquarterUrl.'/pnb/api/changed_location',
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 =>'{
"data": {
"currentCountry": "'.$country.'",
"deviceId": "'.$i42Device.'"
}
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-i42AccountToken: 42'
),
));
curl_exec($curl);
curl_close($curl);
}
$nappCurlOptions = array(
CURLOPT_URL => $nappUrl.'/api/data.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 => '',
CURLOPT_HTTPHEADER => array(),
CURLOPT_POSTFIELDS =>'{
"apiTimestamp": "'.$this->requestData['apiTimestamp'].'",
"appTimestamp": "'.$this->requestData['appTimestamp'].'",
"responseApi": "'.$this->requestData['responseApi'].'",
"requestApi": "'.$this->requestData['requestApi'].'",
"data": {
"device": [
{
"latitude": "'.$data['latitude'].'",
"longitude": "'.$data['longitude'].'",
"userState": '.$data['userState'].',
"emergency": '.$data['emergency'].',
"timestamp": '.$data['timestamp'].',
"accuracy": '.$data['accuracy'].',
"altitude": '.$data['altitude'].'
}
]
}
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'X-i42Device: '.$i42Device,
'X-i42Customer: '.$i42Customer
)
);
$curl = curl_init();
curl_setopt_array($curl, $nappCurlOptions);
$arrResp = json_decode(curl_exec($curl), true);
curl_close($curl);
}
}
else
{
$arrResp = $this->createErrorMessage(21, 'not all necessary data submitted');
}
}
return $this->createJsonResponse($arrResp);
}
/**
* @Route("/api/route.json")
*
* @param type $request
* @return type
*/
public function routeAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$error = $this->validateRequest($request);
$arrRes = [];
if (count($error) > 0)
{
return $this->createErrorResponse( $error );
}
if ($request->headers->has('X-i42AccountToken'))
{
$i42AccountToken = $request->headers->get('X-i42AccountToken', false);
if ($i42AccountToken)
{
if (
is_array($this->requestData) && count($this->requestData) && isset($this->requestData['data']) &&
isset($this->requestData['data']['device_token']) && !empty($this->requestData['data']['device_token'])
)
{
$deviceToken = $this->requestData['data']['device_token'];
$sql = 'SELECT * FROM tracking WHERE device_token = "'.$deviceToken.'" ';
if (isset($this->requestData['data']['timerange']))
{
$sql .= 'AND created_at >= "'.date("Y-m-d H:i:s", time() - $this->requestData['data']['timerange']).'" ';
}
$sql .= 'AND lat <> 0 AND lng <> 0 ORDER BY created_at';
$stm = $em->getConnection()->executeQuery($sql);
$arrResp = $stm->fetchAll();
if ($arrResp)
{
foreach ($arrResp as $res)
{
$resSet = [];
$resSet['id'] = $res['id'];
$resSet['lat'] = $res['lat'];
$resSet['lng'] = $res['lng'];
$arrRes[] = $resSet;
}
}
$arrResp = array('results' => $arrRes);
}
else
{
$arrResp = $this->createErrorMessage(21, 'not all necessary data submitted');
}
}
else
{
$arrResp = $this->createErrorMessage(19, 'unregistered account token');
}
}
else
{
$arrResp = $this->createErrorMessage(18, 'unregistered account');
}
return $this->createJsonResponse($arrResp);
}
/**
* @Route("/api/wakeup.json")
*
* @param type $request
* @return type
*/
public function wakeupAction(Request $request, String $appquarterUrl)
{
$em = $this->getDoctrine()->getManager();
$error = $this->validateRequest($request, true, false);
$arrResp = [];
if (count($error) > 0)
{
return $this->createErrorResponse($error);
}
if ($request->headers->has('X-i42Device') && $request->headers->has('X-i42Customer'))
{
$i42Device = $request->headers->get('X-i42Device', false);
$i42Customer = $request->headers->get('X-i42Customer', false);
if ($i42Device && $i42Customer)
{
$wat = new \DateTime();
$device = $em->getRepository(Device::class)->findOneBy(['deviceToken' => $i42Device, 'customerToken' => $i42Customer]);
if (!$device)
{
$device = new Device();
$device->setDeviceToken($i42Device);
$device->setCustomerToken($i42Customer);
$device->setCreatedAt($wat);
}
if (
isset($this->requestData['data']) && isset($this->requestData['data']['config']) &&
is_array($this->requestData['data']['config']) && count($this->requestData['data']['config'])
)
{
$device->setDeviceConfig(json_encode($this->requestData['data']['config']));
}
$device->setUpdatedAt($wat);
$em->persist($device);
$em->flush();
}
return $this->createJsonResponse($arrResp);
}
}
/**
* @Route(
* "/api/places.json",
* name="api_places",
* methods={"POST", "OPTIONS"}
* )
*/
public function placesAction(Request $request, String $accountToken)
{
$em = $this->getDoctrine()->getManager();
$inputData = $request->getContent();
$arrData = json_decode($inputData, true);
$arrRet = [];
if (!$request->isMethod('POST')) {
$arrRet['message'] = 'Request method is not correct!';
return $this->createJsonResponse($arrRet);
}
if (!$request->headers->has('X-i42AccountToken')) {
$arrRet['message'] = 'Account token is missing!';
return $this->createJsonResponse($arrRet);
}
$i42AccountToken = $request->headers->get('X-i42AccountToken');
if ($i42AccountToken != $accountToken) {
$arrRet['message'] = 'Unregistered account token!';
return $this->createJsonResponse($arrRet);
}
if (!(
is_array($arrData) &&
isset($arrData['lat']) && !empty($arrData['lat']) && is_numeric($arrData['lat']) &&
isset($arrData['lng']) && !empty($arrData['lng']) && is_numeric($arrData['lng']) &&
isset($arrData['distance']) && !empty($arrData['distance']) && ctype_digit((string) $arrData['distance'])
)) {
$arrRet['message'] = 'Not all necessary data submitted!';
return $this->createJsonResponse($arrRet);
}
$lat = $arrData['lat'];
$lng = $arrData['lng'];
$distance = $arrData['distance'];
if (isset($arrData['type']) && !empty(trim($arrData['type'])) && $arrData['type']!=='' ){
$typeId = $arrData['type'];
}
$distClmn = '(6371*acos(cos(radians('.$lat.'))*cos(radians(gp.lat))*cos(radians(gp.lng)-radians('.$lng.'))+sin(radians('.$lat.'))*sin(radians(gp.lat)))) AS distance';
$sql = 'SELECT gp.*, '.$distClmn.' FROM gplace AS gp ';
if (isset($arrData['specialist']) && $arrData['specialist']) {
$sql .= ' Where gp.specialist = true ';
}
if (isset($arrData['gender']) && !empty(trim($arrData['gender']))) {
if (isset($arrData['specialist']) && $arrData['specialist']) {
$sql .= ' AND (gp.gender = "' . trim($arrData['gender']) . '" OR gp.gender = "female / male")';
} else {
$sql .= ' WHERE (gp.gender = "' . trim($arrData['gender']) . '" OR gp.gender = "female / male") ';
}
}
$sql .= 'HAVING distance <= '.$distance.' ';
$sql .= 'ORDER BY distance ASC';
if (isset($arrData['type']) && !empty(trim($arrData['type'])) && $arrData['type']!=='' ){
$sql = 'SELECT gp.*, '.$distClmn.' FROM gplace AS gp ';
$sql .= 'LEFT JOIN gplace_gptype AS gpgpt ON gp.id = gpgpt.gplace_id ';
$sql .= 'LEFT JOIN gptype AS gpt ON gpgpt.gptype_id = gpt.id ';
$sql .= 'WHERE gpt.id = "'.$typeId.'" ';
if (isset($arrData['specialist'])) {
$sql .= ' AND gp.specialist = true ';
}
if (isset($arrData['gender']) && !empty(trim($arrData['gender']))) {
$sql .= ' AND (gp.gender = "' . trim($arrData['gender']) . '" OR gp.gender = "female / male") ';
}
$sql .= 'HAVING distance <= '.$distance.' ';
$sql .= 'ORDER BY distance ASC';
}
if (isset($arrData['specialization']) && !empty($arrData['specialization']) && $arrData['specialization'][0]!==''){
$sql = 'SELECT gp.*, '.$distClmn.' FROM gplace AS gp ';
$sql .= 'INNER JOIN gplace_specialization AS gpspec ON gp.id = gpspec.gplace_id ';
if (is_array($arrData['specialization'])) {
for ($i = 0;$i<count($arrData['specialization']);$i++){
if ($i === 0){
$sql .= 'WHERE (gpspec.specialization_id='.$arrData['specialization'][$i];
} else {
$sql .= ' OR gpspec.specialization_id=' . $arrData['specialization'][$i];
}
if ($i===count($arrData['specialization'])-1){
$sql .= ')';
}
}
if (isset($arrData['specialist']) && $arrData['specialist']) {
$sql .= ' AND gp.specialist = true ';
}
if (isset($arrData['gender']) && !empty(trim($arrData['gender']))) {
$sql .= ' AND (gp.gender = "' . trim($arrData['gender']) . '" OR gp.gender = "female / male") ';
}
$sql .= ' Group BY gp.id ';
$sql .= ' HAVING (((distance <= '.$distance.')) AND ((Count(DISTINCT gpspec.specialization_id) = ' . Count($arrData['specialization']) . ')))';
$sql .= ' ORDER BY distance ASC';
}
}
if (isset($arrData['type']) && !empty(trim($arrData['type'])) && $arrData['type']!=='' && (isset($arrData['specialization']) && !empty($arrData['specialization']) && $arrData['specialization'][0]!=='')){
$sql = 'SELECT DISTINCT gp.*, '.$distClmn.' FROM gplace AS gp ';
$sql .= 'INNER JOIN gplace_specialization AS gpspec ON gp.id = gpspec.gplace_id ';
$sql .= 'LEFT JOIN gplace_gptype AS gpgpt ON gp.id = gpgpt.gplace_id ';
$sql .= 'LEFT JOIN gptype AS gpt ON gpgpt.gptype_id = gpt.id ';
$sql .= 'WHERE gpt.id = "'.$typeId.'" ';
if (isset($arrData['specialist']) && $arrData['specialist']) {
$sql .= ' AND gp.specialist = true ';
}
if (isset($arrData['gender']) && !empty(trim($arrData['gender']))) {
$sql .= ' AND (gp.gender = "' . trim($arrData['gender']) . '" OR gp.gender = "female / male") ';
}
if (is_array($arrData['specialization'])) {
for ($i = 0;$i<count($arrData['specialization']);$i++){
if ($i === 0){
$sql .= 'AND (gpspec.specialization_id='.$arrData['specialization'][$i];
} else {
$sql .= ' OR gpspec.specialization_id=' . $arrData['specialization'][$i];
}
if ($i===count($arrData['specialization'])-1){
$sql .= ')';
}
}
$sql .= ' Group BY gp.id ';
$sql .= ' HAVING (((distance <= '.$distance.')) AND ((Count(DISTINCT gpspec.specialization_id) = ' . Count($arrData['specialization']) . ')))';
$sql .= ' ORDER BY distance ASC';
}
}
$stm = $em->getConnection()->executeQuery($sql);
$arrRess = $stm->fetchAll();
if ($arrRess)
{
foreach ($arrRess as $idx => $res)
{
$arrRet[$idx] = $res;
$arrRet[$idx]['lat'] = (float) $arrRet[$idx]['lat'];
$arrRet[$idx]['lng'] = (float) $arrRet[$idx]['lng'];
$arrRet[$idx]['opening_hour'] = nl2br($arrRet[$idx]['opening_hour']);
}
}
$arrRet = array('results' => $arrRet);
return $this->createJsonResponse($arrRet);
}
/**
* @Route(
* "/api/research.json",
* name="api_research",
* methods={"POST", "OPTIONS"}
* )
*/
public function researchAction(Request $request, AppHelper $appHelper, String $gmapApikey, String $accountToken)
{
$em = $this->getDoctrine()->getManager();
$inputData = $request->getContent();
$arrData = json_decode($inputData, true);
$arrRet = [];
if (!$request->isMethod('POST')) {
$arrRet['message'] = 'Request method is not correct!';
return $this->createJsonResponse($arrRet);
}
if (!$request->headers->has('X-i42AccountToken')) {
$arrRet['message'] = 'Account token is missing!';
return $this->createJsonResponse($arrRet);
}
$i42AccountToken = $request->headers->get('X-i42AccountToken');
if ($i42AccountToken != $accountToken) {
$arrRet['message'] = 'Unregistered account token!';
return $this->createJsonResponse($arrRet);
}
if (!(
is_array($arrData) &&
isset($arrData['zipcode']) && !empty(trim($arrData['zipcode'])) &&
isset($arrData['distance']) && !empty($arrData['distance']) && ctype_digit((string) $arrData['distance']) &&
isset($arrData['specialization']) && !empty(trim($arrData['specialization'])) &&
isset($arrData['limit']) && !empty($arrData['limit']) && ctype_digit((string) $arrData['limit'])
)) {
$arrRet['message'] = 'Not all necessary data submitted!';
return $this->createJsonResponse($arrRet);
}
$rdblog = new Mdmrdblog();
$rdblog->setZipcode((string) $arrData['zipcode']);
$rdblog->setDistance((string) $arrData['distance']);
$rdblog->setSpecialization((string) $arrData['specialization']);
$rdblog->setCountlimit((string) $arrData['limit']);
$rdblog->setOnlyfemaledoctor((string) $arrData['only_female_doctor']);
$rdblog->setOnlymaledoctor((string) $arrData['only_male_doctor']);
$rdblog->setLegal('NaN');
$rdblog->setPrivate('NaN');
$rdblog->setCreatedAt(new \DateTime());
$legal = false;
if (isset($arrData['legal']))
{
$legal = $arrData['legal'];
$rdblog->setLegal((string) $arrData['legal']);
}
$private = false;
if (isset($arrData['private']))
{
$private = $arrData['private'];
$rdblog->setPrivate((string) $arrData['private']);
}
$em->persist($rdblog);
$em->flush();
$location = $appHelper->getPositionByZipcodeFromGoogle($this->getParameter('gmap_apikey', ''), $arrData['zipcode']);
if (count($location) && isset($location['lat']) && isset($location['lng']))
{
$lat = $location['lat'];
$lng = $location['lng'];
$distance = $arrData['distance'];
$distClmn = '(6371*acos(cos(radians('.$lat.'))*cos(radians(gp.lat))*cos(radians(gp.lng)-radians('.$lng.'))+sin(radians('.$lat.'))*sin(radians(gp.lat)))) AS distance';
$sql = 'SELECT DISTINCT gp.*, '.$distClmn.' FROM gplace AS gp ';
$sql .= 'LEFT JOIN gplace_specialization AS gpspec ON gp.id = gpspec.gplace_id ';
$sql .= 'LEFT JOIN specialization AS spec ON gpspec.specialization_id = spec.id ';
if (isset($arrData['only_female_doctor']) && $arrData['only_female_doctor']) {
$sql .= ' WHERE (gp.gender = "female" OR gp.gender = "female / male") ';
} elseif (isset($arrData['only_male_doctor']) && $arrData['only_male_doctor']) {
$sql .= ' WHERE (gp.gender = "male" OR gp.gender = "female / male") ';
}
$arrSpecs = explode(',', $arrData['specialization']);
if (is_array($arrSpecs)) {
for ($i = 0;$i<count($arrSpecs);$i++){
if ($i === 0){
if ((isset($arrData['only_female_doctor']) && $arrData['only_female_doctor']) || (isset($arrData['only_male_doctor']) && $arrData['only_male_doctor'])){
$sql .= 'AND (gpspec.specialization_id='.$arrSpecs[$i];
} else {
$sql .= 'WHERE (gpspec.specialization_id='.$arrSpecs[$i];
}
} else {
$sql .= ' OR gpspec.specialization_id=' . $arrSpecs[$i];
}
if ($i===count($arrSpecs)-1){
$sql .= ')';
}
}
$sql .= ' Group BY gp.id ';
$sql .= ' HAVING (((distance <= '.$distance.')) AND ((Count(DISTINCT gpspec.specialization_id) = ' . Count($arrSpecs) . ')))';
$sql .= ' ORDER BY distance ASC LIMIT 1,'.$arrData['limit'];
}
$stm = $em->getConnection()->executeQuery($sql);
$arrRess = $stm->fetchAll();
if ($arrRess)
{
foreach ($arrRess as $idx => $res)
{
$arrRet[$idx]['name'] = $res['name'];
$arrRet[$idx]['Fachrichtungen'] = $res['specialization'];
$arrRet[$idx]['Strasse'] = $res['street'].' '.$res['street_nr'];
$arrRet[$idx]['PLZ'] = $res['zipcode'];
if ($res['country'] == 'DE' && strlen($res['zipcode']) == 4)
{
$arrRet[$idx]['PLZ'] = '0'.$res['zipcode'];
}
$arrRet[$idx]['Ort'] = $res['city'];
$arrRet[$idx]['Telefon Nummer'] = $res['phonenumber'];
$arrRet[$idx]['Fax Nummer'] = '';
$arrRet[$idx]['Webseite'] = $res['website'];
if (!$res['website'] || empty($res['website']))
{
$arrRet[$idx]['Webseite'] = "";
}
$arrRet[$idx]['Email'] = '';
$arrRet[$idx]['Öffnungszeiten Montag'] = '';
$arrRet[$idx]['Öffnungszeiten Dienstag'] = '';
$arrRet[$idx]['Öffnungszeiten Mittwoch'] = '';
$arrRet[$idx]['Öffnungszeiten Donnerstag'] = '';
$arrRet[$idx]['Öffnungszeiten Freitag'] = '';
$arrRet[$idx]['Öffnungszeiten Samstag'] = '';
$arrRet[$idx]['Öffnungszeiten Sonntag'] = '';
$arrRet[$idx]['Gesetzlich'] = '';
$arrRet[$idx]['Privat'] = '';
}
}
$arrRet = array('results' => $arrRet);
}
return $this->createJsonResponse($arrRet);
}
/**
* @Route(
* "/api/research_old.json",
* name="api_research_old",
* methods={"POST", "OPTIONS"}
* )
*/
public function researchActionOld(Request $request, AppHelper $appHelper, String $gmapApikey, String $accountToken)
{
$em = $this->getDoctrine()->getManager();
$inputData = $request->getContent();
$arrData = json_decode($inputData, true);
$arrRet = [];
if (!$request->isMethod('POST')) {
$arrRet['message'] = 'Request method is not correct!';
return $this->createJsonResponse($arrRet);
}
if (!$request->headers->has('X-i42AccountToken')) {
$arrRet['message'] = 'Account token is missing!';
return $this->createJsonResponse($arrRet);
}
$i42AccountToken = $request->headers->get('X-i42AccountToken');
if ($i42AccountToken != $accountToken) {
$arrRet['message'] = 'Unregistered account token!';
return $this->createJsonResponse($arrRet);
}
if (!(
is_array($arrData) &&
isset($arrData['zipcode']) && !empty(trim($arrData['zipcode'])) &&
isset($arrData['distance']) && !empty($arrData['distance']) && ctype_digit((string) $arrData['distance']) &&
isset($arrData['specialization']) && !empty(trim($arrData['specialization'])) &&
isset($arrData['limit']) && !empty($arrData['limit']) && ctype_digit((string) $arrData['limit'])
)) {
$arrRet['message'] = 'Not all necessary data submitted!';
return $this->createJsonResponse($arrRet);
}
$rdblog = new Mdmrdblog();
$rdblog->setZipcode((string) $arrData['zipcode']);
$rdblog->setDistance((string) $arrData['distance']);
$rdblog->setSpecialization((string) $arrData['specialization']);
$rdblog->setCountlimit((string) $arrData['limit']);
$rdblog->setOnlyfemaledoctor('NaN');
$rdblog->setOnlymaledoctor('NaN');
$rdblog->setLegal('NaN');
$rdblog->setPrivate('NaN');
$rdblog->setCreatedAt(new \DateTime());
$onlyMaleDoctor = false;
if (isset($arrData['only_male_doctor']))
{
$onlyMaleDoctor = $arrData['only_male_doctor'];
$rdblog->setOnlymaledoctor((string) $arrData['only_male_doctor']);
}
$onlyFemaleDoctor = false;
if (isset($arrData['only_female_doctor']))
{
$onlyFemaleDoctor = $arrData['only_female_doctor'];
$rdblog->setOnlyfemaledoctor((string) $arrData['only_male_doctor']);
}
$legal = false;
if (isset($arrData['legal']))
{
$legal = $arrData['legal'];
$rdblog->setLegal((string) $arrData['legal']);
}
$private = false;
if (isset($arrData['private']))
{
$private = $arrData['private'];
$rdblog->setPrivate((string) $arrData['private']);
}
$em->persist($rdblog);
$em->flush();
$location = $appHelper->getPositionByZipcodeFromGoogle($this->getParameter('gmap_apikey', ''), $arrData['zipcode']);
if (count($location) && isset($location['lat']) && isset($location['lng']))
{
$lat = $location['lat'];
$lng = $location['lng'];
$distance = $arrData['distance'];
$distClmn = '(6371*acos(cos(radians('.$lat.'))*cos(radians(gp.lat))*cos(radians(gp.lng)-radians('.$lng.'))+sin(radians('.$lat.'))*sin(radians(gp.lat)))) AS distance';
$sql = 'SELECT DISTINCT gp.*, '.$distClmn.' FROM gplace AS gp ';
$sql .= 'LEFT JOIN gplace_specialization AS gpspec ON gp.id = gpspec.gplace_id ';
$sql .= 'LEFT JOIN specialization AS spec ON gpspec.specialization_id = spec.id ';
$arrSpecs = explode(',', $arrData['specialization']);
if (!empty($arrData['specialization']))
{
$sql .= 'WHERE gpspec.specialization_id IN ('.implode(',', $arrSpecs).') ';
}
$sql .= 'HAVING distance <= '.$distance.' ';
$sql .= 'ORDER BY distance ASC LIMIT 1,'.$arrData['limit'];
$stm = $em->getConnection()->executeQuery($sql);
$arrRess = $stm->fetchAll();
if ($arrRess)
{
foreach ($arrRess as $idx => $res)
{
$arrRet[$idx]['name'] = $res['name'];
$arrRet[$idx]['Fachrichtungen'] = $res['specialization'];
$arrRet[$idx]['Strasse'] = $res['street'].' '.$res['street_nr'];
$arrRet[$idx]['PLZ'] = $res['zipcode'];
if ($res['country'] == 'DE' && strlen($res['zipcode']) == 4)
{
$arrRet[$idx]['PLZ'] = '0'.$res['zipcode'];
}
$arrRet[$idx]['Ort'] = $res['city'];
$arrRet[$idx]['Telefon Nummer'] = $res['phonenumber'];
$arrRet[$idx]['Fax Nummer'] = '';
$arrRet[$idx]['Webseite'] = $res['website'];
if (!$res['website'] || empty($res['website']))
{
$arrRet[$idx]['Webseite'] = "";
}
$arrRet[$idx]['Email'] = '';
$arrRet[$idx]['Öffnungszeiten Montag'] = '';
$arrRet[$idx]['Öffnungszeiten Dienstag'] = '';
$arrRet[$idx]['Öffnungszeiten Mittwoch'] = '';
$arrRet[$idx]['Öffnungszeiten Donnerstag'] = '';
$arrRet[$idx]['Öffnungszeiten Freitag'] = '';
$arrRet[$idx]['Öffnungszeiten Samstag'] = '';
$arrRet[$idx]['Öffnungszeiten Sonntag'] = '';
$arrRet[$idx]['Gesetzlich'] = '';
$arrRet[$idx]['Privat'] = '';
}
}
$arrRet = array('results' => $arrRet);
}
return $this->createJsonResponse($arrRet);
}
/**
* @Route(
* "/api/gp_type.json",
* name="api_gp_typelist",
* methods={"POST", "OPTIONS"}
* )
*/
public function gp_typelistAction(Request $request, String $accountToken)
{
$em = $this->getDoctrine()->getManager();
$arrRet = [];
if (!$request->isMethod('POST')) {
$arrRet['message'] = 'Request method is not correct!';
return $this->createJsonResponse($arrRet);
}
if (!$request->headers->has('X-i42AccountToken')) {
$arrRet['message'] = 'Account token is missing!';
return $this->createJsonResponse($arrRet);
}
$i42AccountToken = $request->headers->get('X-i42AccountToken');
if ($i42AccountToken != $accountToken) {
$arrRet['message'] = 'Unregistered account token!';
return $this->createJsonResponse($arrRet);
}
$types = $em->getRepository(Gptype::class)->findBy(array(), array('type' => 'ASC'));
foreach ($types as $type)
{
$arrRet[$type->getId()] = $type->getType();
}
return $this->createJsonResponse($arrRet);
}
/**
* @Route(
* "/api/specializationlist.json",
* name="api_specializationlist",
* methods={"POST", "OPTIONS"}
* )
*/
public function specializationlistAction(Request $request, String $accountToken)
{
$em = $this->getDoctrine()->getManager();
$inputData = $request->getContent();
$arrData = json_decode($inputData, true);
$arrRet = [];
if (!$request->isMethod('POST')) {
$arrRet['message'] = 'Request method is not correct!';
return $this->createJsonResponse($arrRet);
}
if (!$request->headers->has('X-i42AccountToken')) {
$arrRet['message'] = 'Account token is missing!';
return $this->createJsonResponse($arrRet);
}
$i42AccountToken = $request->headers->get('X-i42AccountToken');
if ($i42AccountToken != $accountToken) {
$arrRet['message'] = 'Unregistered account token!';
return $this->createJsonResponse($arrRet);
}
$language = 'de';
if (is_array($arrData) && isset($arrData['language']) && $arrData['language'] == 'en')
{
$language = 'en';
}
if ($language == 'en')
{
$specializations = $em->getRepository(Specialization::class)->findBy(array('is_active' => true), array('nameEn' => 'ASC'));
}
else
{
$specializations = $em->getRepository(Specialization::class)->findBy(array('is_active' => true), array('name' => 'ASC'));
}
foreach ($specializations as $specialization)
{
if ($language == 'en')
{
$arrRet[$specialization->getId()] = $specialization->getNameEn();
}
else
{
$arrRet[$specialization->getId()] = $specialization->getName();
}
}
return $this->createJsonResponse($arrRet);
}
/**
* A basic validation of the received request data
*
* @param Request $request
* @return false|array
*/
protected function validateRequest(Request $request, $validateContentType = true, $bodycheck = true)
{
$e = array();
//check content-type
if( $validateContentType && $request->headers->get('Content-Type') != "application/json" )
{
$e[] = $this->createErrorMessage(11, 'invalid content type');
}
//check method
if( $request->getMethod() != Request::METHOD_POST )
{
$e[] = $this->createErrorMessage(12, 'invalid request method');
}
//check i43-header information
if ($request->headers->has('X-i43AccountToken'))
{
$i43AccountToken = $request->headers->get('X-i43AccountToken', '');
if ($i43AccountToken !== 'DE955A86-69FF-42D9-9B77-7F00CD69EF4E')
{
$e[] = $this->createErrorMessage(21, 'auth information fault');
}
}
//check body data
$this->requestData = array();
$inputData = $request->getContent();
if ($bodycheck)
{
if (strlen($inputData) == 0)
{
$e[] = $this->createErrorMessage(13, 'no data found in the body');
}
}
if (strlen($inputData))
{
$this->requestData = json_decode($inputData, true);
if(json_last_error())
{
$e[] = $this->createErrorMessage(17, 'json error: '.json_last_error_msg());
}
}
return $e;
}
protected function createErrorMessage($errorCode, $msg)
{
$data = [];
$data['error']['code'] = $errorCode;
$data['error']['msg'] = $msg;
return $data;
}
protected function createJsonResponse($data)
{
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Credentials', true);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Headers', '*');
if (is_array($data) && count($data))
{
return $response->setContent(json_encode($data));
}
else
{
return $response->setContent('{}');
}
}
/**
* Creates the json from given error list
*
* @param type $errorList
* @return type
*/
protected function createErrorResponse($errorList)
{
return $this->createJsonResponse(array_shift($errorList));
}
}