ziptown.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /* Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011-2014 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 3 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 <https://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')) {
  24. define('NOTOKENRENEWAL', 1); // Disables token renewal
  25. }
  26. if (!defined('NOREQUIREMENU')) {
  27. define('NOREQUIREMENU', '1');
  28. }
  29. if (!defined('NOREQUIREHTML')) {
  30. define('NOREQUIREHTML', '1');
  31. }
  32. if (!defined('NOREQUIREAJAX')) {
  33. define('NOREQUIREAJAX', '1');
  34. }
  35. if (!defined('NOREQUIRESOC')) {
  36. define('NOREQUIRESOC', '1');
  37. }
  38. if (!defined('NOCSRFCHECK')) {
  39. define('NOCSRFCHECK', '1');
  40. }
  41. require '../../main.inc.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  43. /*
  44. * View
  45. */
  46. // Ajout directives pour resoudre bug IE
  47. //header('Cache-Control: Public, must-revalidate');
  48. //header('Pragma: public');
  49. //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
  50. top_httphead();
  51. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  52. dol_syslog('ziptown call with MAIN_USE_ZIPTOWN_DICTIONNARY='.(empty($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY) ? '' : $conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY));
  53. //var_dump($_GET);
  54. // Generation of list of zip-town
  55. if (GETPOST('zipcode') || GETPOST('town')) {
  56. $return_arr = array();
  57. $formcompany = new FormCompany($db);
  58. // Define filter on text typed
  59. $zipcode = GETPOST('zipcode');
  60. $town = GETPOST('town');
  61. if (!empty($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY)) { // Use zip-town table
  62. $sql = "SELECT z.rowid, z.zip, z.town, z.fk_county, z.fk_pays as fk_country";
  63. $sql .= ", c.rowid as fk_country, c.code as country_code, c.label as country";
  64. $sql .= ", d.rowid as fk_county, d.code_departement as county_code, d.nom as county";
  65. $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as z";
  66. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON z.fk_county = d.rowid";
  67. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r ON d.fk_region = r.code_region,";
  68. $sql .= " ".MAIN_DB_PREFIX."c_country as c";
  69. $sql .= " WHERE z.fk_pays = c.rowid";
  70. $sql .= " AND z.active = 1 AND c.active = 1";
  71. if ($zipcode) {
  72. $sql .= " AND z.zip LIKE '".$db->escape($zipcode)."%'";
  73. }
  74. if ($town) {
  75. $sql .= " AND z.town LIKE '%".$db->escape($town)."%'";
  76. }
  77. $sql .= " ORDER BY z.zip, z.town";
  78. $sql .= $db->plimit(100); // Avoid pb with bad criteria
  79. } else // Use table of third parties
  80. {
  81. $sql = "SELECT DISTINCT s.zip, s.town, s.fk_departement as fk_county, s.fk_pays as fk_country";
  82. $sql .= ", c.code as country_code, c.label as country";
  83. $sql .= ", d.code_departement as county_code , d.nom as county";
  84. $sql .= " FROM ".MAIN_DB_PREFIX.'societe as s';
  85. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON s.fk_departement = d.rowid";
  86. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
  87. $sql .= " WHERE";
  88. if ($zipcode) {
  89. $sql .= " s.zip LIKE '".$db->escape($zipcode)."%'";
  90. }
  91. if ($town) {
  92. $sql .= " s.town LIKE '%".$db->escape($town)."%'";
  93. }
  94. $sql .= " ORDER BY s.fk_pays, s.zip, s.town";
  95. $sql .= $db->plimit(100); // Avoid pb with bad criteria
  96. }
  97. //print $sql;
  98. $resql = $db->query($sql);
  99. //var_dump($db);
  100. if ($resql) {
  101. while ($row = $db->fetch_array($resql)) {
  102. $country = $row['fk_country'] ? ($langs->transnoentitiesnoconv('Country'.$row['country_code']) != 'Country'.$row['country_code'] ? $langs->transnoentitiesnoconv('Country'.$row['country_code']) : $row['country']) : '';
  103. $county = $row['fk_county'] ? ($langs->transnoentitiesnoconv($row['county_code']) != $row['county_code'] ? $langs->transnoentitiesnoconv($row['county_code']) : ($row['county'] != '-' ? $row['county'] : '')) : '';
  104. $row_array['label'] = $row['zip'].' '.$row['town'];
  105. $row_array['label'] .= ($county || $country) ? ' (' : '';
  106. $row_array['label'] .= $county;
  107. $row_array['label'] .= ($county && $country ? ' - ' : '');
  108. $row_array['label'] .= $country;
  109. $row_array['label'] .= ($county || $country) ? ')' : '';
  110. if ($zipcode) {
  111. $row_array['value'] = $row['zip'];
  112. $row_array['town'] = $row['town'];
  113. }
  114. if ($town) {
  115. $row_array['value'] = $row['town'];
  116. $row_array['zipcode'] = $row['zip'];
  117. }
  118. $row_array['selectcountry_id'] = $row['fk_country'];
  119. $row_array['state_id'] = $row['fk_county'];
  120. // TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
  121. $row_array['states'] = $formcompany->select_state('', $row['fk_country'], '');
  122. array_push($return_arr, $row_array);
  123. }
  124. }
  125. echo json_encode($return_arr);
  126. }
  127. $db->close();