PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/ajax/ziptown.php

https://github.com/asterix14/dolibarr
PHP | 135 lines | 81 code | 20 blank | 34 comment | 21 complexity | 8222d2df9af942775cfdbd6ccd259219 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2010 Regis Houssin <regis@dolibarr.fr>
  3. * Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/ajax/ziptown.php
  20. * \ingroup core
  21. * \brief File to return Ajax response on zipcode or town request
  22. */
  23. if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1); // Disables token renewal
  24. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  25. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  26. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  27. if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
  28. if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1');
  29. require('../../main.inc.php');
  30. require_once(DOL_DOCUMENT_ROOT."/core/class/html.formcompany.class.php");
  31. /*
  32. * View
  33. */
  34. // Ajout directives pour resoudre bug IE
  35. //header('Cache-Control: Public, must-revalidate');
  36. //header('Pragma: public');
  37. //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
  38. top_httphead();
  39. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  40. dol_syslog("GET is ".join(',',$_GET));
  41. //var_dump($_GET);
  42. // Generation of list of zip-town
  43. if (! empty($_GET['zipcode']) || ! empty($_GET['town']))
  44. {
  45. $return_arr = array();
  46. $formcompany = new FormCompany($db);
  47. // Define filter on text typed
  48. $zipcode = $_GET['zipcode']?$_GET['zipcode']:'';
  49. $town = $_GET['town']?$_GET['town']:'';
  50. if ($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY) // Use zip-town table
  51. {
  52. $sql = "SELECT z.rowid, z.zip, z.town, z.fk_county, z.fk_pays as fk_country";
  53. $sql.= ", p.rowid as fk_country, p.code as country_code, p.libelle as country";
  54. $sql.= ", d.rowid as fk_county, d.code_departement as county_code, d.nom as county";
  55. $sql.= " FROM (".MAIN_DB_PREFIX."c_ziptown as z,".MAIN_DB_PREFIX."c_pays as p)";
  56. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX ."c_departements as d ON z.fk_county = d.rowid";
  57. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r ON d.fk_region = r.code_region";
  58. $sql.= " WHERE z.fk_pays = p.rowid";
  59. $sql.= " AND z.active = 1 AND p.active = 1";
  60. if ($zipcode) $sql.=" AND z.zip LIKE '" . $db->escape($zipcode) . "%'";
  61. if ($town) $sql.=" AND z.town LIKE '%" . $db->escape($town) . "%'";
  62. $sql.= " ORDER BY z.zip, z.town";
  63. $sql.= $db->plimit(50); // Avoid pb with bad criteria
  64. }
  65. else // Use table of third parties
  66. {
  67. $sql = "SELECT DISTINCT s.cp as zip, s.ville as town, s.fk_departement as fk_county, s.fk_pays as fk_country";
  68. $sql.= ", p.code as country_code, p.libelle as country";
  69. $sql.= ", d.code_departement as county_code , d.nom as county";
  70. $sql.= " FROM ".MAIN_DB_PREFIX.'societe as s';
  71. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX ."c_departements as d ON fk_departement = d.rowid";
  72. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX.'c_pays as p ON fk_pays = p.rowid';
  73. $sql.= " WHERE";
  74. if ($zipcode) $sql.= " s.cp LIKE '".$db->escape($zipcode)."%'";
  75. if ($town) $sql.= " s.ville LIKE '%" . $db->escape($town) . "%'";
  76. $sql.= " ORDER BY s.fk_pays, s.cp, s.ville";
  77. $sql.= $db->plimit(50); // Avoid pb with bad criteria
  78. }
  79. //print $sql;
  80. $resql=$db->query($sql);
  81. //var_dump($db);
  82. if ($resql)
  83. {
  84. while ($row = $db->fetch_array($resql))
  85. {
  86. $country = $row['fk_country']?($langs->trans('Country'.$row['country_code'])!='Country'.$row['country_code']?$langs->trans('Country'.$row['country_code']):$row['country']):'';
  87. $county = $row['fk_county']?($langs->trans($row['county_code'])!=$row['county_code']?$langs->trans($row['county_code']):($row['county']!='-'?$row['county']:'')):'';
  88. $row_array['label'] = $row['zip'].' '.$row['town'];
  89. $row_array['label'] .= ($county || $country)?' (':'';
  90. $row_array['label'] .= $county;
  91. $row_array['label'] .= ($county && $country?' - ':'');
  92. $row_array['label'] .= $country;
  93. $row_array['label'] .= ($county || $country)?')':'';
  94. if ($zipcode)
  95. {
  96. $row_array['value'] = $row['zip'];
  97. $row_array['town'] = $row['town'];
  98. }
  99. if ($town)
  100. {
  101. $row_array['value'] = $row['town'];
  102. $row_array['zipcode'] = $row['zip'];
  103. }
  104. $row_array['selectpays_id'] = $row['fk_country'];
  105. $row_array['departement_id'] = $row['fk_county'];
  106. $row_array['states'] = $formcompany->select_state('',$row['fk_country'],'');
  107. array_push($return_arr,$row_array);
  108. }
  109. }
  110. echo json_encode($return_arr);
  111. }
  112. else
  113. {
  114. }
  115. ?>