src/Notaris/FrontendBundle/Controller/AddressbookController.php line 13

Open in your IDE?
  1. <?php
  2. namespace Notaris\FrontendBundle\Controller;
  3. use Notaris\AdminBundle\Entity\Notary;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class AddressbookController extends AbstractController
  8. {
  9.     public function searchAction(Request $request)
  10.     {
  11.         $localeService = $this->get('notaris.locale.listener');
  12.         $locale = $localeService->getLocale();
  13.         $manager = $this->getDoctrine()->getManager();
  14.         $subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
  15.         $slug = 'adresboek';
  16.         if ($locale == 'fr') {
  17.             $slug = 'annuaire';
  18.         }
  19.         $subsite = $subsite_repo->getSubsiteBySlugAndLocale($slug, $locale);
  20.         $results = [];
  21.         $query = $request->query->get('query');
  22.         $queryLocation = $request->query->all('queryLocation');
  23.         /** @var  $notabaseOfficeRepository  \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
  24.         $notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
  25.         if (!empty($query) && !str_contains($query, '%')) {
  26.             $results = $notabaseOfficeRepository->getOfficesByName($query, $locale);
  27.         } elseif (!empty($queryLocation)) {
  28.             $postCodes = [];
  29.             foreach ($queryLocation as $location) {
  30.                 $hasPostcode = preg_match('~[0-9]+~', $location);
  31.                 if ($hasPostcode) {
  32.                     $postCodes[] = $location;
  33.                 } else {
  34.                     $postCodes = array_merge($postCodes, $notabaseOfficeRepository->getPostCodesByMunicipality($location, $locale));
  35.                 }
  36.             }
  37.             $mediators = !empty($request->query->get('mediators'));
  38.             $language = $request->query->get('language');
  39.             $results = $notabaseOfficeRepository->getOfficesByPostCodes($postCodes, $locale, $mediators, $language);
  40.         }
  41.         $showOnMap = $request->query->get('show_on_map');
  42.         if (!empty($showOnMap)) {
  43.             /** @var  $notaryRepository \Notaris\AdminBundle\Entity\NotaryRepository */
  44.             $notaryRepository = $manager->getRepository('Notaris\AdminBundle\Entity\Notary');
  45.             /** @var $svenBuildRepository \Notaris\AdminBundle\Entity\SvenBuildRepository * */
  46.             $svenBuildRepository = $manager->getRepository('Notaris\AdminBundle\Entity\SvenBuild');
  47.             $officeNumbers = array_column($results, 'officeNumber');
  48.             $notaries = $notaryRepository->getNotariesByEeNumbers($officeNumbers);
  49.             $results = [];
  50.             if (empty($notaries)) {
  51.                 return $this->render('@NotarisFrontend/addressbook/search.html.twig',
  52.                     [
  53.                     'query' => $query,
  54.                     'subsite' => $subsite,
  55.                     'results' => $results,
  56.                 ]);
  57.             }
  58.             foreach ($notaries as $notary) {
  59.                 $adressen = [];
  60.                 $infoAddress = new \stdClass();
  61.                 $infoAddress->type = 'default';
  62.                 $infoAddress->city = '';
  63.                 $infoAddress->object = false;
  64.                 foreach ($notary->getInfo() as $info) {
  65.                     if ($info->getLocale() == $locale) {
  66.                         $infoAddress->city = $info->getLocality();
  67.                         $infoAddress->object = $info;
  68.                         break;
  69.                     }
  70.                 }
  71.                 $adressen[$notary->getPostal()] = $infoAddress;
  72.                 /** @var  $notary \Notaris\AdminBundle\Entity\Notary */
  73.                 /** @var  $address \Notaris\AdminBundle\Entity\SvenAddress */
  74.                 foreach ($notary->getAddresses() as $address) {
  75.                     $infoAddress = new \stdClass();
  76.                     $infoAddress->type = 'extra';
  77.                     $infoAddress->city = '';
  78.                     $infoAddress->object = false;
  79.                     /** @var  $info \Notaris\AdminBundle\Entity\SvenAddressInfo */
  80.                     foreach ($address->getInfo() as $info) {
  81.                         if ($info->getLocale() == $locale) {
  82.                             $infoAddress->city = $info->getCity();
  83.                             $infoAddress->object = $info;
  84.                             break;
  85.                         }
  86.                     }
  87.                     $adressen[$address->getZipcode()] = $infoAddress;
  88.                 }
  89.                 $currentPostal = $notary->getPostal();
  90.                 $currentAddress = $adressen[$currentPostal];
  91.                 $p = $this->makeNotaryObject($notary, $currentPostal, $currentAddress);
  92.                 $results[] = $p;
  93.                 if (sizeof($adressen) > 1) {
  94.                     foreach ($adressen as $key => $address) {
  95.                         if ($key != $currentPostal) {
  96.                             $p = $this->makeNotaryObject($notary, $key, $address);
  97.                             $results[] = $p;
  98.                         }
  99.                     }
  100.                 }
  101.             }
  102.             return $this->render('@NotarisFrontend/search/notariskaart.html.twig', [
  103.                 'subsite' => $subsite,
  104.                 'results' => $results,
  105.             ]);
  106.         }
  107.         return $this->render('@NotarisFrontend/addressbook/search.html.twig',
  108.             [
  109.             'query' => $query,
  110.             'subsite' => $subsite,
  111.             'results' => $results,
  112.         ]);
  113.     }
  114.     public function autocompleteAction(Request $request)
  115.     {
  116.         $localeService = $this->get('notaris.locale.listener');
  117.         $locale = $localeService->getLocale();
  118.         $manager = $this->getDoctrine()->getManager();
  119.         $results = [];
  120.         $query = $request->query->get('term');
  121.         if (!empty($query) && !str_contains($query, '%')) {
  122.             /** @var  $notabaseOfficeRepository  \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
  123.             $notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
  124.             $results = $notabaseOfficeRepository->getOfficesByName($query, $locale);
  125.         }
  126.         return new JsonResponse($results);
  127.     }
  128.     public function autocompleteByLocationAction(Request $request)
  129.     {
  130.         $localeService = $this->get('notaris.locale.listener');
  131.         $locale = $localeService->getLocale();
  132.         $manager = $this->getDoctrine()->getManager();
  133.         $results = [];
  134.         $query = $request->query->get('term');
  135.         if (!empty($query) && !str_contains($query, '%')) {
  136.             /** @var  $notabaseOfficeRepository  \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
  137.             $notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
  138.             $results = is_numeric($query)
  139.                 ? $notabaseOfficeRepository->getMunicipalitiesByPostCode($query, $locale)
  140.                 : $notabaseOfficeRepository->getMunicipalitiesByAddress($query, $locale);
  141.         }
  142.         return new JsonResponse($results);
  143.     }
  144.     private function makeNotaryObject(Notary $notary, $currentPostal, $currentAddress)
  145.     {
  146.         $notaryObject = new \stdClass();
  147.         $notaryObject->type = 'notary';
  148.         $notaryObject->name = $notary->getName();
  149.         $notaryObject->slug = $notary->getSlug();
  150.         $notaryObject->id = 'n_' . $notary->getId();
  151.         $notaryObject->city = $currentAddress->city;
  152.         if ($currentAddress->type == 'default') {
  153.             $notaryObject->lat = $notary->getLat();
  154.             $notaryObject->lng = $notary->getLng();
  155.             $notaryObject->address = $notary->getStreet();
  156.             $notaryObject->address .= ' ' . $notary->getNumber();
  157.             if ($notary->getBox() != '') {
  158.                 $notaryObject->address .= ' /' . $notary->getBox();
  159.             }
  160.             $notaryObject->address .= ', ' . $notary->getPostal();
  161.             $notaryObject->address .= ' ' . $notaryObject->city;
  162.             $notaryObject->tel = $notary->getPhone();
  163.             $notaryObject->fax = $notary->getFax();
  164.         } else {
  165.             $adresinfo = $currentAddress->object;
  166.             /** @var  $adresinfo \Notaris\AdminBundle\Entity\SvenAddressInfo */
  167.             if ($adresinfo) {
  168.                 $latlng = explode(',', $adresinfo->getAddress()->getLatlng());
  169.                 if (sizeof($latlng) > 1) {
  170.                     $notaryObject->lat = $latlng[0];
  171.                     $notaryObject->lng = $latlng[1];
  172.                 } else {
  173.                     $notaryObject->lat = '';
  174.                     $notaryObject->lng = '';
  175.                 }
  176.                 $notaryObject->address = $adresinfo->getStreet();
  177.                 $notaryObject->address .= ' ' . $adresinfo->getAddress()->getNumber();
  178.                 $notaryObject->address .= ', ' . $currentPostal;
  179.                 $notaryObject->address .= ' ' . $adresinfo->getCity();
  180.                 $notaryObject->tel = $adresinfo->getAddress()->getTel();
  181.                 $notaryObject->fax = $adresinfo->getAddress()->getFax();
  182.             }
  183.         }
  184.         $notaryObject->web = '';
  185.         foreach ($notary->getDomains() as $domain) {
  186.             if ($domain->getIsMain()) {
  187.                 $notaryObject->web = $domain->getUrl();
  188.             }
  189.         }
  190.         return $notaryObject;
  191.     }
  192.     public function searchDeedAction(Request $request)
  193.     {
  194.         $localeService = $this->get('notaris.locale.listener');
  195.         $locale = $localeService->getLocale();
  196.         $manager = $this->getDoctrine()->getManager();
  197.         $subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
  198.         $slug = 'adresboek';
  199.         if ($locale == 'fr') {
  200.             $slug = 'annuaire';
  201.         }
  202.         $subsite = $subsite_repo->getSubsiteBySlugAndLocale($slug, $locale);
  203.         $query = $request->query->get('query');
  204.         $queryLocation = $request->query->all('queryLocation');
  205.         $queryLocationCustom = $request->query->get('query-location');
  206.         $queryYearMin = $request->query->get('query-year-min');
  207.         $queryYearMax = $request->query->get('query-year-max');
  208.         $results = [];
  209.         $processedResults = [];
  210.         if (!empty($queryLocationCustom) && empty($queryLocation)) {
  211.             return $this->render('@NotarisFrontend/addressbook/search_deed.html.twig',
  212.                 [
  213.                 'query' => $query,
  214.                 'subsite' => $subsite,
  215.                 'results' => $processedResults,
  216.             ]);
  217.         }
  218.         if (!empty($queryYearMin) && !empty($queryYearMax)) {
  219.             /** @var  $notabasePeriodRepository  \Notaris\AdminBundle\Entity\NotabasePeriodRepository */
  220.             $notabasePeriodRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabasePeriod');
  221.             /** @var  $notabaseOfficeRepository  \Notaris\AdminBundle\Entity\NotabaseOfficeRepository */
  222.             $notabaseOfficeRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabaseOffice');
  223.             $periods = $notabasePeriodRepository->createQueryBuilder('p')
  224.                 ->select('p, pe.slug')
  225.                 ->leftJoin('NotarisAdminBundle:Person', 'pe', 'WITH', 'pe.rbrnotId = p.personNumber')
  226.                 ->where('(p.addressStartDate > :queryYearMin OR p.addressEndDate > :queryYearMin OR p.addressEndDate IS NULL) AND (p.addressStartDate < :queryYearMax OR p.addressEndDate < :queryYearMax)')
  227.                 ->setParameter('queryYearMin', $queryYearMin . '-01-01')
  228.                 ->setParameter('queryYearMax', $queryYearMax . '-12-31');
  229.             if (!empty($query)) {
  230.                 $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')
  231.                     ->setParameter('name', '%' . $query . '%');
  232.             }
  233.             if (!empty($queryLocation)) {
  234.                 $locale = strtoupper($locale);
  235.                 $where = [];
  236.                 foreach ($queryLocation as $i => $location) {
  237.                     $hasPostcode = preg_match('~[0-9]+~', $location);
  238.                     if ($hasPostcode) {
  239.                         $municipality = $location;
  240.                     } else {
  241.                         $municipality = $notabaseOfficeRepository->getPostCodesByMunicipality($location, $locale);
  242.                     }
  243.                     $query = $manager->createQuery("
  244.                         SELECT DISTINCT ld.nis
  245.                         FROM Notaris\AdminBundle\Entity\NotabaseLocalityData ld
  246.                         WHERE ld.name{$locale}Part1 IN (:postCodes)
  247.                     ")->setParameter('postCodes', $municipality);
  248.                     $results = $query->getResult();
  249.                     if (empty($results) || empty($results[0]['nis'])) {
  250.                         continue;
  251.                     }
  252.                     if ($hasPostcode) {
  253.                         $postCode = substr($location, 0, 4);
  254.                         $where[] = "p.postCode = :postCode$i AND p.nis = :nis$i ";
  255.                         $periods = $periods->setParameter('nis' . $i, $results[0]['nis'])
  256.                             ->setParameter('postCode' . $i, $postCode);
  257.                     } else {
  258.                         $where[] = " p.nis = :nis$i ";
  259.                         $periods = $periods->setParameter('nis' . $i, $results[0]['nis']);
  260.                     }
  261.                 }
  262.                 $where = implode(' OR ', $where);
  263.                 $periods = $periods->andWhere($where);
  264.             }
  265.             $results = $periods->groupBy('p.personNumber')
  266.                 ->getQuery()
  267.                 ->getResult();
  268.             $processedResults = [];
  269.             foreach ($results as $result) {
  270.                 $processedResult = $result[0];
  271.                 $processedResult->slug = $result['slug'];
  272.                 $periodLocations = $notabasePeriodRepository->createQueryBuilder('p')
  273.                     ->select('p, CASE WHEN p.addressEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull')
  274.                     ->where('p.personNumber = :personNumber')
  275.                     ->setParameter('personNumber', $processedResult->getPersonNumber())
  276.                     ->orderBy('_isFieldNull', 'DESC')
  277.                     ->addOrderBy('p.postCode')
  278.                     ->getQuery()
  279.                     ->getResult();
  280.                 $processedResult->location = '';
  281.                 $processedResult->orderPostCode = '';
  282.                 $processedResult->orderLocality = '';
  283.                 if (!empty($periodLocations[0])) {
  284.                     $processedResult->orderPostCode = $periodLocations[0]->getPostCode();
  285.                     $processedResult->orderLocality = $locale === 'nl' ? $periodLocations[0]->getLocalityNL() : $periodLocations[0]->getLocalityFR();
  286.                 }
  287.                 $locations = [];
  288.                 foreach ($periodLocations as $periodLocation) {
  289.                     $locations[] = $periodLocation->getPostCode() . ' ' . ($locale === 'nl' ? $periodLocation->getLocalityNL() : $periodLocation->getLocalityFR());
  290.                 }
  291.                 $locations = array_values(array_unique($locations));
  292.                 $processedResult->location = implode(' - ', $locations);
  293.                 $processedResults[] = $processedResult;
  294.             }
  295.         }
  296.         return $this->render('@NotarisFrontend/addressbook/search_deed.html.twig',
  297.             [
  298.             'query' => $query,
  299.             'subsite' => $subsite,
  300.             'results' => $processedResults,
  301.         ]);
  302.     }
  303.     public function notaryAction(Request $request, $personSlug)
  304.     {
  305.         $localeService = $this->get('notaris.locale.listener');
  306.         $locale = $localeService->getLocale();
  307.         $manager = $this->getDoctrine()->getManager();
  308.         $subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
  309.         $slug = 'adresboek';
  310.         if ($locale == 'fr') {
  311.             $slug = 'annuaire';
  312.         }
  313.         $subsite = $subsite_repo->getSubsiteBySlugAndLocale($slug, $locale);
  314.         /** @var  $notabasePeriodRepository  \Notaris\AdminBundle\Entity\NotabasePeriodRepository */
  315.         $notabasePeriodRepository = $manager->getRepository('Notaris\AdminBundle\Entity\NotabasePeriod');
  316.         /** @var  $personRepository  \Notaris\AdminBundle\Entity\Person */
  317.         $personRepository = $manager->getRepository('Notaris\AdminBundle\Entity\Person');
  318.         $person = $personRepository->findOneBySlug($personSlug);
  319.         if (!$person) {
  320.             throw $this->createNotFoundException('This Notary does not exist');
  321.         }
  322.         $personNumber = $person->getRbrnotId();
  323.         $periods = $notabasePeriodRepository->createQueryBuilder('p')
  324.             ->select('p, CASE WHEN p.addressEndDate IS NULL THEN 1 ELSE 0 END HIDDEN _isFieldNull')
  325.             ->where('p.personNumber = :personNumber AND p.minuteholderLastName IS NOT NULL')
  326.             ->setParameter('personNumber', $personNumber)
  327.             ->groupBy('p.officeNumber, p.addressStartDate, p.replacedNumber')
  328.             ->orderBy('_isFieldNull', 'DESC')
  329.             ->addOrderBy('p.addressEndDate', 'DESC')
  330.             ->getQuery()
  331.             ->getResult();
  332.         foreach ($periods as $period) {
  333.             $period->minuteholderSlug = null;
  334.             if (!empty($period->getMinuteholderOfficeNumber())) {
  335.                 $notaryRepository = $manager->getRepository('Notaris\AdminBundle\Entity\Notary');
  336.                 $notary = $notaryRepository->findOneByEe($period->getMinuteholderOfficeNumber());
  337.                 if (!empty($notary)) {
  338.                     $period->minuteholderSlug = $notary->getSlug();
  339.                 }
  340.             }
  341.         }
  342.         //backlink
  343.         $backlink = $request->headers->get('referer');
  344.         $host = parse_url($backlink, PHP_URL_HOST);
  345.         if ($backlink == '' or $request->getHttpHost() != $host) {
  346.             $backlink = $this->generateUrl('notaris_frontend_addressbook');
  347.         }
  348.         return $this->render('@NotarisFrontend/addressbook/notary.html.twig',
  349.             [
  350.             'periods' => $periods,
  351.             'subsite' => $subsite,
  352.             'personSlug' => $personSlug,
  353.             'backlink' => $backlink
  354.         ]);
  355.     }
  356. }