html.formcompany.class.php 31 KB

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