extralanguages.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /* Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/class/extralanguages.class.php
  19. * \ingroup core
  20. * \brief File of class to manage extra languages for some fields
  21. */
  22. /**
  23. * Class to manage standard extra languages
  24. */
  25. class ExtraLanguages
  26. {
  27. /**
  28. * @var DoliDB Database handler.
  29. */
  30. public $db;
  31. /**
  32. * @var array New array to store extralanguages definition
  33. */
  34. public $attributes;
  35. /**
  36. * @var string Error code (or message)
  37. */
  38. public $error = '';
  39. /**
  40. * @var string[] Array of Error code (or message)
  41. */
  42. public $errors = array();
  43. /**
  44. * @var string DB Error number
  45. */
  46. public $errno;
  47. /**
  48. * Constructor
  49. *
  50. * @param DoliDB $db Database handler
  51. */
  52. public function __construct($db)
  53. {
  54. $this->db = $db;
  55. $this->error = '';
  56. $this->errors = array();
  57. $this->attributes = array();
  58. }
  59. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  60. /**
  61. * Load array this->attributes with list of fields per object that need an alternate translation. The object and field must be managed with
  62. * the widgetForTranslation() method.
  63. * You must set variable MAIN_USE_ALTERNATE_TRANSLATION_FOR=elementA:fieldname,fieldname2;elementB:...
  64. * Example: MAIN_USE_ALTERNATE_TRANSLATION_FOR=societe:name,town;contact:firstname,lastname
  65. *
  66. * @param string $elementtype Type of element ('' = all, 'adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...).
  67. * @param boolean $forceload Force load of extra fields whatever is status of cache.
  68. * @return array Array of attributes keys+label for all extra fields.
  69. */
  70. public function fetch_name_extralanguages($elementtype, $forceload = false)
  71. {
  72. // phpcs:enable
  73. global $conf;
  74. if (empty($elementtype)) {
  75. return array();
  76. }
  77. if ($elementtype == 'thirdparty') {
  78. $elementtype = 'societe';
  79. }
  80. if ($elementtype == 'contact') {
  81. $elementtype = 'socpeople';
  82. }
  83. if ($elementtype == 'order_supplier') {
  84. $elementtype = 'commande_fournisseur';
  85. }
  86. $array_name_label = array();
  87. if (!empty($conf->global->MAIN_USE_ALTERNATE_TRANSLATION_FOR)) {
  88. $tmpelement = explode(';', $conf->global->MAIN_USE_ALTERNATE_TRANSLATION_FOR);
  89. foreach ($tmpelement as $elementstring) {
  90. $reg = array();
  91. preg_match('/^(.*):(.*)$/', $elementstring, $reg);
  92. $element = $reg[1];
  93. $array_name_label[$element] = array();
  94. $tmpfields = explode(',', $reg[2]);
  95. foreach ($tmpfields as $field) {
  96. //var_dump($fields);
  97. //$tmpkeyvar = explode(':', $fields);
  98. //$array_name_label[$element][$tmpkeyvar[0]] = $tmpkeyvar[1];
  99. $array_name_label[$element][$field] = $field;
  100. }
  101. }
  102. }
  103. //var_dump($array_name_label);
  104. $this->attributes = $array_name_label;
  105. return $array_name_label;
  106. }
  107. /**
  108. * Return HTML string to put an input field into a page
  109. * Code very similar with showInputField of common object
  110. *
  111. * @param string $key Key of attribute
  112. * @param string $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value)
  113. * @param string $extrafieldsobjectkey If defined (for example $object->table_element), use the new method to get extrafields data
  114. * @param string $moreparam To add more parametes on html input tag
  115. * @param string $keysuffix Prefix string to add after name and id of field (can be used to avoid duplicate names)
  116. * @param string $keyprefix Suffix string to add before name and id of field (can be used to avoid duplicate names)
  117. * @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric)
  118. * @param int $objectid Current object id
  119. * @param string $mode 1=Used for search filters
  120. * @return string
  121. */
  122. public function showInputField($key, $value, $extrafieldsobjectkey, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $mode = 0)
  123. {
  124. global $conf, $langs, $form;
  125. if (!is_object($form)) {
  126. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  127. $form = new Form($this->db);
  128. }
  129. $out = '';
  130. if (!preg_match('/options_$/', $keyprefix)) { // Because we work on extrafields, we add 'options_' to prefix if not already added
  131. $keyprefix = $keyprefix.'options_';
  132. }
  133. return $out;
  134. }
  135. /**
  136. * Return HTML string to put an output field into a page
  137. *
  138. * @param string $key Key of attribute
  139. * @param string $value Value to show
  140. * @param string $extrafieldsobjectkey If defined (for example $object->table_element), function uses the new method to get extrafields data
  141. * @param string $moreparam To add more parameters on html input tag (only checkbox use html input for output rendering)
  142. * @return string Formated value
  143. */
  144. public function showOutputField($key, $value, $extrafieldsobjectkey, $moreparam = '')
  145. {
  146. global $conf, $langs;
  147. $out = $this->attributes[$extrafieldsobjectkey][$key];
  148. return $out;
  149. }
  150. }