dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27if (!defined('NOTOKENRENEWAL')) {
28 define('NOTOKENRENEWAL', 1); // Disables token renewal
29}
30if (!defined('NOREQUIREMENU')) {
31 define('NOREQUIREMENU', '1');
32}
33if (!defined('NOREQUIREHTML')) {
34 define('NOREQUIREHTML', '1');
35}
36if (!defined('NOREQUIREAJAX')) {
37 define('NOREQUIREAJAX', '1');
38}
39if (!defined('NOREQUIRESOC')) {
40 define('NOREQUIRESOC', '1');
41}
42
43// Load Dolibarr environment
44require '../../main.inc.php';
45require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
54// Security check
55if (!getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY')) {
56 // 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.
57 $result = restrictedArea($user, 'societe', 0, '&societe', '', 'fk_soc', 'rowid', 0);
58}
59
60
61/*
62 * View
63 */
64
65//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
66
67dol_syslog('ziptown call with MAIN_USE_ZIPTOWN_DICTIONNARY='.getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY'));
68
69// Generation of list of zip-town
70if (GETPOST('zipcode') || GETPOST('town')) {
71 top_httphead('application/json');
72
73 $return_arr = array();
74 $formcompany = new FormCompany($db);
75
76 // Define filter on text typed
77 $zipcode = GETPOST('zipcode');
78 $town = GETPOST('town');
79
80 if (getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY')) { // Use zip-town table
81 $sql = "SELECT z.rowid, z.zip, z.town, z.fk_county as state_id, z.fk_pays as country_id";
82 $sql .= ", c.code as country_code, c.label as country_label";
83 $sql .= ", d.code_departement as state_code, d.nom as state_label";
84 $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as z";
85 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON z.fk_county = d.rowid";
86 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_regions as r ON d.fk_region = r.code_region,";
87 $sql .= " ".MAIN_DB_PREFIX."c_country as c";
88 $sql .= " WHERE z.fk_pays = c.rowid";
89 $sql .= " AND z.active = 1 AND c.active = 1";
90 if ($zipcode) {
91 $sql .= " AND z.zip LIKE '".$db->escape($db->escapeforlike($zipcode))."%'";
92 }
93 if ($town) {
94 $sql .= " AND z.town LIKE '%".$db->escape($db->escapeforlike($town))."%'";
95 }
96 $sql .= " ORDER BY z.zip, z.town";
97 $sql .= $db->plimit(100); // Avoid pb with bad criteria
98 } else { // Use table of third parties
99 $sql = "SELECT DISTINCT s.zip, s.town, s.fk_departement as state_id, s.fk_pays as country_id";
100 $sql .= ", c.code as country_code, c.label as country_label";
101 $sql .= ", d.code_departement as state_code, d.nom as state_label";
102 $sql .= " FROM ".MAIN_DB_PREFIX.'societe as s';
103 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON s.fk_departement = d.rowid";
104 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX.'c_country as c ON s.fk_pays = c.rowid';
105 $sql .= " WHERE";
106 if ($zipcode) {
107 $sql .= " s.zip LIKE '".$db->escape($db->escapeforlike($zipcode))."%'";
108 }
109 if ($town) {
110 $sql .= " s.town LIKE '%".$db->escape($db->escapeforlike($town))."%'";
111 }
112 $sql .= " ORDER BY s.fk_pays, s.zip, s.town";
113 $sql .= $db->plimit(100); // Avoid pb with bad criteria
114 }
115
116 //print $sql;
117 $resql = $db->query($sql);
118 //var_dump($db);
119 if ($resql) {
120 while ($row = $db->fetch_array($resql)) {
121 $row_array = [];
122 $country = $row['country_id'] ? ($langs->transnoentitiesnoconv('Country'.$row['country_code']) != 'Country'.$row['country_code'] ? $langs->transnoentitiesnoconv('Country'.$row['country_code']) : $row['country_label']) : '';
123 $county = $row['state_id'] ? ($langs->transnoentitiesnoconv($row['state_code']) != $row['state_code'] ? $langs->transnoentitiesnoconv($row['state_code']) : ($row['state_label'] != '-' ? $row['state_label'] : '')) : '';
124
125 $row_array['label'] = $row['zip'].' '.$row['town'];
126 $row_array['label'] .= ($county || $country) ? ' (' : '';
127 $row_array['label'] .= $county;
128 $row_array['label'] .= ($county && $country ? ' - ' : '');
129 $row_array['label'] .= $country;
130 $row_array['label'] .= ($county || $country) ? ')' : '';
131 if ($zipcode) {
132 $row_array['value'] = $row['zip'];
133 $row_array['town'] = $row['town'];
134 }
135 if ($town) {
136 $row_array['value'] = $row['town'];
137 $row_array['zipcode'] = $row['zip'];
138 }
139 $row_array['selectcountry_id'] = $row['country_id'];
140 $row_array['state_id'] = $row['state_id'];
141
142 // TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
143 $row_array['states'] = $formcompany->select_state(0, $row['country_id'], '');
144
145 array_push($return_arr, $row_array);
146 }
147 }
148
149 echo json_encode($return_arr);
150} elseif (GETPOSTISSET('country_codeid')) {
151 top_httphead('text/html');
152
153 $formcompany = new FormCompany($db);
154 print $formcompany->select_state(GETPOSTINT('selected', 1), GETPOSTINT('country_codeid', 1), GETPOST('htmlname', 'alpha', 1), GETPOST('morecss', 'alpha', 1));
155}
156
157$db->close();
Class to build HTML component for third parties management Only common components are here.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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 a 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.