dolibarr  16.0.5
ziptown.php
Go to the documentation of this file.
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 
25 if (!defined('NOTOKENRENEWAL')) {
26  define('NOTOKENRENEWAL', 1); // Disables token renewal
27 }
28 if (!defined('NOREQUIREMENU')) {
29  define('NOREQUIREMENU', '1');
30 }
31 if (!defined('NOREQUIREHTML')) {
32  define('NOREQUIREHTML', '1');
33 }
34 if (!defined('NOREQUIREAJAX')) {
35  define('NOREQUIREAJAX', '1');
36 }
37 if (!defined('NOREQUIRESOC')) {
38  define('NOREQUIRESOC', '1');
39 }
40 if (!defined('NOCSRFCHECK')) {
41  define('NOCSRFCHECK', '1');
42 }
43 
44 require '../../main.inc.php';
45 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
46 
47 
48 
49 /*
50  * View
51  */
52 
53 // Ajout directives pour resoudre bug IE
54 //header('Cache-Control: Public, must-revalidate');
55 //header('Pragma: public');
56 
57 //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
58 top_httphead();
59 
60 //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
61 
62 dol_syslog('ziptown call with MAIN_USE_ZIPTOWN_DICTIONNARY='.(empty($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY) ? '' : $conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY));
63 //var_dump($_GET);
64 
65 // Generation of list of zip-town
66 if (GETPOST('zipcode') || GETPOST('town')) {
67  $return_arr = array();
68  $formcompany = new FormCompany($db);
69 
70  // Define filter on text typed
71  $zipcode = GETPOST('zipcode');
72  $town = GETPOST('town');
73 
74  if (!empty($conf->global->MAIN_USE_ZIPTOWN_DICTIONNARY)) { // Use zip-town table
75  $sql = "SELECT z.rowid, z.zip, z.town, z.fk_county, z.fk_pays as fk_country";
76  $sql .= ", c.rowid as fk_country, c.code as country_code, c.label as country";
77  $sql .= ", d.rowid as fk_county, d.code_departement as county_code, d.nom as county";
78  $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as z";
79  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON z.fk_county = d.rowid";
80  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r ON d.fk_region = r.code_region,";
81  $sql .= " ".MAIN_DB_PREFIX."c_country as c";
82  $sql .= " WHERE z.fk_pays = c.rowid";
83  $sql .= " AND z.active = 1 AND c.active = 1";
84  if ($zipcode) {
85  $sql .= " AND z.zip LIKE '".$db->escape($zipcode)."%'";
86  }
87  if ($town) {
88  $sql .= " AND z.town LIKE '%".$db->escape($town)."%'";
89  }
90  $sql .= " ORDER BY z.zip, z.town";
91  $sql .= $db->plimit(100); // Avoid pb with bad criteria
92  } else // Use table of third parties
93  {
94  $sql = "SELECT DISTINCT s.zip, s.town, s.fk_departement as fk_county, s.fk_pays as fk_country";
95  $sql .= ", c.code as country_code, c.label as country";
96  $sql .= ", d.code_departement as county_code , d.nom as county";
97  $sql .= " FROM ".MAIN_DB_PREFIX.'societe as s';
98  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON s.fk_departement = d.rowid";
99  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
100  $sql .= " WHERE";
101  if ($zipcode) {
102  $sql .= " s.zip LIKE '".$db->escape($zipcode)."%'";
103  }
104  if ($town) {
105  $sql .= " s.town LIKE '%".$db->escape($town)."%'";
106  }
107  $sql .= " ORDER BY s.fk_pays, s.zip, s.town";
108  $sql .= $db->plimit(100); // Avoid pb with bad criteria
109  }
110 
111  //print $sql;
112  $resql = $db->query($sql);
113  //var_dump($db);
114  if ($resql) {
115  while ($row = $db->fetch_array($resql)) {
116  $country = $row['fk_country'] ? ($langs->transnoentitiesnoconv('Country'.$row['country_code']) != 'Country'.$row['country_code'] ? $langs->transnoentitiesnoconv('Country'.$row['country_code']) : $row['country']) : '';
117  $county = $row['fk_county'] ? ($langs->transnoentitiesnoconv($row['county_code']) != $row['county_code'] ? $langs->transnoentitiesnoconv($row['county_code']) : ($row['county'] != '-' ? $row['county'] : '')) : '';
118 
119  $row_array['label'] = $row['zip'].' '.$row['town'];
120  $row_array['label'] .= ($county || $country) ? ' (' : '';
121  $row_array['label'] .= $county;
122  $row_array['label'] .= ($county && $country ? ' - ' : '');
123  $row_array['label'] .= $country;
124  $row_array['label'] .= ($county || $country) ? ')' : '';
125  if ($zipcode) {
126  $row_array['value'] = $row['zip'];
127  $row_array['town'] = $row['town'];
128  }
129  if ($town) {
130  $row_array['value'] = $row['town'];
131  $row_array['zipcode'] = $row['zip'];
132  }
133  $row_array['selectcountry_id'] = $row['fk_country'];
134  $row_array['state_id'] = $row['fk_county'];
135 
136  // TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
137  $row_array['states'] = $formcompany->select_state('', $row['fk_country'], '');
138 
139  array_push($return_arr, $row_array);
140  }
141  }
142 
143  echo json_encode($return_arr);
144 }
145 
146 $db->close();
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
top_httphead
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1407
FormCompany
Class to build HTML component for third parties management Only common components are here.
Definition: html.formcompany.class.php:40
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742