/_plugins_/autocompletion/inc/autocompletion.php

https://bitbucket.org/pombredanne/spip-zone-treemap · PHP · 96 lines · 71 code · 16 blank · 9 comment · 20 complexity · 2cb36b3272b360367491ad53c5a8a6ee MD5 · raw file

  1. <?php
  2. /* Charger un SPIP minimum => Pour utiliser les foncitons spip (sql_, include_spip(), etc.)
  3. ----------------------------------------------- */
  4. if (!defined('_ECRIRE_INC_VERSION')) {
  5. // recherche du loader SPIP.
  6. $deep = 2;
  7. $lanceur = 'ecrire/inc_version.php';
  8. $include = '../../' . $lanceur;
  9. while (!defined('_ECRIRE_INC_VERSION') && $deep++ < 6) {
  10. // attention a pas descendre trop loin tout de meme !
  11. // plugins/zone/stable/nom/version/tests/ maximum cherche
  12. $include = '../' . $include;
  13. if (file_exists($include)) {
  14. chdir(dirname(dirname($include)));
  15. require $lanceur;
  16. }
  17. }
  18. }
  19. if (!defined('_ECRIRE_INC_VERSION')) {
  20. return 'SPIP NON CHARGE';
  21. }
  22. include_spip('base/abstract_sql');
  23. $ville = (_request('ville'))? trim(_request('ville')) : '' ;
  24. $cp = (_request('codePostal'))? trim(_request('codePostal')) : '' ;
  25. $cpRef = (_request('cpRef'))? trim(_request('cpRef')) : '' ;
  26. $maxRows = (_request('maxRows'))? trim(_request('maxRows')) : '' ;
  27. $delim = ",- "; // Les differents separateurs possible
  28. $message = '';
  29. $infos = array();
  30. if (!empty($cp) || !empty($ville)) {
  31. $liste = array();
  32. $where = '';
  33. $groupby = array();
  34. $orderby = array();
  35. $champs = array('code_postal CodePostal', 'lib_commune Ville', 'latitude Latitude', 'longitude Longitude');
  36. $from = array('spip_communes');
  37. if($ville){
  38. // On coupe la chaine en segments
  39. $tok = strtok($ville, $delim);
  40. // Boucle pour rechercher sur chaque segments
  41. while ($tok !== false) {
  42. // Au minimum 2 characters
  43. if(strlen($tok) > 1){
  44. $whereVille[] = "lib_commune LIKE '%".trim($tok)."%'";
  45. }
  46. $tok = strtok($delim);
  47. }
  48. if($cpRef){
  49. $whereVille[] = "code_postal LIKE '".sql_quote($cpRef)."%'";
  50. $whereCpRef[] = "code_postal LIKE '".sql_quote($cpRef)."%'";
  51. $orderby[] = "CASE WHEN "."(". join("\n\tAND ", $whereVille) .") "." THEN 1 ELSE 2 END";
  52. $message .= str_replace("%cp", $cpRef, _T('autocompletion:message_pas_resultat_commune_cp') ) ;
  53. }
  54. else{
  55. $orderby[] = "CASE WHEN "."(". "lib_commune LIKE '".sql_quote($ville)."%'" .") "." THEN 1 ELSE 2 END";
  56. }
  57. // Ce champ 'type résult' permet de controler s'il y a un résultat en correspondance à la recherche
  58. $champs[] = "(". "CASE WHEN "."(". join("\n\tAND ", $whereVille) .") "." THEN 1 ELSE 2 END" .") as type_result";
  59. $where .= "(". join("\n\tAND ", $whereVille) .") ";
  60. $where .= (!empty($whereCpRef))? "OR (". join("\n\tAND ", $whereCpRef) .") " : '';
  61. $orderby[] = "lib_commune, code_postal";
  62. }
  63. if($cp){
  64. $where[] = "code_postal LIKE '".sql_quote($cp)."%'";
  65. $orderby[] = "code_postal, lib_commune";
  66. $champs[] = "(". "CASE WHEN "."(". "code_postal LIKE '".sql_quote($cp)."%'" .") "." THEN 1 ELSE 2 END" .") as type_result";
  67. $message .= _T('autocompletion:message_pas_resultat_cp');
  68. }
  69. $limit = (!empty($maxRows))? $maxRows : '';
  70. if ($liste_des_communes = sql_select($champs, $from, $where, $groupby, $orderby, $limit)) {
  71. $resultat = false;
  72. while( $row=sql_fetch($liste_des_communes) ){
  73. if( $resultat == false && $row['type_result'] == '1') $resultat = true ;
  74. $liste[] = $row;
  75. }
  76. }
  77. if(!empty($message)) $infos [] = array("message" => $message);
  78. if($resultat === true) echo json_encode($liste);
  79. else echo json_encode(array_merge ($infos, $liste));
  80. }