PageRenderTime 75ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 1ms

/htdocs/core/class/html.form.class.php

https://github.com/asterix14/dolibarr
PHP | 3852 lines | 2695 code | 371 blank | 786 comment | 790 complexity | 4cf4b83662b932defac5e6b03335469a MD5 | raw file
Possible License(s): LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (c) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  6. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  7. * Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
  8. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  9. * Copyright (C) 2006 Marc Barilley/Ocebo <marc@ocebo.com>
  10. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerker@telenet.be>
  11. * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
  12. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  13. * Copyright (C) 2010 Philippe Grand <philippe.grand@atoo-net.com>
  14. * Copyright (C) 2011 Herve Prot <herve.prot@symeos.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. /**
  30. * \file htdocs/core/class/html.form.class.php
  31. * \ingroup core
  32. * \brief File of class with all html predefined components
  33. */
  34. /**
  35. * \class Form
  36. * \brief Class to manage generation of HTML components
  37. * \remarks Only common components must be here.
  38. */
  39. class Form
  40. {
  41. var $db;
  42. var $error;
  43. // Cache arrays
  44. var $cache_types_paiements=array();
  45. var $cache_conditions_paiements=array();
  46. var $cache_availability=array();
  47. var $cache_demand_reason=array();
  48. var $cache_type_fees=array();
  49. var $tva_taux_value;
  50. var $tva_taux_libelle;
  51. /**
  52. * Constructor
  53. *
  54. * @param DoliDB $db Database handler
  55. */
  56. public function __construct($db)
  57. {
  58. $this->db = $db;
  59. }
  60. /**
  61. * Output key field for an editable field
  62. *
  63. * @param string $text Text of label or key to translate
  64. * @param string $htmlname Name of select field
  65. * @param string $preselected Name of Value to show/edit (not used in this function)
  66. * @param object $object Object
  67. * @param boolean $perm Permission to allow button to edit parameter
  68. * @param string $typeofdata Type of data ('string' by default, 'email', 'numeric:99', 'text' or 'textarea', 'day' or 'datepicker', 'ckeditor:dolibarr_zzz:width:height', 'select:xxx'...)
  69. * @return string HTML edit field
  70. */
  71. function editfieldkey($text,$htmlname,$preselected,$object,$perm,$typeofdata='string')
  72. {
  73. global $conf,$langs;
  74. $ret='';
  75. if (! empty($conf->global->MAIN_USE_JQUERY_JEDITABLE))
  76. {
  77. if ($perm)
  78. {
  79. $tmp=explode(':',$typeofdata);
  80. $ret.= '<div class="editkey_'.$tmp[0].'" id="'.$htmlname.'">';
  81. $ret.= $langs->trans($text);
  82. $ret.= '</div>'."\n";
  83. }
  84. else
  85. {
  86. $ret.= $langs->trans($text);
  87. }
  88. }
  89. else
  90. {
  91. $ret.='<table class="nobordernopadding" width="100%"><tr><td nowrap="nowrap">';
  92. $ret.=$langs->trans($text);
  93. $ret.='</td>';
  94. if (GETPOST('action') != 'edit'.$htmlname && $perm) $ret.='<td align="right"><a href="'.$_SERVER["PHP_SELF"].'?action=edit'.$htmlname.'&amp;id='.$object->id.'">'.img_edit($langs->trans('Edit'),1).'</a></td>';
  95. $ret.='</tr></table>';
  96. }
  97. return $ret;
  98. }
  99. /**
  100. * Output val field for an editable field
  101. *
  102. * @param string $text Text of label (not used in this function)
  103. * @param string $htmlname Name of select field
  104. * @param string $value Value to show/edit
  105. * @param object $object Object
  106. * @param boolean $perm Permission to allow button to edit parameter
  107. * @param string $typeofdata Type of data ('string' by default, 'email', 'numeric:99', 'text' or 'textarea', 'day' or 'datepicker', 'ckeditor:dolibarr_zzz:width:height', 'select:xxx'...)
  108. * @param string $editvalue When in edit mode, use this value as $value instead of value
  109. * @param object $extObject External object
  110. * @return string HTML edit field
  111. */
  112. function editfieldval($text,$htmlname,$value,$object,$perm,$typeofdata='string',$editvalue='',$extObject=false)
  113. {
  114. global $conf,$langs,$db;
  115. $ret='';
  116. // When option to edit inline is activated
  117. if (! empty($conf->global->MAIN_USE_JQUERY_JEDITABLE))
  118. {
  119. $ret.=$this->editInPlace($object, $value, $htmlname, $perm, $typeofdata, $extObject);
  120. }
  121. else
  122. {
  123. if (GETPOST('action') == 'edit'.$htmlname)
  124. {
  125. $ret.="\n";
  126. $ret.='<form method="post" action="'.$_SERVER["PHP_SELF"].'">';
  127. $ret.='<input type="hidden" name="action" value="set'.$htmlname.'">';
  128. $ret.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  129. $ret.='<input type="hidden" name="id" value="'.$object->id.'">';
  130. $ret.='<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  131. $ret.='<tr><td>';
  132. if (preg_match('/^(string|email|numeric)/',$typeofdata))
  133. {
  134. $tmp=explode(':',$typeofdata);
  135. $ret.='<input type="text" id="'.$htmlname.'" name="'.$htmlname.'" value="'.($editvalue?$editvalue:$value).'"'.($tmp[1]?' size="'.$tmp[1].'"':'').'>';
  136. }
  137. else if ($typeofdata == 'text' || $typeofdata == 'textarea' || $typeofdata == 'note')
  138. {
  139. $ret.='<textarea id="'.$htmlname.'" name="'.$htmlname.'" wrap="soft" cols="70">'.($editvalue?$editvalue:$value).'</textarea>';
  140. }
  141. else if ($typeofdata == 'day' || $typeofdata == 'datepicker')
  142. {
  143. $ret.=$this->form_date($_SERVER['PHP_SELF'].'?id='.$object->id,$value,$htmlname);
  144. }
  145. else if (preg_match('/^ckeditor/',$typeofdata))
  146. {
  147. $tmp=explode(':',$typeofdata);
  148. require_once(DOL_DOCUMENT_ROOT."/core/class/doleditor.class.php");
  149. $doleditor=new DolEditor($htmlname,($editvalue?$editvalue:$value),($tmp[2]?$tmp[2]:''),($tmp[3]?$tmp[3]:'100'),($tmp[1]?$tmp[1]:'dolibarr_notes'),'In',false,true,true);
  150. $ret.=$doleditor->Create(1);
  151. }
  152. $ret.='</td>';
  153. if ($typeofdata != 'day' && $typeofdata != 'datepicker') $ret.='<td align="left"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></td>';
  154. $ret.='</tr></table>'."\n";
  155. $ret.='</form>'."\n";
  156. }
  157. else
  158. {
  159. if ($typeofdata == 'email') $ret.=dol_print_email($value,0,0,0,0,1);
  160. elseif ($typeofdata == 'day' || $typeofdata == 'datepicker') $ret.=dol_print_date($value,'day');
  161. elseif ($typeofdata == 'text' || $typeofdata == 'textarea') $ret.=dol_htmlentitiesbr($value);
  162. else if (preg_match('/^ckeditor/',$typeofdata))
  163. {
  164. $tmpcontent=dol_htmlentitiesbr($value);
  165. $firstline=preg_replace('/<br>.*/','',$tmpcontent);
  166. $firstline=preg_replace('/[\n\r].*/','',$firstline);
  167. $ret.=$firstline.((strlen($firstline) != strlen($tmpcontent))?'...':'');
  168. }
  169. else $ret.=$value;
  170. }
  171. }
  172. return $ret;
  173. }
  174. /**
  175. * Output edit in place form
  176. *
  177. * @param object $object Object
  178. * @param string $value Value to show/edit
  179. * @param string $htmlname DIV ID (field name)
  180. * @param int $condition Condition to edit
  181. * @param string $inputType Type of input ('numeric', 'datepicker', 'textarea', 'ckeditor:dolibarr_zzz', 'select:xxx')
  182. * @param object $extObject External object
  183. * @return string HTML edit in place
  184. */
  185. private function editInPlace($object, $value, $htmlname, $condition, $inputType='textarea', $extObject=false)
  186. {
  187. global $conf;
  188. $out='';
  189. // Check parameters
  190. if ($inputType == 'textarea') $value = dol_nl2br($value);
  191. else if (preg_match('/^numeric/',$inputType)) $value = price($value);
  192. else if ($inputType == 'datepicker') $value = dol_print_date($value, 'day');
  193. if ($condition)
  194. {
  195. $element = false;
  196. $table_element = false;
  197. $fk_element = false;
  198. $loadmethod = false;
  199. $savemethod = false;
  200. $ext_element = false;
  201. //$ext_table_element = false;
  202. //$ext_fk_element = false;
  203. if (is_object($object))
  204. {
  205. $element = $object->element;
  206. $table_element = $object->table_element;
  207. $fk_element = $object->id;
  208. }
  209. if (is_object($extObject))
  210. {
  211. $ext_element = $extObject->element;
  212. //$ext_table_element = $extObject->table_element;
  213. //$ext_fk_element = $extObject->id;
  214. }
  215. if (preg_match('/^(string|email|numeric)/',$inputType))
  216. {
  217. $tmp=explode(':',$inputType);
  218. $inputType=$tmp[0]; $inputOption=$tmp[1];
  219. if (! empty($tmp[2])) $savemethod=$tmp[2];
  220. }
  221. if (preg_match('/^datepicker/',$inputType))
  222. {
  223. $tmp=explode(':',$inputType);
  224. $inputType=$tmp[0]; $inputOption=$tmp[1];
  225. if (! empty($tmp[2])) $savemethod=$tmp[2];
  226. $out.= '<input id="timestamp" type="hidden"/>'."\n"; // Use for timestamp format
  227. }
  228. else if (preg_match('/^select/',$inputType))
  229. {
  230. $tmp=explode(':',$inputType);
  231. $inputType=$tmp[0]; $loadmethod=$tmp[1];
  232. if (! empty($tmp[2])) $savemethod=$tmp[2];
  233. }
  234. else if (preg_match('/^ckeditor/',$inputType))
  235. {
  236. $tmp=explode(':',$inputType);
  237. $inputType=$tmp[0]; $toolbar=$tmp[1];
  238. if (! empty($tmp[2])) $width=$tmp[2];
  239. if (! empty($tmp[3])) $heigth=$tmp[3];
  240. if (! empty($tmp[4])) $savemethod=$tmp[4];
  241. if (! empty($conf->fckeditor->enabled))
  242. {
  243. $out.= '<input id="ckeditor_toolbar" value="'.$toolbar.'" type="hidden"/>'."\n";
  244. }
  245. else
  246. {
  247. $inputType = 'textarea';
  248. }
  249. }
  250. $out.= '<input id="element_'.$htmlname.'" value="'.$element.'" type="hidden"/>'."\n";
  251. $out.= '<input id="table_element_'.$htmlname.'" value="'.$table_element.'" type="hidden"/>'."\n";
  252. $out.= '<input id="fk_element_'.$htmlname.'" value="'.$fk_element.'" type="hidden"/>'."\n";
  253. $out.= '<input id="loadmethod_'.$htmlname.'" value="'.$loadmethod.'" type="hidden"/>'."\n";
  254. $out.= '<input id="savemethod_'.$htmlname.'" value="'.$savemethod.'" type="hidden"/>'."\n";
  255. $out.= '<input id="ext_element_'.$htmlname.'" value="'.$ext_element.'" type="hidden"/>'."\n";
  256. //$out.= '<input id="ext_table_element_'.$htmlname.'" value="'.$ext_table_element.'" type="hidden"/>'."\n";
  257. //$out.= '<input id="ext_fk_element_'.$htmlname.'" value="'.$ext_fk_element.'" type="hidden"/>'."\n";
  258. $out.= '<div id="val_'.$htmlname.'" class="editval_'.$inputType.'">'.$value.'</div>'."\n";
  259. }
  260. else
  261. {
  262. $out = $value;
  263. }
  264. return $out;
  265. }
  266. /**
  267. * Show a text and picto with tooltip on text or picto
  268. *
  269. * @param string $text Text to show
  270. * @param string $htmltext Content html of tooltip. Must be HTML/UTF8 encoded.
  271. * @param int $tooltipon 1=tooltip sur texte, 2=tooltip sur picto, 3=tooltip sur les 2
  272. * @param int $direction -1=Le picto est avant, 0=pas de picto, 1=le picto est apres
  273. * @param string $img Code img du picto (use img_xxx() function to get it)
  274. * @param string $extracss Add a CSS style to td tags
  275. * @param int $notabs Do not include table and tr tags
  276. * @param string $incbefore Include code before the text
  277. * @param int $noencodehtmltext Do not encode into html entity the htmltext
  278. * @return string Code html du tooltip (texte+picto)
  279. * @see Use function textwithpicto if you can.
  280. */
  281. function textwithtooltip($text,$htmltext,$tooltipon=1,$direction=0,$img='',$extracss='',$notabs=0,$incbefore='',$noencodehtmltext=0)
  282. {
  283. global $conf;
  284. if ($incbefore) $text = $incbefore.$text;
  285. if (! $htmltext) return $text;
  286. // Sanitize tooltip
  287. $htmltext=str_replace("\\","\\\\",$htmltext);
  288. $htmltext=str_replace("\r","",$htmltext);
  289. $htmltext=str_replace("\n","",$htmltext);
  290. $htmltext=str_replace('"',"&quot;",$htmltext);
  291. if ($tooltipon == 2 || $tooltipon == 3) $paramfortooltipimg=' class="classfortooltip'.($extracss?' '.$extracss:'').'" title="'.($noencodehtmltext?$htmltext:dol_escape_htmltag($htmltext,1)).'"'; // Attribut to put on td img tag to store tooltip
  292. else $paramfortooltipimg =($extracss?' class="'.$extracss.'"':''); // Attribut to put on td text tag
  293. if ($tooltipon == 1 || $tooltipon == 3) $paramfortooltiptd=' class="classfortooltip'.($extracss?' '.$extracss:'').'" title="'.($noencodehtmltext?$htmltext:dol_escape_htmltag($htmltext,1)).'"'; // Attribut to put on td tag to store tooltip
  294. else $paramfortooltiptd =($extracss?' class="'.$extracss.'"':''); // Attribut to put on td text tag
  295. $s="";
  296. if (empty($notabs)) $s.='<table class="nobordernopadding" summary=""><tr>';
  297. if ($direction > 0)
  298. {
  299. if ($text != '')
  300. {
  301. $s.='<td'.$paramfortooltiptd.'>'.$text;
  302. if ($direction) $s.='&nbsp;';
  303. $s.='</td>';
  304. }
  305. if ($direction) $s.='<td'.$paramfortooltipimg.' valign="top" width="14">'.$img.'</td>';
  306. }
  307. else
  308. {
  309. if ($direction) $s.='<td'.$paramfortooltipimg.' valign="top" width="14">'.$img.'</td>';
  310. if ($text != '')
  311. {
  312. $s.='<td'.$paramfortooltiptd.'>';
  313. if ($direction) $s.='&nbsp;';
  314. $s.=$text.'</td>';
  315. }
  316. }
  317. if (empty($notabs)) $s.='</tr></table>';
  318. return $s;
  319. }
  320. /**
  321. * Show a text with a picto and a tooltip on picto
  322. *
  323. * @param text Text to show
  324. * @param htmltooltip Content of tooltip
  325. * @param direction 1=Icon is after text, -1=Icon is before text, 0=no icon
  326. * @param type Type of picto (info, help, warning, superadmin...)
  327. * @param extracss Add a CSS style to td tags
  328. * @param noencodehtmltext Do not encode into html entity the htmltext
  329. * @return string HTML code of text, picto, tooltip
  330. */
  331. function textwithpicto($text,$htmltext,$direction=1,$type='help',$extracss='',$noencodehtmltext=0)
  332. {
  333. global $conf;
  334. if ("$type" == "0") $type='info'; // For backward compatibility
  335. $alt='';
  336. // If info or help with no javascript, show only text
  337. if (empty($conf->use_javascript_ajax))
  338. {
  339. if ($type == 'info' || $type == 'help') return $text;
  340. else { $alt=$htmltext; $htmltext=''; }
  341. }
  342. // If info or help with smartphone, show only text
  343. if (! empty($conf->browser->phone))
  344. {
  345. if ($type == 'info' || $type == 'help') return $text;
  346. }
  347. // Info or help
  348. if ($type == 'info') $img=img_help(0,$alt);
  349. if ($type == 'help' || $type ==1) $img=img_help(1,$alt);
  350. if ($type == 'superadmin') $img=img_picto($alt,"redstar");
  351. if ($type == 'admin') $img=img_picto($alt,"star");
  352. // Warnings
  353. if ($type == 'warning') $img=img_warning($alt);
  354. return $this->textwithtooltip($text,$htmltext,2,$direction,$img,$extracss,0,'',$noencodehtmltext);
  355. }
  356. /**
  357. * Return combo list of activated countries, into language of user
  358. *
  359. * @param selected Id or Code or Label of preselected country
  360. * @param htmlname Name of html select object
  361. * @param htmloption Options html on select object
  362. */
  363. function select_pays($selected='',$htmlname='pays_id',$htmloption='')
  364. {
  365. print $this->select_country($selected,$htmlname,$htmloption);
  366. }
  367. /**
  368. * Return combo list of activated countries, into language of user
  369. *
  370. * @param selected Id or Code or Label of preselected country
  371. * @param htmlname Name of html select object
  372. * @param htmloption Options html on select object
  373. * @return string HTML string with select
  374. */
  375. function select_country($selected='',$htmlname='pays_id',$htmloption='')
  376. {
  377. global $conf,$langs;
  378. $langs->load("dict");
  379. $out='';
  380. $countryArray=array();
  381. $label=array();
  382. $sql = "SELECT rowid, code as code_iso, libelle as label";
  383. $sql.= " FROM ".MAIN_DB_PREFIX."c_pays";
  384. $sql.= " WHERE active = 1";
  385. $sql.= " ORDER BY code ASC";
  386. dol_syslog("Form::select_country sql=".$sql);
  387. $resql=$this->db->query($sql);
  388. if ($resql)
  389. {
  390. $out.= '<select id="select'.$htmlname.'" class="flat selectpays" name="'.$htmlname.'" '.$htmloption.'>';
  391. $num = $this->db->num_rows($resql);
  392. $i = 0;
  393. if ($num)
  394. {
  395. $foundselected=false;
  396. while ($i < $num)
  397. {
  398. $obj = $this->db->fetch_object($resql);
  399. $countryArray[$i]['rowid'] = $obj->rowid;
  400. $countryArray[$i]['code_iso'] = $obj->code_iso;
  401. $countryArray[$i]['label'] = ($obj->code_iso && $langs->transnoentitiesnoconv("Country".$obj->code_iso)!="Country".$obj->code_iso?$langs->transnoentitiesnoconv("Country".$obj->code_iso):($obj->label!='-'?$obj->label:''));
  402. $label[$i] = $countryArray[$i]['label'];
  403. $i++;
  404. }
  405. array_multisort($label, SORT_ASC, $countryArray);
  406. foreach ($countryArray as $row)
  407. {
  408. //print 'rr'.$selected.'-'.$row['label'].'-'.$row['code_iso'].'<br>';
  409. if ($selected && $selected != '-1' && ($selected == $row['rowid'] || $selected == $row['code_iso'] || $selected == $row['label']) )
  410. {
  411. $foundselected=true;
  412. $out.= '<option value="'.$row['rowid'].'" selected="selected">';
  413. }
  414. else
  415. {
  416. $out.= '<option value="'.$row['rowid'].'">';
  417. }
  418. $out.= $row['label'];
  419. if ($row['code_iso']) $out.= ' ('.$row['code_iso'] . ')';
  420. $out.= '</option>';
  421. }
  422. }
  423. $out.= '</select>';
  424. }
  425. else
  426. {
  427. dol_print_error($this->db);
  428. }
  429. return $out;
  430. }
  431. /**
  432. * Retourne la liste des types de comptes financiers
  433. *
  434. * @param selected Type pre-selectionne
  435. * @param htmlname Nom champ formulaire
  436. */
  437. function select_type_comptes_financiers($selected=1,$htmlname='type')
  438. {
  439. global $langs;
  440. $langs->load("banks");
  441. $type_available=array(0,1,2);
  442. print '<select class="flat" name="'.$htmlname.'">';
  443. $num = count($type_available);
  444. $i = 0;
  445. if ($num)
  446. {
  447. while ($i < $num)
  448. {
  449. if ($selected == $type_available[$i])
  450. {
  451. print '<option value="'.$type_available[$i].'" selected="selected">'.$langs->trans("BankType".$type_available[$i]).'</option>';
  452. }
  453. else
  454. {
  455. print '<option value="'.$type_available[$i].'">'.$langs->trans("BankType".$type_available[$i]).'</option>';
  456. }
  457. $i++;
  458. }
  459. }
  460. print '</select>';
  461. }
  462. /**
  463. * Return list of social contributions.
  464. * Use mysoc->pays_id or mysoc->pays_code so they must be defined.
  465. *
  466. * @param selected Preselected type
  467. * @param htmlname Name of field in form
  468. * @param useempty Set to 1 if we want an empty value
  469. * @param maxlen Max length of text in combo box
  470. * @param help Add or not the admin help picto
  471. */
  472. function select_type_socialcontrib($selected='',$htmlname='actioncode', $useempty=0, $maxlen=40, $help=1)
  473. {
  474. global $db,$langs,$user,$mysoc;
  475. if (empty($mysoc->pays_id) && empty($mysoc->pays_code))
  476. {
  477. dol_print_error('','Call to select_type_socialcontrib with mysoc country not yet defined');
  478. exit;
  479. }
  480. if (! empty($mysoc->pays_id))
  481. {
  482. $sql = "SELECT c.id, c.libelle as type";
  483. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c";
  484. $sql.= " WHERE c.active = 1";
  485. $sql.= " AND c.fk_pays = ".$mysoc->pays_id;
  486. $sql.= " ORDER BY c.libelle ASC";
  487. }
  488. else
  489. {
  490. $sql = "SELECT c.id, c.libelle as type";
  491. $sql.= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c, ".MAIN_DB_PREFIX."c_pays as p";
  492. $sql.= " WHERE c.active = 1 AND c.fk_pays = p.rowid";
  493. $sql.= " AND p.code = '".$mysoc->pays_code."'";
  494. $sql.= " ORDER BY c.libelle ASC";
  495. }
  496. dol_syslog("Form::select_type_socialcontrib sql=".$sql, LOG_DEBUG);
  497. $resql=$db->query($sql);
  498. if ($resql)
  499. {
  500. $num = $db->num_rows($resql);
  501. if ($num)
  502. {
  503. print '<select class="flat" name="'.$htmlname.'">';
  504. $i = 0;
  505. if ($useempty) print '<option value="0">&nbsp;</option>';
  506. while ($i < $num)
  507. {
  508. $obj = $db->fetch_object($resql);
  509. print '<option value="'.$obj->id.'"';
  510. if ($obj->id == $selected) print ' selected="selected"';
  511. print '>'.dol_trunc($obj->type,$maxlen);
  512. $i++;
  513. }
  514. print '</select>';
  515. if ($user->admin && $help) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  516. }
  517. else
  518. {
  519. print $langs->trans("ErrorNoSocialContributionForSellerCountry",$mysoc->pays_code);
  520. }
  521. }
  522. else
  523. {
  524. dol_print_error($db,$db->lasterror());
  525. }
  526. }
  527. /**
  528. * Return list of types of lines (product or service)
  529. * Example: 0=product, 1=service, 9=other (for external module)
  530. *
  531. * @param selected Preselected type
  532. * @param htmlname Name of field in html form
  533. * @param showempty Add an empty field
  534. * @param hidetext Do not show label before combo box
  535. * @param forceall Force to show products and services in combo list, whatever are activated modules
  536. */
  537. function select_type_of_lines($selected='',$htmlname='type',$showempty=0,$hidetext=0,$forceall=0)
  538. {
  539. global $db,$langs,$user,$conf;
  540. // If product & services are enabled or both disabled.
  541. if ($forceall || ($conf->product->enabled && $conf->service->enabled)
  542. || (empty($conf->product->enabled) && empty($conf->service->enabled)))
  543. {
  544. if (empty($hidetext)) print $langs->trans("Type").': ';
  545. print '<select class="flat" name="'.$htmlname.'">';
  546. if ($showempty)
  547. {
  548. print '<option value="-1"';
  549. if ($selected == -1) print ' selected="selected"';
  550. print '>&nbsp;</option>';
  551. }
  552. print '<option value="0"';
  553. if (0 == $selected) print ' selected="selected"';
  554. print '>'.$langs->trans("Product");
  555. print '<option value="1"';
  556. if (1 == $selected) print ' selected="selected"';
  557. print '>'.$langs->trans("Service");
  558. print '</select>';
  559. //if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  560. }
  561. if (! $forceall && empty($conf->product->enabled) && $conf->service->enabled)
  562. {
  563. print '<input type="hidden" name="'.$htmlname.'" value="1">';
  564. }
  565. if (! $forceall && $conf->product->enabled && empty($conf->service->enabled))
  566. {
  567. print '<input type="hidden" name="'.$htmlname.'" value="0">';
  568. }
  569. }
  570. /**
  571. * Load into cache cache_types_fees, array of types of fees
  572. *
  573. * @return int Nb of lines loaded, 0 if already loaded, <0 if ko
  574. * TODO move in DAO class
  575. */
  576. function load_cache_types_fees()
  577. {
  578. global $langs;
  579. $langs->load("trips");
  580. if (count($this->cache_types_fees)) return 0; // Cache already load
  581. $sql = "SELECT c.code, c.libelle as label";
  582. $sql.= " FROM ".MAIN_DB_PREFIX."c_type_fees as c";
  583. $sql.= " ORDER BY lower(c.libelle) ASC";
  584. dol_syslog(get_class($this).'::load_cache_types_fees sql='.$sql, LOG_DEBUG);
  585. $resql=$this->db->query($sql);
  586. if ($resql)
  587. {
  588. $num = $this->db->num_rows($resql);
  589. $i = 0;
  590. while ($i < $num)
  591. {
  592. $obj = $this->db->fetch_object($resql);
  593. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  594. $label=($obj->code != $langs->trans($obj->code) ? $langs->trans($obj->code) : $langs->trans($obj->label));
  595. $this->cache_types_fees[$obj->code] = $label;
  596. $i++;
  597. }
  598. return $num;
  599. }
  600. else
  601. {
  602. dol_print_error($this->db);
  603. return -1;
  604. }
  605. }
  606. /**
  607. * Return list of types of notes
  608. *
  609. * @param string $selected Preselected type
  610. * @param string $htmlname Name of field in form
  611. * @param int $showempty Add an empty field
  612. * @return void
  613. */
  614. function select_type_fees($selected='',$htmlname='type',$showempty=0)
  615. {
  616. global $user, $langs;
  617. dol_syslog(get_class($this)."::select_type_fees ".$selected.", ".$htmlname, LOG_DEBUG);
  618. $this->load_cache_types_fees();
  619. print '<select class="flat" name="'.$htmlname.'">';
  620. if ($showempty)
  621. {
  622. print '<option value="-1"';
  623. if ($selected == -1) print ' selected="selected"';
  624. print '>&nbsp;</option>';
  625. }
  626. foreach($this->cache_types_fees as $key => $value)
  627. {
  628. print '<option value="'.$key.'"';
  629. if ($key == $selected) print ' selected="selected"';
  630. print '>';
  631. print $value;
  632. print '</option>';
  633. }
  634. print '</select>';
  635. if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  636. }
  637. /**
  638. * Output html form to select a third party
  639. *
  640. * @param selected Preselected type
  641. * @param htmlname Name of field in form
  642. * @param filter Optionnal filters criteras
  643. * @param showempty Add an empty field
  644. * @param showtype Show third party type in combolist (customer, prospect or supplier)
  645. * @param forcecombo Force to use combo box
  646. */
  647. function select_societes($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0)
  648. {
  649. print $this->select_company($selected,$htmlname,$filter,$showempty,$showtype,$forcecombo);
  650. }
  651. /**
  652. * Output html form to select a third party
  653. *
  654. * @param selected Preselected type
  655. * @param htmlname Name of field in form
  656. * @param filter Optionnal filters criteras
  657. * @param showempty Add an empty field
  658. * @param showtype Show third party type in combolist (customer, prospect or supplier)
  659. * @param forcecombo Force to use combo box
  660. */
  661. function select_company($selected='',$htmlname='socid',$filter='',$showempty=0, $showtype=0, $forcecombo=0)
  662. {
  663. global $conf,$user,$langs;
  664. $out='';
  665. // On recherche les societes
  666. $sql = "SELECT s.rowid, s.nom, s.client, s.fournisseur, s.code_client, s.code_fournisseur";
  667. $sql.= " FROM ".MAIN_DB_PREFIX ."societe as s";
  668. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  669. $sql.= " WHERE s.entity = ".$conf->entity;
  670. if ($filter) $sql.= " AND ".$filter;
  671. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  672. $sql.= " ORDER BY nom ASC";
  673. dol_syslog("Form::select_societes sql=".$sql);
  674. $resql=$this->db->query($sql);
  675. if ($resql)
  676. {
  677. if ($conf->use_javascript_ajax && $conf->global->COMPANY_USE_SEARCH_TO_SELECT && ! $forcecombo)
  678. {
  679. //$minLength = (is_numeric($conf->global->COMPANY_USE_SEARCH_TO_SELECT)?$conf->global->COMPANY_USE_SEARCH_TO_SELECT:2);
  680. $out.= ajax_combobox($htmlname);
  681. }
  682. $out.= '<select id="'.$htmlname.'" class="flat" name="'.$htmlname.'">';
  683. if ($showempty) $out.= '<option value="-1">&nbsp;</option>';
  684. $num = $this->db->num_rows($resql);
  685. $i = 0;
  686. if ($num)
  687. {
  688. while ($i < $num)
  689. {
  690. $obj = $this->db->fetch_object($resql);
  691. $label=$obj->nom;
  692. if ($showtype)
  693. {
  694. if ($obj->client || $obj->fournisseur) $label.=' (';
  695. if ($obj->client == 1 || $obj->client == 3) $label.=$langs->trans("Customer");
  696. if ($obj->client == 2 || $obj->client == 3) $label.=($obj->client==3?', ':'').$langs->trans("Prospect");
  697. if ($obj->fournisseur) $label.=($obj->client?', ':'').$langs->trans("Supplier");
  698. if ($obj->client || $obj->fournisseur) $label.=')';
  699. }
  700. if ($selected > 0 && $selected == $obj->rowid)
  701. {
  702. $out.= '<option value="'.$obj->rowid.'" selected="selected">'.$label.'</option>';
  703. }
  704. else
  705. {
  706. $out.= '<option value="'.$obj->rowid.'">'.$label.'</option>';
  707. }
  708. $i++;
  709. }
  710. }
  711. $out.= '</select>';
  712. }
  713. else
  714. {
  715. dol_print_error($this->db);
  716. }
  717. return $out;
  718. }
  719. /**
  720. * Return HTML combo list of absolute discounts
  721. *
  722. * @param selected Id remise fixe pre-selectionnee
  723. * @param htmlname Nom champ formulaire
  724. * @param filter Criteres optionnels de filtre
  725. * @param maxvalue Max value for lines that can be selected
  726. * @return int Return number of qualifed lines in list
  727. */
  728. function select_remises($selected='',$htmlname='remise_id',$filter='',$socid, $maxvalue=0)
  729. {
  730. global $langs,$conf;
  731. // On recherche les remises
  732. $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
  733. $sql.= " re.description, re.fk_facture_source";
  734. $sql.= " FROM ".MAIN_DB_PREFIX ."societe_remise_except as re";
  735. $sql.= " WHERE fk_soc = ".$socid;
  736. if ($filter) $sql.= " AND ".$filter;
  737. $sql.= " ORDER BY re.description ASC";
  738. dol_syslog("Form::select_remises sql=".$sql);
  739. $resql=$this->db->query($sql);
  740. if ($resql)
  741. {
  742. print '<select class="flat" name="'.$htmlname.'">';
  743. $num = $this->db->num_rows($resql);
  744. $qualifiedlines=$num;
  745. $i = 0;
  746. if ($num)
  747. {
  748. print '<option value="0">&nbsp;</option>';
  749. while ($i < $num)
  750. {
  751. $obj = $this->db->fetch_object($resql);
  752. $desc=dol_trunc($obj->description,40);
  753. if ($desc=='(CREDIT_NOTE)') $desc=$langs->trans("CreditNote");
  754. if ($desc=='(DEPOSIT)') $desc=$langs->trans("Deposit");
  755. $selectstring='';
  756. if ($selected > 0 && $selected == $obj->rowid) $selectstring=' selected="selected"';
  757. $disabled='';
  758. if ($maxvalue > 0 && $obj->amount_ttc > $maxvalue)
  759. {
  760. $qualifiedlines--;
  761. $disabled=' disabled="disabled"';
  762. }
  763. print '<option value="'.$obj->rowid.'"'.$selectstring.$disabled.'>'.$desc.' ('.price($obj->amount_ht).' '.$langs->trans("HT").' - '.price($obj->amount_ttc).' '.$langs->trans("TTC").')</option>';
  764. $i++;
  765. }
  766. }
  767. print '</select>';
  768. return $qualifiedlines;
  769. }
  770. else
  771. {
  772. dol_print_error($this->db);
  773. return -1;
  774. }
  775. }
  776. /**
  777. * Return list of all contacts (for a third party or all)
  778. *
  779. * @param socid Id ot third party or 0 for all
  780. * @param selected Id contact pre-selectionne
  781. * @param htmlname Name of HTML field ('none' for a not editable field)
  782. * @param show_empty 0=no empty value, 1=add an empty value
  783. * @param exclude List of contacts id to exclude
  784. * @param limitto Disable answers that are not id in this array list
  785. * @param showfunction Add function into label
  786. * @param moreclass Add more class to class style
  787. * @return int <0 if KO, Nb of contact in list if OK
  788. */
  789. function select_contacts($socid,$selected='',$htmlname='contactid',$showempty=0,$exclude='',$limitto='',$showfunction=0, $moreclass='')
  790. {
  791. global $conf,$langs;
  792. // On recherche les societes
  793. $sql = "SELECT s.rowid, s.name, s.firstname, s.poste FROM";
  794. $sql.= " ".MAIN_DB_PREFIX ."socpeople as s";
  795. $sql.= " WHERE entity = ".$conf->entity;
  796. if ($socid > 0) $sql.= " AND fk_soc=".$socid;
  797. $sql.= " ORDER BY s.name ASC";
  798. dol_syslog("Form::select_contacts sql=".$sql);
  799. $resql=$this->db->query($sql);
  800. if ($resql)
  801. {
  802. $num=$this->db->num_rows($resql);
  803. if ($num == 0) return 0;
  804. if ($htmlname != 'none') print '<select class="flat'.($moreclass?' '.$moreclass:'').'" id="'.$htmlname.'" name="'.$htmlname.'">';
  805. if ($showempty) print '<option value="0"></option>';
  806. $num = $this->db->num_rows($resql);
  807. $i = 0;
  808. if ($num)
  809. {
  810. include_once(DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php');
  811. $contactstatic=new Contact($this->db);
  812. while ($i < $num)
  813. {
  814. $obj = $this->db->fetch_object($resql);
  815. $contactstatic->id=$obj->rowid;
  816. $contactstatic->name=$obj->name;
  817. $contactstatic->firstname=$obj->firstname;
  818. if ($htmlname != 'none')
  819. {
  820. $disabled=0;
  821. if (is_array($exclude) && count($exclude) && in_array($obj->rowid,$exclude)) $disabled=1;
  822. if (is_array($limitto) && count($limitto) && ! in_array($obj->rowid,$limitto)) $disabled=1;
  823. if ($selected && $selected == $obj->rowid)
  824. {
  825. print '<option value="'.$obj->rowid.'"';
  826. if ($disabled) print ' disabled="disabled"';
  827. print ' selected="selected">';
  828. print $contactstatic->getFullName($langs);
  829. if ($showfunction && $obj->poste) print ' ('.$obj->poste.')';
  830. print '</option>';
  831. }
  832. else
  833. {
  834. print '<option value="'.$obj->rowid.'"';
  835. if ($disabled) print ' disabled="disabled"';
  836. print '>';
  837. print $contactstatic->getFullName($langs);
  838. if ($showfunction && $obj->poste) print ' ('.$obj->poste.')';
  839. print '</option>';
  840. }
  841. }
  842. else
  843. {
  844. if ($selected == $obj->rowid)
  845. {
  846. print $contactstatic->getFullName($langs);
  847. if ($showfunction && $obj->poste) print ' ('.$obj->poste.')';
  848. }
  849. }
  850. $i++;
  851. }
  852. }
  853. if ($htmlname != 'none')
  854. {
  855. print '</select>';
  856. }
  857. return $num;
  858. }
  859. else
  860. {
  861. dol_print_error($this->db);
  862. return -1;
  863. }
  864. }
  865. /**
  866. * Return select list of users
  867. *
  868. * @param selected Id user preselected
  869. * @param htmlname Field name in form
  870. * @param show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  871. * @param exclude Array list of users id to exclude
  872. * @param disabled If select list must be disabled
  873. * @param include Array list of users id to include
  874. * @param enableonly Array list of users id to be enabled. All other must be disabled
  875. * @param force_entity Possibility to force entity
  876. */
  877. function select_users($selected='',$htmlname='userid',$show_empty=0,$exclude='',$disabled=0,$include='',$enableonly='',$force_entity=0)
  878. {
  879. print $this->select_dolusers($selected,$htmlname,$show_empty,$exclude,$disabled,$include,$enableonly,$force_entity);
  880. }
  881. /**
  882. * Return select list of users
  883. *
  884. * @param selected User id or user object of user preselected. If -1, we use id of current user.
  885. * @param htmlname Field name in form
  886. * @param show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  887. * @param exclude Array list of users id to exclude
  888. * @param disabled If select list must be disabled
  889. * @param include Array list of users id to include
  890. * @param enableonly Array list of users id to be enabled. All other must be disabled
  891. * @param force_entity Possibility to force entity
  892. */
  893. function select_dolusers($selected='',$htmlname='userid',$show_empty=0,$exclude='',$disabled=0,$include='',$enableonly='',$force_entity=0)
  894. {
  895. global $conf,$user,$langs;
  896. // If no preselected user defined, we take current user
  897. if ($selected < -1 && empty($conf->global->SOCIETE_DISABLE_DEFAULT_SALESREPRESENTATIVE)) $selected=$user->id;
  898. // Permettre l'exclusion d'utilisateurs
  899. if (is_array($exclude)) $excludeUsers = implode("','",$exclude);
  900. // Permettre l'inclusion d'utilisateurs
  901. if (is_array($include)) $includeUsers = implode("','",$include);
  902. $out='';
  903. // On recherche les utilisateurs
  904. $sql = "SELECT u.rowid, u.name, u.firstname, u.login, u.admin, u.entity";
  905. if(! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  906. {
  907. $sql.= ", e.label";
  908. }
  909. $sql.= " FROM ".MAIN_DB_PREFIX ."user as u";
  910. if(! empty($conf->multicompany->enabled) && $conf->entity == 1 && $user->admin && ! $user->entity)
  911. {
  912. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX ."entity as e on e.rowid=u.entity";
  913. if ($force_entity) $sql.= " WHERE u.entity IN (0,".$force_entity.")";
  914. else $sql.= " WHERE u.entity IS NOT NULL";
  915. }
  916. else
  917. {
  918. $sql.= " WHERE u.entity IN (0,".$conf->entity.")";
  919. }
  920. if (is_array($exclude) && $excludeUsers) $sql.= " AND u.rowid NOT IN ('".$excludeUsers."')";
  921. if (is_array($include) && $includeUsers) $sql.= " AND u.rowid IN ('".$includeUsers."')";
  922. $sql.= " ORDER BY u.name ASC";
  923. dol_syslog("Form::select_dolusers sql=".$sql);
  924. $resql=$this->db->query($sql);
  925. if ($resql)
  926. {
  927. $num = $this->db->num_rows($resql);
  928. $i = 0;
  929. if ($num)
  930. {
  931. $out.= '<select class="flat" id="'.$htmlname.'" name="'.$htmlname.'"'.($disabled?' disabled="disabled"':'').'>';
  932. if ($show_empty) $out.= '<option value="-1"'.($id==-1?' selected="selected"':'').'>&nbsp;</option>'."\n";
  933. $userstatic=new User($this->db);
  934. while ($i < $num)
  935. {
  936. $obj = $this->db->fetch_object($resql);
  937. $userstatic->id=$obj->rowid;
  938. $userstatic->nom=$obj->name;
  939. $userstatic->prenom=$obj->firstname;
  940. $disableline=0;
  941. if (is_array($enableonly) && count($enableonly) && ! in_array($obj->rowid,$enableonly)) $disableline=1;
  942. if ((is_object($selected) && $selected->id == $obj->rowid) || (! is_object($selected) && $selected == $obj->rowid))
  943. {
  944. $out.= '<option value="'.$obj->rowid.'"';
  945. if ($disableline) $out.= ' disabled="disabled"';
  946. $out.= ' selected="selected">';
  947. }
  948. else
  949. {
  950. $out.= '<option value="'.$obj->rowid.'"';
  951. if ($disableline) $out.= ' disabled="disabled"';
  952. $out.= '>';
  953. }
  954. $out.= $userstatic->getFullName($langs);
  955. if(! empty($conf->multicompany->enabled) && empty($conf->multicompany->transverse_mode) && $conf->entity == 1 && $user->admin && ! $user->entity)
  956. {
  957. if ($obj->admin && ! $obj->entity) $out.=" (".$langs->trans("AllEntities").")";
  958. else $out.=" (".$obj->label.")";
  959. }
  960. //if ($obj->admin) $out.= ' *';
  961. if ($conf->global->MAIN_SHOW_LOGIN) $out.= ' ('.$obj->login.')';
  962. $out.= '</option>';
  963. $i++;
  964. }
  965. }
  966. else
  967. {
  968. $out.= '<select class="flat" name="'.$htmlname.'" disabled="disabled">';
  969. $out.= '<option value="">'.$langs->trans("None").'</option>';
  970. }
  971. $out.= '</select>';
  972. }
  973. else
  974. {
  975. dol_print_error($this->db);
  976. }
  977. return $out;
  978. }
  979. /**
  980. * Return list of products for customer in Ajax if Ajax activated or go to select_produits_do
  981. *
  982. * @param int $selected Preselected products
  983. * @param string $htmlname Name of HTML seletc field (must be unique in page)
  984. * @param int $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  985. * @param int $limit Limit on number of returned lines
  986. * @param int $price_level Level of price to show
  987. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  988. * @param int $finished 2=all, 1=finished, 0=raw material
  989. * @param string $selected_input_value Value of preselected input text (with ajax)
  990. * @return void
  991. */
  992. function select_produits($selected='',$htmlname='productid',$filtertype='',$limit=20,$price_level=0,$status=1,$finished=2,$selected_input_value='',$hidelabel=0)
  993. {
  994. global $langs,$conf;
  995. $price_level = (! empty($price_level) ? $price_level : 0);
  996. if ($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)
  997. {
  998. if ($selected && empty($selected_input_value))
  999. {
  1000. require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
  1001. $product = new Product($this->db);
  1002. $product->fetch($selected);
  1003. $selected_input_value=$product->ref;
  1004. }
  1005. // mode=1 means customers products
  1006. print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT.'/product/ajaxproducts.php', 'htmlname='.$htmlname.'&outjson=1&price_level='.$price_level.'&type='.$filtertype.'&mode=1&status='.$status.'&finished='.$finished, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT);
  1007. if (! $hidelabel) print $langs->trans("RefOrLabel").' : ';
  1008. print '<input type="text" size="20" name="search_'.$htmlname.'" id="search_'.$htmlname.'" value="'.$selected_input_value.'" />';
  1009. }
  1010. else
  1011. {
  1012. $this->select_produits_do($selected,$htmlname,$filtertype,$limit,$price_level,'',$status,$finished,0);
  1013. }
  1014. print '<br>';
  1015. }
  1016. /**
  1017. * Return list of products for a customer
  1018. *
  1019. * @param int $selected Preselected product
  1020. * @param string $htmlname Name of select html
  1021. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1022. * @param int $limit Limite sur le nombre de lignes retournees
  1023. * @param int $price_level Level of price to show
  1024. * @param string $filterkey Filter on product
  1025. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  1026. * @param int $finished Filter on finished field: 2=No filter
  1027. * @param int $disableout Disable print output
  1028. * @return array Array of keys for json
  1029. */
  1030. function select_produits_do($selected='',$htmlname='productid',$filtertype='',$limit=20,$price_level=0,$filterkey='',$status=1,$finished=2,$disableout=0)
  1031. {
  1032. global $langs,$conf,$user,$db;
  1033. $sql = "SELECT ";
  1034. $sql.= " p.rowid, p.label, p.ref, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.duration, p.stock";
  1035. // Multilang : we add translation
  1036. if ($conf->global->MAIN_MULTILANGS)
  1037. {
  1038. $sql.= ", pl.label as label_translated";
  1039. }
  1040. $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
  1041. // Multilang : we add translation
  1042. if ($conf->global->MAIN_MULTILANGS)
  1043. {
  1044. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lang as pl ON pl.fk_product = p.rowid AND pl.lang='". $langs->getDefaultLang() ."'";
  1045. }
  1046. $sql.= ' WHERE p.entity IN (0,'.(! empty($conf->entities['product']) ? $conf->entities['product'] : $conf->entity).')';
  1047. if ($finished == 0)
  1048. {
  1049. $sql.= " AND p.finished = ".$finished;
  1050. }
  1051. elseif ($finished == 1)
  1052. {
  1053. $sql.= " AND p.finished = ".$finished;
  1054. if ($status >= 0) $sql.= " AND p.tosell = ".$status;
  1055. }
  1056. elseif ($status >= 0)
  1057. {
  1058. $sql.= " AND p.tosell = ".$status;
  1059. }
  1060. if (strval($filtertype) != '') $sql.=" AND p.fk_product_type=".$filtertype;
  1061. // Add criteria on ref/label
  1062. if ($filterkey && $filterkey != '')
  1063. {
  1064. if (! empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)) // Can use index
  1065. {
  1066. $sql.=" AND (p.ref LIKE '".$filterkey."%' OR p.label LIKE '".$filterkey."%'";
  1067. if ($conf->global->MAIN_MULTILANGS) $sql.=" OR pl.label LIKE '".$filterkey."%'";
  1068. $sql.=")";
  1069. }
  1070. else
  1071. {
  1072. $sql.=" AND (p.ref LIKE '%".$filterkey."%' OR p.label LIKE '%".$filterkey."%'";
  1073. if ($conf->global->MAIN_MULTILANGS) $sql.=" OR pl.label LIKE '%".$filterkey."%'";
  1074. $sql.=")";
  1075. }
  1076. }
  1077. $sql.= $db->order("p.ref");
  1078. $sql.= $db->plimit($limit);
  1079. // Build output string
  1080. $outselect='';
  1081. $outjson=array();
  1082. dol_syslog("Form::select_produits_do search product sql=".$sql, LOG_DEBUG);
  1083. $result=$this->db->query($sql);
  1084. if ($result)
  1085. {
  1086. $num = $this->db->num_rows($result);
  1087. $outselect.='<select class="flat" name="'.$htmlname.'" id="'.$htmlname.'">';
  1088. $outselect.='<option value="0" selected="selected">&nbsp;</option>';
  1089. $i = 0;
  1090. while ($num && $i < $num)
  1091. {
  1092. $outkey='';
  1093. $outval='';
  1094. $outref='';
  1095. $objp = $this->db->fetch_object($result);
  1096. $label=$objp->label;
  1097. if (! empty($objp->label_translated)) $label=$objp->label_translated;
  1098. if ($filterkey && $filterkey != '') $label=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$label,1);
  1099. $outkey=$objp->rowid;
  1100. $outref=$objp->ref;
  1101. $opt = '<option value="'.$objp->rowid.'"';
  1102. $opt.= ($objp->rowid == $selected)?' selected="selected"':'';
  1103. if ($conf->stock->enabled && $objp->fk_product_type == 0 && isset($objp->stock))
  1104. {
  1105. if ($objp->stock > 0)
  1106. {
  1107. $opt.= ' style="background-color:#32CD32; color:#F5F5F5;"';
  1108. }
  1109. else if ($objp->stock <= 0)
  1110. {
  1111. $opt.= ' style="background-color:#FF0000; color:#F5F5F5;"';
  1112. }
  1113. }
  1114. $opt.= '>';
  1115. $opt.= $langs->convToOutputCharset($objp->ref).' - '.$langs->convToOutputCharset(dol_trunc($label,32)).' - ';
  1116. $objRef = $objp->ref;
  1117. if ($filterkey && $filterkey != '') $objRef=preg_replace('/('.preg_quote($filterkey).')/i','<strong>$1</strong>',$objRef,1);
  1118. $outval.=$objRef.' - '.dol_trunc($label,32).' - ';
  1119. $found=0;
  1120. $currencytext=$langs->trans("Currency".$conf->monnaie);
  1121. $currencytextnoent=$langs->transnoentities("Currency".$conf->monnaie);
  1122. if (dol_strlen($currencytext) > 10) $currencytext=$conf->monnaie; // If text is too long, we use the short code
  1123. if (dol_strlen($currencytextnoent) > 10) $currencytextnoent=$conf->monnaie; // If text is too long, we use the short code
  1124. // Multiprice
  1125. if ($price_level

Large files files are truncated, but you can click here to view the full file