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