PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/DEV/LIB/homegallery_get_cache.class.php

https://github.com/chrisbst35/LI_LEMONDE
PHP | 548 lines | 322 code | 131 blank | 95 comment | 78 complexity | 4fe7a39ac9aae526da257f4bd62f3598 MD5 | raw file
  1. <?php
  2. /*
  3. Class de gestion de la galerie de la home logic-immo V6 (même si ce n'est pas une V6 gnagnagna)
  4. @Version : V1.0.0
  5. @Creator : SOFLOU
  6. @Date : 2009-12-17
  7. */
  8. if (isset($_GET['isAjax']) && $_GET['isAjax'] == 'yes') {
  9. include(dirname(realpath(__FILE__))."./../DISPLAY/modules/includes.php");
  10. }
  11. Class _Home_Gallery {
  12. private $CookieName = "LISearchHistoryCookie"; //Cookie name who save the last search
  13. private $path_cache; //Path du cache
  14. private $arr_ads_toprint;
  15. private $firstvisit;
  16. private $paramsearch;
  17. public function __construct($val_cookie='', $univers=1, $val_zipcode = '')
  18. {
  19. if ($univers == 1)
  20. {
  21. $path_univers = "sale";
  22. }
  23. else if ($univers == 2)
  24. {
  25. $path_univers = "rent";
  26. }
  27. else
  28. {
  29. $path_univers = "coloc";
  30. }
  31. $this->path_cache = URL_DATA."fr/cache_homegallery/".$path_univers."/"; //Path du cache
  32. // revisite (apres recherche)
  33. if (trim($val_cookie) <> "") {
  34. $this->firstvisit = false;
  35. $this->paramsearch = $this->Get_last_search($val_cookie);
  36. // echo "<hr>"; var_dump($this->paramsearch); echo "<hr>";
  37. if (
  38. (!isset($this->paramsearch['loc_key1']) || empty($this->paramsearch['loc_key1']) || ($this->paramsearch['loc_key1']=='_'))
  39. ) {
  40. // 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)
  41. $this->firstvisit = true;
  42. $user_loc_data = get_object_vars(geoLocateByIp());
  43. if (is_array($user_loc_data) && !empty($user_loc_data['postalCode'])) {
  44. $this->paramsearch['szip'] = substr($user_loc_data['postalCode'], 0,2);
  45. for($i=2; $i>=0; $i--){
  46. // var_dump($user_loc_data["loc_id_$i"]);
  47. if($user_loc_data["loc_id_$i"]) {
  48. $this->paramsearch['loc_key1'] = $user_loc_data["loc_id_$i"]."_$i";
  49. break;
  50. }
  51. }
  52. } else {
  53. // on part sur Paris, a defaut d'avoir un service de geoloc
  54. $this->paramsearch['szip'] = '75';
  55. $this->paramsearch['loc_key1'] = '100_1';
  56. }
  57. }
  58. } else {
  59. // premiere recherche
  60. $this->firstvisit = true;
  61. $user_loc_data = get_object_vars(geoLocateByIp());
  62. if (is_array($user_loc_data) && !empty($user_loc_data['postalCode'])) {
  63. $this->paramsearch['szip'] = substr($user_loc_data['postalCode'], 0,2);
  64. for($i=2; $i>=0; $i--){
  65. // var_dump($user_loc_data["loc_id_$i"]);
  66. if($user_loc_data["loc_id_$i"]) {
  67. $this->paramsearch['loc_key1'] = $user_loc_data["loc_id_$i"]."_$i";
  68. break;
  69. }
  70. }
  71. } else {
  72. // on part sur Paris, a defaut d'avoir un service de geoloc
  73. $this->paramsearch['szip'] = '75';
  74. $this->paramsearch['loc_key1'] = '100_1';
  75. }
  76. }
  77. $this->arr_ads_toprint = $this->getAdsbyLoc($this->paramsearch['szip']);
  78. $this->arr_ads_toprint = $this->OptimizeGallery($this->firstvisit);
  79. if (is_array($this->arr_ads_toprint))
  80. {
  81. $this->PrintGallery($this->arr_ads_toprint);
  82. }
  83. }
  84. /* Décomposition du cookies */
  85. public function Get_last_search($val_cookie)
  86. {
  87. global $_global;
  88. //The real last serach is always at the row number 0
  89. $last_search = fetchcookie(SEARCH_HISTORY_COOKIE_NAME);
  90. $last_search = unserialize($last_search[0]);
  91. if (!isset($last_search['szip']))
  92. {
  93. $last_search['szip'] = '75';
  94. $last_search['loc_key1'] = '100_1';
  95. }else if($last_search['szip'] > 95) { // DOM TOM
  96. $last_search['szip'] = substr($last_search['lzip'], 0, 3);
  97. }
  98. return $last_search;
  99. }
  100. /* Find Ads from SZIP */
  101. function getAdsbyLoc($szip = '75')
  102. {
  103. $arr_gallery = array();
  104. $file_cache = $this->path_cache.$szip.'/gallery.php';
  105. if (file_exists($file_cache)) {
  106. include($file_cache);
  107. //echo "file_cache = $file_cache<br>\n";
  108. // l'include retourne un $arr_gallery
  109. }
  110. return $arr_gallery;
  111. }
  112. /* Try to optimize the gallery array */
  113. public function OptimizeGallery($is_firstvisit=true)
  114. {
  115. global $_global;
  116. $gallery = $this->arr_ads_toprint;
  117. if ($is_firstvisit) { // 1ere visite (pas de recherche)
  118. $value_ads_loc_key = $this->paramsearch['loc_key1'];
  119. list($value_ads_loc_id, $value_ads_loc_level) = explode('_', $value_ads_loc_key);
  120. //echo 'loc_key = '; print_r($value_ads_loc_id); echo '_'; print_r($value_ads_loc_level);
  121. // types de bien Appartement, Maison, Villa
  122. $array_ads_property_type = array(1, 2, 7);
  123. $opti_gallery = $this->OptimizeGalleryCritera(
  124. $gallery,
  125. $array_ads_property_type,
  126. $value_ads_loc_id,
  127. $value_ads_loc_level
  128. );
  129. //echo "Cas 0 : "; //print_r($opti_gallery); echo "<hr>";
  130. if(empty($opti_gallery)){
  131. $opti_gallery = $gallery;
  132. }
  133. } else { // revisite (apres recherche)
  134. $opti_gallery = array();
  135. // 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)
  136. $value_ads_loc_key = $this->paramsearch['loc_key1'];
  137. list($value_ads_loc_id, $value_ads_loc_level) = explode('_', $value_ads_loc_key);
  138. //echo 'loc_key = '; print_r($value_ads_loc_id); echo '_'; print_r($value_ads_loc_level);
  139. // recherche du (des) type(s) de bien, si spécifié(s)
  140. $array_ads_property_type = getPropertiesValuesFromCkPropertyType($this->paramsearch['ck_property_type'], $_global['univers']);
  141. //print_r($array_ads_property_type);
  142. // recherche du prix min et max, si spécifié(s)
  143. $value_ads_price_min = $this->paramsearch['price_min'];
  144. $value_ads_price_max = $this->paramsearch['price_max'];
  145. // recherche de la surface min et max, si spécifiée(s)
  146. $value_ads_area_min = $this->paramsearch['area_min'];
  147. $value_ads_area_max = $this->paramsearch['area_max'];
  148. //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
  149. //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
  150. //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
  151. //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
  152. $opti_gallery = $this->OptimizeGalleryCritera(
  153. $gallery,
  154. $array_ads_property_type,
  155. $value_ads_loc_id,
  156. $value_ads_loc_level,
  157. $value_ads_price_min,
  158. $value_ads_price_max,
  159. $value_ads_area_min,
  160. $value_ads_area_max
  161. );
  162. //echo "Cas 1 : "; //print_r($opti_gallery); echo "<hr>";
  163. // pas d'annonce => surface min diminuée de 20 %, surface max augmentée de 20%
  164. if (empty($opti_gallery)) {
  165. $value_ads_area_min = (!empty($this->paramsearch['area_min']) ? (int) ((1-0.2) * $this->paramsearch['area_min']) : '');
  166. $value_ads_area_max = (!empty($this->paramsearch['area_max']) ? (int) ((1+0.2) * $this->paramsearch['area_max']) : '');
  167. //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
  168. //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
  169. //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
  170. //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
  171. $opti_gallery = $this->OptimizeGalleryCritera(
  172. $gallery,
  173. $array_ads_property_type,
  174. $value_ads_loc_id,
  175. $value_ads_loc_level,
  176. $value_ads_price_min,
  177. $value_ads_price_max,
  178. $value_ads_area_min,
  179. $value_ads_area_max
  180. );
  181. //echo "Cas 2 : "; //print_r($opti_gallery); echo "<hr>";
  182. }
  183. // tjs pas d'annonce => prix min diminué de 20 %, prix max augmenté de 20%
  184. // (avec meme plage de surface que calcule precedemment)
  185. if (empty($opti_gallery)) {
  186. $value_ads_price_min = (!empty($this->paramsearch['price_min']) ? (int) ((1-0.2) * $this->paramsearch['price_min']) : '');
  187. $value_ads_price_max = (!empty($this->paramsearch['price_max']) ? (int) ((1+0.2) * $this->paramsearch['price_max']) : '');
  188. //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
  189. //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
  190. //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
  191. //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
  192. $opti_gallery = $this->OptimizeGalleryCritera(
  193. $gallery,
  194. $array_ads_property_type,
  195. $value_ads_loc_id,
  196. $value_ads_loc_level,
  197. $value_ads_price_min,
  198. $value_ads_price_max,
  199. $value_ads_area_min,
  200. $value_ads_area_max
  201. );
  202. //echo "Cas 3 : "; //print_r($opti_gallery); echo "<hr>";
  203. }
  204. // tjs pas d'annonce => remonter d'un niveau de hiérarchie de localité (si ville => departement)
  205. // (avec meme plage de prix et de surface que calcule precedemment)
  206. if (empty($opti_gallery)) {
  207. if (in_array($value_ads_loc_level, array(2, 98, 99))) {
  208. list($value_ads_loc_id, $value_ads_loc_level) = array();
  209. // requete, afin de recuperer l'id parent (departement)
  210. $infos_ads_loc_key = get_loc_key($this->paramsearch['loc_key1']);
  211. $value_ads_loc_parent_id = $infos_ads_loc_key['lct_parent_id'];
  212. if (($value_ads_loc_parent_id >= 23) && ($value_ads_loc_parent_id <= 120)) {
  213. $value_ads_loc_parent_level = 1; // departement
  214. list($value_ads_loc_id, $value_ads_loc_level) = array($value_ads_loc_parent_id, $value_ads_loc_parent_level);
  215. }
  216. //echo 'loc_key = '; print_r($value_ads_loc_id); echo '_'; print_r($value_ads_loc_level);
  217. //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
  218. //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
  219. //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
  220. //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
  221. $opti_gallery = $this->OptimizeGalleryCritera(
  222. $gallery,
  223. $array_ads_property_type,
  224. $value_ads_loc_id,
  225. $value_ads_loc_level,
  226. $value_ads_price_min,
  227. $value_ads_price_max,
  228. $value_ads_area_min,
  229. $value_ads_area_max
  230. );
  231. //echo "Cas 4 : "; //print_r($opti_gallery); echo "<hr>";
  232. } // sinon, on devrait avoir $value_ads_loc_level = 1
  233. }
  234. // tjs pas d'annonce => tirage au hasard d'une annonce du meme departement
  235. // (sans aucune plage de prix et de surface)
  236. if (empty($opti_gallery)) {
  237. $array_ads_property_type = array();
  238. $value_ads_price_min = $value_ads_price_max = '';
  239. $value_ads_area_min = $value_ads_area_max = '';
  240. //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
  241. //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
  242. //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
  243. //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
  244. $opti_gallery = $this->OptimizeGalleryCritera(
  245. $gallery,
  246. $array_ads_property_type,
  247. $value_ads_loc_id,
  248. $value_ads_loc_level,
  249. $value_ads_price_min,
  250. $value_ads_price_max,
  251. $value_ads_area_min,
  252. $value_ads_area_max
  253. );
  254. //echo "Cas 5 : "; //print_r($opti_gallery); echo "<hr>";
  255. }
  256. // Si tjs pas d'annonce, on prend toutes les annonces presentes dans le ficher de cache
  257. if (empty($opti_gallery) && !empty($gallery)) {
  258. $opti_gallery = $gallery;
  259. //echo "Cas 6 : "; //print_r($opti_gallery); echo "<hr>";
  260. } else
  261. // Si tjs pas d'annonce on prend Paris par defaut
  262. if (empty($gallery)) {
  263. $file_cache = $this->path_cache.'/75/gallery.php';
  264. if (file_exists($file_cache)) {
  265. include($file_cache);
  266. $opti_gallery = $arr_gallery;
  267. //echo "Cas 7 : "; //print_r($opti_gallery); echo "<hr>";
  268. }
  269. }
  270. }
  271. // Bon plan jour = on ne garde que les 3 annonces les moins cheres
  272. //ON SUPPRIMME LE TRI SUR 3 ANNONCES MAIS ON GARDE LE SHUFFLE
  273. //$opti_gallery = array_slice($opti_gallery, 0, 3);
  274. //print_r($opti_gallery);
  275. shuffle($opti_gallery);
  276. // Bon plan jour = 1 annonce parmi parmi les 3 restantes
  277. $opti_gallery = array_slice($opti_gallery, 0, 1);
  278. //print_r($opti_gallery);
  279. return $opti_gallery;
  280. }
  281. /* Return ads filtered by critera */
  282. public function OptimizeGalleryCritera(
  283. $gallery,
  284. $array_ads_property_type,
  285. $value_ads_loc_id,
  286. $value_ads_loc_level,
  287. $value_ads_price_min='',
  288. $value_ads_price_max='',
  289. $value_ads_area_min='',
  290. $value_ads_area_max='') {
  291. //echo "LOC: ".$value_ads_loc_id.' - '.$value_ads_loc_level; echo "<br>";
  292. //echo "TYPE: ".print_r($array_ads_property_type, true); echo "<br>";
  293. //echo "PRICE: ".$value_ads_price_min.' - '.$value_ads_price_max; echo "<br>";
  294. //echo "AREA: ".$value_ads_area_min.' - '.$value_ads_area_max; echo "<br>";
  295. $opti_gallery = array();
  296. foreach ($gallery as $ad) {
  297. if (
  298. (
  299. (!empty($value_ads_loc_id) && !empty($value_ads_loc_level)) &&
  300. // filtrage sur le loc_id (si loc_level = 1 ou 2)
  301. (in_array($value_ads_loc_level, array(1, 2)) && ($ad['hierarchy'][$value_ads_loc_level]['lct_id'] == $value_ads_loc_id)) ||
  302. // filtrage sur le lgc_id (si loc_level = 98)
  303. (in_array($value_ads_loc_level, array(98)) && ($ad['lgc_id'] == $value_ads_loc_id)) ||
  304. // filtrage sur le lgn_id (si loc_level = 99)
  305. (in_array($value_ads_loc_level, array(99)) && ($ad['lgn_id'] == $value_ads_loc_id))
  306. ) &&
  307. // filtrage sur type(s) de bien, si spécifié
  308. (empty($array_ads_property_type) || in_array($ad['property_type_id_orig'], $array_ads_property_type)) &&
  309. // filtrage sur prix min, si spécifié
  310. (empty($value_ads_price_min) || ($ad['real_price'] >= $value_ads_price_min)) &&
  311. // filtrage sur prix max, si spécifié
  312. (empty($value_ads_price_max) || ($ad['real_price'] <= $value_ads_price_max)) &&
  313. // filtrage sur surface min, si spécifiée
  314. (empty($value_ads_area_min) || ($ad['area'] >= $value_ads_area_min)) &&
  315. // filtrage sur surface max, si spécifiée
  316. (empty($value_ads_area_max) || ($ad['area'] <= $value_ads_area_max))
  317. ) {
  318. //echo "[".$ad['lgc_id'].":".$ad['idx']."]"."\n";
  319. array_push($opti_gallery, $ad);
  320. }
  321. }
  322. return $opti_gallery;
  323. }
  324. /* Print Gallery */
  325. public function PrintGallery($tab_ads)
  326. {
  327. global $_global;
  328. if($_global['univers']==1) {
  329. $gallery_univers = "vente";
  330. $xitiClickImgId = 36;
  331. }
  332. else if($_global['univers']==2) {
  333. $gallery_univers = "location";
  334. $xitiClickImgId = 72;
  335. }
  336. else if($_global['univers']==8) {
  337. $gallery_univers = "colocation";
  338. $xitiClickImgId = 108;
  339. }
  340. foreach ($tab_ads as $ad) {
  341. // gallery datas
  342. $gallery_locality = ($ad["default_town_2"] == "" ? $ad["default_town_1"] : $ad["default_town_2"]);
  343. $gallery_zip_code = $ad["lzip"];
  344. $gallery_property_type = $ad["property_type"];
  345. $gallery_area = ($ad["area"] == 0? "" : $ad["area"]." m²");
  346. $gallery_nb_rooms = ($ad["nb_rooms"] == 0? "" : $ad["nb_rooms"]." pièce".($ad["nb_rooms"] > 1 ? "s" : ""));
  347. $gallery_id_photo = $ad["photos_id"][0];
  348. $gallery_price = ($ad["price"] == 0? "" : $ad["price"]." &euro;");
  349. // link to the detail page
  350. $gallery_detail_link = URL_SITE."detail-".$gallery_univers."-".strtolower($ad["id"]).".htm?ext=1";
  351. // bloc infos
  352. $temporary = array();
  353. if ($gallery_property_type != '') {
  354. $temporary[] = $gallery_property_type;
  355. }
  356. if ($gallery_area != 0) {
  357. $temporary[] = $gallery_area;
  358. }
  359. if ($gallery_nb_rooms != 0) {
  360. $temporary[] = $gallery_nb_rooms;
  361. }
  362. $gallery_infos = implode(' &#9679; ', $temporary);
  363. // bloc text
  364. $gallery_local = '';
  365. if (!empty($gallery_locality) && !empty($gallery_zip_code)) {
  366. $gallery_local_title = $gallery_local = $gallery_locality.' ('.$gallery_zip_code.')';
  367. if (strlen($gallery_local) > 25) { // trop long, on ne garde que le nom
  368. $gallery_local = $gallery_locality;
  369. if (strlen($gallery_local) > 25) { // encore trop long, on tronque le nom
  370. $gallery_local = truncate($gallery_local, 25);
  371. }
  372. }
  373. } else if (!empty($gallery_locality)) {
  374. $gallery_local_title = $gallery_local = $gallery_locality;
  375. } else if (!empty($gallery_zip_code)) {
  376. $gallery_local_title = $gallery_local = $gallery_zip_code;
  377. }
  378. // links title
  379. $gallery_title = $gallery_local_title.(!empty($gallery_infos) ? ' - '.$gallery_infos : '').(!empty($gallery_price) ? ' - '.$gallery_price : '');
  380. // url photo
  381. $gallery_photo_url = get_urlmmf_anc_seo(MMF_FORMAT_FR_ANC_CROP_GALLERY, $gallery_id_photo, $ad["id"], $gallery_property_type, $gallery_locality, 'FR');
  382. // html content
  383. $content = "";
  384. $content .= " <div class=\"spir_title spir_title-big\">L'ANNONCE DU JOUR</div>\n";
  385. $content .= " <div class=\"container\">\n";
  386. if (!empty($gallery_photo_url)) {
  387. $img_alt = ucfirst($gallery_univers)." ".$gallery_property_type." ".$gallery_locality." ".$gallery_area." ".$gallery_nb_rooms;
  388. $content .= " <a onclick=\"".getXitiClick($xitiClickImgId)."\" href=\"".$gallery_detail_link."\" title=\"".stripslashes($gallery_title)."\" class=\"img\">\n";
  389. $content .= " <img width=\"280\" height=\"205\" border=\"0\" src=\"".$gallery_photo_url."\" alt=\"".stripslashes($img_alt)."\"/>\n";
  390. $content .= " </a>\n";
  391. }
  392. if (!empty($gallery_detail_link) && !empty($gallery_title)) {
  393. $content .= " <a href=\"".$gallery_detail_link."\" title=\"".stripslashes($gallery_title)."\" class=\"crit\">\n";
  394. $content .= " <div class=\"crit\">\n";
  395. if (!empty($gallery_price)) {
  396. $content .= " <p class=\"crit_price\">".$gallery_price."</p>\n";
  397. }
  398. if (!empty($gallery_local)) {
  399. $content .= " <p class=\"crit_local\">".stripslashes($gallery_local)."</p>\n";
  400. }
  401. if (!empty($gallery_infos)) {
  402. $content .= " <p class=\"crit_infos\">".stripslashes($gallery_infos)."</p>\n";
  403. }
  404. $content .= " </div>\n";
  405. $content .= " </a>\n";
  406. }
  407. $content .= "</div>\n";
  408. }
  409. echo $content;
  410. }
  411. }
  412. ?>