ajaxcompanies.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  21. * \file htdocs/societe/ajax/ajaxcompanies.php
  22. * \brief File to return Ajax response on third parties request. Search is done on name|name_alias|code_client|code_fournisseur
  23. */
  24. if (!defined('NOTOKENRENEWAL')) {
  25. define('NOTOKENRENEWAL', 1); // Disables token renewal
  26. }
  27. if (!defined('NOREQUIREMENU')) {
  28. define('NOREQUIREMENU', '1');
  29. }
  30. if (!defined('NOREQUIREHTML')) {
  31. define('NOREQUIREHTML', '1');
  32. }
  33. if (!defined('NOREQUIREAJAX')) {
  34. define('NOREQUIREAJAX', '1');
  35. }
  36. if (!defined('NOREQUIRESOC')) {
  37. define('NOREQUIRESOC', '1');
  38. }
  39. // Load Dolibarr environment
  40. require '../../main.inc.php';
  41. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  42. $id = GETPOST('socid', 'int') || GETPOST('id_fourn', 'int');
  43. $object = new Societe($db);
  44. if ($id > 0) {
  45. $object->fetch($id);
  46. }
  47. // Security check
  48. if ($user->socid > 0) {
  49. $socid = $user->socid;
  50. if ($object->socid && $socid != $object->socid) {
  51. accessforbidden('Not allowed to access thirdparty id '.$id.' with an external user on id '.$socid);
  52. }
  53. }
  54. restrictedArea($user, 'societe', $object, '&societe');
  55. /*
  56. * View
  57. */
  58. //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
  59. top_httphead('application/json');
  60. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  61. $return_arr = array();
  62. // Define filter on text typed
  63. $socid = GETPOST('newcompany');
  64. if (!$socid) {
  65. $socid = GETPOST('socid', 'int');
  66. }
  67. if (!$socid) {
  68. $socid = GETPOST('id_fourn', 'int');
  69. }
  70. // Generate list of companies
  71. if (! $socid) {
  72. echo json_encode(array('nom'=>'ErrorBadParameter', 'label'=>'ErrorBadParameter', 'key'=>'ErrorBadParameter', 'value'=>'ErrorBadParameter'));
  73. exit;
  74. }
  75. $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";
  76. if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
  77. $sql .= ", dictp.code as country_code";
  78. }
  79. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  80. if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
  81. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as dictp ON dictp.rowid = s.fk_pays";
  82. }
  83. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  84. if ($socid) {
  85. $sql .= " AND (";
  86. // Add criteria on name/code
  87. if (getDolGlobalString('COMPANY_DONOTSEARCH_ANYWHERE')) { // Can use index
  88. $sql .= "s.nom LIKE '".$db->escape($db->escapeforlike($socid))."%'";
  89. $sql .= " OR s.name_alias LIKE '".$db->escape($db->escapeforlike($socid))."%'";
  90. $sql .= " OR s.code_client LIKE '".$db->escape($db->escapeforlike($socid))."%'";
  91. $sql .= " OR s.code_fournisseur LIKE '".$db->escape($db->escapeforlike($socid))."%'";
  92. } else {
  93. $sql .= "s.nom LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
  94. $sql .= " OR s.name_alias LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
  95. $sql .= " OR s.code_client LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
  96. $sql .= " OR s.code_fournisseur LIKE '%".$db->escape($db->escapeforlike($socid))."%'";
  97. }
  98. if (getDolGlobalString('SOCIETE_ALLOW_SEARCH_ON_ROWID')) {
  99. $sql .= " OR s.rowid = ".((int) $socid);
  100. }
  101. $sql .= ")";
  102. }
  103. // Protection for external user access
  104. if ($user->socid > 0) {
  105. $sql .= " AND s.rowid = ".((int) $user->socid);
  106. }
  107. //if (GETPOST("filter")) $sql.= " AND (".GETPOST("filter", "alpha").")"; // Add other filters
  108. $sql .= " ORDER BY s.nom ASC";
  109. //dol_syslog("ajaxcompanies", LOG_DEBUG);
  110. $resql = $db->query($sql);
  111. if ($resql) {
  112. while ($row = $db->fetch_array($resql)) {
  113. $label = '';
  114. if (getDolGlobalString('SOCIETE_ADD_REF_IN_LIST')) {
  115. if (($row['client']) && (!empty($row['code_client']))) {
  116. $label = $row['code_client'].' - ';
  117. }
  118. if (($row['fournisseur']) && (!empty($row['code_fournisseur']))) {
  119. $label .= $row['code_fournisseur'].' - ';
  120. }
  121. }
  122. $label .= $row['nom'];
  123. if (getDolGlobalString('COMPANY_SHOW_ADDRESS_SELECTLIST')) {
  124. $label .= ($row['address'] ? ' - '.$row['address'] : '').($row['zip'] ? ' - '.$row['zip'] : '').($row['town'] ? ' '.$row['town'] : '');
  125. if (!empty($row['country_code'])) {
  126. $label .= ', '.$langs->trans('Country'.$row['country_code']);
  127. }
  128. }
  129. if ($socid) {
  130. $label = preg_replace('/('.preg_quote($socid, '/').')/i', '<strong>$1</strong>', $label, 1);
  131. }
  132. $row_array['label'] = $label;
  133. $row_array['value'] = $row['nom'];
  134. $row_array['key'] = $row['rowid'];
  135. $row_array['name_alias'] = $row['name_alias'];
  136. $row_array['client'] = $row['client'];
  137. $row_array['fournisseur'] = $row['fournisseur'];
  138. $row_array['code_client'] = $row['code_client'];
  139. $row_array['code_fournisseur'] = $row['code_fournisseur'];
  140. $row_array['address'] = $row['address'];
  141. $row_array['zip'] = $row['zip'];
  142. $row_array['town'] = $row['town'];
  143. $row_array['email'] = $row['email'];
  144. $row_array['siren'] = $row['siren'];
  145. $row_array['siret'] = $row['siret'];
  146. $row_array['ape'] = $row['ape'];
  147. $row_array['idprof4'] = $row['idprof4'];
  148. $row_array['idprof5'] = $row['idprof5'];
  149. $row_array['idprof6'] = $row['idprof6'];
  150. $row_array['datec'] = $row['datec'];
  151. $row_array['logo'] = $row['logo'];
  152. array_push($return_arr, $row_array);
  153. }
  154. echo json_encode($return_arr);
  155. } else {
  156. echo json_encode(array('nom'=>'Error', 'label'=>'Error', 'key'=>'Error', 'value'=>'Error'));
  157. }