html.formcompany.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. /* Copyright (C) 2008-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/class/html.formcompany.class.php
  21. * \ingroup core
  22. * \brief File of class to build HTML component for third parties management
  23. */
  24. /**
  25. * Class to build HTML component for third parties management
  26. * Only common components are here.
  27. */
  28. class FormCompany
  29. {
  30. var $db;
  31. var $error;
  32. /**
  33. * Constructor
  34. *
  35. * @param DoliDB $db Database handler
  36. */
  37. function __construct($db)
  38. {
  39. $this->db = $db;
  40. return 1;
  41. }
  42. /**
  43. * Return list of labels (translated) of third parties type
  44. *
  45. * @param int $mode 0=Return id+label, 1=Return code+label
  46. * @param string $filter Add a SQL filter to select
  47. * @return array Array of types
  48. */
  49. function typent_array($mode=0, $filter='')
  50. {
  51. global $langs,$mysoc;
  52. $effs = array();
  53. $sql = "SELECT id, code, libelle";
  54. $sql.= " FROM ".MAIN_DB_PREFIX."c_typent";
  55. $sql.= " WHERE active = 1 AND (fk_country IS NULL OR fk_country = ".(empty($mysoc->country_id)?'0':$mysoc->country_id).")";
  56. if ($filter) $sql.=" ".$filter;
  57. $sql.= " ORDER by position, id";
  58. dol_syslog(get_class($this).'::typent_array', LOG_DEBUG);
  59. $resql=$this->db->query($sql);
  60. if ($resql)
  61. {
  62. $num = $this->db->num_rows($resql);
  63. $i = 0;
  64. while ($i < $num)
  65. {
  66. $objp = $this->db->fetch_object($resql);
  67. if (! $mode) $key=$objp->id;
  68. else $key=$objp->code;
  69. if ($langs->trans($objp->code) != $objp->code) $effs[$key] = $langs->trans($objp->code);
  70. else $effs[$key] = $objp->libelle;
  71. if ($effs[$key]=='-') $effs[$key]='';
  72. $i++;
  73. }
  74. $this->db->free($resql);
  75. }
  76. return $effs;
  77. }
  78. /**
  79. * Renvoie la liste des types d'effectifs possibles (pas de traduction car nombre)
  80. *
  81. * @param int $mode 0=renvoi id+libelle, 1=renvoi code+libelle
  82. * @param string $filter Add a SQL filter to select
  83. * @return array Array of types d'effectifs
  84. */
  85. function effectif_array($mode=0, $filter='')
  86. {
  87. $effs = array();
  88. $sql = "SELECT id, code, libelle";
  89. $sql .= " FROM ".MAIN_DB_PREFIX."c_effectif";
  90. $sql.= " WHERE active = 1";
  91. if ($filter) $sql.=" ".$filter;
  92. $sql .= " ORDER BY id ASC";
  93. dol_syslog(get_class($this).'::effectif_array', LOG_DEBUG);
  94. $resql=$this->db->query($sql);
  95. if ($resql)
  96. {
  97. $num = $this->db->num_rows($resql);
  98. $i = 0;
  99. while ($i < $num)
  100. {
  101. $objp = $this->db->fetch_object($resql);
  102. if (! $mode) $key=$objp->id;
  103. else $key=$objp->code;
  104. $effs[$key] = $objp->libelle!='-'?$objp->libelle:'';
  105. $i++;
  106. }
  107. $this->db->free($resql);
  108. }
  109. return $effs;
  110. }
  111. /**
  112. * Affiche formulaire de selection des modes de reglement
  113. *
  114. * @param int $page Page
  115. * @param int $selected Id or code preselected
  116. * @param string $htmlname Nom du formulaire select
  117. * @param int $empty Add empty value in list
  118. * @return void
  119. */
  120. function form_prospect_level($page, $selected='', $htmlname='prospect_level_id', $empty=0)
  121. {
  122. global $user, $langs;
  123. print '<form method="post" action="'.$page.'">';
  124. print '<input type="hidden" name="action" value="setprospectlevel">';
  125. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  126. dol_syslog(get_class($this).'::form_prospect_level',LOG_DEBUG);
  127. $sql = "SELECT code, label";
  128. $sql.= " FROM ".MAIN_DB_PREFIX."c_prospectlevel";
  129. $sql.= " WHERE active > 0";
  130. $sql.= " ORDER BY sortorder";
  131. $resql = $this->db->query($sql);
  132. if ($resql)
  133. {
  134. $options = array();
  135. if ($empty) {
  136. $options[''] = '';
  137. }
  138. while ($obj = $this->db->fetch_object($resql)) {
  139. $level = $langs->trans($obj->code);
  140. if ($level == $obj->code) {
  141. $level = $langs->trans($obj->label);
  142. }
  143. $options[$obj->code] = $level;
  144. }
  145. print Form::selectarray($htmlname, $options, $selected);
  146. }
  147. else dol_print_error($this->db);
  148. if (! empty($htmlname) && $user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  149. print '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  150. print '</form>';
  151. }
  152. /**
  153. * Retourne la liste deroulante des departements/province/cantons tout pays confondu ou pour un pays donne.
  154. * Dans le cas d'une liste tout pays confondus, l'affichage fait une rupture sur le pays.
  155. * La cle de la liste est le code (il peut y avoir plusieurs entree pour
  156. * un code donnee mais dans ce cas, le champ pays differe).
  157. * Ainsi les liens avec les departements se font sur un departement independemment de son nom.
  158. *
  159. * @param string $selected Code state preselected
  160. * @param int $country_codeid 0=list for all countries, otherwise country code or country rowid to show
  161. * @param string $htmlname Id of department
  162. * @return void
  163. */
  164. function select_departement($selected='',$country_codeid=0, $htmlname='state_id')
  165. {
  166. print $this->select_state($selected,$country_codeid, $htmlname);
  167. }
  168. /**
  169. * Retourne la liste deroulante des departements/province/cantons tout pays confondu ou pour un pays donne.
  170. * Dans le cas d'une liste tout pays confondus, l'affichage fait une rupture sur le pays.
  171. * La cle de la liste est le code (il peut y avoir plusieurs entree pour
  172. * un code donnee mais dans ce cas, le champ pays differe).
  173. * Ainsi les liens avec les departements se font sur un departement independemment de son nom.
  174. *
  175. * @param string $selected Code state preselected (mus be state id)
  176. * @param integer $country_codeid Country code or id: 0=list for all countries, otherwise country code or country rowid to show
  177. * @param string $htmlname Id of department
  178. * @return string String with HTML select
  179. * @see select_country
  180. */
  181. function select_state($selected='',$country_codeid=0, $htmlname='state_id')
  182. {
  183. global $conf,$langs,$user;
  184. dol_syslog(get_class($this)."::select_departement selected=".$selected.", country_codeid=".$country_codeid,LOG_DEBUG);
  185. $langs->load("dict");
  186. $out='';
  187. // On recherche les departements/cantons/province active d'une region et pays actif
  188. $sql = "SELECT d.rowid, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code FROM";
  189. $sql .= " ".MAIN_DB_PREFIX ."c_departements as d, ".MAIN_DB_PREFIX."c_regions as r,".MAIN_DB_PREFIX."c_country as c";
  190. $sql .= " WHERE d.fk_region=r.code_region and r.fk_pays=c.rowid";
  191. $sql .= " AND d.active = 1 AND r.active = 1 AND c.active = 1";
  192. if ($country_codeid && is_numeric($country_codeid)) $sql .= " AND c.rowid = '".$country_codeid."'";
  193. if ($country_codeid && ! is_numeric($country_codeid)) $sql .= " AND c.code = '".$country_codeid."'";
  194. $sql .= " ORDER BY c.code, d.code_departement";
  195. dol_syslog(get_class($this)."::select_departement", LOG_DEBUG);
  196. $result=$this->db->query($sql);
  197. if ($result)
  198. {
  199. if (!empty($htmlname)) $out.= '<select id="'.$htmlname.'" class="flat maxwidth200onsmartphone minwidth300" name="'.$htmlname.'">';
  200. if ($country_codeid) $out.= '<option value="0">&nbsp;</option>';
  201. $num = $this->db->num_rows($result);
  202. $i = 0;
  203. dol_syslog(get_class($this)."::select_departement num=".$num,LOG_DEBUG);
  204. if ($num)
  205. {
  206. $country='';
  207. while ($i < $num)
  208. {
  209. $obj = $this->db->fetch_object($result);
  210. if ($obj->code == '0') // Le code peut etre une chaine
  211. {
  212. $out.= '<option value="0">&nbsp;</option>';
  213. }
  214. else {
  215. if (! $country || $country != $obj->country)
  216. {
  217. // Affiche la rupture si on est en mode liste multipays
  218. if (! $country_codeid && $obj->country_code)
  219. {
  220. $out.= '<option value="-1" disabled>----- '.$obj->country." -----</option>\n";
  221. $country=$obj->country;
  222. }
  223. }
  224. if ((! empty($selected) && $selected == $obj->rowid)
  225. || (empty($selected) && ! empty($conf->global->MAIN_FORCE_DEFAULT_STATE_ID) && $conf->global->MAIN_FORCE_DEFAULT_STATE_ID == $obj->rowid))
  226. {
  227. $out.= '<option value="'.$obj->rowid.'" selected>';
  228. }
  229. else
  230. {
  231. $out.= '<option value="'.$obj->rowid.'">';
  232. }
  233. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  234. $out.= $obj->code . ' - ' . ($langs->trans($obj->code)!=$obj->code?$langs->trans($obj->code):($obj->name!='-'?$obj->name:''));
  235. $out.= '</option>';
  236. }
  237. $i++;
  238. }
  239. }
  240. if (! empty($htmlname)) $out.= '</select>';
  241. if (! empty($htmlname) && $user->admin) $out.= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  242. }
  243. else
  244. {
  245. dol_print_error($this->db);
  246. }
  247. // Make select dynamic
  248. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  249. $out .= ajax_combobox($htmlname);
  250. return $out;
  251. }
  252. /**
  253. * Retourne la liste deroulante des regions actives dont le pays est actif
  254. * La cle de la liste est le code (il peut y avoir plusieurs entree pour
  255. * un code donnee mais dans ce cas, le champ pays et lang differe).
  256. * Ainsi les liens avec les regions se font sur une region independemment de son name.
  257. *
  258. * @param string $selected Preselected value
  259. * @param string $htmlname Name of HTML select field
  260. * @return void
  261. */
  262. function select_region($selected='',$htmlname='region_id')
  263. {
  264. global $conf,$langs;
  265. $langs->load("dict");
  266. $sql = "SELECT r.rowid, r.code_region as code, r.nom as label, r.active, c.code as country_code, c.label as country";
  267. $sql.= " FROM ".MAIN_DB_PREFIX."c_regions as r, ".MAIN_DB_PREFIX."c_country as c";
  268. $sql.= " WHERE r.fk_pays=c.rowid AND r.active = 1 and c.active = 1";
  269. $sql.= " ORDER BY c.code, c.label ASC";
  270. dol_syslog(get_class($this)."::select_region", LOG_DEBUG);
  271. $resql=$this->db->query($sql);
  272. if ($resql)
  273. {
  274. print '<select class="flat" name="'.$htmlname.'">';
  275. $num = $this->db->num_rows($resql);
  276. $i = 0;
  277. if ($num)
  278. {
  279. $country='';
  280. while ($i < $num)
  281. {
  282. $obj = $this->db->fetch_object($resql);
  283. if ($obj->code == 0) {
  284. print '<option value="0">&nbsp;</option>';
  285. }
  286. else {
  287. if ($country == '' || $country != $obj->country)
  288. {
  289. // Show break
  290. $key=$langs->trans("Country".strtoupper($obj->country_code));
  291. $valuetoshow=($key != "Country".strtoupper($obj->country_code))?$obj->country_code." - ".$key:$obj->country;
  292. print '<option value="-1" disabled>----- '.$valuetoshow." -----</option>\n";
  293. $country=$obj->country;
  294. }
  295. if ($selected > 0 && $selected == $obj->code)
  296. {
  297. print '<option value="'.$obj->code.'" selected>'.$obj->label.'</option>';
  298. }
  299. else
  300. {
  301. print '<option value="'.$obj->code.'">'.$obj->label.'</option>';
  302. }
  303. }
  304. $i++;
  305. }
  306. }
  307. print '</select>';
  308. }
  309. else
  310. {
  311. dol_print_error($this->db);
  312. }
  313. }
  314. /**
  315. * Return combo list with people title
  316. *
  317. * @param string $selected Title preselected
  318. * @param string $htmlname Name of HTML select combo field
  319. * @param string $morecss Add more css on SELECT element
  320. * @return string String with HTML select
  321. */
  322. function select_civility($selected='',$htmlname='civility_id',$morecss='maxwidth100')
  323. {
  324. global $conf,$langs,$user;
  325. $langs->load("dict");
  326. $out='';
  327. $sql = "SELECT rowid, code, label, active FROM ".MAIN_DB_PREFIX."c_civility";
  328. $sql.= " WHERE active = 1";
  329. dol_syslog("Form::select_civility", LOG_DEBUG);
  330. $resql=$this->db->query($sql);
  331. if ($resql)
  332. {
  333. $out.= '<select class="flat'.($morecss?' '.$morecss:'').'" name="'.$htmlname.'" id="'.$htmlname.'">';
  334. $out.= '<option value="">&nbsp;</option>';
  335. $num = $this->db->num_rows($resql);
  336. $i = 0;
  337. if ($num)
  338. {
  339. while ($i < $num)
  340. {
  341. $obj = $this->db->fetch_object($resql);
  342. if ($selected == $obj->code)
  343. {
  344. $out.= '<option value="'.$obj->code.'" selected>';
  345. }
  346. else
  347. {
  348. $out.= '<option value="'.$obj->code.'">';
  349. }
  350. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  351. $out.= ($langs->trans("Civility".$obj->code)!="Civility".$obj->code ? $langs->trans("Civility".$obj->code) : ($obj->label!='-'?$obj->label:''));
  352. $out.= '</option>';
  353. $i++;
  354. }
  355. }
  356. $out.= '</select>';
  357. if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  358. }
  359. else
  360. {
  361. dol_print_error($this->db);
  362. }
  363. return $out;
  364. }
  365. /**
  366. * Retourne la liste deroulante des formes juridiques tous pays confondus ou pour un pays donne.
  367. * Dans le cas d'une liste tous pays confondu, on affiche une rupture sur le pays.
  368. *
  369. * @param string $selected Code forme juridique a pre-selectionne
  370. * @param mixed $country_codeid 0=liste tous pays confondus, sinon code du pays a afficher
  371. * @param string $filter Add a SQL filter on list
  372. * @return void
  373. * @deprecated Use print xxx->select_juridicalstatus instead
  374. * @see select_juridicalstatus()
  375. */
  376. function select_forme_juridique($selected='', $country_codeid=0, $filter='')
  377. {
  378. print $this->select_juridicalstatus($selected, $country_codeid, $filter);
  379. }
  380. /**
  381. * Retourne la liste deroulante des formes juridiques tous pays confondus ou pour un pays donne.
  382. * Dans le cas d'une liste tous pays confondu, on affiche une rupture sur le pays
  383. *
  384. * @param string $selected Preselected code of juridical type
  385. * @param int $country_codeid 0=list for all countries, otherwise list only country requested
  386. * @param string $filter Add a SQL filter on list
  387. * @param string $htmlname HTML name of select
  388. * @return string String with HTML select
  389. */
  390. function select_juridicalstatus($selected='', $country_codeid=0, $filter='', $htmlname='forme_juridique_code')
  391. {
  392. global $conf,$langs,$user;
  393. $langs->load("dict");
  394. $out='';
  395. // On recherche les formes juridiques actives des pays actifs
  396. $sql = "SELECT f.rowid, f.code as code , f.libelle as label, f.active, c.label as country, c.code as country_code";
  397. $sql .= " FROM ".MAIN_DB_PREFIX."c_forme_juridique as f, ".MAIN_DB_PREFIX."c_country as c";
  398. $sql .= " WHERE f.fk_pays=c.rowid";
  399. $sql .= " AND f.active = 1 AND c.active = 1";
  400. if ($country_codeid) $sql .= " AND c.code = '".$country_codeid."'";
  401. if ($filter) $sql .= " ".$filter;
  402. $sql .= " ORDER BY c.code";
  403. dol_syslog(get_class($this)."::select_juridicalstatus", LOG_DEBUG);
  404. $resql=$this->db->query($sql);
  405. if ($resql)
  406. {
  407. $out.= '<div id="particulier2" class="visible">';
  408. $out.= '<select class="flat minwidth200" name="'.$htmlname.'" id="'.$htmlname.'">';
  409. if ($country_codeid) $out.= '<option value="0">&nbsp;</option>'; // When country_codeid is set, we force to add an empty line because it does not appears from select. When not set, we already get the empty line from select.
  410. $num = $this->db->num_rows($resql);
  411. if ($num)
  412. {
  413. $i = 0;
  414. $country=''; $arraydata=array();
  415. while ($i < $num)
  416. {
  417. $obj = $this->db->fetch_object($resql);
  418. if ($obj->code) // We exclude empty line, we will add it later
  419. {
  420. $labelcountry=(($langs->trans("Country".$obj->country_code)!="Country".$obj->country_code) ? $langs->trans("Country".$obj->country_code) : $obj->country);
  421. $labeljs=(($langs->trans("JuridicalStatus".$obj->code)!="JuridicalStatus".$obj->code) ? $langs->trans("JuridicalStatus".$obj->code) : ($obj->label!='-'?$obj->label:'')); // $obj->label is already in output charset (converted by database driver)
  422. $arraydata[$obj->code]=array('code'=>$obj->code, 'label'=>$labeljs, 'label_sort'=>$labelcountry.'_'.$labeljs, 'country_code'=>$obj->country_code, 'country'=>$labelcountry);
  423. }
  424. $i++;
  425. }
  426. $arraydata=dol_sort_array($arraydata, 'label_sort', 'ASC');
  427. if (empty($country_codeid)) // Introduce empty value (if $country_codeid not empty, empty value was already added)
  428. {
  429. $arraydata[0]=array('code'=>0, 'label'=>'', 'label_sort'=>'_', 'country_code'=>'', 'country'=>'');
  430. }
  431. foreach($arraydata as $key => $val)
  432. {
  433. if (! $country || $country != $val['country'])
  434. {
  435. // Show break when we are in multi country mode
  436. if (empty($country_codeid) && $val['country_code'])
  437. {
  438. $out.= '<option value="0" disabled class="selectoptiondisabledwhite">----- '.$val['country']." -----</option>\n";
  439. $country=$val['country'];
  440. }
  441. }
  442. if ($selected > 0 && $selected == $val['code'])
  443. {
  444. $out.= '<option value="'.$val['code'].'" selected>';
  445. }
  446. else
  447. {
  448. $out.= '<option value="'.$val['code'].'">';
  449. }
  450. // If translation exists, we use it, otherwise we use default label in database
  451. $out.= $val['label'];
  452. $out.= '</option>';
  453. }
  454. }
  455. $out.= '</select>';
  456. if ($user->admin) $out.= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  457. // Make select dynamic
  458. include_once DOL_DOCUMENT_ROOT . '/core/lib/ajax.lib.php';
  459. $out .= ajax_combobox($htmlname);
  460. $out.= '</div>';
  461. }
  462. else
  463. {
  464. dol_print_error($this->db);
  465. }
  466. return $out;
  467. }
  468. /**
  469. * Output list of third parties.
  470. *
  471. * @param object $object Object we try to find contacts
  472. * @param string $var_id Name of id field
  473. * @param string $selected Pre-selected third party
  474. * @param string $htmlname Name of HTML form
  475. * @param array $limitto Disable answers that are not id in this array list
  476. * @param int $forceid This is to force another object id than object->id
  477. * @param string $moreparam String with more param to add into url when noajax search is used.
  478. * @return int The selected third party ID
  479. */
  480. function selectCompaniesForNewContact($object, $var_id, $selected='', $htmlname='newcompany', $limitto='', $forceid=0, $moreparam='')
  481. {
  482. global $conf, $langs;
  483. if (! empty($conf->use_javascript_ajax) && ! empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT))
  484. {
  485. // Use Ajax search
  486. $minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2);
  487. $socid=0; $name='';
  488. if ($selected > 0)
  489. {
  490. $tmpthirdparty=new Societe($this->db);
  491. $result = $tmpthirdparty->fetch($selected);
  492. if ($result > 0)
  493. {
  494. $socid = $selected;
  495. $name = $tmpthirdparty->name;
  496. }
  497. }
  498. $events=array();
  499. // Add an entry 'method' to say 'yes, we must execute url with param action = method';
  500. // Add an entry 'url' to say which url to execute
  501. // Add an entry htmlname to say which element we must change once url is called
  502. // Add entry params => array('cssid' => 'attr') to say to remov or add attribute attr if answer of url return 0 or >0 lines
  503. // To refresh contacts list on thirdparty list change
  504. $events[]=array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php',1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled'));
  505. if (count($events)) // If there is some ajax events to run once selection is done, we add code here to run events
  506. {
  507. print '<script type="text/javascript">
  508. jQuery(document).ready(function() {
  509. $("#search_'.$htmlname.'").change(function() {
  510. console.log("Call runJsCodeForEvent'.$htmlname.'");
  511. var obj = '.json_encode($events).';
  512. $.each(obj, function(key,values) {
  513. if (values.method.length) {
  514. runJsCodeForEvent'.$htmlname.'(values);
  515. }
  516. });
  517. /* Clean contact */
  518. $("div#s2id_contactid>a>span").html(\'\');
  519. });
  520. // Function used to execute events when search_htmlname change
  521. function runJsCodeForEvent'.$htmlname.'(obj) {
  522. var id = $("#'.$htmlname.'").val();
  523. var method = obj.method;
  524. var url = obj.url;
  525. var htmlname = obj.htmlname;
  526. $.getJSON(url,
  527. {
  528. action: method,
  529. id: id,
  530. htmlname: htmlname
  531. },
  532. function(response) {
  533. if (response != null)
  534. {
  535. $.each(obj.params, function(key,action) {
  536. if (key.length) {
  537. var num = response.num;
  538. if (num > 0) {
  539. $("#" + key).removeAttr(action);
  540. } else {
  541. $("#" + key).attr(action, action);
  542. }
  543. }
  544. });
  545. /* console.log("Change select#"+htmlname+" with content "+response.value) */
  546. $("select#" + htmlname).html(response.value);
  547. }
  548. }
  549. );
  550. };
  551. });
  552. </script>';
  553. }
  554. print "\n".'<!-- Input text for third party with Ajax.Autocompleter (selectCompaniesForNewContact) -->'."\n";
  555. print '<input type="text" size="30" id="search_'.$htmlname.'" name="search_'.$htmlname.'" value="'.$name.'" />';
  556. print ajax_autocompleter(($socid?$socid:-1), $htmlname, DOL_URL_ROOT.'/societe/ajaxcompanies.php', '', $minLength, 0);
  557. return $socid;
  558. }
  559. else
  560. {
  561. // Search to list thirdparties
  562. $sql = "SELECT s.rowid, s.nom as name FROM";
  563. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  564. $sql.= " WHERE s.entity IN (".getEntity('societe', 1).")";
  565. // For ajax search we limit here. For combo list, we limit later
  566. if (is_array($limitto) && count($limitto))
  567. {
  568. $sql.= " AND s.rowid IN (".join(',',$limitto).")";
  569. }
  570. $sql.= " ORDER BY s.nom ASC";
  571. $resql = $this->db->query($sql);
  572. if ($resql)
  573. {
  574. print '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"';
  575. if ($conf->use_javascript_ajax)
  576. {
  577. $javaScript = "window.location='".$_SERVER['PHP_SELF']."?".$var_id."=".($forceid>0?$forceid:$object->id).$moreparam."&".$htmlname."=' + form.".$htmlname.".options[form.".$htmlname.".selectedIndex].value;";
  578. print ' onChange="'.$javaScript.'"';
  579. }
  580. print '>';
  581. $num = $this->db->num_rows($resql);
  582. $i = 0;
  583. if ($num)
  584. {
  585. while ($i < $num)
  586. {
  587. $obj = $this->db->fetch_object($resql);
  588. if ($i == 0) $firstCompany = $obj->rowid;
  589. $disabled=0;
  590. if (is_array($limitto) && count($limitto) && ! in_array($obj->rowid,$limitto)) $disabled=1;
  591. if ($selected > 0 && $selected == $obj->rowid)
  592. {
  593. print '<option value="'.$obj->rowid.'"';
  594. if ($disabled) print ' disabled';
  595. print ' selected>'.dol_trunc($obj->name,24).'</option>';
  596. $firstCompany = $obj->rowid;
  597. }
  598. else
  599. {
  600. print '<option value="'.$obj->rowid.'"';
  601. if ($disabled) print ' disabled';
  602. print '>'.dol_trunc($obj->name,24).'</option>';
  603. }
  604. $i ++;
  605. }
  606. }
  607. print "</select>\n";
  608. return $firstCompany;
  609. }
  610. else
  611. {
  612. dol_print_error($this->db);
  613. print 'Error sql';
  614. }
  615. }
  616. }
  617. /**
  618. * Return a select list with types of contacts
  619. *
  620. * @param object $object Object to use to find type of contact
  621. * @param string $selected Default selected value
  622. * @param string $htmlname HTML select name
  623. * @param string $source Source ('internal' or 'external')
  624. * @param string $sortorder Sort criteria ('position', 'code', ...)
  625. * @param int $showempty 1=Add en empty line
  626. * @return void
  627. */
  628. function selectTypeContact($object, $selected, $htmlname = 'type', $source='internal', $sortorder='position', $showempty=0)
  629. {
  630. global $user, $langs;
  631. if (is_object($object) && method_exists($object, 'liste_type_contact'))
  632. {
  633. $lesTypes = $object->liste_type_contact($source, $sortorder, 0, 1);
  634. print '<select class="flat valignmiddle" name="'.$htmlname.'" id="'.$htmlname.'">';
  635. if ($showempty) print '<option value="0"></option>';
  636. foreach($lesTypes as $key=>$value)
  637. {
  638. print '<option value="'.$key.'"';
  639. if ($key == $selected) print ' selected';
  640. print '>'.$value.'</option>';
  641. }
  642. print "</select>";
  643. if ($user->admin) print ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"),1);
  644. print "\n";
  645. }
  646. }
  647. /**
  648. * Return a select list with zip codes and their town
  649. *
  650. * @param string $selected Preselected value
  651. * @param string $htmlname HTML select name
  652. * @param string $fields Fields
  653. * @param int $fieldsize Field size
  654. * @param int $disableautocomplete 1 To disable ajax autocomplete features (browser autocomplete may still occurs)
  655. * @param string $moreattrib Add more attribute on HTML input field
  656. * @param string $morecss More css
  657. * @return string
  658. */
  659. function select_ziptown($selected='', $htmlname='zipcode', $fields='', $fieldsize=0, $disableautocomplete=0, $moreattrib='',$morecss='')
  660. {
  661. global $conf;
  662. $out='';
  663. $size='';
  664. if (!empty($fieldsize)) $size='size="'.$fieldsize.'"';
  665. if ($conf->use_javascript_ajax && empty($disableautocomplete))
  666. {
  667. $out.= ajax_multiautocompleter($htmlname,$fields,DOL_URL_ROOT.'/core/ajax/ziptown.php')."\n";
  668. $moreattrib.=' autocomplete="off"';
  669. }
  670. $out.= '<input id="'.$htmlname.'" class="maxwidthonsmartphone'.($morecss?' '.$morecss:'').'" type="text"'.($moreattrib?' '.$moreattrib:'').' name="'.$htmlname.'" '.$size.' value="'.$selected.'">'."\n";
  671. return $out;
  672. }
  673. /**
  674. * Return HTML string to use as input of professional id into a HTML page (siren, siret, etc...)
  675. *
  676. * @param int $idprof 1,2,3,4 (Example: 1=siren,2=siret,3=naf,4=rcs/rm)
  677. * @param string $htmlname Name of HTML select
  678. * @param string $preselected Default value to show
  679. * @param string $country_code FR, IT, ...
  680. * @param string $morecss More css
  681. * @return string HTML string with prof id
  682. */
  683. function get_input_id_prof($idprof,$htmlname,$preselected,$country_code,$morecss='maxwidth100onsmartphone quatrevingtpercent')
  684. {
  685. global $conf,$langs;
  686. $formlength=0;
  687. if (empty($conf->global->MAIN_DISABLEPROFIDRULES)) {
  688. if ($country_code == 'FR')
  689. {
  690. if (isset($idprof)) {
  691. if ($idprof==1) $formlength=9;
  692. else if ($idprof==2) $formlength=14;
  693. else if ($idprof==3) $formlength=5; // 4 chiffres et 1 lettre depuis janvier
  694. else if ($idprof==4) $formlength=32; // No maximum as we need to include a town name in this id
  695. }
  696. }
  697. else if ($country_code == 'ES')
  698. {
  699. if ($idprof==1) $formlength=9; //CIF/NIF/NIE 9 digits
  700. if ($idprof==2) $formlength=12; //NASS 12 digits without /
  701. if ($idprof==3) $formlength=5; //CNAE 5 digits
  702. if ($idprof==4) $formlength=32; //depend of college
  703. }
  704. }
  705. $selected=$preselected;
  706. if (! $selected && isset($idprof)) {
  707. if ($idprof==1 && ! empty($this->idprof1)) $selected=$this->idprof1;
  708. else if ($idprof==2 && ! empty($this->idprof2)) $selected=$this->idprof2;
  709. else if ($idprof==3 && ! empty($this->idprof3)) $selected=$this->idprof3;
  710. else if ($idprof==4 && ! empty($this->idprof4)) $selected=$this->idprof4;
  711. }
  712. $maxlength=$formlength;
  713. if (empty($formlength)) { $formlength=24; $maxlength=128; }
  714. $out = '<input type="text" '.($morecss?'class="'.$morecss.'" ':'').'name="'.$htmlname.'" id="'.$htmlname.'" maxlength="'.$maxlength.'" value="'.$selected.'">';
  715. return $out;
  716. }
  717. /**
  718. * Return a HTML select with localtax values for thirdparties
  719. *
  720. * @param int $local LocalTax
  721. * @param int $selected Preselected value
  722. * @param string $htmlname HTML select name
  723. * @return void
  724. */
  725. function select_localtax($local, $selected, $htmlname)
  726. {
  727. $tax=get_localtax_by_third($local);
  728. $num = $this->db->num_rows($tax);
  729. $i = 0;
  730. if ($num)
  731. {
  732. $valors=explode(":", $tax);
  733. if (count($valors) > 1)
  734. {
  735. //montar select
  736. print '<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
  737. while ($i <= (count($valors))-1)
  738. {
  739. if ($selected == $valors[$i])
  740. {
  741. print '<option value="'.$valors[$i].'" selected>';
  742. }
  743. else
  744. {
  745. print '<option value="'.$valors[$i].'">';
  746. }
  747. print $valors[$i];
  748. print '</option>';
  749. $i++;
  750. }
  751. print'</select>';
  752. }
  753. }
  754. }
  755. }