PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/ajax/ziptown.php

https://bitbucket.org/speedealing/speedealing
PHP | 128 lines | 63 code | 22 blank | 43 comment | 17 complexity | 195aa0be4db85971e2eb81ee00eecb02 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2011-2013 Regis Houssin <regis.houssin@capnetworks.com>
  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 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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file 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. if (! defined('NOREQUIREMENU'))
  26. define('NOREQUIREMENU','1');
  27. if (! defined('NOREQUIREHTML'))
  28. define('NOREQUIREHTML','1');
  29. if (! defined('NOREQUIREAJAX'))
  30. define('NOREQUIREAJAX','1');
  31. if (! defined('NOREQUIRESOC'))
  32. define('NOREQUIRESOC','1');
  33. if (! defined('NOCSRFCHECK'))
  34. define('NOCSRFCHECK','1');
  35. require '../../main.inc.php';
  36. require_once DOL_DOCUMENT_ROOT . '/admin/class/dict.class.php';
  37. //require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
  38. $zipcode = GETPOST('zipcode', 'alpha');
  39. $town = GETPOST('town', 'alpha');
  40. /*
  41. * View
  42. */
  43. top_httphead('json');
  44. //print '<!-- Ajax page called with url '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  45. //var_dump($_GET);
  46. // Generation of list of zip-town
  47. if (!empty($zipcode) || !empty($town))
  48. {
  49. $dict = new Dict($db);
  50. if (!empty($zipcode)) {
  51. $startkey = array($zipcode);
  52. if (is_numeric($zipcode))
  53. $endkey = array($zipcode . '9999');
  54. else
  55. $endkey = array($zipcode . '\uffff');
  56. $params = array('group' => true, 'startkey' => $startkey, 'endkey' => $endkey);
  57. $result = $dict->getView('townByZip', $params);
  58. } else {
  59. $startkey = array($town);
  60. $endkey = array($town . '\uffff');
  61. $params = array('group' => true, 'startkey' => $startkey, 'endkey' => $endkey);
  62. $result = $dict->getView('zipByTown', $params);
  63. }
  64. $return_arr = array();
  65. $row_array = array();
  66. if (!empty($result->rows)) {
  67. foreach ($result->rows as $aRow) {
  68. $zipcodeval = (!empty($zipcode)?$aRow->key[0]:$aRow->key[1]);
  69. $townval = (!empty($zipcode)?$aRow->key[1]:$aRow->key[0]);
  70. $country = ($aRow->key[2]?$langs->trans('Country'.$aRow->key[2]):'');
  71. $county = ($aRow->key[3]?$langs->trans($aRow->key[3]):'');
  72. $row_array['label'] = $zipcodeval.' '.$townval;
  73. $row_array['label'] .= ($county || $country)?' (':'';
  74. $row_array['label'] .= $county;
  75. $row_array['label'] .= ($county && $country?' - ':'');
  76. $row_array['label'] .= $country;
  77. $row_array['label'] .= ($county || $country)?')':'';
  78. if (!empty($zipcode)) {
  79. $row_array['value'] = $zipcodeval;
  80. $row_array['town'] = $townval;
  81. } else {
  82. $row_array['value'] = $townval;
  83. $row_array['zipcode'] = $zipcodeval;
  84. }
  85. $row_array['selectcountry_id'] = $aRow->key[2];
  86. array_push($return_arr, $row_array);
  87. }
  88. }
  89. /*
  90. if ($resql)
  91. {
  92. while ($row = $db->fetch_array($resql))
  93. {
  94. $row_array['departement_id'] = $row['fk_county']; // deprecated
  95. $row_array['selectcountry_id'] = $row['fk_country'];
  96. $row_array['state_id'] = $row['fk_county'];
  97. $row_array['states'] = $formcompany->select_state('',$row['fk_country'],'');
  98. array_push($return_arr,$row_array);
  99. }
  100. }
  101. */
  102. echo json_encode($return_arr);
  103. }
  104. ?>