dolibarr 21.0.0-alpha
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', 1); // Disables token renewal
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1');
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', '1');
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', '1');
37}
38if (!defined('NOREQUIRESOC')) {
39 define('NOREQUIRESOC', '1');
40}
41
42// Load Dolibarr environment
43require '../../main.inc.php';
44require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
45
46// Security check
47if (!getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY')) {
48 // 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.
49 $result = restrictedArea($user, 'societe', 0, '&societe', '', 'fk_soc', 'rowid', 0);
50}
51
52
53/*
54 * View
55 */
56
57//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
58
59dol_syslog('ziptown call with MAIN_USE_ZIPTOWN_DICTIONNARY='.getDolGlobalString('MAIN_USE_ZIPTOWN_DICTIONNARY'));
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 as state_id, z.fk_pays as country_id";
74 $sql .= ", c.code as country_code, c.label as country_label";
75 $sql .= ", d.code_departement as state_code, d.nom as state_label";
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 state_id, s.fk_pays as country_id";
92 $sql .= ", c.code as country_code, c.label as country_label";
93 $sql .= ", d.code_departement as state_code, d.nom as state_label";
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 $row_array = [];
114 $country = $row['country_id'] ? ($langs->transnoentitiesnoconv('Country'.$row['country_code']) != 'Country'.$row['country_code'] ? $langs->transnoentitiesnoconv('Country'.$row['country_code']) : $row['country_label']) : '';
115 $county = $row['state_id'] ? ($langs->transnoentitiesnoconv($row['state_code']) != $row['state_code'] ? $langs->transnoentitiesnoconv($row['state_code']) : ($row['state_label'] != '-' ? $row['state_label'] : '')) : '';
116
117 $row_array['label'] = $row['zip'].' '.$row['town'];
118 $row_array['label'] .= ($county || $country) ? ' (' : '';
119 $row_array['label'] .= $county;
120 $row_array['label'] .= ($county && $country ? ' - ' : '');
121 $row_array['label'] .= $country;
122 $row_array['label'] .= ($county || $country) ? ')' : '';
123 if ($zipcode) {
124 $row_array['value'] = $row['zip'];
125 $row_array['town'] = $row['town'];
126 }
127 if ($town) {
128 $row_array['value'] = $row['town'];
129 $row_array['zipcode'] = $row['zip'];
130 }
131 $row_array['selectcountry_id'] = $row['country_id'];
132 $row_array['state_id'] = $row['state_id'];
133
134 // TODO Use a cache here to avoid to make select_state in each pass (this make a SQL and lot of logs)
135 $row_array['states'] = $formcompany->select_state(0, $row['country_id'], '');
136
137 array_push($return_arr, $row_array);
138 }
139 }
140
141 echo json_encode($return_arr);
142} elseif (GETPOSTISSET('country_codeid')) {
143 top_httphead('text/html');
144
145 $formcompany = new FormCompany($db);
146 print $formcompany->select_state(GETPOSTINT('selected', 1), GETPOSTINT('country_codeid', 1), GETPOST('htmlname', 'alpha', 1), GETPOST('morecss', 'alpha', 1));
147}
148
149$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.