ajaxcompanies.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/societe/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. {
  44. $return_arr = array();
  45. // Define filter on text typed
  46. $socid = $_GET['newcompany']?$_GET['newcompany']:'';
  47. if (! $socid) $socid = $_GET['socid']?$_GET['socid']:'';
  48. if (! $socid) $socid = $_GET['id_fourn']?$_GET['id_fourn']:'';
  49. $sql = "SELECT rowid, nom";
  50. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  51. $sql.= " WHERE s.entity IN (".getEntity('societe').")";
  52. if ($socid)
  53. {
  54. $sql.=" AND (";
  55. // Add criteria on name/code
  56. if (! empty($conf->global->COMPANY_DONOTSEARCH_ANYWHERE)) // Can use index
  57. {
  58. $sql.="nom LIKE '" . $db->escape($socid) . "%'";
  59. $sql.=" OR code_client LIKE '" . $db->escape($socid) . "%'";
  60. $sql.=" OR code_fournisseur LIKE '" . $db->escape($socid) . "%'";
  61. }
  62. else
  63. {
  64. $sql.="nom LIKE '%" . $db->escape($socid) . "%'";
  65. $sql.=" OR code_client LIKE '%" . $db->escape($socid) . "%'";
  66. $sql.=" OR code_fournisseur LIKE '%" . $db->escape($socid) . "%'";
  67. }
  68. if (! empty($conf->global->SOCIETE_ALLOW_SEARCH_ON_ROWID)) $sql.=" OR rowid = '" . $db->escape($socid) . "'";
  69. $sql.=")";
  70. }
  71. if (GETPOST("filter")) $sql.= " AND ".GETPOST("filter","alpha"); // Add other filters
  72. $sql.= " ORDER BY nom ASC";
  73. //dol_syslog("ajaxcompanies", LOG_DEBUG);
  74. $resql=$db->query($sql);
  75. if ($resql)
  76. {
  77. while ($row = $db->fetch_array($resql))
  78. {
  79. $label=$row['nom'];
  80. if ($socid) $label=preg_replace('/('.preg_quote($socid,'/').')/i','<strong>$1</strong>',$label,1);
  81. $row_array['label'] = $label;
  82. $row_array['value'] = $row['nom'];
  83. $row_array['key'] = $row['rowid'];
  84. array_push($return_arr,$row_array);
  85. }
  86. echo json_encode($return_arr);
  87. }
  88. else
  89. {
  90. echo json_encode(array('nom'=>'Error','label'=>'Error','key'=>'Error','value'=>'Error'));
  91. }
  92. }
  93. else
  94. {
  95. echo json_encode(array('nom'=>'ErrorBadParameter','label'=>'ErrorBadParameter','key'=>'ErrorBadParameter','value'=>'ErrorBadParameter'));
  96. }