2010-10-12 17:59:31 +02:00
< ? php
2020-07-05 13:50:21 +02:00
/* Copyright ( C ) 2010 Regis Houssin < regis . houssin @ inodbox . com >
* Copyright ( C ) 2011 - 2014 Laurent Destailleur < eldy @ users . sourceforge . net >
2010-10-12 17:59:31 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2010-10-12 17:59:31 +02:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2010-10-12 17:59:31 +02:00
*/
/**
2011-10-25 19:03:12 +02:00
* \file htdocs / core / ajax / ziptown . php
2010-10-27 23:51:41 +02:00
* \ingroup core
* \brief File to return Ajax response on zipcode or town request
2010-10-12 17:59:31 +02:00
*/
2021-02-23 22:03:23 +01:00
if ( ! defined ( 'NOTOKENRENEWAL' )) {
define ( 'NOTOKENRENEWAL' , 1 ); // Disables token renewal
}
if ( ! defined ( 'NOREQUIREMENU' )) {
define ( 'NOREQUIREMENU' , '1' );
}
if ( ! defined ( 'NOREQUIREHTML' )) {
define ( 'NOREQUIREHTML' , '1' );
}
if ( ! defined ( 'NOREQUIREAJAX' )) {
define ( 'NOREQUIREAJAX' , '1' );
}
if ( ! defined ( 'NOREQUIRESOC' )) {
define ( 'NOREQUIRESOC' , '1' );
}
if ( ! defined ( 'NOCSRFCHECK' )) {
define ( 'NOCSRFCHECK' , '1' );
}
2010-10-12 17:59:31 +02:00
2012-08-22 23:24:21 +02:00
require '../../main.inc.php' ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formcompany.class.php' ;
2010-10-12 17:59:31 +02:00
2011-04-10 23:47:24 +02:00
2010-10-12 17:59:31 +02:00
/*
* View
*/
// Ajout directives pour resoudre bug IE
//header('Cache-Control: Public, must-revalidate');
//header('Pragma: public');
//top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
top_httphead ();
2017-06-18 19:42:59 +02:00
//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
2010-10-12 17:59:31 +02:00
2021-03-31 12:41:44 +02:00
dol_syslog ( 'ziptown call with MAIN_USE_ZIPTOWN_DICTIONNARY=' . ( empty ( $conf -> global -> MAIN_USE_ZIPTOWN_DICTIONNARY ) ? '' : $conf -> global -> MAIN_USE_ZIPTOWN_DICTIONNARY ));
2011-05-29 13:14:15 +02:00
//var_dump($_GET);
2010-10-12 17:59:31 +02:00
2011-04-10 23:47:24 +02:00
// Generation of list of zip-town
2021-03-31 12:41:44 +02:00
if ( GETPOST ( 'zipcode' ) || GETPOST ( 'town' )) {
2010-10-12 17:59:31 +02:00
$return_arr = array ();
2010-10-13 18:36:10 +02:00
$formcompany = new FormCompany ( $db );
2010-10-12 17:59:31 +02:00
// Define filter on text typed
2021-03-31 12:41:44 +02:00
$zipcode = GETPOST ( 'zipcode' );
$town = GETPOST ( 'town' );
2010-10-12 17:59:31 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_USE_ZIPTOWN_DICTIONNARY )) { // Use zip-town table
2020-03-23 15:54:02 +01:00
$sql = " SELECT z.rowid, z.zip, z.town, z.fk_county, z.fk_pays as fk_country " ;
2020-04-10 10:59:32 +02:00
$sql .= " , c.rowid as fk_country, c.code as country_code, c.label as country " ;
$sql .= " , d.rowid as fk_county, d.code_departement as county_code, d.nom as county " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_ziptown as z " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_departements as d ON z.fk_county = d.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_regions as r ON d.fk_region = r.code_region, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_country as c " ;
$sql .= " WHERE z.fk_pays = c.rowid " ;
$sql .= " AND z.active = 1 AND c.active = 1 " ;
2021-02-23 22:03:23 +01:00
if ( $zipcode ) {
$sql .= " AND z.zip LIKE ' " . $db -> escape ( $zipcode ) . " %' " ;
}
if ( $town ) {
$sql .= " AND z.town LIKE '% " . $db -> escape ( $town ) . " %' " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY z.zip, z.town " ;
$sql .= $db -> plimit ( 100 ); // Avoid pb with bad criteria
2020-05-21 15:05:19 +02:00
} else // Use table of third parties
2011-04-10 23:47:24 +02:00
{
2020-03-23 15:54:02 +01:00
$sql = " SELECT DISTINCT s.zip, s.town, s.fk_departement as fk_county, s.fk_pays as fk_country " ;
2020-04-10 10:59:32 +02:00
$sql .= " , c.code as country_code, c.label as country " ;
$sql .= " , d.code_departement as county_code , d.nom as county " ;
$sql .= " FROM " . MAIN_DB_PREFIX . 'societe as s' ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_departements as d ON s.fk_departement = d.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . 'c_country as c ON s.fk_pays = c.rowid' ;
$sql .= " WHERE " ;
2021-02-23 22:03:23 +01:00
if ( $zipcode ) {
$sql .= " s.zip LIKE ' " . $db -> escape ( $zipcode ) . " %' " ;
}
if ( $town ) {
$sql .= " s.town LIKE '% " . $db -> escape ( $town ) . " %' " ;
}
2020-04-10 10:59:32 +02:00
$sql .= " ORDER BY s.fk_pays, s.zip, s.town " ;
$sql .= $db -> plimit ( 100 ); // Avoid pb with bad criteria
2011-04-10 23:47:24 +02:00
}
2020-03-23 15:54:02 +01:00
//print $sql;
2020-04-10 10:59:32 +02:00
$resql = $db -> query ( $sql );
2010-11-03 18:47:37 +01:00
//var_dump($db);
2021-02-23 22:03:23 +01:00
if ( $resql ) {
while ( $row = $db -> fetch_array ( $resql )) {
2020-04-10 10:59:32 +02:00
$country = $row [ 'fk_country' ] ? ( $langs -> transnoentitiesnoconv ( 'Country' . $row [ 'country_code' ]) != 'Country' . $row [ 'country_code' ] ? $langs -> transnoentitiesnoconv ( 'Country' . $row [ 'country_code' ]) : $row [ 'country' ]) : '' ;
$county = $row [ 'fk_county' ] ? ( $langs -> transnoentitiesnoconv ( $row [ 'county_code' ]) != $row [ 'county_code' ] ? $langs -> transnoentitiesnoconv ( $row [ 'county_code' ]) : ( $row [ 'county' ] != '-' ? $row [ 'county' ] : '' )) : '' ;
2011-04-10 23:47:24 +02:00
$row_array [ 'label' ] = $row [ 'zip' ] . ' ' . $row [ 'town' ];
2020-04-10 10:59:32 +02:00
$row_array [ 'label' ] .= ( $county || $country ) ? ' (' : '' ;
2020-03-23 15:54:02 +01:00
$row_array [ 'label' ] .= $county ;
2020-04-10 10:59:32 +02:00
$row_array [ 'label' ] .= ( $county && $country ? ' - ' : '' );
2020-03-23 15:54:02 +01:00
$row_array [ 'label' ] .= $country ;
2020-04-10 10:59:32 +02:00
$row_array [ 'label' ] .= ( $county || $country ) ? ')' : '' ;
2021-02-23 22:03:23 +01:00
if ( $zipcode ) {
2011-04-10 23:47:24 +02:00
$row_array [ 'value' ] = $row [ 'zip' ];
$row_array [ 'town' ] = $row [ 'town' ];
2010-10-12 17:59:31 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $town ) {
2011-04-10 23:47:24 +02:00
$row_array [ 'value' ] = $row [ 'town' ];
$row_array [ 'zipcode' ] = $row [ 'zip' ];
}
2011-12-28 21:30:33 +01:00
$row_array [ 'selectcountry_id' ] = $row [ 'fk_country' ];
$row_array [ 'state_id' ] = $row [ 'fk_county' ];
2011-04-10 23:47:24 +02:00
2018-08-13 16:44:49 +02:00
// TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
2019-01-27 11:55:16 +01:00
$row_array [ 'states' ] = $formcompany -> select_state ( '' , $row [ 'fk_country' ], '' );
2011-04-10 23:47:24 +02:00
2019-01-27 11:55:16 +01:00
array_push ( $return_arr , $row_array );
2010-10-12 17:59:31 +02:00
}
}
2010-10-27 23:51:41 +02:00
2010-10-12 17:59:31 +02:00
echo json_encode ( $return_arr );
}
2012-06-27 23:54:31 +02:00
$db -> close ();