/DEV/LIB/homegallery_get_cache.class.php
PHP | 548 lines | 322 code | 131 blank | 95 comment | 78 complexity | 4fe7a39ac9aae526da257f4bd62f3598 MD5 | raw file
- <?php
- /*
- Class de gestion de la galerie de la home logic-immo V6 (même si ce n'est pas une V6 gnagnagna)
- @Version : V1.0.0
- @Creator : SOFLOU
- @Date : 2009-12-17
- */
- if (isset($_GET['isAjax']) && $_GET['isAjax'] == 'yes') {
- include(dirname(realpath(__FILE__))."./../DISPLAY/modules/includes.php");
- }
- Class _Home_Gallery {
- private $CookieName = "LISearchHistoryCookie"; //Cookie name who save the last search
-
- private $path_cache; //Path du cache
-
- private $arr_ads_toprint;
-
- private $firstvisit;
- private $paramsearch;
-
- public function __construct($val_cookie='', $univers=1, $val_zipcode = '')
- {
-
- if ($univers == 1)
- {
- $path_univers = "sale";
- }
- else if ($univers == 2)
- {
- $path_univers = "rent";
- }
- else
- {
- $path_univers = "coloc";
- }
-
- $this->path_cache = URL_DATA."fr/cache_homegallery/".$path_univers."/"; //Path du cache
-
- // revisite (apres recherche)
-
- if (trim($val_cookie) <> "") {
-
- $this->firstvisit = false;
-
- $this->paramsearch = $this->Get_last_search($val_cookie);
-
- // echo "<hr>"; var_dump($this->paramsearch); echo "<hr>";
- if (
- (!isset($this->paramsearch['loc_key1']) || empty($this->paramsearch['loc_key1']) || ($this->paramsearch['loc_key1']=='_'))
- ) {
- // cookie a moitie vide (arrive notamment quand on fait une 1ere visite puisqu'on clique sur le bon plan et qu'on retourne en HP)
-
- $this->firstvisit = true;
-
- $user_loc_data = get_object_vars(geoLocateByIp());
-
- if (is_array($user_loc_data) && !empty($user_loc_data['postalCode'])) {
- $this->paramsearch['szip'] = substr($user_loc_data['postalCode'], 0,2);
- for($i=2; $i>=0; $i--){
- // var_dump($user_loc_data["loc_id_$i"]);
- if($user_loc_data["loc_id_$i"]) {
- $this->paramsearch['loc_key1'] = $user_loc_data["loc_id_$i"]."_$i";
- break;
- }
- }
- } else {
- // on part sur Paris, a defaut d'avoir un service de geoloc
- $this->paramsearch['szip'] = '75';
- $this->paramsearch['loc_key1'] = '100_1';
- }
-
- }
-
- } else {
-
- // premiere recherche
-
- $this->firstvisit = true;
-
- $user_loc_data = get_object_vars(geoLocateByIp());
-
- if (is_array($user_loc_data) && !empty($user_loc_data['postalCode'])) {
- $this->paramsearch['szip'] = substr($user_loc_data['postalCode'], 0,2);
- for($i=2; $i>=0; $i--){
- // var_dump($user_loc_data["loc_id_$i"]);
- if($user_loc_data["loc_id_$i"]) {
- $this->paramsearch['loc_key1'] = $user_loc_data["loc_id_$i"]."_$i";
- break;
- }
- }
- } else {
- // on part sur Paris, a defaut d'avoir un service de geoloc
- $this->paramsearch['szip'] = '75';
- $this->paramsearch['loc_key1'] = '100_1';
- }
-
- }
-
- $this->arr_ads_toprint = $this->getAdsbyLoc($this->paramsearch['szip']);
-
- $this->arr_ads_toprint = $this->OptimizeGallery($this->firstvisit);
-
- if (is_array($this->arr_ads_toprint))
- {
- $this->PrintGallery($this->arr_ads_toprint);
- }
-
- }
-
-
- /* Décomposition du cookies */
- public function Get_last_search($val_cookie)
- {
- global $_global;
- //The real last serach is always at the row number 0
- $last_search = fetchcookie(SEARCH_HISTORY_COOKIE_NAME);
- $last_search = unserialize($last_search[0]);
-
- if (!isset($last_search['szip']))
- {
- $last_search['szip'] = '75';
- $last_search['loc_key1'] = '100_1';
- }else if($last_search['szip'] > 95) { // DOM TOM
- $last_search['szip'] = substr($last_search['lzip'], 0, 3);
- }
-
- return $last_search;
- }
-
- /* Find Ads from SZIP */
- function getAdsbyLoc($szip = '75')
- {
-
- $arr_gallery = array();
-
- $file_cache = $this->path_cache.$szip.'/gallery.php';
-
- if (file_exists($file_cache)) {
-
- include($file_cache);
- //echo "file_cache = $file_cache<br>\n";
- // l'include retourne un $arr_gallery
-
- }
-
- return $arr_gallery;
-
- }
-
-
- /* Try to optimize the gallery array */
- public function OptimizeGallery($is_firstvisit=true)
- {
- global $_global;
-
- $gallery = $this->arr_ads_toprint;
-
- if ($is_firstvisit) { // 1ere visite (pas de recherche)
-
- $value_ads_loc_key = $this->paramsearch['loc_key1'];
- list($value_ads_loc_id, $value_ads_loc_level) = explode('_', $value_ads_loc_key);
- //echo 'loc_key = '; print_r($value_ads_loc_id); echo '_'; print_r($value_ads_loc_level);
-
- // types de bien Appartement, Maison, Villa
- $array_ads_property_type = array(1, 2, 7);
-
- $opti_gallery = $this->OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level
- );
-
- //echo "Cas 0 : "; //print_r($opti_gallery); echo "<hr>";
-
- if(empty($opti_gallery)){
- $opti_gallery = $gallery;
- }
-
- } else { // revisite (apres recherche)
-
- $opti_gallery = array();
-
- // recherche de la localité par loc_id (si loc_level = 1 ou 2) ou lgc_id et lgn_id (si loc_level = 98 ou 99)
- $value_ads_loc_key = $this->paramsearch['loc_key1'];
- list($value_ads_loc_id, $value_ads_loc_level) = explode('_', $value_ads_loc_key);
- //echo 'loc_key = '; print_r($value_ads_loc_id); echo '_'; print_r($value_ads_loc_level);
-
- // recherche du (des) type(s) de bien, si spécifié(s)
- $array_ads_property_type = getPropertiesValuesFromCkPropertyType($this->paramsearch['ck_property_type'], $_global['univers']);
- //print_r($array_ads_property_type);
- // recherche du prix min et max, si spécifié(s)
- $value_ads_price_min = $this->paramsearch['price_min'];
- $value_ads_price_max = $this->paramsearch['price_max'];
- // recherche de la surface min et max, si spécifiée(s)
- $value_ads_area_min = $this->paramsearch['area_min'];
- $value_ads_area_max = $this->paramsearch['area_max'];
-
- //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
- //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
- //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
- //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
-
- $opti_gallery = $this->OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level,
- $value_ads_price_min,
- $value_ads_price_max,
- $value_ads_area_min,
- $value_ads_area_max
- );
-
- //echo "Cas 1 : "; //print_r($opti_gallery); echo "<hr>";
-
- // pas d'annonce => surface min diminuée de 20 %, surface max augmentée de 20%
- if (empty($opti_gallery)) {
-
- $value_ads_area_min = (!empty($this->paramsearch['area_min']) ? (int) ((1-0.2) * $this->paramsearch['area_min']) : '');
- $value_ads_area_max = (!empty($this->paramsearch['area_max']) ? (int) ((1+0.2) * $this->paramsearch['area_max']) : '');
-
- //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
- //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
- //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
- //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
-
- $opti_gallery = $this->OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level,
- $value_ads_price_min,
- $value_ads_price_max,
- $value_ads_area_min,
- $value_ads_area_max
- );
-
- //echo "Cas 2 : "; //print_r($opti_gallery); echo "<hr>";
-
- }
-
- // tjs pas d'annonce => prix min diminué de 20 %, prix max augmenté de 20%
- // (avec meme plage de surface que calcule precedemment)
- if (empty($opti_gallery)) {
-
- $value_ads_price_min = (!empty($this->paramsearch['price_min']) ? (int) ((1-0.2) * $this->paramsearch['price_min']) : '');
- $value_ads_price_max = (!empty($this->paramsearch['price_max']) ? (int) ((1+0.2) * $this->paramsearch['price_max']) : '');
-
- //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
- //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
- //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
- //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
-
- $opti_gallery = $this->OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level,
- $value_ads_price_min,
- $value_ads_price_max,
- $value_ads_area_min,
- $value_ads_area_max
- );
-
- //echo "Cas 3 : "; //print_r($opti_gallery); echo "<hr>";
-
- }
-
- // tjs pas d'annonce => remonter d'un niveau de hiérarchie de localité (si ville => departement)
- // (avec meme plage de prix et de surface que calcule precedemment)
- if (empty($opti_gallery)) {
-
- if (in_array($value_ads_loc_level, array(2, 98, 99))) {
-
- list($value_ads_loc_id, $value_ads_loc_level) = array();
-
- // requete, afin de recuperer l'id parent (departement)
- $infos_ads_loc_key = get_loc_key($this->paramsearch['loc_key1']);
- $value_ads_loc_parent_id = $infos_ads_loc_key['lct_parent_id'];
- if (($value_ads_loc_parent_id >= 23) && ($value_ads_loc_parent_id <= 120)) {
- $value_ads_loc_parent_level = 1; // departement
- list($value_ads_loc_id, $value_ads_loc_level) = array($value_ads_loc_parent_id, $value_ads_loc_parent_level);
- }
- //echo 'loc_key = '; print_r($value_ads_loc_id); echo '_'; print_r($value_ads_loc_level);
-
- //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
- //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
- //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
- //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
-
- $opti_gallery = $this->OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level,
- $value_ads_price_min,
- $value_ads_price_max,
- $value_ads_area_min,
- $value_ads_area_max
- );
-
- //echo "Cas 4 : "; //print_r($opti_gallery); echo "<hr>";
-
- } // sinon, on devrait avoir $value_ads_loc_level = 1
-
- }
-
- // tjs pas d'annonce => tirage au hasard d'une annonce du meme departement
- // (sans aucune plage de prix et de surface)
- if (empty($opti_gallery)) {
-
- $array_ads_property_type = array();
-
- $value_ads_price_min = $value_ads_price_max = '';
-
- $value_ads_area_min = $value_ads_area_max = '';
-
- //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
- //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
- //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
- //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
-
- $opti_gallery = $this->OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level,
- $value_ads_price_min,
- $value_ads_price_max,
- $value_ads_area_min,
- $value_ads_area_max
- );
-
- //echo "Cas 5 : "; //print_r($opti_gallery); echo "<hr>";
-
- }
-
- // Si tjs pas d'annonce, on prend toutes les annonces presentes dans le ficher de cache
- if (empty($opti_gallery) && !empty($gallery)) {
-
- $opti_gallery = $gallery;
-
- //echo "Cas 6 : "; //print_r($opti_gallery); echo "<hr>";
-
- } else
-
- // Si tjs pas d'annonce on prend Paris par defaut
- if (empty($gallery)) {
-
- $file_cache = $this->path_cache.'/75/gallery.php';
-
- if (file_exists($file_cache)) {
-
- include($file_cache);
- $opti_gallery = $arr_gallery;
-
- //echo "Cas 7 : "; //print_r($opti_gallery); echo "<hr>";
-
- }
-
- }
-
- }
-
- // Bon plan jour = on ne garde que les 3 annonces les moins cheres
- //ON SUPPRIMME LE TRI SUR 3 ANNONCES MAIS ON GARDE LE SHUFFLE
- //$opti_gallery = array_slice($opti_gallery, 0, 3);
- //print_r($opti_gallery);
-
- shuffle($opti_gallery);
-
- // Bon plan jour = 1 annonce parmi parmi les 3 restantes
- $opti_gallery = array_slice($opti_gallery, 0, 1);
- //print_r($opti_gallery);
-
- return $opti_gallery;
-
- }
-
-
- /* Return ads filtered by critera */
- public function OptimizeGalleryCritera(
- $gallery,
- $array_ads_property_type,
- $value_ads_loc_id,
- $value_ads_loc_level,
- $value_ads_price_min='',
- $value_ads_price_max='',
- $value_ads_area_min='',
- $value_ads_area_max='') {
-
- //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
- //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
- //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
- //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
-
- $opti_gallery = array();
-
- foreach ($gallery as $ad) {
-
- if (
- (
- (!empty($value_ads_loc_id) && !empty($value_ads_loc_level)) &&
- // filtrage sur le loc_id (si loc_level = 1 ou 2)
- (in_array($value_ads_loc_level, array(1, 2)) && ($ad['hierarchy'][$value_ads_loc_level]['lct_id'] == $value_ads_loc_id)) ||
- // filtrage sur le lgc_id (si loc_level = 98)
- (in_array($value_ads_loc_level, array(98)) && ($ad['lgc_id'] == $value_ads_loc_id)) ||
- // filtrage sur le lgn_id (si loc_level = 99)
- (in_array($value_ads_loc_level, array(99)) && ($ad['lgn_id'] == $value_ads_loc_id))
- ) &&
- // filtrage sur type(s) de bien, si spécifié
- (empty($array_ads_property_type) || in_array($ad['property_type_id_orig'], $array_ads_property_type)) &&
- // filtrage sur prix min, si spécifié
- (empty($value_ads_price_min) || ($ad['real_price'] >= $value_ads_price_min)) &&
- // filtrage sur prix max, si spécifié
- (empty($value_ads_price_max) || ($ad['real_price'] <= $value_ads_price_max)) &&
- // filtrage sur surface min, si spécifiée
- (empty($value_ads_area_min) || ($ad['area'] >= $value_ads_area_min)) &&
- // filtrage sur surface max, si spécifiée
- (empty($value_ads_area_max) || ($ad['area'] <= $value_ads_area_max))
- ) {
- //echo "[".$ad['lgc_id'].":".$ad['idx']."]"."\n";
- array_push($opti_gallery, $ad);
-
- }
-
- }
-
- return $opti_gallery;
-
- }
-
-
- /* Print Gallery */
- public function PrintGallery($tab_ads)
- {
- global $_global;
-
- if($_global['univers']==1) {
- $gallery_univers = "vente";
- $xitiClickImgId = 36;
- }
- else if($_global['univers']==2) {
- $gallery_univers = "location";
- $xitiClickImgId = 72;
- }
- else if($_global['univers']==8) {
- $gallery_univers = "colocation";
- $xitiClickImgId = 108;
- }
-
- foreach ($tab_ads as $ad) {
-
- // gallery datas
- $gallery_locality = ($ad["default_town_2"] == "" ? $ad["default_town_1"] : $ad["default_town_2"]);
- $gallery_zip_code = $ad["lzip"];
- $gallery_property_type = $ad["property_type"];
- $gallery_area = ($ad["area"] == 0? "" : $ad["area"]." m²");
- $gallery_nb_rooms = ($ad["nb_rooms"] == 0? "" : $ad["nb_rooms"]." pièce".($ad["nb_rooms"] > 1 ? "s" : ""));
- $gallery_id_photo = $ad["photos_id"][0];
- $gallery_price = ($ad["price"] == 0? "" : $ad["price"]." €");
-
- // link to the detail page
- $gallery_detail_link = URL_SITE."detail-".$gallery_univers."-".strtolower($ad["id"]).".htm?ext=1";
-
- // bloc infos
- $temporary = array();
- if ($gallery_property_type != '') {
- $temporary[] = $gallery_property_type;
- }
- if ($gallery_area != 0) {
- $temporary[] = $gallery_area;
- }
- if ($gallery_nb_rooms != 0) {
- $temporary[] = $gallery_nb_rooms;
- }
- $gallery_infos = implode(' ● ', $temporary);
-
- // bloc text
- $gallery_local = '';
- if (!empty($gallery_locality) && !empty($gallery_zip_code)) {
- $gallery_local_title = $gallery_local = $gallery_locality.' ('.$gallery_zip_code.')';
- if (strlen($gallery_local) > 25) { // trop long, on ne garde que le nom
- $gallery_local = $gallery_locality;
- if (strlen($gallery_local) > 25) { // encore trop long, on tronque le nom
- $gallery_local = truncate($gallery_local, 25);
- }
- }
- } else if (!empty($gallery_locality)) {
- $gallery_local_title = $gallery_local = $gallery_locality;
- } else if (!empty($gallery_zip_code)) {
- $gallery_local_title = $gallery_local = $gallery_zip_code;
- }
-
- // links title
- $gallery_title = $gallery_local_title.(!empty($gallery_infos) ? ' - '.$gallery_infos : '').(!empty($gallery_price) ? ' - '.$gallery_price : '');
-
- // url photo
- $gallery_photo_url = get_urlmmf_anc_seo(MMF_FORMAT_FR_ANC_CROP_GALLERY, $gallery_id_photo, $ad["id"], $gallery_property_type, $gallery_locality, 'FR');
-
-
- // html content
- $content = "";
- $content .= " <div class=\"spir_title spir_title-big\">L'ANNONCE DU JOUR</div>\n";
- $content .= " <div class=\"container\">\n";
- if (!empty($gallery_photo_url)) {
-
- $img_alt = ucfirst($gallery_univers)." ".$gallery_property_type." ".$gallery_locality." ".$gallery_area." ".$gallery_nb_rooms;
- $content .= " <a onclick=\"".getXitiClick($xitiClickImgId)."\" href=\"".$gallery_detail_link."\" title=\"".stripslashes($gallery_title)."\" class=\"img\">\n";
- $content .= " <img width=\"280\" height=\"205\" border=\"0\" src=\"".$gallery_photo_url."\" alt=\"".stripslashes($img_alt)."\"/>\n";
- $content .= " </a>\n";
- }
-
- if (!empty($gallery_detail_link) && !empty($gallery_title)) {
- $content .= " <a href=\"".$gallery_detail_link."\" title=\"".stripslashes($gallery_title)."\" class=\"crit\">\n";
- $content .= " <div class=\"crit\">\n";
- if (!empty($gallery_price)) {
- $content .= " <p class=\"crit_price\">".$gallery_price."</p>\n";
- }
- if (!empty($gallery_local)) {
- $content .= " <p class=\"crit_local\">".stripslashes($gallery_local)."</p>\n";
- }
- if (!empty($gallery_infos)) {
- $content .= " <p class=\"crit_infos\">".stripslashes($gallery_infos)."</p>\n";
- }
- $content .= " </div>\n";
- $content .= " </a>\n";
- }
- $content .= "</div>\n";
- }
-
- echo $content;
- }
-
- }
- ?>