src/AppBundle/Controller/SearchController.php line 222

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Doctrine\ORM\Query\Expr\Func;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use AppBundle\Includes\Database;
  10. use AppBundle\Includes\Settings;
  11. use AppBundle\Includes\Helper;
  12. use AppBundle\Includes\Tracker;
  13. use AppBundle\Includes\Users;
  14. use AppBundle\Includes\Functions;
  15. use AppBundle\Includes\Constants;
  16. class SearchController extends AbstractController
  17. {
  18.     /*
  19.      * @Route("/{one_type}/", name="search_results_comm_type_only", methods={"GET", "POST"})
  20.      */
  21.     /* TODO: disabled this
  22.     public function resultsCommTypeOnlyAction(Request $request, $one_type)
  23.     {
  24.         if (in_array($one_type, Constants::all_url_types())) {
  25.             if (in_array($one_type, Constants::all_url_types_com())) {
  26.                 $type = 'entreprises-commerces';
  27.             } elseif (in_array($one_type, Constants::all_url_types_murs())) {
  28.                 $type = 'murs-locaux-terrains';
  29.             } else {
  30.                 throw $this->createNotFoundException('PAGE NON TROUVÉE');
  31.             }
  32.             $commerce_type = $one_type;
  33.             $region = Constants::generic_region;
  34.             $dep = Constants::generic_departement;
  35.         } else {
  36.             throw $this->createNotFoundException('PAGE NON TROUVÉE');
  37.         }
  38.         $response = $this->forward('AppBundle:Search:results',
  39.             array(
  40.                 'type' => $type,
  41.                 'region' => $region,
  42.                 'departement' => $dep,
  43.                 'commerce_type' => $commerce_type,
  44.                 '_route' => 'search_results',
  45.                 '_route_params' => array_merge($request->attributes->get('_route_params'),
  46.                     array('type' => $type,
  47.                         'commerce_type' => $commerce_type,
  48.                         'region' => $region,
  49.                         'departement' => $dep,)
  50.                 )),
  51.             $request->query->all());
  52.         #'_route' => $request->attributes->get('_route'),
  53.         #'_route_params' => $request->attributes->get('_route_params')
  54.         return $response;
  55.     }*/
  56.     /**
  57.      * @Route("/{type}/",
  58.      * requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
  59.      * name="search_results_type_only",
  60.      * methods={"GET", "POST"})
  61.      */
  62.     public function resultsTypeOnlyAction (Request $request$type)
  63.     {
  64.         $type_new '';
  65.         if ($type == 'entreprises-commerces') {
  66.             $type_new 'vente';
  67.         } elseif ($type == 'murs-locaux-terrains') {
  68.             $type_new 'tout';
  69.         }
  70.         $dep Constants::generic_departement;
  71.         $commerce_type Constants::generic_commerce_type;
  72.         $response $this->forward('AppBundle:Search:results',
  73.             array(
  74.                 'type' => $type_new,
  75.                 'departement' => $dep,
  76.                 'commerce_type' => $commerce_type,
  77.                 '_route' => 'search_results',
  78.                 '_route_params' => array_merge($request->attributes->get('_route_params'),
  79.                     array('type' => $type_new,
  80.                         'commerce_type' => $commerce_type,
  81.                         'departement' => $dep,)
  82.                 )),
  83.             $request->query->all());
  84.         return $response;
  85.     }
  86.     /**
  87.      * @Route("/{type}/{one_type}/",
  88.      * requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
  89.      * name="search_results_comm_type",
  90.      * methods={"GET", "POST"})
  91.      */
  92.     public function resultsCommTypeAction(Request $request$type$one_type)
  93.     {
  94.         $type_new '';
  95.         if ($type == 'entreprises-commerces') {
  96.             $type_new 'vente';
  97.         } elseif ($type == 'murs-locaux-terrains') {
  98.             $type_new 'tout';
  99.         }
  100.         if (in_array($one_typeConstants::all_url_types())) {
  101.             $commerce_type $one_type;
  102.             $region Constants::generic_region;
  103.             $dep Constants::generic_departement;
  104.         } elseif (in_array($one_typeConstants::all_url_regions())) {
  105.             $commerce_type Constants::generic_commerce_type;
  106.             $region $one_type;
  107.             $dep Constants::generic_departement;
  108.         } else {
  109.             throw $this->createNotFoundException('PAGE NON TROUVÉE');
  110.         }
  111.         $response $this->forward('AppBundle:Search:results',
  112.             array(
  113.                 'type' => $type_new,
  114.                 'departement' => $dep,
  115.                 'commerce_type' => $commerce_type,
  116.                 '_route' => 'search_results',
  117.                 '_route_params' => array_merge($request->attributes->get('_route_params'),
  118.                     array('type' => $type_new,
  119.                         'commerce_type' => $commerce_type,
  120.                         'departement' => $dep,)
  121.                 )),
  122.             $request->query->all());
  123.         return $response;
  124.     }
  125.     # Handle region/department
  126.     /**
  127.      * @Route("/{type}/{region}/{departement}/",
  128.      * requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
  129.      * name="search_results_dep_type",
  130.      * methods={"GET", "POST"})
  131.      */
  132.     public function resultsDepTypeAction(Request $request$type$region$departement)
  133.     {
  134.         if (in_array($regionConstants::all_url_regions()) && in_array($departementConstants::all_url_deps())) {
  135.             $commerce_type Constants::generic_commerce_type;
  136.             $region $region;
  137.             $dep $departement;
  138.         } else {
  139.             throw $this->createNotFoundException('PAGE NON TROUVÉE');
  140.         }
  141.         $response $this->forward('AppBundle:Search:results',
  142.             array(
  143.                 'type' => $type,
  144.                 'region' => $region,
  145.                 'departement' => $dep,
  146.                 'commerce_type' => $commerce_type,
  147.                 '_route' => 'search_results',
  148.                 '_route_params' => array_merge($request->attributes->get('_route_params'),
  149.                     array('type' => $type,
  150.                         'commerce_type' => $commerce_type,
  151.                         'region' => $region,
  152.                         'departement' => $dep,)
  153.                 )),
  154.             $request->query->all());
  155.         return $response;
  156.     }
  157.     /**
  158.      * @Route("/{type}/{region}/{departement}/{commerce_type}/",
  159.      * requirements={"type": "entreprises-commerces|murs-locaux-terrains"},
  160.      * name="search_results_old",
  161.      * methods={"GET", "POST"})
  162.      */
  163.     public function resultsOldAction(Request $request$type$region$departement$commerce_type)
  164.     {
  165.         $type_new '';
  166.         if ($type == 'entreprises-commerces') {
  167.             $type_new 'vente';
  168.         } elseif ($type == 'murs-locaux-terrains') {
  169.             if (!empty($_GET['transaction'])) {
  170.                 if ($_GET['transaction'] == 'vente') {
  171.                     $type_new 'vente-immobilier';
  172.                 } elseif ($_GET['transaction'] == 'location') {
  173.                     $type_new 'location';
  174.                 } elseif ($_GET['transaction'] == 'cession-de-droit-au-bail') {
  175.                     $type_new 'opportunites-investisseurs';
  176.                 } elseif ($_GET['transaction'] == 'tout') {
  177.                     $type_new 'tout';
  178.                 }
  179.             } else {
  180.                 $type_new 'tout';
  181.             }
  182.         }
  183.         # Remove default departement
  184.         if ($departement == Constants::generic_departement) {
  185.             $departement null;
  186.         }
  187.         # This should redirect to new
  188.         $input = [
  189.             'type' => $type_new,
  190.             'commerce_type' => $commerce_type,
  191.             'departement' => $departement
  192.         ];
  193.         return $this->redirectToRoute('search_results'$input301);
  194.     }
  195.     /**
  196.      * @Route("/{type}/{commerce_type}/{departement}",
  197.      * requirements={"type": "vente|vente-immobilier|location|opportunites-investisseurs|tout"},
  198.      * defaults={"commerce_type"=null, "departement"=null},
  199.      * name="search_results",
  200.      * methods={"GET", "POST"})
  201.      */
  202.     public function resultsAction(Request $request$type$commerce_type=null$departement=null)
  203.     {
  204.         # Instant redirect /vente/commerce_type/departement -> remove departement
  205.         if ($departement == Constants::generic_departement && $commerce_type != null) {
  206.             $input_red = [
  207.                 'type' => $type,
  208.                 'commerce_type' => $commerce_type,
  209.                 'departement' => null
  210.             ];
  211.             return $this->redirectToRoute('search_results'$input_red301);
  212.         } elseif ($departement == Constants::generic_departement && $commerce_type == null) {
  213.             $input_red = [
  214.                 'type' => $type,
  215.                 'commerce_type' => null,
  216.                 'departement' => null
  217.             ];
  218.             return $this->redirectToRoute('search_results'$input_red301);
  219.         }
  220.         if ($commerce_type == Constants::generic_commerce_type && !$departement) {
  221.             $input_red = [
  222.                 'type' => $type,
  223.                 'commerce_type' => null,
  224.                 'departement' => null
  225.             ];
  226.             return $this->redirectToRoute('search_results'$input_red301);
  227.         }
  228.         # Handle the case of optional commerce_type
  229.         if (!$commerce_type) {
  230.             $commerce_type Constants::generic_commerce_type;
  231.         }
  232.         $users = new Users();
  233.         $helper = new Helper($this->get('router'));
  234.         # This gets $_GET params
  235.         $get_params $request->query->all();
  236.         $session $request->getSession();
  237.         # Changed links
  238.         if ($commerce_type == 'boutiques-diverses-pret-a-porter-chaussures') {
  239.             $input array_merge(
  240.                 Array('type' => $type,
  241.                     'departement' => $departement,
  242.                     'commerce-type' => $commerce_type,
  243.                     'found_communes' => isset($_GET['ville']) ? Functions::str_filter($_GET['ville']) : null),
  244.                 $get_params);
  245.             $input['commerce_type'] = 'boutiques-diverses';
  246.             unset($input['commerce-type']);
  247.             return $this->redirectToRoute('search_results'$input301);
  248.         }
  249.         # Handle malformed urls
  250.         if (# !in_array($departement, Constants::all_url_deps()) ||  # Make departement optional
  251.             !in_array($commerce_typeConstants::all_url_types())
  252.         ) {
  253.             throw $this->createNotFoundException('PAGE NON TROUVÉE');
  254.         }
  255.         $seo_strategy 'index';
  256.         # Decide canonical, or noindex
  257.         if ($type == 'tout') {  # In case of tout, do no-index
  258.             $seo_strategy 'noindex';
  259.         } elseif ($departement == Constants::generic_departement && $commerce_type == Constants::generic_commerce_type) {
  260.             $seo_strategy 'noindex';
  261.         } elseif ($commerce_type == Constants::generic_commerce_type && $departement) {
  262.             # vente/commerce-a-vendre/3-allier no index
  263.             $seo_strategy 'noindex';
  264.         } elseif (isset($_GET['order']) && $_GET['order'] != 'default'){
  265.             $seo_strategy 'noindex';
  266.         } elseif (isset($_GET['ville']) && strpos($_GET['ville'], ',')) {
  267.             # In case of multiple ville, do not index
  268.             $seo_strategy 'noindex';
  269.         } elseif (isset($_GET['agent_id'])) {
  270.             $seo_strategy 'noindex';
  271.         } elseif (isset($_GET['price-low']) || isset($_GET['price-high'])) {
  272.             $seo_strategy 'noindex';
  273.         } elseif (isset($_GET['surface-min']) || isset($_GET['surface-max'])) {
  274.             $seo_strategy 'noindex';
  275.         } elseif (isset($_GET['mode'])) {
  276.             $seo_strategy 'noindex';
  277.         } else {
  278.             $seo_strategy 'index';
  279.         }
  280.         if ($departement == Constants::generic_departement) {
  281.             $departement Null;
  282.         }
  283.         if ($commerce_type == Constants::generic_commerce_type){
  284.             $commerce_type Null;
  285.         }
  286.         # Check if page is viewall
  287.         if (isset($get_params['mode']) && $get_params['mode'] == 'viewall') {
  288.             $viewall True;
  289.         } else {
  290.             $viewall False;
  291.         }
  292.         $input array_merge(
  293.             Array('type' => $type,
  294.                 'departement' => $departement,
  295.                 'commerce-type' => $commerce_type,
  296.                 'found_communes' => isset($_GET['ville']) ? Functions::str_filter($_GET['ville']) : null),
  297.             $get_params);
  298.         $results $helper->db_select($input'bulk'$viewall);
  299.         $num_results Functions::euro_num($helper->total_results);
  300.         # Empty $results means some error, redirect to homepage
  301.         if (empty($results)) {
  302.             # if dep set, go to comm_type
  303.             if (!empty($input['departement']) && !empty($input['commerce-type'])) {
  304.                 # $input['region'] = Constants::generic_region;
  305.                 $input['departement'] = Constants::generic_departement;
  306.                 $input['commerce_type'] = $input['commerce-type'];
  307.                 unset($input['commerce-type']);
  308.                 return $this->redirectToRoute('search_results'$input301);
  309.             }
  310.             $session->set('error'$helper->error_obj);
  311.             return $this->redirect('/'301);
  312.         }
  313.         $tracker = new Tracker($helper->refined_input);
  314.         # Handle problematic agents with their images:
  315.         for ($i=0$i<count($results); $i++) {
  316.             if (in_array($results[$i]['site_id'], Constants::$block_images_agents)) {
  317.                 $agents_info $users->get_agents_list($results[$i]['site_id']);
  318.                 $replace_img = !empty($agents_info['logo']) ? $agents_info['logo'] : '/images/agents/default.png';
  319.                 $results[$i] = $helper->replace_image($results[$i], $replace_img);
  320.             }
  321.         }
  322.         # Do some stuff before sending results array to screen
  323.         $results_refined $helper->refine_results($results);
  324.         # Create title and H1
  325.         if (isset($input_arr['transaction'])) {
  326.             if ($input_arr['transaction'] == 'vente' || $input_arr['transaction'] == 1) {
  327.                 $pre_com_type 'Vente';
  328.             }
  329.             elseif ($input_arr['transaction'] == 'location' || $input_arr['transaction'] == 2) {
  330.                 $pre_com_type 'Location';
  331.             }
  332.             elseif ($input_arr['transaction'] == 'cession-de-droit-au-bail' || $input_arr['transaction'] == 3) {
  333.                 $pre_com_type 'Cession du droit au bail';
  334.             }
  335.         }
  336.         #$page_title_ch = implode(' - ', array_filter(array($commerce_type, $region, $departement)));
  337.         #<title>Vente bar-snack (63)puy-de-dome, auvergne</title>
  338.         $page_title_part = Array();
  339.         # Look for most important keywords and use them to create title
  340.         foreach(array('com_type''dep1''dep2') as $ctrl) {
  341.             if (isset($helper->keywords[$ctrl]['text'])) {
  342.                 $page_title_part[] = $helper->keywords[$ctrl]['text'];
  343.             }
  344.         }
  345.         if (isset($_GET['ville'])) {
  346.             $page_title_part[] = Functions::str_filter($_GET['ville']);
  347.         }
  348.         $page_title_ch implode(' - '$page_title_part);
  349.         if (!isset($helper->keywords['com_type'])) {
  350.             if ($input['type'] == 'location') {
  351.                 $page_title_ch 'Local commercial à louer ' $page_title_ch;
  352.             } elseif ($input['type'] == 'opportunites-investisseurs') {
  353.                 $page_title_ch 'Cession droit au bail ' $page_title_ch;
  354.             } else {
  355.                 $page_title_ch 'Fonds de commerce à vendre ' $page_title_ch;
  356.             }
  357.         }
  358.         if (isset($_GET['page']) && $_GET['page'] > 1) {
  359.             $page_title_ch .= ' - ' $_GET['page'];
  360.         }
  361.         # Save last search setting to a session cookie, so the form is pre-set already
  362.         $session->set('last_search'$input);
  363.         # In case of agent_id set, display only annonces of a specif agent + some info
  364.         $agent_info = [];
  365.         if (isset($_GET['agent_id'])) {
  366.             $agent_info $users->ret_user_info(nullintval($_GET['agent_id']));
  367.             # $agent_info = $users->ret_user_info(null, -45);  # Test sql injection, bad input
  368.         }
  369.         # print_r($results_refined);
  370.         # $session->set('last_search_results', $results_refined);
  371.         $output = array(
  372.             'page_title'    => $page_title_ch,
  373.             'page_description' => $page_title_ch,
  374.             'page'          => 'search',
  375.             'h1_main'       => $page_title_ch,
  376.             'loggedin'      => $users->loggedin(),
  377.             'items'            => $results_refined,
  378.             'number_results' => $num_results,
  379.             'seo_strategy'  => $seo_strategy,
  380.             'pagination'    => $helper->list_pagination(),
  381.             'keywords'        => $helper->keywords,
  382.             'keywords_suggestions' => $helper->keywords_suggestions,
  383.             'order_select'  => $helper->list_order_select(),
  384.             'agent_info'    => $agent_info,
  385.             );
  386.         # Check for errors to display
  387.         if ($session->get('error')) {
  388.             $output['error'] = $session->get('error');
  389.             $session->set('error'null);
  390.         }
  391.         return $this->render('search/results.html.twig'$output);
  392.     }
  393. }