dolibarr  18.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  *
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 
26 if (!defined('NOTOKENRENEWAL')) {
27  define('NOTOKENRENEWAL', 1); // Disables token renewal
28 }
29 if (!defined('NOREQUIREMENU')) {
30  define('NOREQUIREMENU', '1');
31 }
32 if (!defined('NOREQUIREHTML')) {
33  define('NOREQUIREHTML', '1');
34 }
35 if (!defined('NOREQUIREAJAX')) {
36  define('NOREQUIREAJAX', '1');
37 }
38 if (!defined('NOREQUIRESOC')) {
39  define('NOREQUIRESOC', '1');
40 }
41 
42 // Load Dolibarr environment
43 require '../../main.inc.php';
44 require_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);
49 if ($id > 0) {
50  $object->fetch($id);
51 }
52 
53 // Security check
54 if ($user->socid > 0) {
55  $socid = $user->socid;
56  $object->id = $socid;
57 }
58 restrictedArea($user, 'societe', $object->id, '&societe');
59 
60 
61 /*
62  * View
63  */
64 
65 // Ajout directives pour resoudre bug IE
66 //header('Cache-Control: Public, must-revalidate');
67 //header('Pragma: public');
68 
69 //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
70 top_httphead();
71 
72 //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
73 
74 
75 // Generate list of companies
76 if (GETPOST('newcompany') || GETPOST('socid', 'int') || GETPOST('id_fourn', 'int')) {
77  $return_arr = array();
78 
79  // Define filter on text typed
80  $socid = GETPOST('newcompany');
81  if (!$socid) {
82  $socid = GETPOST('socid', 'int');
83  }
84  if (!$socid) {
85  $socid = GETPOST('id_fourn', 'int');
86  }
87 
88  $sql = "SELECT s.rowid, s.nom";
89  if (!empty($conf->global->SOCIETE_ADD_REF_IN_LIST)) {
90  $sql .= ", s.client, s.fournisseur, s.code_client, s.code_fournisseur";
91  }
92  if (!empty($conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST)) {
93  $sql .= ", s.address, s.zip, s.town";
94  $sql .= ", dictp.code as country_code";
95  }
96  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
97  if (!empty($conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST)) {
98  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as dictp ON dictp.rowid = s.fk_pays";
99  }
100  $sql .= " WHERE s.entity IN (".getEntity('societe').")";
101  if ($socid) {
102  $sql .= " AND (";
103  // Add criteria on name/code
104  if (!empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) { // Can use index
105  $sql .= "nom LIKE '".$db->escape($socid)."%'";
106  $sql .= " OR code_client LIKE '".$db->escape($socid)."%'";
107  $sql .= " OR code_fournisseur LIKE '".$db->escape($socid)."%'";
108  } else {
109  $sql .= "nom LIKE '%".$db->escape($socid)."%'";
110  $sql .= " OR code_client LIKE '%".$db->escape($socid)."%'";
111  $sql .= " OR code_fournisseur LIKE '%".$db->escape($socid)."%'";
112  }
113  if (!empty($conf->global->SOCIETE_ALLOW_SEARCH_ON_ROWID)) {
114  $sql .= " OR rowid = ".((int) $socid);
115  }
116  $sql .= ")";
117  }
118  //if (GETPOST("filter")) $sql.= " AND (".GETPOST("filter", "alpha").")"; // Add other filters
119  $sql .= " ORDER BY nom ASC";
120 
121  //dol_syslog("ajaxcompanies", LOG_DEBUG);
122  $resql = $db->query($sql);
123  if ($resql) {
124  while ($row = $db->fetch_array($resql)) {
125  $label = '';
126  if (!empty($conf->global->SOCIETE_ADD_REF_IN_LIST)) {
127  if (($row['client']) && (!empty($row['code_client']))) {
128  $label = $row['code_client'].' - ';
129  }
130  if (($row['fournisseur']) && (!empty($row['code_fournisseur']))) {
131  $label .= $row['code_fournisseur'].' - ';
132  }
133  }
134 
135  $label .= $row['nom'];
136 
137  if (!empty($conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST)) {
138  $label .= ($row['address'] ? ' - '.$row['address'] : '').($row['zip'] ? ' - '.$row['zip'] : '').($row['town'] ? ' '.$row['town'] : '');
139  if (!empty($row['country_code'])) {
140  $label .= ', '.$langs->trans('Country'.$row['country_code']);
141  }
142  }
143  if ($socid) {
144  $label = preg_replace('/('.preg_quote($socid, '/').')/i', '<strong>$1</strong>', $label, 1);
145  }
146  $row_array['label'] = $label;
147  $row_array['value'] = $row['nom'];
148  $row_array['key'] = $row['rowid'];
149 
150  array_push($return_arr, $row_array);
151  }
152 
153  echo json_encode($return_arr);
154  } else {
155  echo json_encode(array('nom'=>'Error', 'label'=>'Error', 'key'=>'Error', 'value'=>'Error'));
156  }
157 } else {
158  echo json_encode(array('nom'=>'ErrorBadParameter', 'label'=>'ErrorBadParameter', 'key'=>'ErrorBadParameter', 'value'=>'ErrorBadParameter'));
159 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:50
top_httphead
if(!defined('NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
Definition: main.inc.php:1480
$sql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:745
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:567
restrictedArea
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.
Definition: security.lib.php:350