PageRenderTime 179ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 1ms

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

https://bitbucket.org/speedealing/speedealing
PHP | 3267 lines | 1799 code | 306 blank | 1162 comment | 600 complexity | 82c19266cd83ba69aa57d06d8ea0ab60 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (c) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 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-2013 Regis Houssin <regis.houssin@capnetworks.com>
  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-2012 Herve Prot <herve.prot@symeos.com>
  15. * Copyright (C) 2012 Marcos GarcĂ­a <marcosgdf@gmail.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. */
  30. /**
  31. * \file htdocs/core/class/html.form.class.php
  32. * \ingroup core
  33. * \brief File of class with all html predefined components
  34. */
  35. /**
  36. * Class to manage generation of HTML components
  37. * Only common components must be here.
  38. */
  39. class Form {
  40. var $db;
  41. var $error;
  42. var $num;
  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_types_fees = array();
  49. var $cache_currencies = array();
  50. var $cache_vatrates = array();
  51. var $tva_taux_value;
  52. var $tva_taux_libelle;
  53. /**
  54. * Constructor
  55. */
  56. public function __construct($db = '') {
  57. $this->db = $db;
  58. }
  59. /**
  60. * Output key field for an editable field
  61. *
  62. * @param string $text Text of label or key to translate
  63. * @param string $htmlname Name of select field
  64. * @param string $preselected Name of Value to show/edit (not used in this function)
  65. * @param object $object Object
  66. * @param boolean $perm Permission to allow button to edit parameter
  67. * @param string $typeofdata Type of data ('string' by default, 'email', 'numeric:99', 'text' or 'textarea:rows:cols', 'day' or 'datepicker', 'ckeditor:dolibarr_zzz:width:height:savemethod:1:rows:cols', 'select:xxx'...)
  68. * @param string $moreparam More param to add on a href URL
  69. * @return string HTML edit field
  70. */
  71. function editfieldkey($text, $htmlname, $preselected, $object, $perm, $typeofdata = 'string', $moreparam = '') {
  72. global $conf, $langs;
  73. $ret = '';
  74. // TODO change for compatibility
  75. if (!preg_match('/^select;/', $typeofdata)) {
  76. if ($perm) {
  77. $tmp = explode(':', $typeofdata);
  78. $ret.= '<div class="editkey_' . $tmp[0] . (!empty($tmp[1]) ? ' ' . $tmp[1] : '') . '" id="' . $htmlname . '">';
  79. $ret.= $langs->trans($text);
  80. $ret.= '</div>' . "\n";
  81. } else {
  82. $ret.= $langs->trans($text);
  83. }
  84. } else {
  85. $ret.='<table class="nobordernopadding" width="100%"><tr><td nowrap="nowrap">';
  86. $ret.=$langs->trans($text);
  87. $ret.='</td>';
  88. if (GETPOST('action') != 'edit' . $htmlname && $perm)
  89. $ret.='<td align="right"><a href="' . $_SERVER["PHP_SELF"] . '?action=edit' . $htmlname . '&amp;id=' . $object->id . $moreparam . '">' . img_edit($langs->trans('Edit'), 1) . '</a></td>';
  90. $ret.='</tr></table>';
  91. }
  92. return $ret;
  93. }
  94. /**
  95. * Output val field for an editable field
  96. *
  97. * @param string $text Text of label (not used in this function)
  98. * @param string $htmlname Name of select field
  99. * @param string $value Value to show/edit
  100. * @param object $object Object
  101. * @param boolean $perm Permission to allow button to edit parameter
  102. * @param string $typeofdata Type of data ('string' by default, 'email', 'numeric:99', 'text' or 'textarea:rows:cols', 'day' or 'datepicker', 'ckeditor:dolibarr_zzz:width:height:savemethod:toolbarstartexpanded:rows:cols', 'select:xxx'...)
  103. * @param string $editvalue When in edit mode, use this value as $value instead of value
  104. * @param object $extObject External object
  105. * @param string $success Success message
  106. * @param string $moreparam More param to add on a href URL
  107. * @return string HTML edit field
  108. */
  109. function editfieldval($text, $htmlname, $value, $object, $perm, $typeofdata = 'string', $editvalue = '', $extObject = null, $success = null, $moreparam = '') {
  110. global $conf, $langs, $db;
  111. $ret = '';
  112. // When option to edit inline is activated
  113. $ret.=$this->editInPlace($object, $value, $htmlname, $perm, $typeofdata, $editvalue, $extObject, $success);
  114. return $ret;
  115. }
  116. /**
  117. * Output edit in place form
  118. *
  119. * @param object $object Object
  120. * @param string $value Value to show/edit
  121. * @param string $htmlname DIV ID (field name)
  122. * @param int $condition Condition to edit
  123. * @param string $inputType Type of input ('numeric', 'datepicker', 'textarea:rows:cols', 'ckeditor:dolibarr_zzz:width:height:?:1:rows:cols', 'select:xxx')
  124. * @param string $editvalue When in edit mode, use this value as $value instead of value
  125. * @param object $extObject External object
  126. * @param string $success Success message
  127. * @return string HTML edit in place
  128. */
  129. private function editInPlace($object, $value, $htmlname, $condition, $inputType = 'text', $editvalue = null, $extObject = null, $success = null) {
  130. global $conf;
  131. $out = '';
  132. // Check parameters
  133. if (preg_match('/^text/', $inputType))
  134. $value = dol_nl2br($value);
  135. else if (preg_match('/^numeric/', $inputType))
  136. $value = price($value);
  137. else if ($inputType == 'datepicker')
  138. $value = dol_print_date($value, 'day');
  139. if ($condition) {
  140. $element = false;
  141. $table_element = false;
  142. $fk_element = false;
  143. $loadmethod = false;
  144. $savemethod = false;
  145. $ext_element = false;
  146. $button_only = true;
  147. if (is_object($object)) {
  148. $element = $object->element;
  149. $table_element = $object->table_element;
  150. $fk_element = $object->id;
  151. }
  152. if (is_object($extObject)) {
  153. $ext_element = $extObject->element;
  154. }
  155. if (preg_match('/^(string|email|numeric)/', $inputType)) {
  156. $tmp = explode(':', $inputType);
  157. $inputType = $tmp[0];
  158. if (!empty($tmp[1]))
  159. $inputOption = $tmp[1];
  160. if (!empty($tmp[2]))
  161. $savemethod = $tmp[2];
  162. }
  163. else if (preg_match('/^datepicker/', $inputType)) {
  164. $tmp = explode(':', $inputType);
  165. $inputType = $tmp[0];
  166. if (!empty($tmp[1]))
  167. $inputOption = $tmp[1];
  168. if (!empty($tmp[2]))
  169. $savemethod = $tmp[2];
  170. $out.= '<input id="timestamp" type="hidden"/>' . "\n"; // Use for timestamp format
  171. }
  172. else if (preg_match('/^(select|autocomplete)/', $inputType)) {
  173. $tmp = explode(':', $inputType);
  174. $inputType = $tmp[0];
  175. $loadmethod = $tmp[1];
  176. if (!empty($tmp[2]))
  177. $savemethod = $tmp[2];
  178. if (!empty($tmp[3]))
  179. $button_only = true;
  180. }
  181. else if (preg_match('/^text/', $inputType)) {
  182. $tmp = explode(':', $inputType);
  183. $inputType = $tmp[0];
  184. if (!empty($tmp[1]))
  185. $rows = $tmp[1];
  186. if (!empty($tmp[2]))
  187. $cols = $tmp[2];
  188. }
  189. else if (preg_match('/^ckeditor/', $inputType)) {
  190. $tmp = explode(':', $inputType);
  191. $inputType = $tmp[0];
  192. $toolbar = $tmp[1];
  193. if (!empty($tmp[2]))
  194. $width = $tmp[2];
  195. if (!empty($tmp[3]))
  196. $heigth = $tmp[3];
  197. if (!empty($tmp[4]))
  198. $savemethod = $tmp[4];
  199. $out.= '<input id="ckeditor_toolbar" value="' . $toolbar . '" type="hidden"/>' . "\n";
  200. }
  201. $out.= '<input id="element_id_' . $htmlname . '" type="hidden" value="' . $object->id . '"/>';
  202. $out.= '<input id="element_class_' . $htmlname . '" type="hidden" value="' . get_class($object) . '"/>';
  203. if (!empty($loadmethod))
  204. $out.= '<input id="loadmethod_' . $htmlname . '" value="' . $loadmethod . '" type="hidden"/>' . "\n";
  205. if (!empty($savemethod))
  206. $out.= '<input id="savemethod_' . $htmlname . '" value="' . $savemethod . '" type="hidden"/>' . "\n";
  207. if (!empty($ext_element))
  208. $out.= '<input id="ext_element_' . $htmlname . '" value="' . $ext_element . '" type="hidden"/>' . "\n";
  209. if (!empty($success))
  210. $out.= '<input id="success_' . $htmlname . '" value="' . $success . '" type="hidden"/>' . "\n";
  211. $out.= '<span id="viewval_' . $htmlname . '" class="viewval_' . $inputType . ($button_only ? ' inactive' : ' active') . '">';
  212. $out.= $object->print_fk_extrafields($htmlname);
  213. $out.= '</span>' . "\n";
  214. if (preg_match('/^tag/', $inputType)) {
  215. $out.= '<ul class="array_tag_handler" id="editval_' . $htmlname . '"></ul>';
  216. } else {
  217. $out.= '<span id="editval_' . $htmlname . '" class="editval_' . $inputType . ($button_only ? ' inactive' : ' active') . ' hideobject">';
  218. $out.= (!empty($editvalue) ? $editvalue : $value);
  219. $out.= '</span>' . "\n";
  220. }
  221. } else {
  222. if (preg_match('/^tag/', $inputType)) {
  223. $out.= $object->LibTag($value, array("key" => $htmlname));
  224. } else {
  225. $out.= $object->print_fk_extrafields($htmlname);
  226. }
  227. }
  228. return $out;
  229. }
  230. /**
  231. * Show a text and picto with tooltip on text or picto
  232. *
  233. * @param string $text Text to show
  234. * @param string $htmltext Content html of tooltip. Must be HTML/UTF8 encoded.
  235. * @param int $tooltipon 1=tooltip sur texte, 2=tooltip sur picto, 3=tooltip sur les 2
  236. * @param int $direction -1=Le picto est avant, 0=pas de picto, 1=le picto est apres
  237. * @param string $img Code img du picto (use img_xxx() function to get it)
  238. * @param string $extracss Add a CSS style to td tags
  239. * @param int $notabs 0=Include table and tr tags, 1=Do not include table and tr tags, 2=use div, 3=use span
  240. * @param string $incbefore Include code before the text
  241. * @param int $noencodehtmltext Do not encode into html entity the htmltext
  242. * @return string Code html du tooltip (texte+picto)
  243. * @see Use function textwithpicto if you can.
  244. */
  245. function textwithtooltip($text, $htmltext, $tooltipon = 1, $direction = 0, $img = '', $extracss = '', $notabs = 0, $incbefore = '', $noencodehtmltext = 0) {
  246. global $conf;
  247. if ($incbefore)
  248. $text = $incbefore . $text;
  249. if (!$htmltext)
  250. return $text;
  251. $tag = 'td';
  252. if ($notabs == 2)
  253. $tag = 'div';
  254. if ($notabs == 3)
  255. $tag = 'span';
  256. // Sanitize tooltip
  257. $htmltext = str_replace("\\", "\\\\", $htmltext);
  258. $htmltext = str_replace("\r", "", $htmltext);
  259. $htmltext = str_replace("\n", "", $htmltext);
  260. $htmltext = str_replace('"', "&quot;", $htmltext);
  261. if ($tooltipon == 2 || $tooltipon == 3)
  262. $paramfortooltipimg = ' class="classfortooltip' . ($extracss ? ' ' . $extracss : '') . '" title="' . ($noencodehtmltext ? $htmltext : dol_escape_htmltag($htmltext, 1)) . '"'; // Attribut to put on td img tag to store tooltip
  263. else
  264. $paramfortooltipimg = ($extracss ? ' class="' . $extracss . '"' : ''); // Attribut to put on td text tag
  265. if ($tooltipon == 1 || $tooltipon == 3)
  266. $paramfortooltiptd = ' class="classfortooltip' . ($extracss ? ' ' . $extracss : '') . '" title="' . ($noencodehtmltext ? $htmltext : dol_escape_htmltag($htmltext, 1)) . '"'; // Attribut to put on td tag to store tooltip
  267. else
  268. $paramfortooltiptd = ($extracss ? ' class="' . $extracss . '"' : ''); // Attribut to put on td text tag
  269. $s = "";
  270. if (empty($notabs))
  271. $s.='<table class="nobordernopadding" summary=""><tr>';
  272. if ($direction < 0)
  273. $s.='<' . $tag . $paramfortooltipimg . ' valign="top" width="14">' . $img . '</' . $tag . '>';
  274. if ($text != '')
  275. $s.='<' . $tag . $paramfortooltiptd . '>' . (($direction < 0) ? '&nbsp;' : '') . $text . (($direction > 0) ? '&nbsp;' : '') . '</' . $tag . '>';
  276. if ($direction > 0)
  277. $s.='<' . $tag . $paramfortooltipimg . ' valign="top" width="14">' . $img . '</' . $tag . '>';
  278. if (empty($notabs))
  279. $s.='</tr></table>';
  280. return $s;
  281. }
  282. /**
  283. * Show a text with a picto and a tooltip on picto
  284. *
  285. * @param string $text Text to show
  286. * @param string $htmltext Content of tooltip
  287. * @param int $direction 1=Icon is after text, -1=Icon is before text, 0=no icon
  288. * @param string $type Type of picto (info, help, warning, superadmin...)
  289. * @param string $extracss Add a CSS style to td tags
  290. * @param int $noencodehtmltext Do not encode into html entity the htmltext
  291. * @param int $notabs 0=Include table and tr tags, 1=Do not include table and tr tags, 2=use div, 3=use span
  292. * @return string HTML code of text, picto, tooltip
  293. */
  294. function textwithpicto($text, $htmltext, $direction = 1, $type = 'help', $extracss = '', $noencodehtmltext = 0, $notabs = 0) {
  295. global $conf;
  296. $alt = '';
  297. //For backwards compatibility
  298. if ($type == '0')
  299. $type = 'info';
  300. elseif ($type == '1')
  301. $type = 'help';
  302. // If info or help with no javascript, show only text
  303. if (empty($conf->use_javascript_ajax)) {
  304. if ($type == 'info' || $type == 'help')
  305. return $text;
  306. else {
  307. $alt = $htmltext;
  308. $htmltext = '';
  309. }
  310. }
  311. // If info or help with smartphone, show only text
  312. if (!empty($conf->browser->phone)) {
  313. if ($type == 'info' || $type == 'help')
  314. return $text;
  315. }
  316. if ($type == 'info')
  317. $img = img_help(0, $alt);
  318. elseif ($type == 'help')
  319. $img = img_help(1, $alt);
  320. elseif ($type == 'superadmin')
  321. $img = img_picto($alt, 'redstar');
  322. elseif ($type == 'admin')
  323. $img = img_picto($alt, 'star');
  324. elseif ($type == 'warning')
  325. $img = img_warning($alt);
  326. return $this->textwithtooltip($text, $htmltext, 2, $direction, $img, $extracss, $notabs, '', $noencodehtmltext);
  327. }
  328. /**
  329. * Return list of types of lines (product or service)
  330. * Example: 0=product, 1=service, 9=other (for external module)
  331. *
  332. * @param string $selected Preselected type
  333. * @param string $htmlname Name of field in html form
  334. * @param int $showempty Add an empty field
  335. * @param int $hidetext Do not show label before combo box
  336. * @param string $forceall Force to show products and services in combo list, whatever are activated modules
  337. * @return void
  338. */
  339. function select_type_of_lines($selected = '', $htmlname = 'type', $showempty = 0, $hidetext = 0, $forceall = 0) {
  340. global $db, $langs, $user, $conf;
  341. // If product & services are enabled or both disabled.
  342. if ($forceall || (!empty($conf->product->enabled) && !empty($conf->service->enabled))
  343. || (empty($conf->product->enabled) && empty($conf->service->enabled))) {
  344. if (empty($hidetext))
  345. print $langs->trans("Type") . ': ';
  346. print '<select class="flat" id="select_' . $htmlname . '" name="' . $htmlname . '">';
  347. if ($showempty) {
  348. print '<option value="-1"';
  349. if ($selected == -1)
  350. print ' selected="selected"';
  351. print '>&nbsp;</option>';
  352. }
  353. print '<option value="0"';
  354. if (0 == $selected)
  355. print ' selected="selected"';
  356. print '>' . $langs->trans("Product");
  357. print '<option value="1"';
  358. if (1 == $selected)
  359. print ' selected="selected"';
  360. print '>' . $langs->trans("Service");
  361. print '</select>';
  362. //if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"),1);
  363. }
  364. if (!$forceall && empty($conf->product->enabled) && !empty($conf->service->enabled)) {
  365. print '<input type="hidden" name="' . $htmlname . '" value="1">';
  366. }
  367. if (!$forceall && !empty($conf->product->enabled) && empty($conf->service->enabled)) {
  368. print '<input type="hidden" name="' . $htmlname . '" value="0">';
  369. }
  370. }
  371. /**
  372. * Load into cache cache_types_fees, array of types of fees
  373. *
  374. * @return int Nb of lines loaded, 0 if already loaded, <0 if ko
  375. * TODO move in DAO class
  376. */
  377. function load_cache_types_fees() {
  378. return false;
  379. /*
  380. global $langs;
  381. $langs->load("trips");
  382. if (count($this->cache_types_fees))
  383. return 0; // Cache already load
  384. $sql = "SELECT c.code, c.libelle as label";
  385. $sql.= " FROM " . MAIN_DB_PREFIX . "c_type_fees as c";
  386. $sql.= " ORDER BY lower(c.libelle) ASC";
  387. dol_syslog(get_class($this) . '::load_cache_types_fees sql=' . $sql, LOG_DEBUG);
  388. $resql = $this->db->query($sql);
  389. if ($resql) {
  390. $num = $this->db->num_rows($resql);
  391. $i = 0;
  392. while ($i < $num) {
  393. $obj = $this->db->fetch_object($resql);
  394. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  395. $label = ($obj->code != $langs->trans($obj->code) ? $langs->trans($obj->code) : $langs->trans($obj->label));
  396. $this->cache_types_fees[$obj->code] = $label;
  397. $i++;
  398. }
  399. return $num;
  400. } else {
  401. dol_print_error($this->db);
  402. return -1;
  403. }
  404. */
  405. }
  406. /**
  407. * Return list of types of notes
  408. *
  409. * @param string $selected Preselected type
  410. * @param string $htmlname Name of field in form
  411. * @param int $showempty Add an empty field
  412. * @return void
  413. */
  414. function select_type_fees($selected = '', $htmlname = 'type', $showempty = 0) {
  415. global $user, $langs;
  416. dol_syslog(get_class($this) . "::select_type_fees " . $selected . ", " . $htmlname, LOG_DEBUG);
  417. $this->load_cache_types_fees();
  418. print '<select class="flat" name="' . $htmlname . '">';
  419. if ($showempty) {
  420. print '<option value="-1"';
  421. if ($selected == -1)
  422. print ' selected="selected"';
  423. print '>&nbsp;</option>';
  424. }
  425. foreach ($this->cache_types_fees as $key => $value) {
  426. print '<option value="' . $key . '"';
  427. if ($key == $selected)
  428. print ' selected="selected"';
  429. print '>';
  430. print $value;
  431. print '</option>';
  432. }
  433. print '</select>';
  434. if ($user->admin)
  435. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"), 1);
  436. }
  437. /**
  438. * Output html form to select a third party
  439. *
  440. * @param string $selected Preselected type
  441. * @param string $htmlname Name of field in form
  442. * @param string $filter Optionnal filters criteras
  443. * @param int $showempty Add an empty field
  444. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  445. * @param int $forcecombo Force to use combo box
  446. * @param array $event Event options
  447. * @return string HTML string with
  448. */
  449. function select_company($selected = '', $htmlname = 'socid', $filter = '', $showempty = 0, $showtype = 0, $forcecombo = 0, $event = array()) {
  450. global $conf, $user, $langs;
  451. $out = '';
  452. $object = new Societe($this->db);
  453. try {
  454. $result = $object->getView("list");
  455. } catch (Exception $e) {
  456. $this->error = "Fetch : Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  457. dol_print_error($this->db, $this->error);
  458. return 0;
  459. }
  460. $out.= '<select id="' . $htmlname . '" class="flat" name="' . $htmlname . '">';
  461. if ($showempty)
  462. $out.= '<option value="0"></option>';
  463. if (count($result->rows) > 0) {
  464. foreach ($result->rows as $aRow) {
  465. $obj = $aRow->value;
  466. $label = $obj->name;
  467. if ($showtype) {
  468. if ($obj->client || $obj->fournisseur)
  469. $label.=' (';
  470. if ($obj->client == 1 || $obj->client == 3)
  471. $label.=$langs->trans("Customer");
  472. if ($obj->client == 2 || $obj->client == 3)
  473. $label.=($obj->client == 3 ? ', ' : '') . $langs->trans("Prospect");
  474. if ($obj->fournisseur)
  475. $label.=($obj->client ? ', ' : '') . $langs->trans("Supplier");
  476. if ($obj->client || $obj->fournisseur)
  477. $label.=')';
  478. }
  479. if ($selected > 0 && $selected == $obj->_id) {
  480. $out.= '<option value="' . $obj->_id . '" selected="selected">' . $label . '</option>';
  481. } else {
  482. $out.= '<option value="' . $obj->_id . '">' . $label . '</option>';
  483. }
  484. $i++;
  485. }
  486. }
  487. $out.= '</select>';
  488. return $out;
  489. }
  490. /**
  491. * Return HTML combo list of absolute discounts
  492. *
  493. * @param string $selected Id remise fixe pre-selectionnee
  494. * @param string $htmlname Nom champ formulaire
  495. * @param string $filter Criteres optionnels de filtre
  496. * @param int $socid Id of thirdparty
  497. * @param int $maxvalue Max value for lines that can be selected
  498. * @return int Return number of qualifed lines in list
  499. */
  500. function select_remises($selected, $htmlname, $filter, $socid, $maxvalue = 0) {
  501. global $langs, $conf, $db;
  502. $discount = new DiscountAbsolute($db);
  503. $result = $discount->getView('listAvailableDiscountsPerSociete', array('key' => $socid));
  504. // On recherche les remises
  505. // $sql = "SELECT re.rowid, re.amount_ht, re.amount_tva, re.amount_ttc,";
  506. // $sql.= " re.description, re.fk_facture_source";
  507. // $sql.= " FROM " . MAIN_DB_PREFIX . "societe_remise_except as re";
  508. // $sql.= " WHERE fk_soc = " . $socid;
  509. // if ($filter)
  510. // $sql.= " AND " . $filter;
  511. // $sql.= " ORDER BY re.description ASC";
  512. //
  513. // dol_syslog(get_class($this) . "::select_remises sql=" . $sql);
  514. // $resql = $this->db->query($sql);
  515. if (true) {
  516. print '<select class="flat" name="' . $htmlname . '">';
  517. $num = count($result->rows); //$this->db->num_rows($resql);
  518. $qualifiedlines = $num;
  519. $i = 0;
  520. if ($num) {
  521. print '<option value="0">&nbsp;</option>';
  522. while ($i < $num) {
  523. $obj = new DiscountAbsolute($this->db); //$this->db->fetch_object($resql);
  524. $obj->fetch($result->rows[$i]->id);
  525. $desc = dol_trunc($obj->description, 40);
  526. if ($desc == '(CREDIT_NOTE)')
  527. $desc = $langs->trans("CreditNote");
  528. if ($desc == '(DEPOSIT)')
  529. $desc = $langs->trans("Deposit");
  530. $selectstring = '';
  531. if ($selected > 0 && $selected == $obj->rowid)
  532. $selectstring = ' selected="selected"';
  533. $disabled = '';
  534. if ($maxvalue > 0 && $obj->amount_ttc > $maxvalue) {
  535. $qualifiedlines--;
  536. $disabled = ' disabled="disabled"';
  537. }
  538. print '<option value="' . $obj->id . '"' . $selectstring . $disabled . '>' . $desc . ' (' . price($obj->amount_ht) . ' ' . $langs->trans("HT") . ' - ' . price($obj->amount_ttc) . ' ' . $langs->trans("TTC") . ')</option>';
  539. $i++;
  540. }
  541. }
  542. print '</select>';
  543. return $qualifiedlines;
  544. } else {
  545. dol_print_error($this->db);
  546. return -1;
  547. }
  548. }
  549. /**
  550. * Return list of all contacts (for a third party or all)
  551. *
  552. * @param int $socid Id ot third party or 0 for all
  553. * @param string $selected Id contact pre-selectionne
  554. * @param string $htmlname Name of HTML field ('none' for a not editable field)
  555. * @param int $showempty 0=no empty value, 1=add an empty value
  556. * @param string $exclude List of contacts id to exclude
  557. * @param string $limitto Disable answers that are not id in this array list
  558. * @param string $showfunction Add function into label
  559. * @param string $moreclass Add more class to class style
  560. * @param string $showsoc Add company into label
  561. * @return int <0 if KO, Nb of contact in list if OK
  562. */
  563. function select_contacts($socid, $selected = '', $htmlname = 'contactid', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $moreclass = '', $showsoc = 0) {
  564. print $this->selectcontacts($socid, $selected, $htmlname, $showempty, $exclude, $limitto, $showfunction, $moreclass, $showsoc);
  565. return $this->num;
  566. }
  567. /**
  568. * Return list of all contacts (for a third party or all)
  569. *
  570. * @param int $socid Id ot third party or 0 for all
  571. * @param string $selected Id contact pre-selectionne
  572. * @param string $htmlname Name of HTML field ('none' for a not editable field)
  573. * @param int $showempty 0=no empty value, 1=add an empty value
  574. * @param string $exclude List of contacts id to exclude
  575. * @param string $limitto Disable answers that are not id in this array list
  576. * @param string $showfunction Add function into label
  577. * @param string $moreclass Add more class to class style
  578. * @param bool $options_only Return options only (for ajax treatment)
  579. * @param string $showsoc Add company into label
  580. * @return int <0 if KO, Nb of contact in list if OK
  581. */
  582. function selectcontacts($socid, $selected = '', $htmlname = 'contactid', $showempty = 0, $exclude = '', $limitto = '', $showfunction = 0, $moreclass = '', $options_only = false, $showsoc = 0) {
  583. return false;
  584. /*
  585. global $conf, $langs;
  586. $langs->load('companies');
  587. $out = '';
  588. // On recherche les societes
  589. $sql = "SELECT sp.rowid, sp.name as name, sp.firstname, sp.poste";
  590. if ($showsoc > 0) {
  591. $sql.= " , s.nom as company";
  592. }
  593. $sql.= " FROM " . MAIN_DB_PREFIX . "socpeople as sp";
  594. if ($showsoc > 0) {
  595. $sql.= " LEFT OUTER JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid=sp.fk_soc ";
  596. }
  597. $sql.= " WHERE sp.entity IN (" . getEntity('societe', 1) . ")";
  598. if ($socid > 0)
  599. $sql.= " AND sp.fk_soc=" . $socid;
  600. $sql.= " ORDER BY sp.name ASC";
  601. dol_syslog(get_class($this) . "::select_contacts sql=" . $sql);
  602. $resql = $this->db->query($sql);
  603. if ($resql) {
  604. $num = $this->db->num_rows($resql);
  605. if ($htmlname != 'none' || $options_only)
  606. $out.= '<select class="flat' . ($moreclass ? ' ' . $moreclass : '') . '" id="' . $htmlname . '" name="' . $htmlname . '">';
  607. if ($showempty)
  608. $out.= '<option value="0"></option>';
  609. $num = $this->db->num_rows($resql);
  610. $i = 0;
  611. if ($num) {
  612. include_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  613. $contactstatic = new Contact($this->db);
  614. while ($i < $num) {
  615. $obj = $this->db->fetch_object($resql);
  616. $contactstatic->id = $obj->rowid;
  617. $contactstatic->name = $obj->name;
  618. $contactstatic->lastname = $obj->name;
  619. $contactstatic->firstname = $obj->firstname;
  620. if ($htmlname != 'none') {
  621. $disabled = 0;
  622. if (is_array($exclude) && count($exclude) && in_array($obj->rowid, $exclude))
  623. $disabled = 1;
  624. if (is_array($limitto) && count($limitto) && !in_array($obj->rowid, $limitto))
  625. $disabled = 1;
  626. if ($selected && $selected == $obj->rowid) {
  627. $out.= '<option value="' . $obj->rowid . '"';
  628. if ($disabled)
  629. $out.= ' disabled="disabled"';
  630. $out.= ' selected="selected">';
  631. $out.= $contactstatic->getFullName($langs);
  632. if ($showfunction && $obj->poste)
  633. $out.= ' (' . $obj->poste . ')';
  634. if (($showsoc > 0) && $obj->company)
  635. $out.= ' - (' . $obj->company . ')';
  636. $out.= '</option>';
  637. }
  638. else {
  639. $out.= '<option value="' . $obj->rowid . '"';
  640. if ($disabled)
  641. $out.= ' disabled="disabled"';
  642. $out.= '>';
  643. $out.= $contactstatic->getFullName($langs);
  644. if ($showfunction && $obj->poste)
  645. $out.= ' (' . $obj->poste . ')';
  646. if (($showsoc > 0) && $obj->company)
  647. $out.= ' - (' . $obj->company . ')';
  648. $out.= '</option>';
  649. }
  650. }
  651. else {
  652. if ($selected == $obj->rowid) {
  653. $out.= $contactstatic->getFullName($langs);
  654. if ($showfunction && $obj->poste)
  655. $out.= ' (' . $obj->poste . ')';
  656. if (($showsoc > 0) && $obj->company)
  657. $out.= ' - (' . $obj->company . ')';
  658. }
  659. }
  660. $i++;
  661. }
  662. }
  663. else {
  664. $out.= '<option value="-1" selected="selected" disabled="disabled">' . $langs->trans("NoContactDefined") . '</option>';
  665. }
  666. if ($htmlname != 'none' || $options_only) {
  667. $out.= '</select>';
  668. }
  669. $this->num = $num;
  670. return $out;
  671. } else {
  672. dol_print_error($this->db);
  673. return -1;
  674. }
  675. */
  676. }
  677. /**
  678. * Return list of products for customer in Ajax if Ajax activated or go to select_produits_do
  679. *
  680. * @param int $selected Preselected products
  681. * @param string $htmlname Name of HTML seletc field (must be unique in page)
  682. * @param int $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  683. * @param int $limit Limit on number of returned lines
  684. * @param int $price_level Level of price to show
  685. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  686. * @param int $finished 2=all, 1=finished, 0=raw material
  687. * @param string $selected_input_value Value of preselected input text (with ajax)
  688. * @param int $hidelabel Hide label (0=no, 1=yes, 2=show search icon (before) and placeholder, 3 search icon after)
  689. * @param array $ajaxoptions Options for ajax_autocompleter
  690. * @return void
  691. */
  692. function select_produits($selected = '', $htmlname = 'productid', $filtertype = '', $limit = 20, $price_level = 0, $status = 1, $finished = 2, $selected_input_value = '', $hidelabel = 0, $ajaxoptions = array()) {
  693. global $langs, $conf;
  694. $price_level = (!empty($price_level) ? $price_level : 0);
  695. if (!empty($conf->use_javascript_ajax) && !empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)) {
  696. $placeholder = '';
  697. if ($selected && empty($selected_input_value)) {
  698. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  699. $product = new Product($this->db);
  700. $product->fetch($selected);
  701. $selected_input_value = $product->ref;
  702. }
  703. // mode=1 means customers products
  704. $urloption = 'htmlname=' . $htmlname . '&outjson=1&price_level=' . $price_level . '&type=' . $filtertype . '&mode=1&status=' . $status . '&finished=' . $finished;
  705. print ajax_autocompleter($selected, $htmlname, DOL_URL_ROOT . '/product/ajax/products.php', $urloption, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
  706. if (empty($hidelabel))
  707. print $langs->trans("RefOrLabel") . ' : ';
  708. else if ($hidelabel > 1) {
  709. if (!empty($conf->global->MAIN_HTML5_PLACEHOLDER))
  710. $placeholder = ' placeholder="' . $langs->trans("RefOrLabel") . '"';
  711. else
  712. $placeholder = ' title="' . $langs->trans("RefOrLabel") . '"';
  713. if ($hidelabel == 2) {
  714. print img_picto($langs->trans("Search"), 'search');
  715. }
  716. }
  717. print '<input type="text" size="20" name="search_' . $htmlname . '" id="search_' . $htmlname . '" value="' . $selected_input_value . '"' . $placeholder . ' />';
  718. if ($hidelabel == 3) {
  719. print img_picto($langs->trans("Search"), 'search');
  720. }
  721. } else {
  722. $this->select_produits_do($selected, $htmlname, $filtertype, $limit, $price_level, '', $status, $finished, 0);
  723. }
  724. }
  725. /**
  726. * Return list of products for a customer
  727. *
  728. * @param int $selected Preselected product
  729. * @param string $htmlname Name of select html
  730. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  731. * @param int $limit Limite sur le nombre de lignes retournees
  732. * @param int $price_level Level of price to show
  733. * @param string $filterkey Filter on product
  734. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  735. * @param int $finished Filter on finished field: 2=No filter
  736. * @param int $disableout Disable print output
  737. * @return array Array of keys for json
  738. */
  739. function select_produits_do($selected = '', $htmlname = 'productid', $filtertype = '', $limit = 20, $price_level = 0, $filterkey = '', $status = 1, $finished = 2, $disableout = 0) {
  740. return false;
  741. /*
  742. global $langs, $conf, $user, $db;
  743. $sql = "SELECT ";
  744. $sql.= " p.rowid, p.label, p.ref, p.description, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.duration, p.stock";
  745. // Multilang : we add translation
  746. if (!empty($conf->global->MAIN_MULTILANGS)) {
  747. $sql.= ", pl.label as label_translated";
  748. }
  749. $sql.= " FROM " . MAIN_DB_PREFIX . "product as p";
  750. // Multilang : we add translation
  751. if (!empty($conf->global->MAIN_MULTILANGS)) {
  752. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product_lang as pl ON pl.fk_product = p.rowid AND pl.lang='" . $langs->getDefaultLang() . "'";
  753. }
  754. $sql.= ' WHERE p.entity IN (' . getEntity('product', 1) . ')';
  755. if ($finished == 0) {
  756. $sql.= " AND p.finished = " . $finished;
  757. } elseif ($finished == 1) {
  758. $sql.= " AND p.finished = " . $finished;
  759. if ($status >= 0)
  760. $sql.= " AND p.tosell = " . $status;
  761. }
  762. elseif ($status >= 0) {
  763. $sql.= " AND p.tosell = " . $status;
  764. }
  765. if (strval($filtertype) != '')
  766. $sql.=" AND p.fk_product_type=" . $filtertype;
  767. // Add criteria on ref/label
  768. if ($filterkey && $filterkey != '') {
  769. if (!empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)) { // Can use index
  770. $sql.=" AND (p.ref LIKE '" . $filterkey . "%' OR p.label LIKE '" . $filterkey . "%'";
  771. if (!empty($conf->global->MAIN_MULTILANGS))
  772. $sql.=" OR pl.label LIKE '" . $filterkey . "%'";
  773. $sql.=")";
  774. }
  775. else {
  776. $sql.=" AND (p.ref LIKE '%" . $filterkey . "%' OR p.label LIKE '%" . $filterkey . "%'";
  777. if (!empty($conf->global->MAIN_MULTILANGS))
  778. $sql.=" OR pl.label LIKE '%" . $filterkey . "%'";
  779. $sql.=")";
  780. }
  781. if (!empty($conf->barcode->enabled)) {
  782. $sql .= " OR p.barcode LIKE '" . $filterkey . "'";
  783. }
  784. }
  785. $sql.= $db->order("p.ref");
  786. $sql.= $db->plimit($limit);
  787. // Build output string
  788. $outselect = '';
  789. $outjson = array();
  790. $result = $this->db->query($sql);
  791. if ($result) {
  792. $num = $this->db->num_rows($result);
  793. $outselect.='<select class="flat" name="' . $htmlname . '" id="' . $htmlname . '">';
  794. $outselect.='<option value="0" selected="selected">&nbsp;</option>';
  795. $i = 0;
  796. while ($num && $i < $num) {
  797. $outkey = '';
  798. $outval = '';
  799. $outref = '';
  800. $outlabel = '';
  801. $outdesc = '';
  802. $outtype = '';
  803. $outprice_ht = '';
  804. $outprice_ttc = '';
  805. $outpricebasetype = '';
  806. $outtva_tx = '';
  807. $objp = $this->db->fetch_object($result);
  808. $label = $objp->label;
  809. if (!empty($objp->label_translated))
  810. $label = $objp->label_translated;
  811. if ($filterkey && $filterkey != '')
  812. $label = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $label, 1);
  813. $outkey = $objp->rowid;
  814. $outref = $objp->ref;
  815. $outlabel = $objp->label;
  816. $outdesc = $objp->description;
  817. $outtype = $objp->fk_product_type;
  818. $opt = '<option value="' . $objp->rowid . '"';
  819. $opt.= ($objp->rowid == $selected) ? ' selected="selected"' : '';
  820. if (!empty($conf->stock->enabled) && $objp->fk_product_type == 0 && isset($objp->stock)) {
  821. if ($objp->stock > 0) {
  822. $opt.= ' style="background-color:#32CD32; color:#F5F5F5;"';
  823. } else if ($objp->stock <= 0) {
  824. $opt.= ' style="background-color:#FF0000; color:#F5F5F5;"';
  825. }
  826. }
  827. $opt.= '>';
  828. $opt.= $objp->ref . ' - ' . dol_trunc($label, 32) . ' - ';
  829. $objRef = $objp->ref;
  830. if ($filterkey && $filterkey != '')
  831. $objRef = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $objRef, 1);
  832. $outval.=$objRef . ' - ' . dol_trunc($label, 32) . ' - ';
  833. $found = 0;
  834. $currencytext = $langs->trans("Currency" . $conf->currency);
  835. $currencytextnoent = $langs->transnoentities("Currency" . $conf->currency);
  836. if (dol_strlen($currencytext) > 10)
  837. $currencytext = $conf->currency; // If text is too long, we use the short code
  838. if (dol_strlen($currencytextnoent) > 10)
  839. $currencytextnoent = $conf->currency; // If text is too long, we use the short code
  840. // Multiprice
  841. if ($price_level >= 1) { // If we need a particular price level (from 1 to 6)
  842. $sql = "SELECT price, price_ttc, price_base_type, tva_tx";
  843. $sql.= " FROM " . MAIN_DB_PREFIX . "product_price";
  844. $sql.= " WHERE fk_product='" . $objp->rowid . "'";
  845. $sql.= " AND price_level=" . $price_level;
  846. $sql.= " ORDER BY date_price";
  847. $sql.= " DESC LIMIT 1";
  848. dol_syslog(get_class($this) . "::select_produits_do search price for level '.$price_level.' sql=" . $sql);
  849. $result2 = $this->db->query($sql);
  850. if ($result2) {
  851. $objp2 = $this->db->fetch_object($result2);
  852. if ($objp2) {
  853. $found = 1;
  854. if ($objp2->price_base_type == 'HT') {
  855. $opt.= price($objp2->price, 1) . ' ' . $currencytext . ' ' . $langs->trans("HT");
  856. $outval.= price($objp2->price, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("HT");
  857. } else {
  858. $opt.= price($objp2->price_ttc, 1) . ' ' . $currencytext . ' ' . $langs->trans("TTC");
  859. $outval.= price($objp2->price_ttc, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("TTC");
  860. }
  861. $outprice_ht = price($objp2->price);
  862. $outprice_ttc = price($objp2->price_ttc);
  863. $outpricebasetype = $objp2->price_base_type;
  864. $outtva_tx = $objp2->tva_tx;
  865. }
  866. } else {
  867. dol_print_error($this->db);
  868. }
  869. }
  870. // If level no defined or multiprice not found, we used the default price
  871. if (!$found) {
  872. if ($objp->price_base_type == 'HT') {
  873. $opt.= price($objp->price, 1) . ' ' . $currencytext . ' ' . $langs->trans("HT");
  874. $outval.= price($objp->price, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("HT");
  875. } else {
  876. $opt.= price($objp->price_ttc, 1) . ' ' . $currencytext . ' ' . $langs->trans("TTC");
  877. $outval.= price($objp->price_ttc, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("TTC");
  878. }
  879. $outprice_ht = price($objp->price);
  880. $outprice_ttc = price($objp->price_ttc);
  881. $outpricebasetype = $objp->price_base_type;
  882. $outtva_tx = $objp->tva_tx;
  883. }
  884. if (!empty($conf->stock->enabled) && isset($objp->stock) && $objp->fk_product_type == 0) {
  885. $opt.= ' - ' . $langs->trans("Stock") . ':' . $objp->stock;
  886. $outval.=' - ' . $langs->transnoentities("Stock") . ':' . $objp->stock;
  887. }
  888. if ($objp->duration) {
  889. $duration_value = substr($objp->duration, 0, dol_strlen($objp->duration) - 1);
  890. $duration_unit = substr($objp->duration, -1);
  891. if ($duration_value > 1) {
  892. $dur = array("h" => $langs->trans("Hours"), "d" => $langs->trans("Days"), "w" => $langs->trans("Weeks"), "m" => $langs->trans("Months"), "y" => $langs->trans("Years"));
  893. } else {
  894. $dur = array("h" => $langs->trans("Hour"), "d" => $langs->trans("Day"), "w" => $langs->trans("Week"), "m" => $langs->trans("Month"), "y" => $langs->trans("Year"));
  895. }
  896. $opt.= ' - ' . $duration_value . ' ' . $langs->trans($dur[$duration_unit]);
  897. $outval.=' - ' . $duration_value . ' ' . $langs->transnoentities($dur[$duration_unit]);
  898. }
  899. $opt.= "</option>\n";
  900. // Add new entry
  901. // "key" value of json key array is used by jQuery automatically as selected value
  902. // "label" value of json key array is used by jQuery automatically as text for combo box
  903. $outselect.=$opt;
  904. array_push($outjson, array('key' => $outkey, 'value' => $outref, 'label' => $outval, 'label2' => $outlabel, 'desc' => $outdesc, 'type' => $outtype, 'price_ht' => $outprice_ht, 'price_ttc' => $outprice_ttc, 'pricebasetype' => $outpricebasetype, 'tva_tx' => $outtva_tx));
  905. $i++;
  906. }
  907. $outselect.='</select>';
  908. $this->db->free($result);
  909. if (empty($disableout))
  910. print $outselect;
  911. return $outjson;
  912. }
  913. else {
  914. dol_print_error($db);
  915. }
  916. */
  917. }
  918. /**
  919. * Return list of products for a customer
  920. *
  921. * @param int $selected Preselected product
  922. * @param string $htmlname Name of select html
  923. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  924. * @param int $limit Limite sur le nombre de lignes retournees
  925. * @param int $price_level Level of price to show
  926. * @param string $filterkey Filter on product
  927. * @param int $status -1=Return all products, 0=Products not on sell, 1=Products on sell
  928. * @param int $finished Filter on finished field: 2=No filter
  929. * @param int $disableout Disable print output
  930. * @return array Array of keys for json
  931. */
  932. function select_products($selected = '', $htmlname = 'productid', $filtertype = '', $limit = 20, $price_level = 0, $filterkey = '', $status = 1, $finished = 2, $disableout = 0) {
  933. global $langs, $conf, $user, $db;
  934. require_once(DOL_DOCUMENT_ROOT . '/product/class/product.class.php');
  935. $object = new Product($db);
  936. $result = $object->getView('list', array('startkey' => $filterkey, 'endkey' => $filterkey . 'Z'));
  937. // $sql = "SELECT ";
  938. // $sql.= " p.rowid, p.label, p.ref, p.description, p.fk_product_type, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.duration, p.stock";
  939. // // Multilang : we add translation
  940. // if (!empty($conf->global->MAIN_MULTILANGS)) {
  941. // $sql.= ", pl.label as label_translated";
  942. // }
  943. // $sql.= " FROM " . MAIN_DB_PREFIX . "product as p";
  944. // // Multilang : we add translation
  945. // if (!empty($conf->global->MAIN_MULTILANGS)) {
  946. // $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product_lang as pl ON pl.fk_product = p.rowid AND pl.lang='" . $langs->getDefaultLang() . "'";
  947. // }
  948. // $sql.= ' WHERE p.entity IN (' . getEntity('product', 1) . ')';
  949. // if ($finished == 0) {
  950. // $sql.= " AND p.finished = " . $finished;
  951. // } elseif ($finished == 1) {
  952. // $sql.= " AND p.finished = " . $finished;
  953. // if ($status >= 0)
  954. // $sql.= " AND p.tosell = " . $status;
  955. // }
  956. // elseif ($status >= 0) {
  957. // $sql.= " AND p.tosell = " . $status;
  958. // }
  959. // if (strval($filtertype) != '')
  960. // $sql.=" AND p.fk_product_type=" . $filtertype;
  961. // // Add criteria on ref/label
  962. // if ($filterkey && $filterkey != '') {
  963. // if (!empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)) { // Can use index
  964. // $sql.=" AND (p.ref LIKE '" . $filterkey . "%' OR p.label LIKE '" . $filterkey . "%'";
  965. // if (!empty($conf->global->MAIN_MULTILANGS))
  966. // $sql.=" OR pl.label LIKE '" . $filterkey . "%'";
  967. // $sql.=")";
  968. // }
  969. // else {
  970. // $sql.=" AND (p.ref LIKE '%" . $filterkey . "%' OR p.label LIKE '%" . $filterkey . "%'";
  971. // if (!empty($conf->global->MAIN_MULTILANGS))
  972. // $sql.=" OR pl.label LIKE '%" . $filterkey . "%'";
  973. // $sql.=")";
  974. // }
  975. //
  976. // if (!empty($conf->barcode->enabled)) {
  977. // $sql .= " OR p.barcode LIKE '" . $filterkey . "'";
  978. // }
  979. // }
  980. // $sql.= $db->order("p.ref");
  981. // $sql.= $db->plimit($limit);
  982. //
  983. // // Build output string
  984. $outselect = '';
  985. $outjson = array();
  986. //
  987. // dol_syslog(get_class($this) . "::select_produits_do search product sql=" . $sql, LOG_DEBUG);
  988. // $result = $this->db->query($sql);
  989. if (!empty($result->rows)) {
  990. $num = count($result->rows);
  991. $outselect.='<select class="flat" name="' . $htmlname . '" id="' . $htmlname . '">';
  992. $outselect.='<option value="0" selected="selected">&nbsp;</option>';
  993. $i = 0;
  994. while ($num && $i < $num) {
  995. $outkey = '';
  996. $outval = '';
  997. $outref = '';
  998. $outlabel = '';
  999. $outdesc = '';
  1000. $outtype = '';
  1001. $outprice_ht = '';
  1002. $outprice_ttc = '';
  1003. $outpricebasetype = '';
  1004. $outtva_tx = '';
  1005. // $objp = $this->db->fetch_object($result);
  1006. $objp = new Product($db);
  1007. $objp->fetch($result->rows[$i]->value->_id);
  1008. $label = $objp->label;
  1009. if (!empty($objp->label_translated))
  1010. $label = $objp->label_translated;
  1011. if ($filterkey && $filterkey != '')
  1012. $label = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $label, 1);
  1013. $outkey = $objp->id;
  1014. $outref = $objp->name;
  1015. $outlabel = $objp->label;
  1016. $outdesc = isset($objp->description) ? $objp->description : '';
  1017. $outtype = $objp->type;
  1018. $opt = '<option value="' . $objp->id . '"';
  1019. $opt.= ($objp->id == $selected) ? ' selected="selected"' : '';
  1020. if (!empty($conf->stock->enabled) && $objp->type == 'PRODUCT' && isset($objp->stock)) {
  1021. if ($objp->stock > 0) {
  1022. $opt.= ' style="background-color:#32CD32; color:#F5F5F5;"';
  1023. } else if ($objp->stock <= 0) {
  1024. $opt.= ' style="background-color:#FF0000; color:#F5F5F5;"';
  1025. }
  1026. }
  1027. $opt.= '>';
  1028. $opt.= $objp->name . ' - ' . dol_trunc($label, 32) . ' - ';
  1029. $objRef = $objp->name;
  1030. if ($filterkey && $filterkey != '')
  1031. $objRef = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $objRef, 1);
  1032. $outval.=$objRef . ' - ' . dol_trunc($label, 32) . ' - ';
  1033. $found = 0;
  1034. $currencytext = $langs->trans("Currency" . $conf->currency);
  1035. $currencytextnoent = $langs->transnoentities("Currency" . $conf->currency);
  1036. if (dol_strlen($currencytext) > 10)
  1037. $currencytext = $conf->currency; // If text is too long, we use the short code
  1038. if (dol_strlen($currencytextnoent) > 10)
  1039. $currencytextnoent = $conf->currency; // If text is too long, we use the short code
  1040. // Multiprice
  1041. // if ($price_level >= 1) { // If we need a particular price level (from 1 to 6)
  1042. // $sql = "SELECT price, price_ttc, price_base_type, tva_tx";
  1043. // $sql.= " FROM " . MAIN_DB_PREFIX . "product_price";
  1044. // $sql.= " WHERE fk_product='" . $objp->rowid . "'";
  1045. // $sql.= " AND price_level=" . $price_level;
  1046. // $sql.= " ORDER BY date_price";
  1047. // $sql.= " DESC LIMIT 1";
  1048. //
  1049. // dol_syslog(get_class($this) . "::select_produits_do search price for level '.$price_level.' sql=" . $sql);
  1050. // $result2 = $this->db->query($sql);
  1051. // if ($result2) {
  1052. // $objp2 = $this->db->fetch_object($result2);
  1053. // if ($objp2) {
  1054. // $found = 1;
  1055. // if ($objp2->price_base_type == 'HT') {
  1056. // $opt.= price($objp2->price, 1) . ' ' . $currencytext . ' ' . $langs->trans("HT");
  1057. // $outval.= price($objp2->price, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("HT");
  1058. // } else {
  1059. // $opt.= price($objp2->price_ttc, 1) . ' ' . $currencytext . ' ' . $langs->trans("TTC");
  1060. // $outval.= price($objp2->price_ttc, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("TTC");
  1061. // }
  1062. // $outprice_ht = price($objp2->price);
  1063. // $outprice_ttc = price($objp2->price_ttc);
  1064. // $outpricebasetype = $objp2->price_base_type;
  1065. // $outtva_tx = $objp2->tva_tx;
  1066. // }
  1067. // } else {
  1068. // dol_print_error($this->db);
  1069. // }
  1070. // }
  1071. // If level no defined or multiprice not found, we used the default price
  1072. if (!$found) {
  1073. if ($objp->price->price_base_type == 'HT') {
  1074. $opt.= price($objp->price->price, 1) . ' ' . $currencytext . ' ' . $langs->trans("HT");
  1075. $outval.= price($objp->price->price, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("HT");
  1076. } else {
  1077. $opt.= price($objp->price->price_ttc, 1) . ' ' . $currencytext . ' ' . $langs->trans("TTC");
  1078. $outval.= price($objp->price->price_ttc, 1) . ' ' . $currencytextnoent . ' ' . $langs->transnoentities("TTC");
  1079. }
  1080. $outprice_ht = price($objp->price->price);
  1081. $outprice_ttc = price($objp->price->price_ttc);
  1082. $outpricebasetype = $objp->price->price_base_type;
  1083. $outtva_tx = $objp->price->tva_tx;
  1084. }
  1085. if (!empty($conf->stock->enabled) && isset($objp->stock) && $objp->type == "PRODUCT") {
  1086. $opt.= ' - ' . $langs->trans("Stock") . ':' . $objp->stock;
  1087. $outval.=' - ' . $langs->transnoentities("Stock") . ':' . $objp->stock;
  1088. }
  1089. if ($objp->duration) {
  1090. $duration_value = substr($objp->duration, 0, dol_strlen($objp->duration) - 1);
  1091. $duration_unit = substr($objp->duration, -1);
  1092. if ($duration_value > 1) {
  1093. $dur = array("h" => $langs->trans("Hours"), "d" => $langs->trans("Days"), "w" => $langs->trans("Weeks"), "m" => $langs->trans("Months"), "y" => $langs->trans("Years"));
  1094. } else {
  1095. $dur = array("h" => $langs->trans("Hour"), "d" => $langs->trans("Day"), "w" => $langs->trans("Week"), "m" => $langs->trans("Month"), "y" => $langs->trans("Year"));
  1096. }
  1097. $opt.= ' - ' . $duration_value . ' ' . $langs->trans($dur[$duration_unit]);
  1098. $outval.=' - ' . $duration_value . ' ' . $langs->transnoentities($dur[$duration_unit]);
  1099. }
  1100. $opt.= "</option>\n";
  1101. // Add new entry
  1102. // "key" value of json key array is used by jQuery automatically as selected value
  1103. // "label" value of json key array is used by jQuery automatically as text for combo box
  1104. $outselect.=$opt;
  1105. array_push($outjson, array('key' => $outkey, 'value' => $outref, 'label' => $outval, 'label2' => $outlabel, 'desc' => $outdesc, 'type' => $outtype, 'price_ht' => $outprice_ht, 'price_ttc' => $outprice_ttc, 'pricebasetype' => $outpricebasetype, 'tva_tx' => $outtva_tx));
  1106. $i++;
  1107. }
  1108. $outselect.='</select>';
  1109. // $this->db->free($result);
  1110. if (empty($disableout))
  1111. print $outselect;
  1112. return $outjson;
  1113. }
  1114. }
  1115. /**
  1116. * Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_do)
  1117. *
  1118. * @param int $socid Id third party
  1119. * @param string $selected Preselected product
  1120. * @param string $htmlname Name of HTML Select
  1121. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1122. * @param string $filtre For a SQL filter
  1123. * @param array $ajaxoptions Options for ajax_autocompleter
  1124. * @return void
  1125. */
  1126. function select_produits_fournisseurs($socid, $selected = '', $htmlname = 'productid', $filtertype = '', $filtre = '', $ajaxoptions = array()) {
  1127. global $langs, $conf;
  1128. global $price_level, $status, $finished;
  1129. if (!empty($conf->use_javascript_ajax) && !empty($conf->global->PRODUIT_USE_SEARCH_TO_SELECT)) {
  1130. // mode=2 means suppliers products
  1131. $urloption = ($socid > 0 ? 'socid=' . $socid . '&' : '') . 'htmlname=' . $htmlname . '&outjson=1&price_level=' . $price_level . '&type=' . $filtertype . '&mode=2&status=' . $status . '&finished=' . $finished;
  1132. print ajax_autocompleter('', $htmlname, DOL_URL_ROOT . '/product/ajax/products.php', $urloption, $conf->global->PRODUIT_USE_SEARCH_TO_SELECT, 0, $ajaxoptions);
  1133. print $langs->trans("RefOrLabel") . ' : <input type="text" size="16" name="search_' . $htmlname . '" id="search_' . $htmlname . '">';
  1134. print '<br>';
  1135. } else {
  1136. $this->select_produits_fournisseurs_do($socid, $selected, $htmlname, $filtertype, $filtre, '', -1, 0);
  1137. }
  1138. }
  1139. /**
  1140. * Return list of suppliers products
  1141. *
  1142. * @param int $socid Id societe fournisseur (0 pour aucun filtre)
  1143. * @param int $selected Produit pre-selectionne
  1144. * @param string $htmlname Nom de la zone select
  1145. * @param string $filtertype Filter on product type (''=nofilter, 0=product, 1=service)
  1146. * @param string $filtre Pour filtre sql
  1147. * @param string $filterkey Filtre des produits
  1148. * @param int $statut -1=Return all products, 0=Products not on sell, 1=Products on sell
  1149. * @param int $disableout Disable print output
  1150. * @return array Array of keys for json
  1151. */
  1152. function select_produits_fournisseurs_do($socid, $selected = '', $htmlname = 'productid', $filtertype = '', $filtre = '', $filterkey = '', $statut = -1, $disableout = 0) {
  1153. global $langs, $conf;
  1154. $langs->load('stocks');
  1155. $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
  1156. $sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.remise_percent, pfp.remise, pfp.unitprice,";
  1157. $sql.= " s.nom";
  1158. $sql.= " FROM " . MAIN_DB_PREFIX . "product as p";
  1159. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
  1160. if ($socid)
  1161. $sql.= " AND pfp.fk_soc = " . $socid;
  1162. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON pfp.fk_soc = s.rowid";
  1163. $sql.= " WHERE p.entity IN (" . getEntity('product', 1) . ")";
  1164. $sql.= " AND p.tobuy = 1";
  1165. if (strval($filtertype) != '')
  1166. $sql.=" AND p.fk_product_type=" . $filtertype;
  1167. if (!empty($filtre))
  1168. $sql.=" " . $filtre;
  1169. // Add criteria on ref/label
  1170. if ($filterkey && $filterkey != '') {
  1171. if (!empty($conf->global->PRODUCT_DONOTSEARCH_ANYWHERE)) {
  1172. $sql.=" AND (pfp.ref_fourn LIKE '" . $filterkey . "%' OR p.ref LIKE '" . $filterkey . "%' OR p.label LIKE '" . $filterkey . "%')";
  1173. } else {
  1174. $sql.=" AND (pfp.ref_fourn LIKE '%" . $filterkey . "%' OR p.ref LIKE '%" . $filterkey . "%' OR p.label LIKE '%" . $filterkey . "%')";
  1175. }
  1176. if (!empty($conf->barcode->enabled)) {
  1177. $sql .= " OR p.barcode LIKE '" . $filterkey . "'";
  1178. }
  1179. }
  1180. $sql.= " ORDER BY pfp.ref_fourn DESC, pfp.quantity ASC";
  1181. // Build output string
  1182. $outselect = '';
  1183. $outjson = array();
  1184. dol_syslog(get_class($this) . "::select_produits_fournisseurs_do sql=" . $sql, LOG_DEBUG);
  1185. $result = $this->db->query($sql);
  1186. if ($result) {
  1187. $num = $this->db->num_rows($result);
  1188. $outselect.='<select class="flat" id="select' . $htmlname . '" name="' . $htmlname . '">';
  1189. if (!$selected)
  1190. $outselect.='<option value="0" selected="selected">&nbsp;</option>';
  1191. else
  1192. $outselect.='<option value="0">&nbsp;</option>';
  1193. $i = 0;
  1194. while ($i < $num) {
  1195. $objp = $this->db->fetch_object($result);
  1196. $outkey = $objp->idprodfournprice;
  1197. $outref = $objp->ref;
  1198. $outval = '';
  1199. $outqty = 1;
  1200. $outdiscount = 0;
  1201. $opt = '<option value="' . $objp->idprodfournprice . '"';
  1202. if ($selected && $selected == $objp->idprodfournprice)
  1203. $opt.= ' selected="selected"';
  1204. if (empty($objp->idprodfournprice))
  1205. $opt.=' disabled="disabled"';
  1206. $opt.= '>';
  1207. $objRef = $objp->ref;
  1208. if ($filterkey && $filterkey != '')
  1209. $objRef = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $objRef, 1);
  1210. $objRefFourn = $objp->ref_fourn;
  1211. if ($filterkey && $filterkey != '')
  1212. $objRefFourn = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $objRefFourn, 1);
  1213. $label = $objp->label;
  1214. if ($filterkey && $filterkey != '')
  1215. $label = preg_replace('/(' . preg_quote($filterkey) . ')/i', '<strong>$1</strong>', $label, 1);
  1216. $opt.=$objp->ref . ' (' . $objp->ref_fourn . ') - ';
  1217. $outval.=$objRef . ' (' . $objRefFourn . ') - ';
  1218. $opt.=dol_trunc($objp->label, 18) . ' - ';
  1219. $outval.=dol_trunc($label, 18) . ' - ';
  1220. if (!empty($objp->idprodfournprice)) {
  1221. $currencytext = $langs->trans("Currency" . $conf->currency);
  1222. $currencytextnoent = $langs->transnoentities("Currency" . $conf->currency);
  1223. if (dol_strlen($currencytext) > 10)
  1224. $currencytext = $conf->currency; // If text is too long, we use the short code
  1225. if (dol_strlen($currencytextnoent) > 10)
  1226. $currencytextnoent = $conf->currency; // If text is too long, we use the short code
  1227. $opt.= price($objp->fprice) . ' ' . $currencytext . "/" . $objp->quantity;
  1228. $outval.= price($objp->fprice) . ' ' . $currencytextnoent . "/" . $objp->quantity;
  1229. $outqty = $objp->quantity;
  1230. $outdiscount = $objp->remise_percent;
  1231. if ($objp->quantity == 1) {
  1232. $opt.= strtolower($langs->trans("Unit"));
  1233. $outval.=strtolower($langs->transnoentities("Unit"));
  1234. } else {
  1235. $opt.= strtolower($langs->trans("Units"));
  1236. $outval.=strtolower($langs->transnoentities("Units"));
  1237. }
  1238. if ($objp->quantity >= 1) {
  1239. $opt.=" (" . price($objp->unitprice) . ' ' . $currencytext . "/" . strtolower($langs->trans("Unit")) . ")";
  1240. $outval.=" (" . price($objp->unitprice) . ' ' . $currencytextnoent . "/" . strtolower($langs->transnoentities("Unit")) . ")";
  1241. }
  1242. if ($objp->remise_percent >= 1) {
  1243. $opt.=" - " . $langs->trans("Discount") . " : " . vatrate($objp->remise_percent) . ' %';
  1244. $outval.=" - " . $langs->transnoentities("Discount") . " : " . vatrate($objp->remise_percent) . ' %';
  1245. }
  1246. if ($objp->duration) {
  1247. $opt .= " - " . $objp->duration;
  1248. $outval.=" - " . $objp->duration;
  1249. }
  1250. if (!$socid) {
  1251. $opt .= " - " . dol_trunc($objp->nom, 8);
  1252. $outval.=" - " . dol_trunc($objp->nom, 8);
  1253. }
  1254. } else {
  1255. $opt.= $langs->trans("NoPriceDefinedForThisSupplier");
  1256. $outval.=$langs->transnoentities("NoPriceDefinedForThisSupplier");
  1257. }
  1258. $opt .= "</option>\n";
  1259. // Add new entry
  1260. // "key" value of json key array is used by jQuery automatically as selected value
  1261. // "label" value of json key array is used by jQuery automatically as text for combo box
  1262. $outselect.=$opt;
  1263. array_push($outjson, array('key' => $outkey, 'value' => $outref, 'label' => $outval, 'qty' => $outqty, 'discount' => $outdiscount, 'disabled' => (empty($objp->idprodfournprice) ? true : false)));
  1264. $i++;
  1265. }
  1266. $outselect.='</select>';
  1267. $this->db->free($result);
  1268. if (empty($disableout))
  1269. print $outselect;
  1270. return $outjson;
  1271. }
  1272. else {
  1273. dol_print_error($this->db);
  1274. }
  1275. }
  1276. /**
  1277. * Return list of suppliers prices for a product
  1278. *
  1279. * @param int $productid Id of product
  1280. * @param string $htmlname Name of HTML field
  1281. * @return void
  1282. */
  1283. function select_product_fourn_price($productid, $htmlname = 'productfournpriceid') {
  1284. global $langs, $conf;
  1285. $langs->load('stocks');
  1286. $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.duration,";
  1287. $sql.= " pfp.ref_fourn, pfp.rowid as idprodfournprice, pfp.price as fprice, pfp.quantity, pfp.unitprice,";
  1288. $sql.= " s.nom";
  1289. $sql.= " FROM " . MAIN_DB_PREFIX . "product as p";
  1290. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "product_fournisseur_price as pfp ON p.rowid = pfp.fk_product";
  1291. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON pfp.fk_soc = s.rowid";
  1292. $sql.= " WHERE p.entity IN (" . getEntity('product', 1) . ")";
  1293. $sql.= " AND p.tobuy = 1";
  1294. $sql.= " AND s.fournisseur = 1";
  1295. $sql.= " AND p.rowid = " . $productid;
  1296. $sql.= " ORDER BY s.nom, pfp.ref_fourn DESC";
  1297. dol_syslog(get_class($this) . "::select_product_fourn_price sql=" . $sql, LOG_DEBUG);
  1298. $result = $this->db->query($sql);
  1299. if ($result) {
  1300. $num = $this->db->num_rows($result);
  1301. $form = '<select class="flat" name="' . $htmlname . '">';
  1302. if (!$num) {
  1303. $form.= '<option value="0">-- ' . $langs->trans("NoSupplierPriceDefinedForThisProduct") . ' --</option>';
  1304. } else {
  1305. $form.= '<option value="0">&nbsp;</option>';
  1306. $i = 0;
  1307. while ($i < $num) {
  1308. $objp = $this->db->fetch_object($result);
  1309. $opt = '<option value="' . $objp->idprodfournprice . '"';
  1310. $opt.= '>' . $objp->nom . ' - ' . $objp->ref_fourn . ' - ';
  1311. if ($objp->quantity == 1) {
  1312. $opt.= price($objp->fprice);
  1313. $opt.= $langs->trans("Currency" . $conf->currency) . "/";
  1314. }
  1315. $opt.= $objp->quantity . ' ';
  1316. if ($objp->quantity == 1) {
  1317. $opt.= strtolower($langs->trans("Unit"));
  1318. } else {
  1319. $opt.= strtolower($langs->trans("Units"));
  1320. }
  1321. if ($objp->quantity > 1) {
  1322. $opt.=" - ";
  1323. $opt.= price($objp->unitprice) . $langs->trans("Currency" . $conf->currency) . "/" . strtolower($langs->trans("Unit"));
  1324. }
  1325. if ($objp->duration)
  1326. $opt .= " - " . $objp->duration;
  1327. $opt .= "</option>\n";
  1328. $form.= $opt;
  1329. $i++;
  1330. }
  1331. $form.= '</select>';
  1332. $this->db->free($result);
  1333. }
  1334. return $form;
  1335. }
  1336. else {
  1337. dol_print_error($this->db);
  1338. }
  1339. }
  1340. /**
  1341. * Return list of delivery address
  1342. *
  1343. * @param string $selected Id contact pre-selectionn
  1344. * @param int $socid Id of company
  1345. * @param string $htmlname Name of HTML field
  1346. * @param int $showempty Add an empty field
  1347. * @return void
  1348. */
  1349. function select_address($selected, $socid, $htmlname = 'address_id', $showempty = 0) {
  1350. // On recherche les utilisateurs
  1351. $sql = "SELECT a.rowid, a.label";
  1352. $sql .= " FROM " . MAIN_DB_PREFIX . "societe_address as a";
  1353. $sql .= " WHERE a.fk_soc = " . $socid;
  1354. $sql .= " ORDER BY a.label ASC";
  1355. dol_syslog(get_class($this) . "::select_address sql=" . $sql);
  1356. $resql = $this->db->query($sql);
  1357. if ($resql) {
  1358. print '<select class="flat" name="' . $htmlname . '">';
  1359. if ($showempty)
  1360. print '<option value="0">&nbsp;</option>';
  1361. $num = $this->db->num_rows($resql);
  1362. $i = 0;
  1363. if ($num) {
  1364. while ($i < $num) {
  1365. $obj = $this->db->fetch_object($resql);
  1366. if ($selected && $selected == $obj->rowid) {
  1367. print '<option value="' . $obj->rowid . '" selected="selected">' . $obj->label . '</option>';
  1368. } else {
  1369. print '<option value="' . $obj->rowid . '">' . $obj->label . '</option>';
  1370. }
  1371. $i++;
  1372. }
  1373. }
  1374. print '</select>';
  1375. return $num;
  1376. } else {
  1377. dol_print_error($this->db);
  1378. }
  1379. }
  1380. /**
  1381. * Charge dans cache la liste des délais de livraison possibles
  1382. *
  1383. * @return int Nb lignes chargees, 0 si deja chargees, <0 si ko
  1384. */
  1385. function load_cache_availability() {
  1386. global $langs;
  1387. if (count($this->cache_availability))
  1388. return 0; // Cache deja charge
  1389. $sql = "SELECT rowid, code, label";
  1390. $sql.= " FROM " . MAIN_DB_PREFIX . 'c_availability';
  1391. $sql.= " WHERE active=1";
  1392. $sql.= " ORDER BY rowid";
  1393. dol_syslog(get_class($this) . '::load_cache_availability sql=' . $sql, LOG_DEBUG);
  1394. $resql = $this->db->query($sql);
  1395. if ($resql) {
  1396. $num = $this->db->num_rows($resql);
  1397. $i = 0;
  1398. while ($i < $num) {
  1399. $obj = $this->db->fetch_object($resql);
  1400. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  1401. $label = ($langs->trans("AvailabilityType" . $obj->code) != ("AvailabilityType" . $obj->code) ? $langs->trans("AvailabilityType" . $obj->code) : ($obj->label != '-' ? $obj->label : ''));
  1402. $this->cache_availability[$obj->rowid]['code'] = $obj->code;
  1403. $this->cache_availability[$obj->rowid]['label'] = $label;
  1404. $i++;
  1405. }
  1406. return 1;
  1407. } else {
  1408. dol_print_error($this->db);
  1409. return -1;
  1410. }
  1411. }
  1412. /**
  1413. * Retourne la liste des types de delais de livraison possibles
  1414. *
  1415. * @param int $selected Id du type de delais pre-selectionne
  1416. * @param string $htmlname Nom de la zone select
  1417. * @param string $filtertype To add a filter
  1418. * @param int $addempty Add empty entry
  1419. * @return void
  1420. */
  1421. function select_availability($selected = '', $htmlname = 'availid', $filtertype = '', $addempty = 0) {
  1422. global $langs, $user;
  1423. $this->load_cache_availability();
  1424. print '<select class="flat" name="' . $htmlname . '">';
  1425. if ($addempty)
  1426. print '<option value="0">&nbsp;</option>';
  1427. foreach ($this->cache_availability as $id => $arrayavailability) {
  1428. if ($selected == $id) {
  1429. print '<option value="' . $id . '" selected="selected">';
  1430. } else {
  1431. print '<option value="' . $id . '">';
  1432. }
  1433. print $arrayavailability['label'];
  1434. print '</option>';
  1435. }
  1436. print '</select>';
  1437. if ($user->admin)
  1438. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"), 1);
  1439. }
  1440. /**
  1441. * Load into cache cache_demand_reason, array of input reasons
  1442. *
  1443. * @return int Nb of lines loaded, 0 if already loaded, <0 if ko
  1444. */
  1445. function load_cache_demand_reason() {
  1446. global $langs;
  1447. if (count($this->cache_demand_reason))
  1448. return 0; // Cache already loaded
  1449. $sql = "SELECT rowid, code, label";
  1450. $sql.= " FROM " . MAIN_DB_PREFIX . 'c_input_reason';
  1451. $sql.= " WHERE active=1";
  1452. $sql.= " ORDER BY rowid";
  1453. dol_syslog(get_class($this) . "::load_cache_demand_reason sql=" . $sql, LOG_DEBUG);
  1454. $resql = $this->db->query($sql);
  1455. if ($resql) {
  1456. $num = $this->db->num_rows($resql);
  1457. $i = 0;
  1458. $tmparray = array();
  1459. while ($i < $num) {
  1460. $obj = $this->db->fetch_object($resql);
  1461. // Si traduction existe, on l'utilise, sinon on prend le libelle par defaut
  1462. $label = ($langs->trans("DemandReasonType" . $obj->code) != ("DemandReasonType" . $obj->code) ? $langs->trans("DemandReasonType" . $obj->code) : ($obj->label != '-' ? $obj->label : ''));
  1463. $tmparray[$obj->rowid]['id'] = $obj->rowid;
  1464. $tmparray[$obj->rowid]['code'] = $obj->code;
  1465. $tmparray[$obj->rowid]['label'] = $label;
  1466. $i++;
  1467. }
  1468. $this->cache_demand_reason = dol_sort_array($tmparray, 'label', 'asc');
  1469. unset($tmparray);
  1470. return 1;
  1471. } else {
  1472. dol_print_error($this->db);
  1473. return -1;
  1474. }
  1475. }
  1476. /**
  1477. * Return list of events that triggered an object creation
  1478. *
  1479. * @param int $selected Id du type d'origine pre-selectionne
  1480. * @param string $htmlname Nom de la zone select
  1481. * @param string $exclude To exclude a code value (Example: SRC_PROP)
  1482. * @param int $addempty Add an empty entry
  1483. * @return void
  1484. */
  1485. function select_demand_reason($selected = '', $htmlname = 'demandreasonid', $exclude = '', $addempty = 0) {
  1486. global $langs, $user;
  1487. $this->load_cache_demand_reason();
  1488. print '<select class="flat" name="' . $htmlname . '">';
  1489. if ($addempty)
  1490. print '<option value="0"' . (empty($selected) ? ' selected="selected"' : '') . '>&nbsp;</option>';
  1491. foreach ($this->cache_demand_reason as $id => $arraydemandreason) {
  1492. if ($arraydemandreason['code'] == $exclude)
  1493. continue;
  1494. if ($selected == $arraydemandreason['id']) {
  1495. print '<option value="' . $arraydemandreason['id'] . '" selected="selected">';
  1496. } else {
  1497. print '<option value="' . $arraydemandreason['id'] . '">';
  1498. }
  1499. print $arraydemandreason['label'];
  1500. print '</option>';
  1501. }
  1502. print '</select>';
  1503. if ($user->admin)
  1504. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"), 1);
  1505. }
  1506. /**
  1507. * Selection HT or TTC
  1508. *
  1509. * @param string $selected Id pre-selectionne
  1510. * @param string $htmlname Nom de la zone select
  1511. * @return void
  1512. */
  1513. function select_PriceBaseType($selected = '', $htmlname = 'price_base_type') {
  1514. print $this->load_PriceBaseType($selected, $htmlname);
  1515. }
  1516. /**
  1517. * Selection HT or TTC
  1518. *
  1519. * @param string $selected Id pre-selectionne
  1520. * @param string $htmlname Nom de la zone select
  1521. * @return void
  1522. */
  1523. function load_PriceBaseType($selected = '', $htmlname = 'price_base_type') {
  1524. global $langs;
  1525. $return = '';
  1526. $return.= '<select class="flat" name="' . $htmlname . '">';
  1527. $options = array(
  1528. 'HT' => $langs->trans("HT"),
  1529. 'TTC' => $langs->trans("TTC")
  1530. );
  1531. foreach ($options as $id => $value) {
  1532. if ($selected == $id) {
  1533. $return.= '<option value="' . $id . '" selected="selected">' . $value;
  1534. } else {
  1535. $return.= '<option value="' . $id . '">' . $value;
  1536. }
  1537. $return.= '</option>';
  1538. }
  1539. $return.= '</select>';
  1540. return $return;
  1541. }
  1542. /**
  1543. * Return a HTML select list of bank accounts
  1544. *
  1545. * @param string $selected Id account pre-selected
  1546. * @param string $htmlname Name of select zone
  1547. * @param int $statut Status of searched accounts (0=open, 1=closed)
  1548. * @param string $filtre To filter list
  1549. * @param int $useempty 1=Add an empty value in list, 2=Add an empty value in list only if there is more than 2 entries.
  1550. * @param string $moreattrib To add more attribute on select
  1551. * @return void
  1552. */
  1553. function select_comptes($selected = '', $htmlname = 'accountid', $statut = 0, $filtre = '', $useempty = 0, $moreattrib = '') {
  1554. global $langs, $conf;
  1555. $langs->load("admin");
  1556. $sql = "SELECT rowid, label, bank";
  1557. $sql.= " FROM " . MAIN_DB_PREFIX . "bank_account";
  1558. $sql.= " WHERE clos = '" . $statut . "'";
  1559. $sql.= " AND entity = " . $conf->entity;
  1560. if ($filtre)
  1561. $sql.=" AND " . $filtre;
  1562. $sql.= " ORDER BY label";
  1563. dol_syslog(get_class($this) . "::select_comptes sql=" . $sql);
  1564. $result = $this->db->query($sql);
  1565. if ($result) {
  1566. $num = $this->db->num_rows($result);
  1567. $i = 0;
  1568. if ($num) {
  1569. print '<select id="select' . $htmlname . '" class="flat selectbankaccount" name="' . $htmlname . '"' . ($moreattrib ? ' ' . $moreattrib : '') . '>';
  1570. if ($useempty == 1 || ($useempty == 2 && $num > 1)) {
  1571. print '<option value="-1">&nbsp;</option>';
  1572. }
  1573. while ($i < $num) {
  1574. $obj = $this->db->fetch_object($result);
  1575. if ($selected == $obj->rowid) {
  1576. print '<option value="' . $obj->rowid . '" selected="selected">';
  1577. } else {
  1578. print '<option value="' . $obj->rowid . '">';
  1579. }
  1580. print $obj->label;
  1581. print '</option>';
  1582. $i++;
  1583. }
  1584. print "</select>";
  1585. } else {
  1586. print $langs->trans("NoActiveBankAccountDefined");
  1587. }
  1588. } else {
  1589. dol_print_error($this->db);
  1590. }
  1591. }
  1592. /**
  1593. * Return list of categories having choosed type
  1594. *
  1595. * @param int $type Type de categories (0=product, 1=supplier, 2=customer, 3=member)
  1596. * @param string $selected Id of category preselected
  1597. * @param string $htmlname HTML field name
  1598. * @param int $maxlength Maximum length for labels
  1599. * @param int $excludeafterid Exclude all categories after this leaf in category tree.
  1600. * @return void
  1601. */
  1602. function select_all_categories($type, $selected = '', $htmlname = "parent", $maxlength = 64, $excludeafterid = 0) {
  1603. global $langs;
  1604. $langs->load("categories");
  1605. $cat = new Categorie($this->db);
  1606. $cate_arbo = $cat->get_full_arbo($type, $excludeafterid);
  1607. $output = '<select class="flat" name="' . $htmlname . '">';
  1608. if (is_array($cate_arbo)) {
  1609. if (!count($cate_arbo))
  1610. $output.= '<option value="-1" disabled="disabled">' . $langs->trans("NoCategoriesDefined") . '</option>';
  1611. else {
  1612. $output.= '<option value="-1">&nbsp;</option>';
  1613. foreach ($cate_arbo as $key => $value) {
  1614. if ($cate_arbo[$key]['id'] == $selected) {
  1615. $add = 'selected="selected" ';
  1616. } else {
  1617. $add = '';
  1618. }
  1619. $output.= '<option ' . $add . 'value="' . $cate_arbo[$key]['id'] . '">' . dol_trunc($cate_arbo[$key]['fulllabel'], $maxlength, 'middle') . '</option>';
  1620. }
  1621. }
  1622. }
  1623. $output.= '</select>';
  1624. $output.= "\n";
  1625. return $output;
  1626. }
  1627. /**
  1628. * Show a confirmation HTML form or AJAX popup
  1629. *
  1630. * @param string $page Url of page to call if confirmation is OK
  1631. * @param string $title Title
  1632. * @param string $question Question
  1633. * @param string $action Action
  1634. * @param array $formquestion An array with forms complementary inputs
  1635. * @param string $selectedchoice "" or "no" or "yes"
  1636. * @param int $useajax 0=No, 1=Yes, 2=Yes but submit page with &confirm=no if choice is No
  1637. * @param int $height Force height of box
  1638. * @param int $width Force width of box
  1639. * @return void
  1640. */
  1641. function form_confirm($page, $title, $question, $action, $formquestion = '', $selectedchoice = "", $useajax = 0, $height = 170, $width = 500) {
  1642. print $this->formconfirm($page, $title, $question, $action, $formquestion, $selectedchoice, $useajax, $height, $width);
  1643. }
  1644. /**
  1645. * Show a confirmation HTML form or AJAX popup
  1646. *
  1647. * @param string $page Url of page to call if confirmation is OK
  1648. * @param string $title Title
  1649. * @param string $question Question
  1650. * @param string $action Action
  1651. * @param array $formquestion An array with complementary inputs to add into forms: array(array('label'=> ,'type'=> , ))
  1652. * @param string $selectedchoice "" or "no" or "yes"
  1653. * @param int $useajax 0=No, 1=Yes, 2=Yes but submit page with &confirm=no if choice is No, 'xxx'=preoutput confirm box with div id=dialog-confirm-xxx
  1654. * @param int $height Force height of box
  1655. * @param int $width Force width of bow
  1656. * @return string HTML ajax code if a confirm ajax popup is required, Pure HTML code if it's an html form
  1657. */
  1658. function formconfirm($page, $title, $question, $action, $formquestion = '', $selectedchoice = "", $useajax = 0, $height = 170, $width = 500) {
  1659. global $langs, $conf;
  1660. global $useglobalvars;
  1661. $more = '';
  1662. $formconfirm = '';
  1663. $inputok = array();
  1664. $inputko = array();
  1665. if (is_array($formquestion) && !empty($formquestion)) {
  1666. $more.='<table class="paddingrightonly" width="100%">' . "\n";
  1667. $more.='<tr><td colspan="3" valign="top">' . (!empty($formquestion['text']) ? $formquestion['text'] : '') . '</td></tr>' . "\n";
  1668. foreach ($formquestion as $key => $input) {
  1669. if (is_array($input) && !empty($input)) {
  1670. $size = (!empty($input['size']) ? ' size="' . $input['size'] . '"' : '');
  1671. if ($input['type'] == 'text') {
  1672. $more.='<tr><td valign="top">' . $input['label'] . '</td><td valign="top" colspan="2" align="left"><input type="text" class="flat" id="' . $input['name'] . '" name="' . $input['name'] . '"' . $size . ' value="' . $input['value'] . '" /></td></tr>' . "\n";
  1673. } else if ($input['type'] == 'password') {
  1674. $more.='<tr><td valign="top">' . $input['label'] . '</td><td valign="top" colspan="2" align="left"><input type="password" class="flat" id="' . $input['name'] . '" name="' . $input['name'] . '"' . $size . ' value="' . $input['value'] . '" /></td></tr>' . "\n";
  1675. } else if ($input['type'] == 'select') {
  1676. $more.='<tr><td valign="top" style="padding: 4px !important;">';
  1677. if (!empty($input['label']))
  1678. $more.=$input['label'] . '</td><td valign="top" colspan="2" align="left" style="padding: 4px !important;">';
  1679. $more.=$this->selectarray($input['name'], $input['values'], $input['default'], 1);
  1680. $more.='</td></tr>' . "\n";
  1681. }
  1682. else if ($input['type'] == 'checkbox') {
  1683. $more.='<tr>';
  1684. $more.='<td valign="top">' . $input['label'] . ' </td><td valign="top" align="left">';
  1685. $more.='<input type="checkbox" class="flat" id="' . $input['name'] . '" name="' . $input['name'] . '"';
  1686. if (!is_bool($input['value']) && $input['value'] != 'false')
  1687. $more.=' checked="checked"';
  1688. if (is_bool($input['value']) && $input['value'])
  1689. $more.=' checked="checked"';
  1690. if (isset($input['disabled']))
  1691. $more.=' disabled="disabled"';
  1692. $more.=' /></td>';
  1693. $more.='<td valign="top" align="left">&nbsp;</td>';
  1694. $more.='</tr>' . "\n";
  1695. }
  1696. else if ($input['type'] == 'radio') {
  1697. $i = 0;
  1698. foreach ($input['values'] as $selkey => $selval) {
  1699. $more.='<tr>';
  1700. if ($i == 0)
  1701. $more.='<td valign="top">' . $input['label'] . '</td>';
  1702. else
  1703. $more.='<td>&nbsp;</td>';
  1704. $more.='<td valign="top" width="20"><input type="radio" class="flat" id="' . $input['name'] . '" name="' . $input['name'] . '" value="' . $selkey . '"';
  1705. if ($input['disabled'])
  1706. $more.=' disabled="disabled"';
  1707. $more.=' /></td>';
  1708. $more.='<td valign="top" align="left">';
  1709. $more.=$selval;
  1710. $more.='</td></tr>' . "\n";
  1711. $i++;
  1712. }
  1713. }
  1714. else if ($input['type'] == 'other') {
  1715. $more.='<tr><td valign="top">';
  1716. if (!empty($input['label']))
  1717. $more.=$input['label'] . '</td><td valign="top" colspan="2" align="left">';
  1718. $more.=$input['value'];
  1719. $more.='</td></tr>' . "\n";
  1720. }
  1721. else if ($input['type'] == 'hidden') {
  1722. $more.='<input type="hidden" id="' . $input['name'] . '" name="' . $input['name'] . '" value="' . $input['value'] . '">';
  1723. }
  1724. }
  1725. }
  1726. $more.='</table>' . "\n";
  1727. }
  1728. if ($useajax && $conf->use_javascript_ajax) {
  1729. $autoOpen = true;
  1730. $dialogconfirm = 'dialog-confirm';
  1731. $button = '';
  1732. if (!is_int($useajax)) {
  1733. $button = $useajax;
  1734. $useajax = 1;
  1735. $autoOpen = false;
  1736. $dialogconfirm.='-' . $button;
  1737. }
  1738. $pageyes = $page . '&action=' . $action . '&confirm=yes';
  1739. $pageno = ($useajax == 2 ? $page . '&confirm=no' : '');
  1740. // Add input fields into list of fields to read during submit (inputok and inputko)
  1741. if (is_array($formquestion)) {
  1742. foreach ($formquestion as $key => $input) {
  1743. if (isset($input['name']))
  1744. array_push($inputok, $input['name']);
  1745. if (isset($input['inputko']) && $input['inputko'] == 1)
  1746. array_push($inputko, $input['name']);
  1747. }
  1748. }
  1749. // Show JQuery confirm box. Note that global var $useglobalvars is used inside this template
  1750. $formconfirm.= '<div id="' . $dialogconfirm . '" title="' . dol_escape_htmltag($title) . '" style="display: none;">';
  1751. if (!empty($more)) {
  1752. $formconfirm.= '<p>' . $more . '</p>';
  1753. }
  1754. $formconfirm.= img_help('', '') . ' ' . $question;
  1755. $formconfirm.= '</div>';
  1756. $formconfirm.= '<script type="text/javascript">';
  1757. $formconfirm.='
  1758. $(function() {
  1759. $( "#' . $dialogconfirm . '" ).dialog({
  1760. autoOpen: ' . ($autoOpen ? "true" : "false") . ',
  1761. resizable: false,
  1762. height: "' . $height . '",
  1763. width: "' . $width . '",
  1764. modal: true,
  1765. closeOnEscape: false,
  1766. buttons: {
  1767. "' . dol_escape_js($langs->transnoentities("Yes")) . '": function() {
  1768. var options="";
  1769. var inputok = ' . json_encode($inputok) . ';
  1770. var pageyes = "' . dol_escape_js(!empty($pageyes) ? $pageyes : '') . '";
  1771. if (inputok.length>0) {
  1772. $.each(inputok, function(i, inputname) {
  1773. var more = "";
  1774. if ($("#" + inputname).attr("type") == "checkbox") { more = ":checked"; }
  1775. var inputvalue = $("#" + inputname + more).val();
  1776. if (typeof inputvalue == "undefined") { inputvalue=""; }
  1777. options += "&" + inputname + "=" + inputvalue;
  1778. });
  1779. }
  1780. var urljump = pageyes + (pageyes.indexOf("?") < 0 ? "?" : "") + options;
  1781. //alert(urljump);
  1782. if (pageyes.length > 0) { location.href = urljump; }
  1783. $(this).dialog("close");
  1784. },
  1785. "' . dol_escape_js($langs->transnoentities("No")) . '": function() {
  1786. var options = "";
  1787. var inputko = ' . json_encode($inputko) . ';
  1788. var pageno="' . dol_escape_js(!empty($pageno) ? $pageno : '') . '";
  1789. if (inputko.length>0) {
  1790. $.each(inputko, function(i, inputname) {
  1791. var more = "";
  1792. if ($("#" + inputname).attr("type") == "checkbox") { more = ":checked"; }
  1793. var inputvalue = $("#" + inputname + more).val();
  1794. if (typeof inputvalue == "undefined") { inputvalue=""; }
  1795. options += "&" + inputname + "=" + inputvalue;
  1796. });
  1797. }
  1798. var urljump=pageno + (pageno.indexOf("?") < 0 ? "?" : "") + options;
  1799. //alert(urljump);
  1800. if (pageno.length > 0) { location.href = urljump; }
  1801. $(this).dialog("close");
  1802. }
  1803. }
  1804. });
  1805. var button = "' . $button . '";
  1806. if (button.length > 0) {
  1807. $( "#" + button ).click(function() {
  1808. $("#' . $dialogconfirm . '").dialog("open");
  1809. });
  1810. }
  1811. });
  1812. </script>';
  1813. } else {
  1814. $formconfirm.= "\n<!-- begin form_confirm page=" . $page . " -->\n";
  1815. $formconfirm.= '<form method="POST" action="' . $page . '" class="notoptoleftroright">' . "\n";
  1816. $formconfirm.= '<input type="hidden" name="action" value="' . $action . '">';
  1817. $formconfirm.= '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">' . "\n";
  1818. $formconfirm.= '<table width="100%" class="valid">' . "\n";
  1819. // Line title
  1820. $formconfirm.= '<tr class="validtitre"><td class="validtitre" colspan="3">' . img_picto('', 'recent') . ' ' . $title . '</td></tr>' . "\n";
  1821. // Line form fields
  1822. if ($more) {
  1823. $formconfirm.='<tr class="valid"><td class="valid" colspan="3">' . "\n";
  1824. $formconfirm.=$more;
  1825. $formconfirm.='</td></tr>' . "\n";
  1826. }
  1827. // Line with question
  1828. $formconfirm.= '<tr class="valid">';
  1829. $formconfirm.= '<td class="valid">' . $question . '</td>';
  1830. $formconfirm.= '<td class="valid">';
  1831. $newselectedchoice = empty($selectedchoice) ? "no" : $selectedchoice;
  1832. $formconfirm.= $this->selectyesno("confirm", $newselectedchoice);
  1833. $formconfirm.= '</td>';
  1834. $formconfirm.= '<td class="valid" align="center"><input class="button" type="submit" value="' . $langs->trans("Validate") . '"></td>';
  1835. $formconfirm.= '</tr>' . "\n";
  1836. $formconfirm.= '</table>' . "\n";
  1837. $formconfirm.= "</form>\n";
  1838. $formconfirm.= '<br>';
  1839. $formconfirm.= "<!-- end form_confirm -->\n";
  1840. }
  1841. return $formconfirm;
  1842. }
  1843. /**
  1844. * Show a form to select a project
  1845. *
  1846. * @param int $page Page
  1847. * @param int $socid Id third party
  1848. * @param int $selected Id pre-selected project
  1849. * @param string $htmlname Name of select field
  1850. * @return void
  1851. */
  1852. function form_project($page, $socid, $selected = '', $htmlname = 'projectid') {
  1853. global $langs;
  1854. require_once DOL_DOCUMENT_ROOT . '/core/lib/project.lib.php';
  1855. $langs->load("project");
  1856. if ($htmlname != "none") {
  1857. print '<form method="post" action="' . $page . '">';
  1858. print '<input type="hidden" name="action" value="classin">';
  1859. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  1860. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1861. print '<tr><td>';
  1862. //print "$socid,$selected,$htmlname";
  1863. select_projects($socid, $selected, $htmlname);
  1864. print '</td>';
  1865. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  1866. print '</tr></table></form>';
  1867. } else {
  1868. if ($selected) {
  1869. $projet = new Project($this->db);
  1870. $projet->fetch($selected);
  1871. //print '<a href="'.DOL_URL_ROOT.'/projet/fiche.php?id='.$selected.'">'.$projet->title.'</a>';
  1872. print $projet->getNomUrl(0, '', 1);
  1873. } else {
  1874. print "&nbsp;";
  1875. }
  1876. }
  1877. }
  1878. /**
  1879. * Show a form to select a delivery delay
  1880. *
  1881. * @param int $page Page
  1882. * @param string $selected Id condition pre-selectionne
  1883. * @param string $htmlname Name of select html field
  1884. * @param int $addempty Ajoute entree vide
  1885. * @return void
  1886. */
  1887. function form_availability($page, $selected = '', $htmlname = 'availability', $addempty = 0) {
  1888. global $langs;
  1889. if ($htmlname != "none") {
  1890. print '<form method="post" action="' . $page . '">';
  1891. print '<input type="hidden" name="action" value="setavailability">';
  1892. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  1893. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1894. print '<tr><td>';
  1895. $this->select_availability($selected, $htmlname, -1, $addempty);
  1896. print '</td>';
  1897. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  1898. print '</tr></table></form>';
  1899. } else {
  1900. if ($selected) {
  1901. $this->load_cache_availability();
  1902. print $this->cache_availability[$selected]['label'];
  1903. } else {
  1904. print "&nbsp;";
  1905. }
  1906. }
  1907. }
  1908. /**
  1909. * Show a select form to select origin
  1910. *
  1911. * @param string $page Page
  1912. * @param string $selected Id condition pre-selectionne
  1913. * @param string $htmlname Name of select html field
  1914. * @param int $addempty Add empty entry
  1915. * @return void
  1916. */
  1917. function form_demand_reason($page, $selected = '', $htmlname = 'demandreason', $addempty = 0) {
  1918. global $langs;
  1919. if ($htmlname != "none") {
  1920. print '<form method="post" action="' . $page . '">';
  1921. print '<input type="hidden" name="action" value="setdemandreason">';
  1922. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  1923. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1924. print '<tr><td>';
  1925. $this->select_demand_reason($selected, $htmlname, -1, $addempty);
  1926. print '</td>';
  1927. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  1928. print '</tr></table></form>';
  1929. } else {
  1930. if ($selected) {
  1931. $this->load_cache_demand_reason();
  1932. foreach ($this->cache_demand_reason as $key => $val) {
  1933. if ($val['id'] == $selected) {
  1934. print $val['label'];
  1935. break;
  1936. }
  1937. }
  1938. } else {
  1939. print "&nbsp;";
  1940. }
  1941. }
  1942. }
  1943. /**
  1944. * Show a form to select a date
  1945. *
  1946. * @param string $page Page
  1947. * @param string $selected Date preselected
  1948. * @param string $htmlname Name of input html field
  1949. * @return void
  1950. */
  1951. function form_date($page, $selected, $htmlname) {
  1952. global $langs;
  1953. if ($htmlname != "none") {
  1954. print '<form method="post" action="' . $page . '" name="form' . $htmlname . '">';
  1955. print '<input type="hidden" name="action" value="set' . $htmlname . '">';
  1956. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  1957. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1958. print '<tr><td>';
  1959. print $this->select_date($selected, $htmlname, 0, 0, 1, 'form' . $htmlname);
  1960. print '</td>';
  1961. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  1962. print '</tr></table></form>';
  1963. } else {
  1964. if ($selected) {
  1965. $this->load_cache_types_paiements();
  1966. print $this->cache_types_paiements[$selected]['label'];
  1967. } else {
  1968. print "&nbsp;";
  1969. }
  1970. }
  1971. }
  1972. /**
  1973. * Show a select form to choose a user
  1974. *
  1975. * @param string $page Page
  1976. * @param string $selected Id of user preselected
  1977. * @param string $htmlname Name of input html field
  1978. * @param array $exclude List of users id to exclude
  1979. * @param array $include List of users id to include
  1980. * @return void
  1981. */
  1982. function form_users($page, $selected = '', $htmlname = 'userid', $exclude = '', $include = '') {
  1983. global $langs;
  1984. if ($htmlname != "none") {
  1985. print '<form method="POST" action="' . $page . '" name="form' . $htmlname . '">';
  1986. print '<input type="hidden" name="action" value="set' . $htmlname . '">';
  1987. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  1988. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  1989. print '<tr><td>';
  1990. print $this->select_users($selected, $htmlname, 1, $exclude, 0, $include); // TODO where is this function
  1991. print '</td>';
  1992. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  1993. print '</tr></table></form>';
  1994. } else {
  1995. if ($selected) {
  1996. require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
  1997. //$this->load_cache_contacts();
  1998. //print $this->cache_contacts[$selected];
  1999. $theuser = new User($this->db);
  2000. $theuser->fetch($selected);
  2001. print $theuser->getNomUrl(1);
  2002. } else {
  2003. print "&nbsp;";
  2004. }
  2005. }
  2006. }
  2007. /**
  2008. * Show a select box with available absolute discounts
  2009. *
  2010. * @param string $page Page URL where form is shown
  2011. * @param int $selected Value pre-selected
  2012. * @param string $htmlname Nom du formulaire select. Si 'none', non modifiable. Example 'remise_id'.
  2013. * @param int $socid Third party id
  2014. * @param float $amount Total amount available
  2015. * @param string $filter SQL filter on discounts
  2016. * @param int $maxvalue Max value for lines that can be selected
  2017. * @param string $more More string to add
  2018. * @return void
  2019. */
  2020. function form_remise_dispo($page, $selected, $htmlname, $socid, $amount, $filter = '', $maxvalue = 0, $more = '') {
  2021. global $conf, $langs;
  2022. if ($htmlname != "none") {
  2023. print '<form method="post" action="' . $page . '">';
  2024. print '<input type="hidden" name="action" value="setabsolutediscount">';
  2025. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  2026. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2027. print '<tr><td nowrap="nowrap">';
  2028. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  2029. if (!$filter || $filter == "fk_facture_source IS NULL")
  2030. print $langs->trans("CompanyHasAbsoluteDiscount", price($amount), $langs->transnoentities("Currency" . $conf->currency)) . ': '; // If we want deposit to be substracted to payments only and not to total of final invoice
  2031. else
  2032. print $langs->trans("CompanyHasCreditNote", price($amount), $langs->transnoentities("Currency" . $conf->currency)) . ': ';
  2033. }
  2034. else {
  2035. if (!$filter || $filter == "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND description='(DEPOSIT)')")
  2036. print $langs->trans("CompanyHasAbsoluteDiscount", price($amount), $langs->transnoentities("Currency" . $conf->currency)) . ': ';
  2037. else
  2038. print $langs->trans("CompanyHasCreditNote", price($amount), $langs->transnoentities("Currency" . $conf->currency)) . ': ';
  2039. }
  2040. $newfilter = 'fk_facture IS NULL AND fk_facture_line IS NULL'; // Remises disponibles
  2041. if ($filter)
  2042. $newfilter.=' AND (' . $filter . ')';
  2043. $nbqualifiedlines = $this->select_remises($selected, $htmlname, $newfilter, $socid, $maxvalue);
  2044. print '</td>';
  2045. print '<td nowrap="nowrap">';
  2046. if ($nbqualifiedlines > 0) {
  2047. print ' &nbsp; <input type="submit" class="button" value="' . dol_escape_htmltag($langs->trans("UseLine")) . '"';
  2048. if ($filter && $filter != "fk_facture_source IS NULL OR (fk_facture_source IS NOT NULL AND description='(DEPOSIT)')")
  2049. print ' title="' . $langs->trans("UseCreditNoteInInvoicePayment") . '"';
  2050. print '>';
  2051. }
  2052. if ($more)
  2053. print $more;
  2054. print '</td>';
  2055. print '</tr></table></form>';
  2056. }
  2057. else {
  2058. if ($selected) {
  2059. print $selected;
  2060. } else {
  2061. print "0";
  2062. }
  2063. }
  2064. }
  2065. /**
  2066. * Affiche formulaire de selection des contacts
  2067. *
  2068. * @param string $page Page
  2069. * @param Societe $societe Third party
  2070. * @param int $selected Id contact pre-selectionne
  2071. * @param string $htmlname Nom du formulaire select
  2072. * @return void
  2073. */
  2074. function form_contacts($page, $societe, $selected = '', $htmlname = 'contactidp') {
  2075. global $langs;
  2076. if ($htmlname != "none") {
  2077. print '<form method="post" action="' . $page . '">';
  2078. print '<input type="hidden" name="action" value="set_contact">';
  2079. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  2080. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2081. print '<tr><td>';
  2082. $num = $this->select_contacts($societe->id, $selected, $htmlname);
  2083. if ($num == 0) {
  2084. $addcontact = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("AddContact") : $langs->trans("AddContactAddress"));
  2085. print '<font class="error">Cette societe n\'a pas de contact, veuillez en cr�er un avant de faire votre proposition commerciale</font><br>';
  2086. print '<a href="' . DOL_URL_ROOT . '/contact/fiche.php?socid=' . $societe->id . '&amp;action=create&amp;backtoreferer=1">' . $addcontact . '</a>';
  2087. }
  2088. print '</td>';
  2089. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  2090. print '</tr></table></form>';
  2091. } else {
  2092. if ($selected) {
  2093. require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  2094. //$this->load_cache_contacts();
  2095. //print $this->cache_contacts[$selected];
  2096. $contact = new Contact($this->db);
  2097. $contact->fetch($selected);
  2098. print $contact->getFullName($langs);
  2099. } else {
  2100. print "&nbsp;";
  2101. }
  2102. }
  2103. }
  2104. /**
  2105. * Output html select to select thirdparty
  2106. *
  2107. * @param string $page Page
  2108. * @param string $selected Id preselected
  2109. * @param string $htmlname Name of HTML select
  2110. * @param string $filter Optionnal filters criteras
  2111. * @param int $showempty Add an empty field
  2112. * @param int $showtype Show third party type in combolist (customer, prospect or supplier)
  2113. * @param int $forcecombo Force to use combo box
  2114. * @param array $event Event options
  2115. * @return void
  2116. */
  2117. function form_thirdparty($page, $selected = '', $htmlname = 'socid', $filter = '', $showempty = 0, $showtype = 0, $forcecombo = 0, $event = array()) {
  2118. global $langs;
  2119. if ($htmlname != "none") {
  2120. print '<form method="post" action="' . $page . '">';
  2121. print '<input type="hidden" name="action" value="set_thirdparty">';
  2122. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  2123. print '<table class="nobordernopadding" cellpadding="0" cellspacing="0">';
  2124. print '<tr><td>';
  2125. print $this->select_company($selected, $htmlname, $filter, $showempty, $showtype, $forcecombo, $event);
  2126. print '</td>';
  2127. print '<td align="left"><input type="submit" class="button" value="' . $langs->trans("Modify") . '"></td>';
  2128. print '</tr></table></form>';
  2129. } else {
  2130. if ($selected) {
  2131. require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  2132. $soc = new Societe($this->db);
  2133. $soc->fetch($selected);
  2134. print $soc->getNomUrl($langs);
  2135. } else {
  2136. print "&nbsp;";
  2137. }
  2138. }
  2139. }
  2140. /**
  2141. * Retourne la liste des devises, dans la langue de l'utilisateur
  2142. *
  2143. * @param string $selected preselected currency code
  2144. * @param string $htmlname name of HTML select list
  2145. * @return void
  2146. */
  2147. function select_currency($selected = '', $htmlname = 'currency_id') {
  2148. print $this->selectcurrency($selected, $htmlname);
  2149. }
  2150. /**
  2151. * Retourne la liste des devises, dans la langue de l'utilisateur
  2152. *
  2153. * @param string $selected preselected currency code
  2154. * @param string $htmlname name of HTML select list
  2155. * @return void
  2156. */
  2157. function selectcurrency($selected = '', $htmlname = 'currency_id') {
  2158. global $conf, $langs, $user;
  2159. $langs->load("dict");
  2160. $langs->load_cache_currencies();
  2161. $out = '';
  2162. if ($selected == 'euro' || $selected == 'euros')
  2163. $selected = 'EUR'; // Pour compatibilite
  2164. $out.= '<select class="flat" name="' . $htmlname . '">';
  2165. foreach ($langs->cache_currencies as $code_iso => $currency) {
  2166. if ($selected && $selected == $code_iso) {
  2167. $out.= '<option value="' . $code_iso . '" selected="selected">';
  2168. } else {
  2169. $out.= '<option value="' . $code_iso . '">';
  2170. }
  2171. $out.= $currency['label'];
  2172. $out.= ' (' . $langs->getCurrencySymbol($code_iso) . ')';
  2173. $out.= '</option>';
  2174. }
  2175. $out.= '</select>';
  2176. if ($user->admin)
  2177. $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionnarySetup"), 1);
  2178. return $out;
  2179. }
  2180. /**
  2181. * Load into the cache vat rates of a country
  2182. *
  2183. * @param string $country_code Country code
  2184. * @return int Nb of loaded lines, 0 if already loaded, <0 if KO
  2185. */
  2186. function load_cache_vatrates($country_code) {
  2187. return false;
  2188. /*
  2189. global $langs;
  2190. $num = count($this->cache_vatrates);
  2191. if ($num > 0)
  2192. return $num; // Cache deja charge
  2193. $sql = "SELECT DISTINCT t.taux, t.recuperableonly";
  2194. $sql.= " FROM " . MAIN_DB_PREFIX . "c_tva as t, " . MAIN_DB_PREFIX . "c_pays as p";
  2195. $sql.= " WHERE t.fk_pays = p.rowid";
  2196. $sql.= " AND t.active = 1";
  2197. $sql.= " AND p.code IN (" . $country_code . ")";
  2198. $sql.= " ORDER BY t.taux ASC, t.recuperableonly ASC";
  2199. $resql = $this->db->query($sql);
  2200. if ($resql) {
  2201. $num = $this->db->num_rows($resql);
  2202. if ($num) {
  2203. for ($i = 0; $i < $num; $i++) {
  2204. $obj = $this->db->fetch_object($resql);
  2205. $this->cache_vatrates[$i]['txtva'] = $obj->taux;
  2206. $this->cache_vatrates[$i]['libtva'] = $obj->taux . '%';
  2207. $this->cache_vatrates[$i]['nprtva'] = $obj->recuperableonly;
  2208. }
  2209. return $num;
  2210. } else {
  2211. $this->error = '<font class="error">' . $langs->trans("ErrorNoVATRateDefinedForSellerCountry", $country_code) . '</font>';
  2212. return -1;
  2213. }
  2214. } else {
  2215. $this->error = '<font class="error">' . $this->db->error() . '</font>';
  2216. return -2;
  2217. }
  2218. */
  2219. }
  2220. /**
  2221. * Output an HTML select vat rate
  2222. *
  2223. * @param string $htmlname Nom champ html
  2224. * @param float $selectedrate Forcage du taux tva pre-selectionne. Mettre '' pour aucun forcage.
  2225. * @param Societe $societe_vendeuse Objet societe vendeuse
  2226. * @param Societe $societe_acheteuse Objet societe acheteuse
  2227. * @param int $idprod Id product
  2228. * @param int $info_bits Miscellaneous information on line (1 for NPR)
  2229. * @param int $type ''=Unknown, 0=Product, 1=Service (Used if idprod not defined)
  2230. * Si vendeur non assujeti a TVA, TVA par defaut=0. Fin de regle.
  2231. * Si le (pays vendeur = pays acheteur) alors la TVA par defaut=TVA du produit vendu. Fin de regle.
  2232. * Si (vendeur et acheteur dans Communaute europeenne) et bien vendu = moyen de transports neuf (auto, bateau, avion), TVA par defaut=0 (La TVA doit etre paye par l'acheteur au centre d'impots de son pays et non au vendeur). Fin de regle.
  2233. * Si (vendeur et acheteur dans Communaute europeenne) et bien vendu autre que transport neuf alors la TVA par defaut=TVA du produit vendu. Fin de regle.
  2234. * Sinon la TVA proposee par defaut=0. Fin de regle.
  2235. * @param bool $options_only Return options only (for ajax treatment)
  2236. * @return void
  2237. */
  2238. function load_tva($htmlname = 'tauxtva', $selectedrate = '', $societe_vendeuse = '', $societe_acheteuse = '', $idprod = 0, $info_bits = 0, $type = '', $options_only = false) {
  2239. global $langs, $conf, $mysoc;
  2240. $return = '';
  2241. $txtva = array();
  2242. $libtva = array();
  2243. $nprtva = array();
  2244. // Define defaultnpr and defaultttx
  2245. $defaultnpr = ($info_bits & 0x01);
  2246. $defaultnpr = (preg_match('/\*/', $selectedrate) ? 1 : $defaultnpr);
  2247. $defaulttx = str_replace('*', '', $selectedrate);
  2248. // Check parameters
  2249. if (is_object($societe_vendeuse) && !$societe_vendeuse->country_id) {
  2250. if ($societe_vendeuse->id == $mysoc->id) {
  2251. $return.= '<font class="error">' . $langs->trans("ErrorYourCountryIsNotDefined") . '</div>';
  2252. } else {
  2253. $return.= '<font class="error">' . $langs->trans("ErrorSupplierCountryIsNotDefined") . '</div>';
  2254. }
  2255. return $return;
  2256. }
  2257. //var_dump($societe_acheteuse);
  2258. //print "name=$name, selectedrate=$selectedrate, seller=".$societe_vendeuse->country_id." buyer=".$societe_acheteuse->country_id." buyer is company=".$societe_acheteuse->isACompany()." idprod=$idprod, info_bits=$info_bits type=$type";
  2259. //exit;
  2260. // Get list of all VAT rates to show
  2261. // First we defined code_pays to use to find list
  2262. if (is_object($societe_vendeuse)) {
  2263. $code_pays = "'" . $societe_vendeuse->country_id . "'";
  2264. } else {
  2265. $code_pays = "'" . $mysoc->country_id . "'"; // Pour compatibilite ascendente
  2266. }
  2267. if (!empty($conf->global->SERVICE_ARE_ECOMMERCE_200238EC)) { // If option to have vat for end customer for services is on
  2268. if (!$societe_vendeuse->isInEEC() && $societe_acheteuse->isInEEC() && !$societe_acheteuse->isACompany()) {
  2269. // We also add the buyer
  2270. if (is_numeric($type)) {
  2271. if ($type == 1) { // We know product is a service
  2272. $code_pays.=",'" . $societe_acheteuse->country_id . "'";
  2273. }
  2274. } else if (!$idprod) { // We don't know type of product
  2275. $code_pays.=",'" . $societe_acheteuse->country_id . "'";
  2276. } else {
  2277. $prodstatic = new Product($this->db);
  2278. $prodstatic->fetch($idprod);
  2279. if ($prodstatic->type == 1) { // We know product is a service
  2280. $code_pays.=",'" . $societe_acheteuse->country_id . "'";
  2281. }
  2282. }
  2283. }
  2284. }
  2285. // Now we get list
  2286. $num = $this->load_cache_vatrates($code_pays);
  2287. if ($num > 0) {
  2288. // Definition du taux a pre-selectionner (si defaulttx non force et donc vaut -1 ou '')
  2289. if ($defaulttx < 0 || dol_strlen($defaulttx) == 0) {
  2290. $defaulttx = get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod);
  2291. $defaultnpr = get_default_npr($societe_vendeuse, $societe_acheteuse, $idprod);
  2292. }
  2293. // Si taux par defaut n'a pu etre determine, on prend dernier de la liste.
  2294. // Comme ils sont tries par ordre croissant, dernier = plus eleve = taux courant
  2295. if ($defaulttx < 0 || dol_strlen($defaulttx) == 0) {
  2296. $defaulttx = $this->cache_vatrates[$num - 1]['txtva'];
  2297. }
  2298. // Disabled if seller is not subject to VAT
  2299. $disabled = false;
  2300. $title = '';
  2301. if (is_object($societe_vendeuse) && $societe_vendeuse->id == $mysoc->id && $societe_vendeuse->tva_assuj == "0") {
  2302. $title = ' title="' . $langs->trans('VATIsNotUsed') . '"';
  2303. $disabled = true;
  2304. }
  2305. if (!$options_only)
  2306. $return.= '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '"' . ($disabled ? ' disabled="disabled"' : '') . $title . '>';
  2307. foreach ($this->cache_vatrates as $rate) {
  2308. // Keep only 0 if seller is not subject to VAT
  2309. if ($disabled && $rate['txtva'] != 0)
  2310. continue;
  2311. $return.= '<option value="' . $rate['txtva'];
  2312. $return.= $rate['nprtva'] ? '*' : '';
  2313. $return.= '"';
  2314. if ($rate['txtva'] == $defaulttx && $rate['nprtva'] == $defaultnpr) {
  2315. $return.= ' selected="selected"';
  2316. }
  2317. $return.= '>' . vatrate($rate['libtva']);
  2318. $return.= $rate['nprtva'] ? ' *' : '';
  2319. $return.= '</option>';
  2320. $this->tva_taux_value[] = $rate['txtva'];
  2321. $this->tva_taux_libelle[] = $rate['libtva'];
  2322. $this->tva_taux_npr[] = $rate['nprtva'];
  2323. }
  2324. if (!$options_only)
  2325. $return.= '</select>';
  2326. }
  2327. else {
  2328. $return.= $this->error;
  2329. }
  2330. $this->num = $num;
  2331. return $return;
  2332. }
  2333. /**
  2334. * Show a HTML widget to input a date or combo list for day, month, years and optionnaly hours and minutes
  2335. * Fields are preselected with :
  2336. * - set_time date (Local PHP server timestamps or date format YYYY-MM-DD or YYYY-MM-DD HH:MM)
  2337. * - local date of PHP server if set_time is ''
  2338. * - Empty (fields empty) if set_time is -1 (in this case, parameter empty must also have value 1)
  2339. *
  2340. * @param timestamp $set_time Pre-selected date (must be a local PHP server timestamp)
  2341. * @param string $prefix Prefix for fields name
  2342. * @param int $h 1=Show also hours
  2343. * @param int $m 1=Show also minutes
  2344. * @param int $empty 0=Fields required, 1=Empty input is allowed
  2345. * @param string $form_name Not used
  2346. * @param int $d 1=Show days, month, years
  2347. * @param int $addnowbutton Add a button "Now"
  2348. * @param int $nooutput Do not output html string but return it
  2349. * @param int $disabled Disable input fields
  2350. * @param int $fullday When a checkbox with this html name is on, hour and day are set with 00:00 or 23:59
  2351. * @return mixed Nothing or string if nooutput is 1
  2352. */
  2353. function select_date($set_time = '', $prefix = 're', $h = 0, $m = 0, $empty = 0, $form_name = "", $d = 1, $addnowbutton = 0, $nooutput = 0, $disabled = 0, $fullday = '') {
  2354. global $conf, $langs;
  2355. $retstring = '';
  2356. if ($prefix == '')
  2357. $prefix = 're';
  2358. if ($h == '')
  2359. $h = 0;
  2360. if ($m == '')
  2361. $m = 0;
  2362. if ($empty == '')
  2363. $empty = 0;
  2364. if (!$set_time && $empty == 0)
  2365. $set_time = dol_now('tzuser');
  2366. // Analysis of the pre-selection date
  2367. if (preg_match('/^([0-9]+)\-([0-9]+)\-([0-9]+)T?([0-9]+)?:?([0-9]+)?/', $set_time, $reg)) {
  2368. // Date format 'YYYY-MM-DD' or 'YYYY-MM-DDTHH:MM:SS'
  2369. $syear = (!empty($reg[1]) ? $reg[1] : '');
  2370. $smonth = (!empty($reg[2]) ? $reg[2] : '');
  2371. $sday = (!empty($reg[3]) ? $reg[3] : '');
  2372. $shour = (!empty($reg[4]) ? $reg[4] : '');
  2373. $smin = (!empty($reg[5]) ? $reg[5] : '');
  2374. } else {
  2375. // Date est '' ou vaut -1
  2376. $syear = '';
  2377. $smonth = '';
  2378. $sday = '';
  2379. $shour = '';
  2380. $smin = '';
  2381. }
  2382. if ($d) {
  2383. // Show date with popup
  2384. $formated_date = '';
  2385. //print "e".$set_time." t ".$conf->format_date_short;
  2386. if (strval($set_time) != '' && $set_time != -1) {
  2387. if ($h)
  2388. $formated_date = dol_print_date($set_time, "%d-%m-%Y %H:%M");
  2389. else
  2390. $formated_date = dol_print_date($set_time, "%d-%m-%Y");
  2391. //$formated_date = dol_print_date($set_time, $langs->trans("FormatDateShort")); // FormatDateShort for dol_print_date/FormatDateShortJava that is same for javascript
  2392. }
  2393. // Zone de saisie manuelle de la date
  2394. $retstring.='<span class="input">';
  2395. $retstring.='<span class="icon-calendar"></span>';
  2396. $retstring.='<input id="' . $prefix . '" name="' . $prefix . '" class="input-unstyled datepicker _gldp" type="text" value="' . $formated_date . '"';
  2397. $retstring.=($disabled ? ' disabled="disabled"' : '');
  2398. //$retstring.=' onChange="dpChangeDay(\'' . $prefix . '\',\'' . $langs->trans("FormatDateShortJava") . '\'); "'; // FormatDateShort for dol_print_date/FormatDateShortJava that is same for javascript
  2399. $retstring.='>';
  2400. $retstring.='</span>';
  2401. if ($h)
  2402. $retstring.='<script> $(function() {
  2403. $("input#' . $prefix . '").datetimepicker({
  2404. dateFormat: "dd-mm-yy",
  2405. timeFormat: "HH:mm",
  2406. showAnim: "slide"
  2407. });
  2408. }); </script>';
  2409. else
  2410. $retstring.='<script> $(function() {
  2411. $("input#' . $prefix . '").datepicker({
  2412. dateFormat: "dd-mm-yy",
  2413. showAnim: "slide"
  2414. });
  2415. }); </script>';
  2416. }
  2417. if ($d && $h)
  2418. $retstring.='&nbsp;';
  2419. if (!empty($nooutput))
  2420. return $retstring;
  2421. print $retstring;
  2422. return;
  2423. }
  2424. /**
  2425. * Function to show a form to select a duration on a page
  2426. *
  2427. * @param string $prefix prefix
  2428. * @param int $iSecond Default preselected duration (number of seconds)
  2429. * @param int $disabled Disable the combo box
  2430. * @return void
  2431. */
  2432. function select_duration($prefix, $iSecond = '', $disabled = 0) {
  2433. if ($iSecond) {
  2434. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  2435. $hourSelected = convertSecondToTime($iSecond, 'hour');
  2436. $minSelected = convertSecondToTime($iSecond, 'min');
  2437. }
  2438. print '<select class="flat" name="' . $prefix . 'hour"' . ($disabled ? ' disabled="disabled"' : '') . '>';
  2439. for ($hour = 0; $hour < 24; $hour++) {
  2440. print '<option value="' . $hour . '"';
  2441. if ($hourSelected == $hour) {
  2442. print " selected=\"true\"";
  2443. }
  2444. print ">" . $hour . "</option>";
  2445. }
  2446. print "</select>";
  2447. print "H &nbsp;";
  2448. print '<select class="flat" name="' . $prefix . 'min"' . ($disabled ? ' disabled="disabled"' : '') . '>';
  2449. for ($min = 0; $min <= 55; $min = $min + 5) {
  2450. print '<option value="' . $min . '"';
  2451. if ($minSelected == $min)
  2452. print ' selected="selected"';
  2453. print '>' . $min . '</option>';
  2454. }
  2455. print "</select>";
  2456. print "M&nbsp;";
  2457. }
  2458. /**
  2459. * Show a select form from an array
  2460. *
  2461. * @param string $htmlname Name of html select area
  2462. * @param array $array Array with key+value
  2463. * @param int $id Preselected key
  2464. * @param int $show_empty 1 si il faut ajouter une valeur vide dans la liste, 0 sinon
  2465. * @param int $key_in_label 1 pour afficher la key dans la valeur "[key] value"
  2466. * @param int $value_as_key 1 to use value as key
  2467. * @param string $option Valeur de l'option en fonction du type choisi
  2468. * @param int $translate Translate and encode value
  2469. * @param int $maxlen Length maximum for labels
  2470. * @param int $disabled Html select box is disabled
  2471. * @return string HTML select string
  2472. */
  2473. function selectarray($htmlname, $array, $id = '', $show_empty = 0, $key_in_label = 0, $value_as_key = 0, $option = '', $translate = 0, $maxlen = 0, $disabled = 0) {
  2474. global $langs;
  2475. if ($value_as_key)
  2476. $array = array_combine($array, $array);
  2477. $out = '<select id="' . $htmlname . '" ' . ($disabled ? 'disabled="disabled" ' : '') . 'class="flat" name="' . $htmlname . '" ' . ($option != '' ? $option : '') . '>';
  2478. if ($show_empty) {
  2479. $out.='<option value="-1"' . ($id == -1 ? ' selected="selected"' : '') . '>&nbsp;</option>' . "\n";
  2480. }
  2481. if (is_array($array)) {
  2482. foreach ($array as $key => $value) {
  2483. $out.='<option value="' . $key . '"';
  2484. // Si il faut pre-selectionner une valeur
  2485. if ($id != '' && $id == $key) {
  2486. $out.=' selected="selected"';
  2487. }
  2488. $out.='>';
  2489. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  2490. if ($key_in_label) {
  2491. $selectOptionValue = dol_htmlentitiesbr($key . ' - ' . ($maxlen ? dol_trunc($newval, $maxlen) : $newval));
  2492. } else {
  2493. $selectOptionValue = dol_htmlentitiesbr($maxlen ? dol_trunc($newval, $maxlen) : $newval);
  2494. if ($value == '' || $value == '-') {
  2495. $selectOptionValue = '&nbsp;';
  2496. }
  2497. }
  2498. $out.=$selectOptionValue;
  2499. $out.="</option>\n";
  2500. }
  2501. }
  2502. $out.="</select>";
  2503. return $out;
  2504. }
  2505. /**
  2506. * Show a multiselect form from an array.
  2507. *
  2508. * @param string $htmlname Name of select
  2509. * @param array $array Array with key+value
  2510. * @param array $selected Preselected keys
  2511. * @param int $key_in_label 1 pour afficher la key dans la valeur "[key] value"
  2512. * @param int $value_as_key 1 to use value as key
  2513. * @param string $option Valeur de l'option en fonction du type choisi
  2514. * @param int $translate Translate and encode value
  2515. * @return string HTML multiselect string
  2516. */
  2517. function multiselectarray($htmlname, $array, $selected = array(), $key_in_label = 0, $value_as_key = 0, $option = '', $translate = 0) {
  2518. global $conf, $langs;
  2519. $out = '<select id="' . $htmlname . '" class="multiselect" multiple="multiple" name="' . $htmlname . '[]"' . $option . '>' . "\n";
  2520. if (is_array($array) && !empty($array)) {
  2521. if ($value_as_key)
  2522. $array = array_combine($array, $array);
  2523. if (!empty($conf->global->MAIN_USE_JQUERY_MULTISELECT) && is_array($selected) && !empty($selected)) {
  2524. foreach ($selected as $selected_value) {
  2525. foreach ($array as $key => $value) {
  2526. if ($selected_value == $key) {
  2527. $value = $array[$selected_value];
  2528. $out.= '<option value="' . $key . '" selected="selected">';
  2529. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  2530. $newval = ($key_in_label ? $key . ' - ' . $newval : $newval);
  2531. $out.= dol_htmlentitiesbr($newval);
  2532. $out.= '</option>' . "\n";
  2533. unset($array[$key]);
  2534. }
  2535. }
  2536. }
  2537. if (!empty($array)) {
  2538. foreach ($array as $key => $value) {
  2539. $out.= '<option value="' . $key . '">';
  2540. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  2541. $newval = ($key_in_label ? $key . ' - ' . $newval : $newval);
  2542. $out.= dol_htmlentitiesbr($newval);
  2543. $out.= '</option>' . "\n";
  2544. }
  2545. }
  2546. } else {
  2547. foreach ($array as $key => $value) {
  2548. $out.= '<option value="' . $key . '"';
  2549. if (is_array($selected) && !empty($selected) && in_array($key, $selected)) {
  2550. $out.= ' selected="selected"';
  2551. }
  2552. $out.= '>';
  2553. $newval = ($translate ? $langs->trans(ucfirst($value)) : $value);
  2554. $newval = ($key_in_label ? $key . ' - ' . $newval : $newval);
  2555. $out.= dol_htmlentitiesbr($newval);
  2556. $out.= '</option>' . "\n";
  2557. }
  2558. }
  2559. }
  2560. $out.= '</select>' . "\n";
  2561. return $out;
  2562. }
  2563. /**
  2564. * Return an html string with a select combo box to choose yes or no
  2565. *
  2566. * @param string $htmlname Name of html select field
  2567. * @param string $value Pre-selected value
  2568. * @param int $option 0 return yes/no, 1 return 1/0
  2569. * @param bool $disabled true or false
  2570. * @return mixed See option
  2571. */
  2572. function selectyesno($htmlname, $value = '', $option = 0, $disabled = false) {
  2573. global $langs;
  2574. $yes = "yes";
  2575. $no = "no";
  2576. if ($option) {
  2577. $yes = "1";
  2578. $no = "0";
  2579. }
  2580. $disabled = ($disabled ? ' disabled="disabled"' : '');
  2581. $resultyesno = '<select class="flat" id="' . $htmlname . '" name="' . $htmlname . '"' . $disabled . '>' . "\n";
  2582. if (("$value" == 'yes') || ($value == 1)) {
  2583. $resultyesno .= '<option value="' . $yes . '" selected="selected">' . $langs->trans("Yes") . '</option>' . "\n";
  2584. $resultyesno .= '<option value="' . $no . '">' . $langs->trans("No") . '</option>' . "\n";
  2585. } else {
  2586. $resultyesno .= '<option value="' . $yes . '">' . $langs->trans("Yes") . '</option>' . "\n";
  2587. $resultyesno .= '<option value="' . $no . '" selected="selected">' . $langs->trans("No") . '</option>' . "\n";
  2588. }
  2589. $resultyesno .= '</select>' . "\n";
  2590. return $resultyesno;
  2591. }
  2592. /**
  2593. * Return list of export templates
  2594. *
  2595. * @param string $selected Id modele pre-selectionne
  2596. * @param string $htmlname Name of HTML select
  2597. * @param string $type Type of searched templates
  2598. * @param int $useempty Affiche valeur vide dans liste
  2599. * @return void
  2600. */
  2601. function select_export_model($selected = '', $htmlname = 'exportmodelid', $type = '', $useempty = 0) {
  2602. $sql = "SELECT rowid, label";
  2603. $sql.= " FROM " . MAIN_DB_PREFIX . "export_model";
  2604. $sql.= " WHERE type = '" . $type . "'";
  2605. $sql.= " ORDER BY rowid";
  2606. $result = $this->db->query($sql);
  2607. if ($result) {
  2608. print '<select class="flat" name="' . $htmlname . '">';
  2609. if ($useempty) {
  2610. print '<option value="-1">&nbsp;</option>';
  2611. }
  2612. $num = $this->db->num_rows($result);
  2613. $i = 0;
  2614. while ($i < $num) {
  2615. $obj = $this->db->fetch_object($result);
  2616. if ($selected == $obj->rowid) {
  2617. print '<option value="' . $obj->rowid . '" selected="selected">';
  2618. } else {
  2619. print '<option value="' . $obj->rowid . '">';
  2620. }
  2621. print $obj->label;
  2622. print '</option>';
  2623. $i++;
  2624. }
  2625. print "</select>";
  2626. } else {
  2627. dol_print_error($this->db);
  2628. }
  2629. }
  2630. /**
  2631. * Return a HTML area with the reference of object and a navigation bar for a business object
  2632. * To add a particular filter on select, you must set $object->next_prev_filter to SQL criteria.
  2633. *
  2634. * @param Object $object Object to show
  2635. * @param string $paramid Name of parameter to use to name the id into the URL link
  2636. * @param string $morehtml More html content to output just before the nav bar
  2637. * @param int $shownav Show Condition (navigation is shown if value is 1)
  2638. * @param string $fieldid Nom du champ en base a utiliser pour select next et previous
  2639. * @param string $fieldref Nom du champ objet ref (object->ref) a utiliser pour select next et previous
  2640. * @param string $morehtmlref Code html supplementaire a afficher apres ref
  2641. * @param string $moreparam More param to add in nav link url.
  2642. * @return tring Portion HTML avec ref + boutons nav
  2643. */
  2644. function showrefnav($object, $paramid, $morehtml = '', $shownav = 1, $fieldid = 'rowid', $fieldref = 'ref', $morehtmlref = '', $moreparam = '') {
  2645. $ret = '';
  2646. if (empty($fieldid))
  2647. $fieldid = 'rowid';
  2648. if (empty($fieldref))
  2649. $fieldref = 'ref';
  2650. if (isset($object->class))
  2651. return $object->_id . ' / ' . $object->_rev;
  2652. //print "paramid=$paramid,morehtml=$morehtml,shownav=$shownav,$fieldid,$fieldref,$morehtmlref,$moreparam";
  2653. $object->load_previous_next_ref((isset($object->next_prev_filter) ? $object->next_prev_filter : ''), $fieldid);
  2654. $previous_ref = $object->ref_previous ? '<a href="' . $_SERVER["PHP_SELF"] . '?' . $paramid . '=' . urlencode($object->ref_previous) . $moreparam . '">' . img_previous() . '</a>' : '';
  2655. $next_ref = $object->ref_next ? '<a href="' . $_SERVER["PHP_SELF"] . '?' . $paramid . '=' . urlencode($object->ref_next) . $moreparam . '">' . img_next() . '</a>' : '';
  2656. //print "xx".$previous_ref."x".$next_ref;
  2657. if ($previous_ref || $next_ref || $morehtml) {
  2658. $ret.='<table class="nobordernopadding" width="100%"><tr class="nobordernopadding"><td class="nobordernopadding">';
  2659. }
  2660. $ret.=$object->$fieldref;
  2661. if ($morehtmlref) {
  2662. $ret.=' ' . $morehtmlref;
  2663. }
  2664. if ($morehtml) {
  2665. $ret.='</td><td class="nobordernopadding" align="right">' . $morehtml;
  2666. }
  2667. if ($shownav && ($previous_ref || $next_ref)) {
  2668. $ret.='</td><td class="nobordernopadding" align="center" width="20">' . $previous_ref . '</td>';
  2669. $ret.='<td class="nobordernopadding" align="center" width="20">' . $next_ref;
  2670. }
  2671. if ($previous_ref || $next_ref || $morehtml) {
  2672. $ret.='</td></tr></table>';
  2673. }
  2674. return $ret;
  2675. }
  2676. /**
  2677. * Return HTML code to output a barcode
  2678. *
  2679. * @param Object &$object Object containing data to retrieve file name
  2680. * @param int $width Width of photo
  2681. * @return string HTML code to output barcode
  2682. */
  2683. function showbarcode(&$object, $width = 100) {
  2684. global $conf;
  2685. //Check if barcode is filled in the card
  2686. if (empty($object->barcode))
  2687. return '';
  2688. // Complete object if not complete
  2689. if (empty($object->barcode_type_code) || empty($object->barcode_type_coder)) {
  2690. $result = $object->fetch_barcode();
  2691. //Check if fetch_barcode() failed
  2692. if ($result < 1)
  2693. return '<!-- ErrorFetchBarcode -->';
  2694. }
  2695. // Barcode image
  2696. $url = DOL_URL_ROOT . '/viewimage.php?modulepart=barcode&generator=' . urlencode($object->barcode_type_coder) . '&code=' . urlencode($object->barcode) . '&encoding=' . urlencode($object->barcode_type_code);
  2697. $out = '<!-- url barcode = ' . $url . ' -->';
  2698. $out.='<img src="' . $url . '">';
  2699. return $out;
  2700. }
  2701. /**
  2702. * Return HTML code to output a photo
  2703. *
  2704. * @param string $modulepart Key to define module concerned ('societe', 'userphoto', 'memberphoto')
  2705. * @param Object $object Object containing data to retrieve file name
  2706. * @param int $width Width of photo
  2707. * @return string HTML code to output photo
  2708. */
  2709. function showphoto($modulepart, $object, $width = 180) {
  2710. global $conf;
  2711. $ret = '';
  2712. $dir = '';
  2713. $file = '';
  2714. $altfile = '';
  2715. $email = '';
  2716. if ($modulepart == 'societe') {
  2717. $dir = $conf->societe->multidir_output[$object->entity];
  2718. $smallfile = $object->logo;
  2719. $smallfile = preg_replace('/(\.png|\.gif|\.jpg|\.jpeg|\.bmp)/i', '_small\\1', $smallfile);
  2720. if ($object->logo)
  2721. $file = $object->id . '/logos/thumbs/' . $smallfile;
  2722. }
  2723. else if ($modulepart == 'userphoto') {
  2724. if ($object->Photo)
  2725. $file = $object->Photo;
  2726. $email = $object->email;
  2727. }
  2728. else if ($modulepart == 'memberphoto') {
  2729. if ($object->photo)
  2730. $file = $object->photo;
  2731. $email = $object->email;
  2732. }
  2733. if ($object->class) { // Photo in the database
  2734. if ($file)
  2735. $ret.='<img alt="Photo" id="photologo' . (preg_replace('/[^a-z]/i', '_', $file)) . '" class="photologo" border="0" width="' . $width . '" src="' . $object->getFile($file) . '">';
  2736. else
  2737. $ret.='<img alt="No photo" border="0" width="' . $width . '" src="' . DOL_URL_ROOT . '/theme/common/nophoto.jpg">';
  2738. } elseif ($dir) {
  2739. $cache = '0';
  2740. if ($file && file_exists($dir . "/" . $file)) {
  2741. // TODO Link to large image
  2742. $ret.='<a href="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $object->entity . '&file=' . urlencode($file) . '&cache=' . $cache . '">';
  2743. $ret.='<img alt="Photo" id="photologo' . (preg_replace('/[^a-z]/i', '_', $file)) . '" class="photologo" border="0" width="' . $width . '" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $object->entity . '&file=' . urlencode($file) . '&cache=' . $cache . '">';
  2744. $ret.='</a>';
  2745. } elseif ($altfile && file_exists($dir . "/" . $altfile)) {
  2746. $ret.='<a href="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $object->entity . '&file=' . urlencode($file) . '&cache=' . $cache . '">';
  2747. $ret.='<img alt="Photo alt" id="photologo' . (preg_replace('/[^a-z]/i', '_', $file)) . '" class="photologo" border="0" width="' . $width . '" src="' . DOL_URL_ROOT . '/viewimage.php?modulepart=' . $modulepart . '&entity=' . $object->entity . '&file=' . urlencode($altfile) . '&cache=' . $cache . '">';
  2748. $ret.='</a>';
  2749. } else {
  2750. if (!empty($conf->gravatar->enabled) && $email) {
  2751. global $dolibarr_main_url_root;
  2752. $ret.='<!-- Put link to gravatar -->';
  2753. $ret.='<img alt="Photo found on Gravatar" title="Photo Gravatar.com - email ' . $email . '" border="0" width="' . $width . '" src="http://www.gravatar.com/avatar/' . dol_hash($email) . '?s=' . $width . '&d=' . urlencode(dol_buildpath('/theme/common/nophoto.jpg', 2)) . '">';
  2754. } else {
  2755. $ret.='<img alt="No photo" border="0" width="' . $width . '" src="' . DOL_URL_ROOT . '/theme/common/nophoto.jpg">';
  2756. }
  2757. }
  2758. }
  2759. else
  2760. dol_print_error('', 'Call of showphoto with wrong parameters');
  2761. /* Disabled. lightbox seems to not work. I don't know why.
  2762. $ret.="\n<script type=\"text/javascript\">
  2763. jQuery(function() {
  2764. jQuery('.photologo').lightBox();
  2765. });
  2766. </script>\n";
  2767. $ret.="\n<script type=\"text/javascript\">
  2768. jQuery(function() {
  2769. jQuery('.photologo').lightBox({
  2770. overlayBgColor: '#FFF',
  2771. overlayOpacity: 0.6,
  2772. imageLoading: '".DOL_URL_ROOT."/includes/jquery/plugins/lightbox/images/lightbox-ico-loading.gif',
  2773. imageBtnClose: '".DOL_URL_ROOT."/includes/jquery/plugins/lightbox/images/lightbox-btn-close.gif',
  2774. imageBtnPrev: '".DOL_URL_ROOT."/includes/jquery/plugins/lightbox/images/lightbox-btn-prev.gif',
  2775. imageBtnNext: '".DOL_URL_ROOT."/includes/jquery/plugins/lightbox/images/lightbox-btn-next.gif',
  2776. containerResizeSpeed: 350,
  2777. txtImage: 'Imagem',
  2778. txtOf: 'de'
  2779. });
  2780. });
  2781. </script>\n";
  2782. */
  2783. return $ret;
  2784. }
  2785. /**
  2786. * Return select list of groups
  2787. *
  2788. * @param string $selected Id group preselected
  2789. * @param string $htmlname Field name in form
  2790. * @param int $show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  2791. * @param string $exclude Array list of groups id to exclude
  2792. * @param int $disabled If select list must be disabled
  2793. * @param string $include Array list of groups id to include
  2794. * @param int $enableonly Array list of groups id to be enabled. All other must be disabled
  2795. * @return void
  2796. */
  2797. function select_dolgroups($selected = '', $htmlname = 'groupid', $show_empty = 0, $exclude = '', $disabled = 0, $include = '', $enableonly = '') {
  2798. global $conf, $user, $langs;
  2799. $out = '';
  2800. $object = new UserGroup($db);
  2801. $result = $object->getView("list");
  2802. if (count($result->rows) && is_array($exclude)) {
  2803. foreach ($result->rows as $key => $obj) {
  2804. if (in_array($obj->value->name, $exclude, true)) {
  2805. unset($result->rows[$key]);
  2806. }
  2807. }
  2808. }
  2809. $i = 0;
  2810. if (count($result->rows)) {
  2811. $out.= '<select class="flat" name="' . $htmlname . '"' . ($disabled ? ' disabled="disabled"' : '') . '>';
  2812. if ($show_empty)
  2813. $out.= '<option value="-1"' . ($selected == -1 ? ' selected="selected"' : '') . '>&nbsp;</option>' . "\n";
  2814. foreach ($result->rows as $obj) {
  2815. $disableline = 0;
  2816. if (is_array($enableonly) && count($enableonly) && !in_array($obj->value->name, $enableonly))
  2817. $disableline = 1;
  2818. $out.= '<option value="' . $obj->value->name . '"';
  2819. if ($disableline)
  2820. $out.= ' disabled="disabled"';
  2821. if ((is_object($selected) && $selected->id == $obj->value->name) || (!is_object($selected) && $selected == $obj->value->name)) {
  2822. $out.= ' selected="selected"';
  2823. }
  2824. $out.= '>';
  2825. $out.= $obj->value->name;
  2826. $out.= '</option>';
  2827. $i++;
  2828. }
  2829. } else {
  2830. $out.= '<select class="flat" name="' . $htmlname . '" disabled="disabled">';
  2831. $out.= '<option value="">' . $langs->trans("None") . '</option>';
  2832. }
  2833. $out.= '</select>';
  2834. return $out;
  2835. }
  2836. /**
  2837. * Return select list of databases
  2838. *
  2839. * @param string $selected Id group preselected
  2840. * @param string $htmlname Field name in form
  2841. * @param int $show_empty 0=liste sans valeur nulle, 1=ajoute valeur inconnue
  2842. * @param string $exclude Array list of groups id to exclude
  2843. * @param int $disabled If select list must be disabled
  2844. * @param string $include Array list of groups id to include
  2845. * @param int $enableonly Array list of groups id to be enabled. All other must be disabled
  2846. * @return void
  2847. */
  2848. function select_doldatabases($selected = '', $htmlname = 'databaseid', $show_empty = 0, $exclude = '', $disabled = 0, $include = '', $enableonly = '') {
  2849. global $conf, $user, $langs;
  2850. $out = '';
  2851. $object = new UserDatabase($db);
  2852. try {
  2853. $result = $object->couchdb->listDatabases();
  2854. } catch (Exception $exc) {
  2855. print $exc->getMessage();
  2856. }
  2857. if (count($result) && is_array($exclude)) {
  2858. foreach ($result as $key => $obj) {
  2859. if (in_array($obj, $exclude, true)) {
  2860. unset($result[$key]);
  2861. }
  2862. }
  2863. }
  2864. $i = 0;
  2865. if (count($result)) {
  2866. $out.= '<select class="flat" name="' . $htmlname . '"' . ($disabled ? ' disabled="disabled"' : '') . '>';
  2867. if ($show_empty)
  2868. $out.= '<option value="-1"' . ($selected == -1 ? ' selected="selected"' : '') . '>&nbsp;</option>' . "\n";
  2869. foreach ($result as $aRow) {
  2870. if ($aRow[0] != "_") { // Not _users and _replicator
  2871. try {
  2872. $object->fetch($aRow);
  2873. $obj = $object->values;
  2874. $secu = $object->couchAdmin->getSecurity();
  2875. if (count($secu->admins->names) + count($secu->readers->names) + count($secu->admins->roles) + count($secu->admins->roles) > 0)
  2876. $obj->Status = "SECURE";
  2877. else
  2878. $obj->Status = "INSECURE";
  2879. } catch (Exception $exc) {
  2880. print $exc->getMessage();
  2881. }
  2882. $disableline = 0;
  2883. if (is_array($enableonly) && count($enableonly) && !in_array($obj->db_name, $enableonly))
  2884. $disableline = 1;
  2885. $out.= '<option value="' . $obj->db_name . '"';
  2886. if ($disableline)
  2887. $out.= ' disabled="disabled"';
  2888. if ((is_object($selected) && $selected->id == $obj->db_name) || (!is_object($selected) && $selected == $obj->db_name)) {
  2889. $out.= ' selected="selected"';
  2890. }
  2891. $out.= '>';
  2892. $out.= $obj->db_name;
  2893. $out.= '</option>';
  2894. $i++;
  2895. }
  2896. }
  2897. } else {
  2898. $out.= '<select class="flat" name="' . $htmlname . '" disabled="disabled">';
  2899. $out.= '<option value="">' . $langs->trans("None") . '</option>';
  2900. }
  2901. $out.= '</select>';
  2902. return $out;
  2903. }
  2904. }
  2905. ?>