<?php
namespace AppBundle\Controller;
use Symfony\Component\Routing\Annotation\Route;
# use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use AppBundle\Includes\Database;
use AppBundle\Includes\Settings;
use AppBundle\Includes\Helper;
use AppBundle\Includes\Tracker;
use AppBundle\Includes\Users;
use AppBundle\Includes\Functions;
use AppBundle\Includes\Constants;
//http://bizenco2/entreprises-commerces/provence-alpes-azur/13-bouches-du-rhone/commerce-a-vendre/vente-bouches-du-rhone-provence-alpes-azur----66-1324.html
//http://bizenco2/entreprises-commerces/provence-alpes-azur/13-bouches-du-rhone/commerce-a-vendre/
class BienController extends AbstractController
{
//entreprises-commerces/basse-normandie/14-calvados/bar-tabac/vente-bar-licence-iv-petite-restauration-geo_situtation--1-3412.html
/**
* @Route("/{type}/{region}/{departement}/{commerce_type}/{title}--{geo_situation}--{id}.html",
* requirements={"type": "entreprises-commerces|murs-locaux-terrains", "title": "[a-zA-Z0-9\-]+", "geo_situation": "[a-zA-Z0-9\-]*"},
* defaults={"departement": "departement", "commerce_type": "commerce-type"},
* name="bien_display_old", methods={"GET", "POST"})
*/
public function displayOldAction(Request $request, $type, $region, $departement, $commerce_type, $title, $geo_situation, $id)
{
$helper = new Helper($this->get('router'));
$input = Array('id' => $id);
$bien = $helper->db_select($input, 'id');
if (empty($bien)) {
$session = $request->getSession();
# Bien not found
$error_obj = Functions::alert_obj('Référence invalide!', 'Ce bien n’est pas présent dans notre base de donnée', 0);
# Get commerce text and department text
$com_id = Functions::com_type2id($commerce_type);
$dep_id = Functions::departement2id("$departement");
$com_str = "";
if ($com_id) {
$com_str = Functions::id2com_type($com_id, false);
}
$departement_str = "";
if ($dep_id) {
$dep_strs = Functions::id2departement($dep_id, false, false);
$departement_str = "{$dep_strs[0]}";
}
if ($type == 'murs-locaux-terrains') {
$type = 'tout';
} else {
$type = 'vente';
}
$session->set('error', $error_obj);
if ($com_str && $com_str) {
$session->set('redirect_page', "/$type/$commerce_type/$departement/");
$session->set('redirect_text', "$com_str à vendre en $departement_str");
} else {
$session->set('redirect_text', "");
}
# Cause an better 404 error with more info using session variables
# Use the throw, or else it really causes a blank 404 error
throw $this->createNotFoundException('Ce bien n’est pas présent dans notre base de donnée');
}
$bien = $bien[0]; # We have only one bien
$params = $helper->bien_url_params($bien);
return $this->redirect($this->get('router')->generate('bien_display', $params), 301);
}
//entreprises-commerces/basse-normandie/14-calvados/bar-tabac/vente-bar-licence-iv-petite-restauration-geo_situtation--1-3412.html
/**
* @Route("/{type}/{commerce_type}/{departement}/{title}--{geo_situation}--{id}.html",
* requirements={"type": "vente|vente-immobilier|location|opportunites-investisseurs", "title": "[a-zA-Z0-9\-]+", "geo_situation": "[a-zA-Z0-9\-]*"},
* defaults={"departement": "departement", "commerce_type": "commerce-type"},
* name="bien_display", methods={"GET", "POST"})
*/
public function displayAction(Request $request, $type, $departement, $commerce_type, $title, $geo_situation, $id)
{
$helper = new Helper($this->get('router'));
$user = $this->get('app.users');
$tracker = new Tracker();
$input = Array('id' => $id);
$bien = $helper->db_select($input, 'id');
$session = $request->getSession();
if (empty($bien)) {
# Bien not found
$error_obj = Functions::alert_obj('Référence invalide!', 'Ce bien n’est pas présent dans notre base de donnée', 0);
# Get commerce text and department text
$com_id = Functions::com_type2id($commerce_type);
$dep_id = Functions::departement2id("$departement");
$com_str = "";
if ($com_id) {
$com_str = Functions::id2com_type($com_id, false);
}
$departement_str = "";
if ($dep_id) {
$dep_strs = Functions::id2departement($dep_id, false, false);
$departement_str = "{$dep_strs[0]}";
}
$session->set('error', $error_obj);
if ($com_str && $departement_str) {
$session->set('redirect_page', "/$type/$commerce_type/$departement/");
$session->set('redirect_text', "$com_str à vendre en $departement_str");
}
# Cause an better 404 error with more info using session variables
# Use the throw, or else it really causes a blank 404 error
throw $this->createNotFoundException('Ce bien n’est pas présent dans notre base de donnée');
}
$bien = $bien[0]; # We have only one bien
if ($geo_situation == 'n-a') {
# url contains geo_situation, which could be empty. Correct and redirect
$geo_situation = '';
#print_r($bien);
$params = $helper->bien_url_params($bien);
return $this->redirect($this->get('router')->generate('bien_display', $params), 301);
}
$params = $helper->bien_url_params($bien);
if ($params['departement'] != $departement ||
$params['commerce_type'] != $commerce_type ||
$params['title'] != $title ||
$params['geo_situation'] != $geo_situation)
{
# Instead of doing an error, redirect to correct page!
#$error_obj = Functions::alert_obj('Paramètres de l’URL non valides!', 'Une erreur est intervenue lors de la construction de l’URL', 0);
#$session->set('error', $error_obj);
#return $this->redirect('/', 301);
$params = $helper->bien_url_params($bien);
return $this->redirect($this->get('router')->generate('bien_display', $params), 301);
}
# $bien variable is used later, but I need title set
$bien['title'] = $helper->bien_title($bien);
$bien_refined['title'] = $helper->bien_title($bien);
$bien_refined['id'] = $id;
$bien_refined['biz_id'] = $bien['biz_id'];
$bien_refined['reference'] = $bien['reference'];
$bien_refined['date'] = $bien['date'];
$bien_refined['type'] = $bien['type'];
$bien_refined['type_transaction'] = $bien['type_transaction'];
$bien_refined['forme_juridique'] = $bien['forme_juridique'];
$bien_refined['licence'] = $bien['licence'];
$bien_refined['rent_price'] = Functions::euro_num($bien['rent_price']);
$bien_refined['designation'] = $bien['designation'];
$bien_refined['geo_situation'] = $bien['geo_situation'];
$bien_refined['departement'] = $bien['departement'];
$bien_refined['commerce_type'] = $bien['commerce_type'];
$bien_refined['ca_ht'] = Functions::euro_num($bien['price_ht']);
$bien_refined['profit'] = Functions::euro_num($bien['profit']);
$bien_refined['salaire_dirigeant']= Functions::euro_num($bien['salaire_dirigeant']);
$bien_refined['price'] = Functions::euro_num($bien['price']);
$bien_refined['price_com'] = Functions::euro_num($bien['price_com']);
$bien_refined['price_fonds'] = Functions::euro_num($bien['price_fonds']);
$bien_refined['price_murs'] = Functions::euro_num($bien['price_murs']);
$bien_refined['description'] = Functions::str_filter(strip_tags($bien['description']));
$bien_refined['conditions'] = $bien['conditions'];
$bien_refined['picture1'] = $bien['picture1'];
$bien_refined['stationnement'] = $bien['stationnement'];
$bien_refined['appartement'] = $bien['appartement'];
$bien_refined['superficie'] = $bien['superficie'];
$bien_refined['achat_marchandise'] = Functions::euro_num($bien['achat_marchandise']);
if ($bien['logement'] == 1)
$bien_refined['logement'] = 'Oui';
else
$bien_refined['logement'] = 'Non';
$bien_refined['motif_cession'] = $bien['motif_cession'];
$bien_refined['detail_bail'] = $bien['detail_bail'] ? $bien['detail_bail'] : 'N/A';
$bien_refined['locaux'] = $bien['locaux'];
$bien_refined['materiel'] = $bien['materiel'];
$bien_refined['horaire_ouverture']= $bien['horaire_ouverture'];
$bien_refined['fermeture_hebdo'] = $bien['fermeture_hebdo'];
$bien_refined['fermeture_an'] = $bien['fermeture_an'];
$bien_refined['salaire'] = Functions::euro_num($bien['salaire']);
$bien_refined['ebe'] = Functions::euro_num($bien['ebe']);
# Calculate Loyer Annuel HT et HC / m2
$bien_refined['ht_m2'] = Functions::euro_num('');
$bien_refined['found_communes'] = $bien['found_communes'];
$bien_refined['url'] = $request->getUri();
$bien_refined['region_ld'] = Functions::id2region($bien['departement']);
$bien_refined['departement_ld'] = Functions::id2departement($bien['departement'], False)[0];
$bien_refined['commerce_type_ld'] = Functions::id2com_type($bien['commerce_type'], False);
# Mail nego stuff
if (!empty($bien_refined['superficie']) && $bien_refined['superficie'] != 0)
$bien_refined['ht_m2'] = Functions::euro_num(intval($bien['rent_price']) * 12 / intval($bien['superficie']));
foreach ($bien_refined as $key => $value) {
if ($value == '') {
if ($key == 'picture1') { # In case of empty image, place a filler at least
$bien_refined["$key"] = '/images/bizenco_b.png';
} else {
$bien_refined["$key"] = 'N/A';
}
} else {
$bien_refined["$key"] = Functions::str_filter($value);
}
}
# Comparer section
if (in_array($bien_refined['id'], $helper->cookie['comparer']))
$in_comparer = True;
else
$in_comparer = False;
# most likely will use agent data already in bien (specific agent), else use agent defaults!
$ag_keys = Array(
'tele_portable' => 'portable_nego',
'tele_fixe' => 'tel_nego',
'prenom' => 'prenom_nego',
'nom' => 'nom_nego',
'email' => 'mail_nego'
);
$agents_info = $user->get_agents_list($bien['site_id']);
# Handle problematic agents with their images:
if (in_array($agents_info['id'], Constants::$block_images_agents)) {
$replace_img = !empty($agents_info['logo']) ? $agents_info['logo'] : '/images/agents/default.png';
$bien_refined = $helper->replace_image($bien_refined, $replace_img);
}
foreach ($ag_keys as $key => $value) {
if (isset($bien[$value]) && $bien[$value] != '') {
$agents_info[$key] = $bien[$value];
}
}
# In some formats, they only have NOM for agence
if (!empty($bien['nom_nego']) && empty($bien['prenom_nego'])) {
$agents_info['prenom'] = '';
}
# Also add agent email to bien_refined, used later
$bien_refined['email'] = $agents_info['email'];
# Add bien to the visited list
$helper->history_add($id, $bien);
# print_r($helper->breadcrumb($bien_refined)); exit();
# Limit page description & remove html tags
$page_description = "{$bien_refined['description']}";
$page_description = Functions::str_filter($page_description);
$page_description = substr($page_description, 0, 50);
$output = array(
'page_title' => Functions::str_filter("{$bien_refined['title']} - $id"),
'page_description' => $page_description,
'social_title' => Functions::str_filter("{$bien_refined['title']}"),
'social_image' => $bien_refined['picture1'],
'social_description' => $bien_refined['description'],
'breadcrumb' => $helper->breadcrumb($bien_refined),
'page' => 'display',
'loggedin' => $user->loggedin(),
'bien' => $bien_refined,
'history' => $helper->history_get(),
'in_comparer' => $in_comparer,
'agent_info' => $agents_info,
);
# Handle user interest in Bien
if (isset($_POST) && !empty($_POST)) {
// TODO: check bien_interest function that handles the email
$output['error'] = $user->bien_interest($_POST, $bien_refined);
}
$session = $request->getSession();
# If logged in, can prepare some info to sent mail fast
if ($session->get('email') && !$session->get('user_info')) {
$session_userinfo = Array();
$user_info = $user->ret_user_info($session->get('email') );
$session_userinfo['name'] = "{$user_info['nom']} {$user_info['prenom']}";
$session_userinfo['email'] = $session->get('email');
$session_userinfo['subject'] = "Demande d’information sur la réf. {$bien_refined['reference']}";
$session_userinfo['telephone'] = "";
$session_userinfo['message'] = "";
$session->set('user_info', $session_userinfo);
}
# Auto-fill some info
if ($session->get('user_info'))
$output['user_info'] = $session->get('user_info');
# Pick between print or normal mode
if (isset($_GET['mode']) && $_GET['mode'] == 'print') {
return $this->render('display/bien_printable.html.twig', $output);
} else {
$lsrcInput = $session->get('last_search');
if (is_array($lsrcInput)) {
# print_r($lsrcInput);
$results = $helper->db_select($lsrcInput, 'similar');
$srcRes = $helper->refine_results($results);
# Handle similar pages
$prev = null;
if (is_array($srcRes) && !empty($srcRes)) {
# Find current ID
foreach ($srcRes as $key => $res) {
if ($bien_refined['biz_id'] == $res['biz_id']) {
$numIt = 5;
if ($numIt > count($srcRes)) $numIt = count($srcRes);
# Emulate circular relation for array
$slice = array_slice($srcRes, $key+1, $numIt);
if ($numIt > count($slice)) {
$slice2 = array_slice($srcRes, 0, $numIt - count($slice));
$slice = array_merge($slice, $slice2);
}
$output['last_search_results'] = $slice;
# Next item for nagivate prev, next
if (count($slice)) {
$output['next_search'] = $slice[0]['url'];
} else {
$output['next_search'] = Array();
}
$output['prev_search'] = $prev;
break;
}
$prev = $res['url'];
}
}
}
# Do those things only in normal mode
# Add bien to the visited list
$helper->history_add($id, $bien_refined);
# Latest Entries Display
$tracker->add_bien($bien_refined);
return $this->render('display/bien.html.twig', $output);
}
}
}