ajaxcompanies.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/core/ajax/ajaxcompanies.php
  22. * \brief File to return Ajax response on third parties request
  23. */
  24. if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', 1); // Disables token renewal
  25. if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
  26. if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
  27. if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');
  28. if (!defined('NOREQUIRESOC')) define('NOREQUIRESOC', '1');
  29. if (!defined('NOCSRFCHECK')) define('NOCSRFCHECK', '1');
  30. require '../../main.inc.php';
  31. /*
  32. * View
  33. */
  34. // Ajout directives pour resoudre bug IE
  35. //header('Cache-Control: Public, must-revalidate');
  36. //header('Pragma: public');
  37. //top_htmlhead("", "", 1); // Replaced with top_httphead. An ajax page does not need html header.
  38. top_httphead();
  39. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  40. dol_syslog(join(',', $_GET));
  41. // Generation liste des societes
  42. if (GETPOST('newcompany') || GETPOST('socid', 'int') || GETPOST('id_fourn')) {
  43. $return_arr = array();
  44. // Define filter on text typed
  45. $socid = $_GET['newcompany'] ? $_GET['newcompany'] : '';
  46. if (!$socid) $socid = $_GET['socid'] ? $_GET['socid'] : '';
  47. if (!$socid) $socid = $_GET['id_fourn'] ? $_GET['id_fourn'] : '';
  48. $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.client, s.fournisseur, s.datec, s.logo";
  49. $sql .= " , c.label as country, d.nom as departement";
  50. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  51. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON c.rowid = s.fk_pays";
  52. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON d.rowid = s.fk_departement";
  53. $sql .= " WHERE s.entity IN (".getEntity('societe').")";
  54. if ($socid) {
  55. $sql .= " AND (";
  56. // Add criteria on name/code
  57. if (!empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) { // Can use index
  58. $sql .= "s.nom LIKE '".$db->escape($socid)."%'";
  59. $sql .= " OR s.code_client LIKE '".$db->escape($socid)."%'";
  60. $sql .= " OR s.code_fournisseur LIKE '".$db->escape($socid)."%'";
  61. } else {
  62. $sql .= "s.nom LIKE '%".$db->escape($socid)."%'";
  63. $sql .= " OR s.code_client LIKE '%".$db->escape($socid)."%'";
  64. $sql .= " OR s.code_fournisseur LIKE '%".$db->escape($socid)."%'";
  65. }
  66. if (!empty($conf->global->SOCIETE_ALLOW_SEARCH_ON_ROWID)) $sql .= " OR s.rowid = '".$db->escape($socid)."'";
  67. $sql .= ")";
  68. }
  69. //if (GETPOST("filter")) $sql.= " AND (".GETPOST("filter", "alpha").")"; // Add other filters
  70. $sql .= " ORDER BY s.nom ASC";
  71. //dol_syslog("ajaxcompanies", LOG_DEBUG);
  72. $resql = $db->query($sql);
  73. if ($resql) {
  74. while ($row = $db->fetch_array($resql)) {
  75. $label = $row['nom'];
  76. if ($socid) $label = $label;
  77. $row_array['label'] = $label;
  78. $row_array['value'] = $row['nom'];
  79. $row_array['key'] = $row['rowid'];
  80. $row_array['name_alias'] = $row['name_alias'];
  81. $row_array['code_client'] = $row['code_client'];
  82. $row_array['code_fournisseur'] = $row['code_fournisseur'];
  83. $row_array['address'] = $row['address'];
  84. $row_array['zip'] = $row['zip'];
  85. $row_array['town'] = $row['town'];
  86. $row_array['email'] = $row['email'];
  87. $row_array['siren'] = $row['siren'];
  88. $row_array['siret'] = $row['siret'];
  89. $row_array['ape'] = $row['ape'];
  90. $row_array['idprof4'] = $row['idprof4'];
  91. $row_array['client'] = $row['client'];
  92. $row_array['fournisseur'] = $row['fournisseur'];
  93. $row_array['datec'] = $row['datec'];
  94. $row_array['logo'] = $row['logo'];
  95. $row_array['country'] = $row['country'];
  96. $row_array['departement'] = $row['departement'];
  97. array_push($return_arr, $row_array);
  98. }
  99. echo json_encode($return_arr);
  100. } else {
  101. echo json_encode(array('nom'=>'Error', 'label'=>'Error', 'key'=>'Error', 'value'=>'Error'));
  102. }
  103. } else {
  104. echo json_encode(array('nom'=>'ErrorBadParameter', 'label'=>'ErrorBadParameter', 'key'=>'ErrorBadParameter', 'value'=>'ErrorBadParameter'));
  105. }