dolibarr 19.0.4
ajaxcompanies.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
3 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
5 * Copyright (C) 2010 Cyrille de Lambert <info@auguria.net>
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
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.'/societe/class/societe.class.php';
45
46$id = GETPOST('socid', 'int') || GETPOST('id_fourn', 'int');
47
48$object = new Societe($db);
49if ($id > 0) {
50 $object->fetch($id);
51}
52
53// Security check
54if ($user->socid > 0) {
55 $socid = $user->socid;
56 if ($object->socid && $socid != $object->socid) {
57 accessforbidden('Not allowed to access thirdparty id '.$id.' with an external user on id '.$socid);
58 }
59}
60restrictedArea($user, 'societe', $object, '&societe');
61
62
63/*
64 * View
65 */
66
67//top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
68top_httphead('application/json');
69
70//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
71
72
73$return_arr = array();
74
75// Define filter on text typed
76$socid = GETPOST('newcompany');
77if (!$socid) {
78 $socid = GETPOST('socid', 'int');
79}
80if (!$socid) {
81 $socid = GETPOST('id_fourn', 'int');
82}
83
84// Generate list of companies
85if (! $socid) {
86 echo json_encode(array('nom'=>'ErrorBadParameter', 'label'=>'ErrorBadParameter', 'key'=>'ErrorBadParameter', 'value'=>'ErrorBadParameter'));
87 exit;
88}
89
90$sql = "SELECT s.rowid, s.nom, s.name_alias, s.code_client, s.code_fournisseur, s.address, s.zip, s.town, s.email, s.siren, s.siret, s.ape, s.idprof4, s.idprof5, s.idprof6, s.client, s.fournisseur, s.datec, s.logo";
91if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
92 $sql .= ", dictp.code as country_code";
93}
94$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
95if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
96 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as dictp ON dictp.rowid = s.fk_pays";
97}
98$sql .= " WHERE s.entity IN (".getEntity('societe').")";
99if ($socid) {
100 $sql .= " AND (";
101 // Add criteria on name/code
102 if (getDolGlobalString('COMPANY_DONOTSEARCH_ANYWHERE')) { // Can use index
103 $sql .= "s.nom LIKE '".$db->escape($db->escapeforlike($socid))."%'";
104 $sql .= " OR s.name_alias LIKE '".$db->escape($db->escapeforlike($socid))."%'";
105 $sql .= " OR s.code_client LIKE '".$db->escape($db->escapeforlike($socid))."%'";
106 $sql .= " OR s.code_fournisseur LIKE '".$db->escape($db->escapeforlike($socid))."%'";
107 } else {
108 $sql .= "s.nom LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
109 $sql .= " OR s.name_alias LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
110 $sql .= " OR s.code_client LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
111 $sql .= " OR s.code_fournisseur LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
112 }
113 if (getDolGlobalString('SOCIETE_ALLOW_SEARCH_ON_ROWID')) {
114 $sql .= " OR s.rowid = ".((int) $socid);
115 }
116 $sql .= ")";
117}
118// Protection for external user access
119if ($user->socid > 0) {
120 $sql .= " AND s.rowid = ".((int) $user->socid);
121}
122//if (GETPOST("filter")) $sql.= " AND (".GETPOST("filter", "alpha").")"; // Add other filters
123$sql .= " ORDER BY s.nom ASC";
124
125//dol_syslog("ajaxcompanies", LOG_DEBUG);
126$resql = $db->query($sql);
127if ($resql) {
128 while ($row = $db->fetch_array($resql)) {
129 $label = '';
130 if (getDolGlobalString('SOCIETE_ADD_REF_IN_LIST')) {
131 if (($row['client']) && (!empty($row['code_client']))) {
132 $label = $row['code_client'].' - ';
133 }
134 if (($row['fournisseur']) && (!empty($row['code_fournisseur']))) {
135 $label .= $row['code_fournisseur'].' - ';
136 }
137 }
138
139 $label .= $row['nom'];
140
141 if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
142 $label .= ($row['address'] ? ' - '.$row['address'] : '').($row['zip'] ? ' - '.$row['zip'] : '').($row['town'] ? ' '.$row['town'] : '');
143 if (!empty($row['country_code'])) {
144 $label .= ', '.$langs->trans('Country'.$row['country_code']);
145 }
146 }
147 if ($socid) {
148 $label = preg_replace('/('.preg_quote($socid, '/').')/i', '<strong>$1</strong>', $label, 1);
149 }
150 $row_array['label'] = $label;
151
152 $row_array['value'] = $row['nom'];
153 $row_array['key'] = $row['rowid'];
154
155 $row_array['name_alias'] = $row['name_alias'];
156 $row_array['client'] = $row['client'];
157 $row_array['fournisseur'] = $row['fournisseur'];
158 $row_array['code_client'] = $row['code_client'];
159 $row_array['code_fournisseur'] = $row['code_fournisseur'];
160 $row_array['address'] = $row['address'];
161 $row_array['zip'] = $row['zip'];
162 $row_array['town'] = $row['town'];
163 $row_array['email'] = $row['email'];
164 $row_array['siren'] = $row['siren'];
165 $row_array['siret'] = $row['siret'];
166 $row_array['ape'] = $row['ape'];
167 $row_array['idprof4'] = $row['idprof4'];
168 $row_array['idprof5'] = $row['idprof5'];
169 $row_array['idprof6'] = $row['idprof6'];
170 $row_array['datec'] = $row['datec'];
171 $row_array['logo'] = $row['logo'];
172
173 array_push($return_arr, $row_array);
174 }
175
176 echo json_encode($return_arr);
177} else {
178 echo json_encode(array('nom'=>'Error', 'label'=>'Error', 'key'=>'Error', 'value'=>'Error'));
179}
Class to manage third parties objects (customers, suppliers, prospects...)
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.
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.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.