dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', 1); // Disables token renewal
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', '1');
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', '1');
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', '1');
39}
40if (!defined('NOREQUIRESOC')) {
41 define('NOREQUIRESOC', '1');
42}
43
44// Load Dolibarr environment
45require '../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
56$id = GETPOSTINT('socid');
57if ($id == 0) {
58 $id = GETPOSTINT('id_fourn');
59}
60
61$object = new Societe($db);
62if ($id > 0) {
63 $object->fetch($id);
64}
65
66// Security check
67if ($user->socid > 0) {
68 $socid = $user->socid;
69 if ($object->socid && $socid != $object->socid) {
70 accessforbidden('Not allowed to access thirdparty id '.$id.' with an external user on id '.$socid);
71 }
72}
73restrictedArea($user, 'societe', $object, '&societe');
74
75
76/*
77 * View
78 */
79
80//top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
81top_httphead('application/json');
82
83//print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
84
85
86$return_arr = array();
87
88// Define filter on text typed
89$socid = GETPOST('newcompany');
90if (!$socid) {
91 $socid = GETPOSTINT('socid');
92}
93if (!$socid) {
94 $socid = GETPOSTINT('id_fourn');
95}
96
97// Generate list of companies
98if (! $socid) {
99 echo json_encode(array('nom' => 'ErrorBadParameter', 'label' => 'ErrorBadParameter', 'key' => 'ErrorBadParameter', 'value' => 'ErrorBadParameter'));
100 exit;
101}
102
103$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";
104if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
105 $sql .= ", dictp.code as country_code";
106}
107$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
108if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
109 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as dictp ON dictp.rowid = s.fk_pays";
110}
111$sql .= " WHERE s.entity IN (".getEntity('societe').")";
112if ($socid) {
113 $sql .= " AND (";
114 // Add criteria on name/code
115 if (getDolGlobalString('COMPANY_DONOTSEARCH_ANYWHERE')) { // Can use index
116 $sql .= "s.nom LIKE '".$db->escape($db->escapeforlike($socid))."%'";
117 $sql .= " OR s.name_alias LIKE '".$db->escape($db->escapeforlike($socid))."%'";
118 $sql .= " OR s.code_client LIKE '".$db->escape($db->escapeforlike($socid))."%'";
119 $sql .= " OR s.code_fournisseur LIKE '".$db->escape($db->escapeforlike($socid))."%'";
120 } else {
121 $sql .= "s.nom LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
122 $sql .= " OR s.name_alias LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
123 $sql .= " OR s.code_client LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
124 $sql .= " OR s.code_fournisseur LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
125 }
126 if (getDolGlobalString('SOCIETE_ALLOW_SEARCH_ON_ROWID')) {
127 $sql .= " OR s.rowid = ".((int) $socid);
128 }
129 $sql .= ")";
130}
131// Protection for external user access
132if ($user->socid > 0) {
133 $sql .= " AND s.rowid = ".((int) $user->socid);
134}
135//if (GETPOST("filter")) $sql.= " AND (".GETPOST("filter", "alpha").")"; // Add other filters
136$sql .= " ORDER BY s.nom ASC";
137
138//dol_syslog("ajaxcompanies", LOG_DEBUG);
139$resql = $db->query($sql);
140if ($resql) {
141 while ($row = $db->fetch_array($resql)) {
142 $label = '';
143 if (getDolGlobalString('SOCIETE_ADD_REF_IN_LIST')) {
144 if (($row['client']) && (!empty($row['code_client']))) {
145 $label = $row['code_client'].' - ';
146 }
147 if (($row['fournisseur']) && (!empty($row['code_fournisseur']))) {
148 $label .= $row['code_fournisseur'].' - ';
149 }
150 }
151
152 $label .= $row['nom'];
153
154 if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
155 $label .= ($row['address'] ? ' - '.$row['address'] : '').($row['zip'] ? ' - '.$row['zip'] : '').($row['town'] ? ' '.$row['town'] : '');
156 if (!empty($row['country_code'])) {
157 $label .= ', '.$langs->trans('Country'.$row['country_code']);
158 }
159 }
160 if ($socid) {
161 $label = preg_replace('/('.preg_quote($socid, '/').')/i', '<strong>$1</strong>', $label, 1);
162 }
163 $row_array = array();
164 $row_array['label'] = $label;
165
166 $row_array['value'] = $row['nom'];
167 $row_array['key'] = $row['rowid'];
168
169 $row_array['name_alias'] = $row['name_alias'];
170 $row_array['client'] = $row['client'];
171 $row_array['fournisseur'] = $row['fournisseur'];
172 $row_array['code_client'] = $row['code_client'];
173 $row_array['code_fournisseur'] = $row['code_fournisseur'];
174 $row_array['address'] = $row['address'];
175 $row_array['zip'] = $row['zip'];
176 $row_array['town'] = $row['town'];
177 $row_array['email'] = $row['email'];
178 $row_array['siren'] = $row['siren'];
179 $row_array['siret'] = $row['siret'];
180 $row_array['ape'] = $row['ape'];
181 $row_array['idprof4'] = $row['idprof4'];
182 $row_array['idprof5'] = $row['idprof5'];
183 $row_array['idprof6'] = $row['idprof6'];
184 $row_array['datec'] = $row['datec'];
185 $row_array['logo'] = $row['logo'];
186
187 array_push($return_arr, $row_array);
188 }
189
190 echo json_encode($return_arr);
191} else {
192 echo json_encode(array('nom' => 'Error', 'label' => 'Error', 'key' => 'Error', 'value' => 'Error'));
193}
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
Class to manage third parties objects (customers, suppliers, prospects...)
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.
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.