dolibarr 19.0.3
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-2023 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
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', 1); // Disables token renewal
27}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREHTML')) {
32 define('NOREQUIREHTML', '1');
33}
34if (!defined('NOREQUIREAJAX')) {
35 define('NOREQUIREAJAX', '1');
36}
37if (!defined('NOREQUIRESOC')) {
38 define('NOREQUIRESOC', '1');
39}
40
41// Load Dolibarr environment
42require '../../main.inc.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
44
45// Security check
46if (!getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY')) {
47 // If MAIN_USE_ZIPTOWN_DICTIONNARY is set, we make a search into public data (official list of zip/town). If not we search into company data, so we must check we have read permission.
48 $result = restrictedArea($user, 'societe', 0, '&societe', '', 'fk_soc', 'rowid', 0);
49}
50
51
52/*
53 * View
54 */
55
56//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
57
58dol_syslog('ziptown call with MAIN_USE_ZIPTOWN_DICTIONNARY='.getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY'));
59//var_dump($_GET);
60
61// Generation of list of zip-town
62if (GETPOST('zipcode') || GETPOST('town')) {
63 top_httphead('application/json');
64
65 $return_arr = array();
66 $formcompany = new FormCompany($db);
67
68 // Define filter on text typed
69 $zipcode = GETPOST('zipcode');
70 $town = GETPOST('town');
71
72 if (getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY')) { // Use zip-town table
73 $sql = "SELECT z.rowid, z.zip, z.town, z.fk_county, z.fk_pays as fk_country";
74 $sql .= ", c.rowid as fk_country, c.code as country_code, c.label as country";
75 $sql .= ", d.rowid as fk_county, d.code_departement as county_code, d.nom as county";
76 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as z";
77 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON z.fk_county = d.rowid";
78 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r ON d.fk_region = r.code_region,";
79 $sql .= " ".MAIN_DB_PREFIX."c_country as c";
80 $sql .= " WHERE z.fk_pays = c.rowid";
81 $sql .= " AND z.active = 1 AND c.active = 1";
82 if ($zipcode) {
83 $sql .= " AND z.zip LIKE '".$db->escape($db->escapeforlike($zipcode))."%'";
84 }
85 if ($town) {
86 $sql .= " AND z.town LIKE '%".$db->escape($db->escapeforlike($town))."%'";
87 }
88 $sql .= " ORDER BY z.zip, z.town";
89 $sql .= $db->plimit(100); // Avoid pb with bad criteria
90 } else { // Use table of third parties
91 $sql = "SELECT DISTINCT s.zip, s.town, s.fk_departement as fk_county, s.fk_pays as fk_country";
92 $sql .= ", c.code as country_code, c.label as country";
93 $sql .= ", d.code_departement as county_code , d.nom as county";
94 $sql .= " FROM ".MAIN_DB_PREFIX.'societe as s';
95 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON s.fk_departement = d.rowid";
96 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
97 $sql .= " WHERE";
98 if ($zipcode) {
99 $sql .= " s.zip LIKE '".$db->escape($db->escapeforlike($zipcode))."%'";
100 }
101 if ($town) {
102 $sql .= " s.town LIKE '%".$db->escape($db->escapeforlike($town))."%'";
103 }
104 $sql .= " ORDER BY s.fk_pays, s.zip, s.town";
105 $sql .= $db->plimit(100); // Avoid pb with bad criteria
106 }
107
108 //print $sql;
109 $resql = $db->query($sql);
110 //var_dump($db);
111 if ($resql) {
112 while ($row = $db->fetch_array($resql)) {
113 $country = $row['fk_country'] ? ($langs->transnoentitiesnoconv('Country'.$row['country_code']) != 'Country'.$row['country_code'] ? $langs->transnoentitiesnoconv('Country'.$row['country_code']) : $row['country']) : '';
114 $county = $row['fk_county'] ? ($langs->transnoentitiesnoconv($row['county_code']) != $row['county_code'] ? $langs->transnoentitiesnoconv($row['county_code']) : ($row['county'] != '-' ? $row['county'] : '')) : '';
115
116 $row_array['label'] = $row['zip'].' '.$row['town'];
117 $row_array['label'] .= ($county || $country) ? ' (' : '';
118 $row_array['label'] .= $county;
119 $row_array['label'] .= ($county && $country ? ' - ' : '');
120 $row_array['label'] .= $country;
121 $row_array['label'] .= ($county || $country) ? ')' : '';
122 if ($zipcode) {
123 $row_array['value'] = $row['zip'];
124 $row_array['town'] = $row['town'];
125 }
126 if ($town) {
127 $row_array['value'] = $row['town'];
128 $row_array['zipcode'] = $row['zip'];
129 }
130 $row_array['selectcountry_id'] = $row['fk_country'];
131 $row_array['state_id'] = $row['fk_county'];
132
133 // TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
134 $row_array['states'] = $formcompany->select_state('', $row['fk_country'], '');
135
136 array_push($return_arr, $row_array);
137 }
138 }
139
140 echo json_encode($return_arr);
141} elseif (GETPOSTISSET('country_codeid')) {
142 top_httphead('text/html');
143
144 $formcompany = new FormCompany($db);
145 print $formcompany->select_state(GETPOST('selected', 'int', 1), GETPOST('country_codeid', 'int', 1), GETPOST('htmlname', 'alpha', 1), GETPOST('morecss', 'alpha', 1));
146}
147
148$db->close();
Class to build HTML component for third parties management Only common components are here.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.