<?php
namespace Notaris\FrontendBundle\Controller;
use Notaris\AdminBundle\Entity\Notary;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class AddressbookController extends AbstractController
{
public function searchAction(Request $request)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
$manager = $this->getDoctrine()->getManager();
$subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
$slug = 'adresboek';
if ($locale == 'fr') {
$slug = 'annuaire';
}
$subsite = $subsite_repo->getSubsiteBySlugAndLocale($slug, $locale);
$results = [];
$query = $request->query->get('query');
$queryLocation = $request->query->all('queryLocation');
/** @var $notabaseOfficeRepository \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
$notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
if (!empty($query) && !str_contains($query, '%')) {
$results = $notabaseOfficeRepository->getOfficesByName($query, $locale);
} elseif (!empty($queryLocation)) {
$postCodes = [];
foreach ($queryLocation as $location) {
$hasPostcode = preg_match('~[0-9]+~', $location);
if ($hasPostcode) {
$postCodes[] = $location;
} else {
$postCodes = array_merge($postCodes, $notabaseOfficeRepository->getPostCodesByMunicipality($location, $locale));
}
}
$mediators = !empty($request->query->get('mediators'));
$language = $request->query->get('language');
$results = $notabaseOfficeRepository->getOfficesByPostCodes($postCodes, $locale, $mediators, $language);
}
$showOnMap = $request->query->get('show_on_map');
if (!empty($showOnMap)) {
/** @var $notaryRepository \Notaris\AdminBundle\Entity\NotaryRepository */
$notaryRepository = $manager->getRepository('Notaris\AdminBundle\Entity\Notary');
/** @var $svenBuildRepository \Notaris\AdminBundle\Entity\SvenBuildRepository * */
$svenBuildRepository = $manager->getRepository('Notaris\AdminBundle\Entity\SvenBuild');
$officeNumbers = array_column($results, 'officeNumber');
$notaries = $notaryRepository->getNotariesByEeNumbers($officeNumbers);
$results = [];
if (empty($notaries)) {
return $this->render('@NotarisFrontend/addressbook/search.html.twig',
[
'query' => $query,
'subsite' => $subsite,
'results' => $results,
]);
}
foreach ($notaries as $notary) {
$adressen = [];
$infoAddress = new \stdClass();
$infoAddress->type = 'default';
$infoAddress->city = '';
$infoAddress->object = false;
foreach ($notary->getInfo() as $info) {
if ($info->getLocale() == $locale) {
$infoAddress->city = $info->getLocality();
$infoAddress->object = $info;
break;
}
}
$adressen[$notary->getPostal()] = $infoAddress;
/** @var $notary \Notaris\AdminBundle\Entity\Notary */
/** @var $address \Notaris\AdminBundle\Entity\SvenAddress */
foreach ($notary->getAddresses() as $address) {
$infoAddress = new \stdClass();
$infoAddress->type = 'extra';
$infoAddress->city = '';
$infoAddress->object = false;
/** @var $info \Notaris\AdminBundle\Entity\SvenAddressInfo */
foreach ($address->getInfo() as $info) {
if ($info->getLocale() == $locale) {
$infoAddress->city = $info->getCity();
$infoAddress->object = $info;
break;
}
}
$adressen[$address->getZipcode()] = $infoAddress;
}
$currentPostal = $notary->getPostal();
$currentAddress = $adressen[$currentPostal];
$p = $this->makeNotaryObject($notary, $currentPostal, $currentAddress);
$results[] = $p;
if (sizeof($adressen) > 1) {
foreach ($adressen as $key => $address) {
if ($key != $currentPostal) {
$p = $this->makeNotaryObject($notary, $key, $address);
$results[] = $p;
}
}
}
}
return $this->render('@NotarisFrontend/search/notariskaart.html.twig', [
'subsite' => $subsite,
'results' => $results,
]);
}
return $this->render('@NotarisFrontend/addressbook/search.html.twig',
[
'query' => $query,
'subsite' => $subsite,
'results' => $results,
]);
}
public function autocompleteAction(Request $request)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
$manager = $this->getDoctrine()->getManager();
$results = [];
$query = $request->query->get('term');
if (!empty($query) && !str_contains($query, '%')) {
/** @var $notabaseOfficeRepository \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
$notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
$results = $notabaseOfficeRepository->getOfficesByName($query, $locale);
}
return new JsonResponse($results);
}
public function autocompleteByLocationAction(Request $request)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
$manager = $this->getDoctrine()->getManager();
$results = [];
$query = $request->query->get('term');
if (!empty($query) && !str_contains($query, '%')) {
/** @var $notabaseOfficeRepository \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
$notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
$results = is_numeric($query)
? $notabaseOfficeRepository->getMunicipalitiesByPostCode($query, $locale)
: $notabaseOfficeRepository->getMunicipalitiesByAddress($query, $locale);
}
return new JsonResponse($results);
}
private function makeNotaryObject(Notary $notary, $currentPostal, $currentAddress)
{
$notaryObject = new \stdClass();
$notaryObject->type = 'notary';
$notaryObject->name = $notary->getName();
$notaryObject->slug = $notary->getSlug();
$notaryObject->id = 'n_' . $notary->getId();
$notaryObject->city = $currentAddress->city;
if ($currentAddress->type == 'default') {
$notaryObject->lat = $notary->getLat();
$notaryObject->lng = $notary->getLng();
$notaryObject->address = $notary->getStreet();
$notaryObject->address .= ' ' . $notary->getNumber();
if ($notary->getBox() != '') {
$notaryObject->address .= ' /' . $notary->getBox();
}
$notaryObject->address .= ', ' . $notary->getPostal();
$notaryObject->address .= ' ' . $notaryObject->city;
$notaryObject->tel = $notary->getPhone();
$notaryObject->fax = $notary->getFax();
} else {
$adresinfo = $currentAddress->object;
/** @var $adresinfo \Notaris\AdminBundle\Entity\SvenAddressInfo */
if ($adresinfo) {
$latlng = explode(',', $adresinfo->getAddress()->getLatlng());
if (sizeof($latlng) > 1) {
$notaryObject->lat = $latlng[0];
$notaryObject->lng = $latlng[1];
} else {
$notaryObject->lat = '';
$notaryObject->lng = '';
}
$notaryObject->address = $adresinfo->getStreet();
$notaryObject->address .= ' ' . $adresinfo->getAddress()->getNumber();
$notaryObject->address .= ', ' . $currentPostal;
$notaryObject->address .= ' ' . $adresinfo->getCity();
$notaryObject->tel = $adresinfo->getAddress()->getTel();
$notaryObject->fax = $adresinfo->getAddress()->getFax();
}
}
$notaryObject->web = '';
foreach ($notary->getDomains() as $domain) {
if ($domain->getIsMain()) {
$notaryObject->web = $domain->getUrl();
}
}
return $notaryObject;
}
public function searchDeedAction(Request $request)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
$manager = $this->getDoctrine()->getManager();
$subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
$slug = 'adresboek';
if ($locale == 'fr') {
$slug = 'annuaire';
}
$subsite = $subsite_repo->getSubsiteBySlugAndLocale($slug, $locale);
$query = $request->query->get('query');
$queryLocation = $request->query->all('queryLocation');
$queryLocationCustom = $request->query->get('query-location');
$queryYearMin = $request->query->get('query-year-min');
$queryYearMax = $request->query->get('query-year-max');
$results = [];
$processedResults = [];
if (!empty($queryLocationCustom) && empty($queryLocation)) {
return $this->render('@NotarisFrontend/addressbook/search_deed.html.twig',
[
'query' => $query,
'subsite' => $subsite,
'results' => $processedResults,
]);
}
if (!empty($queryYearMin) && !empty($queryYearMax)) {
/** @var $notabasePeriodRepository \Notaris\AdminBundle\Entity\NotabasePeriodRepository */
$notabasePeriodRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabasePeriod');
/** @var $notabaseOfficeRepository \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
$notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
$periods = $notabasePeriodRepository->createQueryBuilder('p')
->select('p, pe.slug')
->leftJoin('NotarisAdminBundle:Person', 'pe', 'WITH', 'pe.rbrnotId = p.personNumber')
->where('(p.addressStartDate > :queryYearMin OR p.addressEndDate > :queryYearMin OR p.addressEndDate IS NULL) AND (p.addressStartDate < :queryYearMax OR p.addressEndDate < :queryYearMax)')
->setParameter('queryYearMin', $queryYearMin . '-01-01')
->setParameter('queryYearMax', $queryYearMax . '-12-31');
if (!empty($query)) {
$periods = $periods->andWhere('p.firstName LIKE :name OR p.lastName LIKE :name OR CONCAT(p.firstName, \' \', p.lastName) LIKE :name OR CONCAT(p.lastName, \' \', p.firstName) LIKE :name')
->setParameter('name', '%' . $query . '%');
}
if (!empty($queryLocation)) {
$locale = strtoupper($locale);
$where = [];
foreach ($queryLocation as $i => $location) {
$hasPostcode = preg_match('~[0-9]+~', $location);
if ($hasPostcode) {
$municipality = $location;
} else {
$municipality = $notabaseOfficeRepository->getPostCodesByMunicipality($location, $locale);
}
$query = $manager->createQuery("
SELECT DISTINCT ld.nis
FROM Notaris\AdminBundle\Entity\NotabaseLocalityData ld
WHERE ld.name{$locale}Part1 IN (:postCodes)
")->setParameter('postCodes', $municipality);
$results = $query->getResult();
if (empty($results) || empty($results[0]['nis'])) {
continue;
}
if ($hasPostcode) {
$postCode = substr($location, 0, 4);
$where[] = "p.postCode = :postCode$i AND p.nis = :nis$i ";
$periods = $periods->setParameter('nis' . $i, $results[0]['nis'])
->setParameter('postCode' . $i, $postCode);
} else {
$where[] = " p.nis = :nis$i ";
$periods = $periods->setParameter('nis' . $i, $results[0]['nis']);
}
}
$where = implode(' OR ', $where);
$periods = $periods->andWhere($where);
}
$results = $periods->groupBy('p.personNumber')
->getQuery()
->getResult();
$processedResults = [];
foreach ($results as $result) {
$processedResult = $result[0];
$processedResult->slug = $result['slug'];
$periodLocations = $notabasePeriodRepository->createQueryBuilder('p')
->select('p, CASE WHEN p.addressEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull')
->where('p.personNumber = :personNumber')
->setParameter('personNumber', $processedResult->getPersonNumber())
->orderBy('_isFieldNull', 'DESC')
->addOrderBy('p.postCode')
->getQuery()
->getResult();
$processedResult->location = '';
$processedResult->orderPostCode = '';
$processedResult->orderLocality = '';
if (!empty($periodLocations[0])) {
$processedResult->orderPostCode = $periodLocations[0]->getPostCode();
$processedResult->orderLocality = $locale === 'nl' ? $periodLocations[0]->getLocalityNL() : $periodLocations[0]->getLocalityFR();
}
$locations = [];
foreach ($periodLocations as $periodLocation) {
$locations[] = $periodLocation->getPostCode() . ' ' . ($locale === 'nl' ? $periodLocation->getLocalityNL() : $periodLocation->getLocalityFR());
}
$locations = array_values(array_unique($locations));
$processedResult->location = implode(' - ', $locations);
$processedResults[] = $processedResult;
}
}
return $this->render('@NotarisFrontend/addressbook/search_deed.html.twig',
[
'query' => $query,
'subsite' => $subsite,
'results' => $processedResults,
]);
}
public function notaryAction(Request $request, $personSlug)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
$manager = $this->getDoctrine()->getManager();
$subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
$slug = 'adresboek';
if ($locale == 'fr') {
$slug = 'annuaire';
}
$subsite = $subsite_repo->getSubsiteBySlugAndLocale($slug, $locale);
/** @var $notabasePeriodRepository \Notaris\AdminBundle\Entity\NotabasePeriodRepository */
$notabasePeriodRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabasePeriod');
/** @var $personRepository \Notaris\AdminBundle\Entity\Person */
$personRepository = $manager->getRepository('Notaris\AdminBundle\Entity\Person');
$person = $personRepository->findOneBySlug($personSlug);
if (!$person) {
throw $this->createNotFoundException('This Notary does not exist');
}
$personNumber = $person->getRbrnotId();
$periods = $notabasePeriodRepository->createQueryBuilder('p')
->select('p, CASE WHEN p.addressEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull')
->where('p.personNumber = :personNumber AND p.minuteholderLastName IS NOT NULL')
->setParameter('personNumber', $personNumber)
->groupBy('p.officeNumber, p.addressStartDate, p.replacedNumber')
->orderBy('_isFieldNull', 'DESC')
->addOrderBy('p.addressEndDate', 'DESC')
->getQuery()
->getResult();
foreach ($periods as $period) {
$period->minuteholderSlug = null;
if (!empty($period->getMinuteholderOfficeNumber())) {
$notaryRepository = $manager->getRepository('Notaris\AdminBundle\Entity\Notary');
$notary = $notaryRepository->findOneByEe($period->getMinuteholderOfficeNumber());
if (!empty($notary)) {
$period->minuteholderSlug = $notary->getSlug();
}
}
}
//backlink
$backlink = $request->headers->get('referer');
$host = parse_url($backlink, PHP_URL_HOST);
if ($backlink == '' or $request->getHttpHost() != $host) {
$backlink = $this->generateUrl('notaris_frontend_addressbook');
}
return $this->render('@NotarisFrontend/addressbook/notary.html.twig',
[
'periods' => $periods,
'subsite' => $subsite,
'personSlug' => $personSlug,
'backlink' => $backlink
]);
}
}