ajaxcompanies.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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
  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. $object->id = $socid;
  51. }
  52. restrictedArea($user, 'societe', $object->id, '&societe');
  53. /*
  54. * View
  55. */
  56. // Ajout directives pour resoudre bug IE
  57. //header('Cache-Control: Public, must-revalidate');
  58. //header('Pragma: public');
  59. //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
  60. top_httphead();
  61. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  62. // Generate list of companies
  63. if (GETPOST('newcompany') || GETPOST('socid', 'int') || GETPOST('id_fourn', 'int')) {
  64. $return_arr = array();
  65. // Define filter on text typed
  66. $socid = GETPOST('newcompany');
  67. if (!$socid) {
  68. $socid = GETPOST('socid', 'int');
  69. }
  70. if (!$socid) {
  71. $socid = GETPOST('id_fourn', 'int');
  72. }
  73. $sql = "SELECT s.rowid, s.nom";
  74. if (!empty($conf->global->SOCIETE_ADD_REF_IN_LIST)) {
  75. $sql .= ", s.client, s.fournisseur, s.code_client, s.code_fournisseur";
  76. }
  77. if (!empty($conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST)) {
  78. $sql .= ", s.address, s.zip, s.town";
  79. $sql .= ", dictp.code as country_code";
  80. }
  81. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  82. if (!empty($conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST)) {
  83. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as dictp ON dictp.rowid = s.fk_pays";
  84. }
  85. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  86. if ($socid) {
  87. $sql .= " AND (";
  88. // Add criteria on name/code
  89. if (!empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) { // Can use index
  90. $sql .= "nom LIKE '".$db->escape($socid)."%'";
  91. $sql .= " OR code_client LIKE '".$db->escape($socid)."%'";
  92. $sql .= " OR code_fournisseur LIKE '".$db->escape($socid)."%'";
  93. } else {
  94. $sql .= "nom LIKE '%".$db->escape($socid)."%'";
  95. $sql .= " OR code_client LIKE '%".$db->escape($socid)."%'";
  96. $sql .= " OR code_fournisseur LIKE '%".$db->escape($socid)."%'";
  97. }
  98. if (!empty($conf->global->SOCIETE_ALLOW_SEARCH_ON_ROWID)) {
  99. $sql .= " OR rowid = ".((int) $socid);
  100. }
  101. $sql .= ")";
  102. }
  103. //if (GETPOST("filter")) $sql.= " AND (".GETPOST("filter", "alpha").")"; // Add other filters
  104. $sql .= " ORDER BY nom ASC";
  105. //dol_syslog("ajaxcompanies", LOG_DEBUG);
  106. $resql = $db->query($sql);
  107. if ($resql) {
  108. while ($row = $db->fetch_array($resql)) {
  109. $label = '';
  110. if (!empty($conf->global->SOCIETE_ADD_REF_IN_LIST)) {
  111. if (($row['client']) && (!empty($row['code_client']))) {
  112. $label = $row['code_client'].' - ';
  113. }
  114. if (($row['fournisseur']) && (!empty($row['code_fournisseur']))) {
  115. $label .= $row['code_fournisseur'].' - ';
  116. }
  117. }
  118. $label .= $row['nom'];
  119. if (!empty($conf->global->COMPANY_SHOW_ADDRESS_SELECTLIST)) {
  120. $label .= ($row['address'] ? ' - '.$row['address'] : '').($row['zip'] ? ' - '.$row['zip'] : '').($row['town'] ? ' '.$row['town'] : '');
  121. if (!empty($row['country_code'])) {
  122. $label .= ', '.$langs->trans('Country'.$row['country_code']);
  123. }
  124. }
  125. if ($socid) {
  126. $label = preg_replace('/('.preg_quote($socid, '/').')/i', '<strong>$1</strong>', $label, 1);
  127. }
  128. $row_array['label'] = $label;
  129. $row_array['value'] = $row['nom'];
  130. $row_array['key'] = $row['rowid'];
  131. array_push($return_arr, $row_array);
  132. }
  133. echo json_encode($return_arr);
  134. } else {
  135. echo json_encode(array('nom'=>'Error', 'label'=>'Error', 'key'=>'Error', 'value'=>'Error'));
  136. }
  137. } else {
  138. echo json_encode(array('nom'=>'ErrorBadParameter', 'label'=>'ErrorBadParameter', 'key'=>'ErrorBadParameter', 'value'=>'ErrorBadParameter'));
  139. }