extrafields.class.php 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. <?php
  2. /* Copyright (C) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
  4. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  6. * Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
  8. * Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro>
  9. * Copyright (C) 2015 Charles-Fr BENKE <charles.fr@benke.fr>
  10. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  11. * Copyright (C) 2017 Nicolas ZABOURI <info@inovea-conseil.com>
  12. * Copyright (C) 2018-2022 Frédéric France <frederic.france@netlogic.fr>
  13. * Copyright (C) 2022 Antonin MARCHAL <antonin@letempledujeu.fr>
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  27. */
  28. /**
  29. * \file htdocs/core/class/extrafields.class.php
  30. * \ingroup core
  31. * \brief File of class to manage extra fields
  32. */
  33. /**
  34. * Class to manage standard extra fields
  35. */
  36. class ExtraFields
  37. {
  38. /**
  39. * @var DoliDB Database handler.
  40. */
  41. public $db;
  42. /**
  43. * @var array Array with type of the extra field
  44. * @deprecated
  45. */
  46. public $attribute_type;
  47. /**
  48. * @var array Array with label of extra field
  49. * @deprecated
  50. */
  51. public $attribute_label;
  52. /**
  53. * @var array Array with list of possible values for some types of extra fields
  54. * @deprecated
  55. */
  56. public $attribute_choice;
  57. /**
  58. * @var array array to store extrafields definition
  59. * @deprecated
  60. */
  61. public $attribute_list;
  62. /**
  63. * @var array New array to store extrafields definition
  64. */
  65. public $attributes;
  66. /**
  67. * @var array Array with boolean of status of groups
  68. */
  69. public $expand_display;
  70. /**
  71. * @var string Error code (or message)
  72. */
  73. public $error = '';
  74. /**
  75. * @var string[] Array of Error code (or message)
  76. */
  77. public $errors = array();
  78. /**
  79. * @var string DB Error number
  80. */
  81. public $errno;
  82. /**
  83. * @var array array of type to label
  84. */
  85. public static $type2label = array(
  86. 'varchar'=>'String1Line',
  87. 'text'=>'TextLongNLines',
  88. 'html'=>'HtmlText',
  89. 'int'=>'Int',
  90. 'double'=>'Float',
  91. 'date'=>'Date',
  92. 'datetime'=>'DateAndTime',
  93. 'boolean'=>'Boolean',
  94. 'price'=>'ExtrafieldPrice',
  95. 'pricecy'=>'ExtrafieldPriceWithCurrency',
  96. 'phone'=>'ExtrafieldPhone',
  97. 'mail'=>'ExtrafieldMail',
  98. 'url'=>'ExtrafieldUrl',
  99. 'ip'=>'ExtrafieldIP',
  100. 'password' => 'ExtrafieldPassword',
  101. 'select' => 'ExtrafieldSelect',
  102. 'sellist' => 'ExtrafieldSelectList',
  103. 'radio' => 'ExtrafieldRadio',
  104. 'checkbox' => 'ExtrafieldCheckBox',
  105. 'chkbxlst' => 'ExtrafieldCheckBoxFromList',
  106. 'link' => 'ExtrafieldLink',
  107. 'separate' => 'ExtrafieldSeparator',
  108. );
  109. /**
  110. * Constructor
  111. *
  112. * @param DoliDB $db Database handler
  113. */
  114. public function __construct($db)
  115. {
  116. $this->db = $db;
  117. $this->error = '';
  118. $this->errors = array();
  119. $this->attributes = array();
  120. $this->attribute_type = array();
  121. $this->attribute_label = array();
  122. }
  123. /**
  124. * Add a new extra field parameter
  125. *
  126. * @param string $attrname Code of attribute
  127. * @param string $label label of attribute
  128. * @param string $type Type of attribute ('boolean','int','varchar','text','html','date','datehour','price', 'pricecy', 'phone','mail','password','url','select','checkbox','separate',...)
  129. * @param int $pos Position of attribute
  130. * @param string $size Size/length definition of attribute ('5', '24,8', ...). For float, it contains 2 numeric separated with a comma.
  131. * @param string $elementtype Element type. Same value than object->table_element (Example 'member', 'product', 'thirdparty', ...)
  132. * @param int $unique Is field unique or not
  133. * @param int $required Is field required or not
  134. * @param string $default_value Defaulted value (In database. use the default_value feature for default value on screen. Example: '', '0', 'null', 'avalue')
  135. * @param array|string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  136. * @param int $alwayseditable Is attribute always editable regardless of the document status
  137. * @param string $perms Permission to check
  138. * @param string $list Visibilty ('0'=never visible, '1'=visible on list+forms, '2'=list only, '3'=form only or 'eval string')
  139. * @param string $help Text with help tooltip
  140. * @param string $computed Computed value
  141. * @param string $entity Entity of extrafields (for multicompany modules)
  142. * @param string $langfile Language file
  143. * @param string $enabled Condition to have the field enabled or not
  144. * @param int $totalizable Is a measure. Must show a total on lists
  145. * @param int $printable Is extrafield displayed on PDF
  146. * @param array $moreparams More parameters. Example: array('css'=>, 'csslist'=>Css on list, 'cssview'=>...)
  147. * @return int <=0 if KO, >0 if OK
  148. */
  149. public function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique = 0, $required = 0, $default_value = '', $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array())
  150. {
  151. if (empty($attrname)) {
  152. return -1;
  153. }
  154. if (empty($label)) {
  155. return -1;
  156. }
  157. $result = 0;
  158. if ($type == 'separate') {
  159. $unique = 0;
  160. $required = 0;
  161. } // Force unique and not required if this is a separator field to avoid troubles.
  162. if ($elementtype == 'thirdparty') {
  163. $elementtype = 'societe';
  164. }
  165. if ($elementtype == 'contact') {
  166. $elementtype = 'socpeople';
  167. }
  168. // Create field into database except for separator type which is not stored in database
  169. if ($type != 'separate') {
  170. $result = $this->create($attrname, $type, $size, $elementtype, $unique, $required, $default_value, $param, $perms, $list, $computed, $help, $moreparams);
  171. }
  172. $err1 = $this->errno;
  173. if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate') {
  174. // Add declaration of field into table
  175. $result2 = $this->create_label($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $help, $default_value, $computed, $entity, $langfile, $enabled, $totalizable, $printable, $moreparams);
  176. $err2 = $this->errno;
  177. if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS')) {
  178. $this->error = '';
  179. $this->errno = 0;
  180. return 1;
  181. } else {
  182. return -2;
  183. }
  184. } else {
  185. return -1;
  186. }
  187. }
  188. /**
  189. * Add a new optional attribute.
  190. * This is a private method. For public method, use addExtraField.
  191. *
  192. * @param string $attrname code of attribute
  193. * @param int $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datehour','price','pricecy','phone','mail','password','url','select','checkbox', ...)
  194. * @param string $length Size/length of attribute ('5', '24,8', ...)
  195. * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
  196. * @param int $unique Is field unique or not
  197. * @param int $required Is field required or not
  198. * @param string $default_value Default value for field (in database)
  199. * @param array $param Params for field (ex for select list : array('options'=>array('value'=>'label of option'))
  200. * @param string $perms Permission
  201. * @param string $list Into list view by default
  202. * @param string $computed Computed value
  203. * @param string $help Help on tooltip
  204. * @param array $moreparams More parameters. Example: array('css'=>, 'csslist'=>, 'cssview'=>...)
  205. * @return int <=0 if KO, >0 if OK
  206. */
  207. private function create($attrname, $type = 'varchar', $length = 255, $elementtype = 'member', $unique = 0, $required = 0, $default_value = '', $param = '', $perms = '', $list = '0', $computed = '', $help = '', $moreparams = array())
  208. {
  209. if ($elementtype == 'thirdparty') {
  210. $elementtype = 'societe';
  211. }
  212. if ($elementtype == 'contact') {
  213. $elementtype = 'socpeople';
  214. }
  215. $table = $elementtype.'_extrafields';
  216. if ($elementtype == 'categorie') {
  217. $table = 'categories_extrafields';
  218. }
  219. if (!empty($attrname) && preg_match("/^\w[a-zA-Z0-9_]*$/", $attrname) && !is_numeric($attrname)) {
  220. if ($type == 'boolean') {
  221. $typedb = 'int';
  222. $lengthdb = '1';
  223. } elseif ($type == 'price') {
  224. $typedb = 'double';
  225. $lengthdb = '24,8';
  226. } elseif ($type == 'pricecy') {
  227. $typedb = 'varchar';
  228. $lengthdb = '64';
  229. } elseif ($type == 'phone') {
  230. $typedb = 'varchar';
  231. $lengthdb = '20';
  232. } elseif ($type == 'mail' || $type == 'ip') {
  233. $typedb = 'varchar';
  234. $lengthdb = '128';
  235. } elseif ($type == 'url') {
  236. $typedb = 'varchar';
  237. $lengthdb = '255';
  238. } elseif (($type == 'select') || ($type == 'sellist') || ($type == 'radio') || ($type == 'checkbox') || ($type == 'chkbxlst')) {
  239. $typedb = 'varchar';
  240. $lengthdb = '255';
  241. } elseif ($type == 'link') {
  242. $typedb = 'int';
  243. $lengthdb = '11';
  244. } elseif ($type == 'html') {
  245. $typedb = 'text';
  246. $lengthdb = $length;
  247. } elseif ($type == 'password') {
  248. $typedb = 'varchar';
  249. $lengthdb = '128';
  250. } else {
  251. $typedb = $type;
  252. $lengthdb = $length;
  253. if ($type == 'varchar' && empty($lengthdb)) {
  254. $lengthdb = '255';
  255. }
  256. }
  257. $field_desc = array(
  258. 'type'=>$typedb,
  259. 'value'=>$lengthdb,
  260. 'null'=>($required ? 'NOT NULL' : 'NULL'),
  261. 'default' => $default_value
  262. );
  263. $result = $this->db->DDLAddField($this->db->prefix().$table, $attrname, $field_desc);
  264. if ($result > 0) {
  265. if ($unique) {
  266. $sql = "ALTER TABLE ".$this->db->prefix().$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
  267. $resql = $this->db->query($sql, 1, 'dml');
  268. }
  269. return 1;
  270. } else {
  271. $this->error = $this->db->lasterror();
  272. $this->errno = $this->db->lasterrno();
  273. return -1;
  274. }
  275. } else {
  276. return 0;
  277. }
  278. }
  279. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  280. /**
  281. * Add description of a new optional attribute
  282. *
  283. * @param string $attrname code of attribute
  284. * @param string $label label of attribute
  285. * @param int $type Type of attribute ('int', 'varchar', 'text', 'html', 'date', 'datehour', 'float')
  286. * @param int $pos Position of attribute
  287. * @param string $size Size/length of attribute ('5', '24,8', ...)
  288. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  289. * @param int $unique Is field unique or not
  290. * @param int $required Is field required or not
  291. * @param array|string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  292. * @param int $alwayseditable Is attribute always editable regardless of the document status
  293. * @param string $perms Permission to check
  294. * @param string $list Visibily
  295. * @param string $help Help on tooltip
  296. * @param string $default Default value (in database. use the default_value feature for default value on screen).
  297. * @param string $computed Computed value
  298. * @param string $entity Entity of extrafields
  299. * @param string $langfile Language file
  300. * @param string $enabled Condition to have the field enabled or not
  301. * @param int $totalizable Is a measure. Must show a total on lists
  302. * @param int $printable Is extrafield displayed on PDF
  303. * @param array $moreparams More parameters. Example: array('css'=>, 'csslist'=>, 'cssview'=>...)
  304. * @return int <=0 if KO, >0 if OK
  305. * @throws Exception
  306. */
  307. private function create_label($attrname, $label = '', $type = '', $pos = 0, $size = 0, $elementtype = 'member', $unique = 0, $required = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array())
  308. {
  309. // phpcs:enable
  310. global $conf, $user;
  311. if ($elementtype == 'thirdparty') {
  312. $elementtype = 'societe';
  313. }
  314. if ($elementtype == 'contact') {
  315. $elementtype = 'socpeople';
  316. }
  317. // Clean parameters
  318. if (empty($pos)) {
  319. $pos = 0;
  320. }
  321. if (empty($list)) {
  322. $list = '0';
  323. }
  324. if (empty($required)) {
  325. $required = 0;
  326. }
  327. if (empty($unique)) {
  328. $unique = 0;
  329. }
  330. if (empty($printable)) {
  331. $printable = 0;
  332. }
  333. if (empty($alwayseditable)) {
  334. $alwayseditable = 0;
  335. }
  336. if (empty($totalizable)) {
  337. $totalizable = 0;
  338. }
  339. $css = '';
  340. if (!empty($moreparams) && !empty($moreparams['css'])) {
  341. $css = $moreparams['css'];
  342. }
  343. $csslist = '';
  344. if (!empty($moreparams) && !empty($moreparams['csslist'])) {
  345. $csslist = $moreparams['csslist'];
  346. }
  347. $cssview = '';
  348. if (!empty($moreparams) && !empty($moreparams['cssview'])) {
  349. $cssview = $moreparams['cssview'];
  350. }
  351. if (!empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname) && !is_numeric($attrname)) {
  352. if (is_array($param) && count($param) > 0) {
  353. $params = serialize($param);
  354. } elseif (strlen($param) > 0) {
  355. $params = trim($param);
  356. } else {
  357. $params = '';
  358. }
  359. $sql = "INSERT INTO ".$this->db->prefix()."extrafields(";
  360. $sql .= " name,";
  361. $sql .= " label,";
  362. $sql .= " type,";
  363. $sql .= " pos,";
  364. $sql .= " size,";
  365. $sql .= " entity,";
  366. $sql .= " elementtype,";
  367. $sql .= " fieldunique,";
  368. $sql .= " fieldrequired,";
  369. $sql .= " param,";
  370. $sql .= " alwayseditable,";
  371. $sql .= " perms,";
  372. $sql .= " langs,";
  373. $sql .= " list,";
  374. $sql .= " printable,";
  375. $sql .= " fielddefault,";
  376. $sql .= " fieldcomputed,";
  377. $sql .= " fk_user_author,";
  378. $sql .= " fk_user_modif,";
  379. $sql .= " datec,";
  380. $sql .= " enabled,";
  381. $sql .= " help,";
  382. $sql .= " totalizable,";
  383. $sql .= " css,";
  384. $sql .= " csslist,";
  385. $sql .= " cssview";
  386. $sql .= " )";
  387. $sql .= " VALUES('".$this->db->escape($attrname)."',";
  388. $sql .= " '".$this->db->escape($label)."',";
  389. $sql .= " '".$this->db->escape($type)."',";
  390. $sql .= " ".((int) $pos).",";
  391. $sql .= " '".$this->db->escape($size)."',";
  392. $sql .= " ".($entity === '' ? $conf->entity : $entity).",";
  393. $sql .= " '".$this->db->escape($elementtype)."',";
  394. $sql .= " ".((int) $unique).",";
  395. $sql .= " ".((int) $required).",";
  396. $sql .= " '".$this->db->escape($params)."',";
  397. $sql .= " ".((int) $alwayseditable).",";
  398. $sql .= " ".($perms ? "'".$this->db->escape($perms)."'" : "null").",";
  399. $sql .= " ".($langfile ? "'".$this->db->escape($langfile)."'" : "null").",";
  400. $sql .= " '".$this->db->escape($list)."',";
  401. $sql .= " '".$this->db->escape($printable)."',";
  402. $sql .= " ".($default ? "'".$this->db->escape($default)."'" : "null").",";
  403. $sql .= " ".($computed ? "'".$this->db->escape($computed)."'" : "null").",";
  404. $sql .= " ".(is_object($user) ? $user->id : 0).",";
  405. $sql .= " ".(is_object($user) ? $user->id : 0).",";
  406. $sql .= "'".$this->db->idate(dol_now())."',";
  407. $sql .= " ".($enabled ? "'".$this->db->escape($enabled)."'" : "1").",";
  408. $sql .= " ".($help ? "'".$this->db->escape($help)."'" : "null").",";
  409. $sql .= " ".($totalizable ? 'TRUE' : 'FALSE').",";
  410. $sql .= " ".($css ? "'".$this->db->escape($css)."'" : "null").",";
  411. $sql .= " ".($csslist ? "'".$this->db->escape($csslist)."'" : "null").",";
  412. $sql .= " ".($cssview ? "'".$this->db->escape($cssview)."'" : "null");
  413. $sql .= ')';
  414. dol_syslog(get_class($this)."::create_label", LOG_DEBUG);
  415. if ($this->db->query($sql)) {
  416. return 1;
  417. } else {
  418. $this->error = $this->db->lasterror();
  419. $this->errno = $this->db->lasterrno();
  420. return -1;
  421. }
  422. }
  423. }
  424. /**
  425. * Delete an optional attribute
  426. *
  427. * @param string $attrname Code of attribute to delete
  428. * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
  429. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  430. */
  431. public function delete($attrname, $elementtype = 'member')
  432. {
  433. if ($elementtype == 'thirdparty') {
  434. $elementtype = 'societe';
  435. }
  436. if ($elementtype == 'contact') {
  437. $elementtype = 'socpeople';
  438. }
  439. $table = $elementtype.'_extrafields';
  440. if ($elementtype == 'categorie') {
  441. $table = 'categories_extrafields';
  442. }
  443. $error = 0;
  444. if (!empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  445. $result = $this->delete_label($attrname, $elementtype);
  446. if ($result < 0) {
  447. $this->error = $this->db->lasterror();
  448. $this->errors[] = $this->db->lasterror();
  449. $error++;
  450. }
  451. if (!$error) {
  452. $sql = "SELECT COUNT(rowid) as nb";
  453. $sql .= " FROM ".$this->db->prefix()."extrafields";
  454. $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'";
  455. $sql .= " AND name = '".$this->db->escape($attrname)."'";
  456. //$sql.= " AND entity IN (0,".$conf->entity.")"; Do not test on entity here. We want to see if there is still on field remaning in other entities before deleting field in table
  457. $resql = $this->db->query($sql);
  458. if ($resql) {
  459. $obj = $this->db->fetch_object($resql);
  460. if ($obj->nb <= 0) {
  461. $result = $this->db->DDLDropField($this->db->prefix().$table, $attrname); // This also drop the unique key
  462. if ($result < 0) {
  463. $this->error = $this->db->lasterror();
  464. $this->errors[] = $this->db->lasterror();
  465. $error++;
  466. }
  467. }
  468. }
  469. }
  470. return $result;
  471. } else {
  472. return 0;
  473. }
  474. }
  475. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  476. /**
  477. * Delete description of an optional attribute
  478. *
  479. * @param string $attrname Code of attribute to delete
  480. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  481. * @return int < 0 if KO, 0 if nothing is done, 1 if OK
  482. */
  483. private function delete_label($attrname, $elementtype = 'member')
  484. {
  485. // phpcs:enable
  486. global $conf;
  487. if ($elementtype == 'thirdparty') {
  488. $elementtype = 'societe';
  489. }
  490. if ($elementtype == 'contact') {
  491. $elementtype = 'socpeople';
  492. }
  493. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  494. $sql = "DELETE FROM ".$this->db->prefix()."extrafields";
  495. $sql .= " WHERE name = '".$this->db->escape($attrname)."'";
  496. $sql .= " AND entity IN (0,".$conf->entity.')';
  497. $sql .= " AND elementtype = '".$this->db->escape($elementtype)."'";
  498. dol_syslog(get_class($this)."::delete_label", LOG_DEBUG);
  499. $resql = $this->db->query($sql);
  500. if ($resql) {
  501. return 1;
  502. } else {
  503. dol_print_error($this->db);
  504. return -1;
  505. }
  506. } else {
  507. return 0;
  508. }
  509. }
  510. /**
  511. * Modify type of a personalized attribute
  512. *
  513. * @param string $attrname Name of attribute
  514. * @param string $label Label of attribute
  515. * @param string $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datehour','price','phone','mail','password','url','select','checkbox', ...)
  516. * @param int $length Length of attribute
  517. * @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
  518. * @param int $unique Is field unique or not
  519. * @param int $required Is field required or not
  520. * @param int $pos Position of attribute
  521. * @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  522. * @param int $alwayseditable Is attribute always editable regardless of the document status
  523. * @param string $perms Permission to check
  524. * @param string $list Visibility
  525. * @param string $help Help on tooltip
  526. * @param string $default Default value (in database. use the default_value feature for default value on screen).
  527. * @param string $computed Computed value
  528. * @param string $entity Entity of extrafields
  529. * @param string $langfile Language file
  530. * @param string $enabled Condition to have the field enabled or not
  531. * @param int $totalizable Is extrafield totalizable on list
  532. * @param int $printable Is extrafield displayed on PDF
  533. * @param array $moreparams More parameters. Example: array('css'=>, 'csslist'=>, 'cssview'=>...)
  534. * @return int >0 if OK, <=0 if KO
  535. * @throws Exception
  536. */
  537. public function update($attrname, $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array())
  538. {
  539. global $hookmanager;
  540. if ($elementtype == 'thirdparty') {
  541. $elementtype = 'societe';
  542. }
  543. if ($elementtype == 'contact') {
  544. $elementtype = 'socpeople';
  545. }
  546. $table = $elementtype.'_extrafields';
  547. if ($elementtype == 'categorie') {
  548. $table = 'categories_extrafields';
  549. }
  550. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  551. if ($type == 'boolean') {
  552. $typedb = 'int';
  553. $lengthdb = '1';
  554. } elseif ($type == 'price') {
  555. $typedb = 'double';
  556. $lengthdb = '24,8';
  557. } elseif ($type == 'pricecy') {
  558. $typedb = 'varchar';
  559. $lengthdb = '64';
  560. } elseif ($type == 'phone') {
  561. $typedb = 'varchar';
  562. $lengthdb = '20';
  563. } elseif ($type == 'mail' || $type == 'ip') {
  564. $typedb = 'varchar';
  565. $lengthdb = '128';
  566. } elseif ($type == 'url') {
  567. $typedb = 'varchar';
  568. $lengthdb = '255';
  569. } elseif (($type == 'select') || ($type == 'sellist') || ($type == 'radio') || ($type == 'checkbox') || ($type == 'chkbxlst')) {
  570. $typedb = 'varchar';
  571. $lengthdb = '255';
  572. } elseif ($type == 'html') {
  573. $typedb = 'text';
  574. } elseif ($type == 'link') {
  575. $typedb = 'int';
  576. $lengthdb = '11';
  577. } elseif ($type == 'password') {
  578. $typedb = 'varchar';
  579. $lengthdb = '50';
  580. } else {
  581. $typedb = $type;
  582. $lengthdb = $length;
  583. }
  584. $field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required ? 'NOT NULL' : 'NULL'), 'default'=>$default);
  585. if (is_object($hookmanager)) {
  586. $hookmanager->initHooks(array('extrafieldsdao'));
  587. $parameters = array('field_desc'=>&$field_desc, 'table'=>$table, 'attr_name'=>$attrname, 'label'=>$label, 'type'=>$type, 'length'=>$length, 'unique'=>$unique, 'required'=>$required, 'pos'=>$pos, 'param'=>$param, 'alwayseditable'=>$alwayseditable, 'perms'=>$perms, 'list'=>$list, 'help'=>$help, 'default'=>$default, 'computed'=>$computed, 'entity'=>$entity, 'langfile'=>$langfile, 'enabled'=>$enabled, 'totalizable'=>$totalizable, 'printable'=>$printable);
  588. $reshook = $hookmanager->executeHooks('updateExtrafields', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  589. if ($reshook < 0) {
  590. $this->error = $this->db->lasterror();
  591. return -1;
  592. }
  593. }
  594. if ($type != 'separate') { // No table update when separate type
  595. $result = $this->db->DDLUpdateField($this->db->prefix().$table, $attrname, $field_desc);
  596. }
  597. if ($result > 0 || $type == 'separate') {
  598. if ($label) {
  599. $result = $this->update_label($attrname, $label, $type, $length, $elementtype, $unique, $required, $pos, $param, $alwayseditable, $perms, $list, $help, $default, $computed, $entity, $langfile, $enabled, $totalizable, $printable, $moreparams);
  600. }
  601. if ($result > 0) {
  602. $sql = '';
  603. if ($unique) {
  604. $sql = "ALTER TABLE ".$this->db->prefix().$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
  605. } else {
  606. $sql = "ALTER TABLE ".$this->db->prefix().$table." DROP INDEX IF EXISTS uk_".$table."_".$attrname;
  607. }
  608. dol_syslog(get_class($this).'::update', LOG_DEBUG);
  609. $resql = $this->db->query($sql, 1, 'dml');
  610. /*if ($resql < 0) {
  611. $this->error = $this->db->lasterror();
  612. return -1;
  613. }*/
  614. return 1;
  615. } else {
  616. $this->error = $this->db->lasterror();
  617. return -1;
  618. }
  619. } else {
  620. $this->error = $this->db->lasterror();
  621. return -1;
  622. }
  623. } else {
  624. return 0;
  625. }
  626. }
  627. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  628. /**
  629. * Modify description of personalized attribute
  630. *
  631. * @param string $attrname Name of attribute
  632. * @param string $label Label of attribute
  633. * @param string $type Type of attribute
  634. * @param int $size Length of attribute
  635. * @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
  636. * @param int $unique Is field unique or not
  637. * @param int $required Is field required or not
  638. * @param int $pos Position of attribute
  639. * @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
  640. * @param int $alwayseditable Is attribute always editable regardless of the document status
  641. * @param string $perms Permission to check
  642. * @param string $list Visiblity
  643. * @param string $help Help on tooltip.
  644. * @param string $default Default value (in database. use the default_value feature for default value on screen).
  645. * @param string $computed Computed value
  646. * @param string $entity Entity of extrafields
  647. * @param string $langfile Language file
  648. * @param string $enabled Condition to have the field enabled or not
  649. * @param int $totalizable Is extrafield totalizable on list
  650. * @param int $printable Is extrafield displayed on PDF
  651. * @param array $moreparams More parameters. Example: array('css'=>, 'csslist'=>, 'cssview'=>...)
  652. * @return int <=0 if KO, >0 if OK
  653. * @throws Exception
  654. */
  655. private function update_label($attrname, $label, $type, $size, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '0', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array())
  656. {
  657. // phpcs:enable
  658. global $conf, $user;
  659. dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$default.", ".$computed.", ".$entity.", ".$langfile.", ".$enabled.", ".$totalizable.", ".$printable);
  660. // Clean parameters
  661. if ($elementtype == 'thirdparty') {
  662. $elementtype = 'societe';
  663. }
  664. if ($elementtype == 'contact') {
  665. $elementtype = 'socpeople';
  666. }
  667. if (empty($pos)) {
  668. $pos = 0;
  669. }
  670. if (empty($list)) {
  671. $list = '0';
  672. }
  673. if (empty($totalizable)) {
  674. $totalizable = 0;
  675. }
  676. if (empty($required)) {
  677. $required = 0;
  678. }
  679. if (empty($unique)) {
  680. $unique = 0;
  681. }
  682. if (empty($alwayseditable)) {
  683. $alwayseditable = 0;
  684. }
  685. $css = '';
  686. if (!empty($moreparams) && !empty($moreparams['css'])) {
  687. $css = $moreparams['css'];
  688. }
  689. $csslist = '';
  690. if (!empty($moreparams) && !empty($moreparams['csslist'])) {
  691. $csslist = $moreparams['csslist'];
  692. }
  693. $cssview = '';
  694. if (!empty($moreparams) && !empty($moreparams['cssview'])) {
  695. $cssview = $moreparams['cssview'];
  696. }
  697. if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname)) {
  698. $this->db->begin();
  699. if (is_array($param) && count($param) > 0) {
  700. $params = serialize($param);
  701. } elseif (strlen($param) > 0) {
  702. $params = trim($param);
  703. } else {
  704. $params = '';
  705. }
  706. if ($entity === '' || $entity != '0') {
  707. // We dont want on all entities, we delete all and current
  708. $sql_del = "DELETE FROM ".$this->db->prefix()."extrafields";
  709. $sql_del .= " WHERE name = '".$this->db->escape($attrname)."'";
  710. $sql_del .= " AND entity IN (0, ".($entity === '' ? $conf->entity : $entity).")";
  711. $sql_del .= " AND elementtype = '".$this->db->escape($elementtype)."'";
  712. } else {
  713. // We want on all entities ($entities = '0'), we delete on all only (we keep setup specific to each entity)
  714. $sql_del = "DELETE FROM ".$this->db->prefix()."extrafields";
  715. $sql_del .= " WHERE name = '".$this->db->escape($attrname)."'";
  716. $sql_del .= " AND entity = 0";
  717. $sql_del .= " AND elementtype = '".$this->db->escape($elementtype)."'";
  718. }
  719. $resql1 = $this->db->query($sql_del);
  720. $sql = "INSERT INTO ".$this->db->prefix()."extrafields(";
  721. $sql .= " name,"; // This is code
  722. $sql .= " entity,";
  723. $sql .= " label,";
  724. $sql .= " type,";
  725. $sql .= " size,";
  726. $sql .= " elementtype,";
  727. $sql .= " fieldunique,";
  728. $sql .= " fieldrequired,";
  729. $sql .= " perms,";
  730. $sql .= " langs,";
  731. $sql .= " pos,";
  732. $sql .= " alwayseditable,";
  733. $sql .= " param,";
  734. $sql .= " list,";
  735. $sql .= " printable,";
  736. $sql .= " totalizable,";
  737. $sql .= " fielddefault,";
  738. $sql .= " fieldcomputed,";
  739. $sql .= " fk_user_author,";
  740. $sql .= " fk_user_modif,";
  741. $sql .= " datec,";
  742. $sql .= " enabled,";
  743. $sql .= " help,";
  744. $sql .= " css,";
  745. $sql .= " csslist,";
  746. $sql .= " cssview";
  747. $sql .= ") VALUES (";
  748. $sql .= "'".$this->db->escape($attrname)."',";
  749. $sql .= " ".($entity === '' ? $conf->entity : $entity).",";
  750. $sql .= " '".$this->db->escape($label)."',";
  751. $sql .= " '".$this->db->escape($type)."',";
  752. $sql .= " '".$this->db->escape($size)."',";
  753. $sql .= " '".$this->db->escape($elementtype)."',";
  754. $sql .= " ".$unique.",";
  755. $sql .= " ".$required.",";
  756. $sql .= " ".($perms ? "'".$this->db->escape($perms)."'" : "null").",";
  757. $sql .= " ".($langfile ? "'".$this->db->escape($langfile)."'" : "null").",";
  758. $sql .= " ".$pos.",";
  759. $sql .= " '".$this->db->escape($alwayseditable)."',";
  760. $sql .= " '".$this->db->escape($params)."',";
  761. $sql .= " '".$this->db->escape($list)."', ";
  762. $sql .= " '".$this->db->escape($printable)."', ";
  763. $sql .= " ".($totalizable ? 'TRUE' : 'FALSE').",";
  764. $sql .= " ".(($default != '') ? "'".$this->db->escape($default)."'" : "null").",";
  765. $sql .= " ".($computed ? "'".$this->db->escape($computed)."'" : "null").",";
  766. $sql .= " ".$user->id.",";
  767. $sql .= " ".$user->id.",";
  768. $sql .= "'".$this->db->idate(dol_now())."',";
  769. $sql .= "'".$this->db->escape($enabled)."',";
  770. $sql .= " ".($help ? "'".$this->db->escape($help)."'" : "null").",";
  771. $sql .= " ".($css ? "'".$this->db->escape($css)."'" : "null").",";
  772. $sql .= " ".($csslist ? "'".$this->db->escape($csslist)."'" : "null").",";
  773. $sql .= " ".($cssview ? "'".$this->db->escape($cssview)."'" : "null");
  774. $sql .= ")";
  775. $resql2 = $this->db->query($sql);
  776. if ($resql1 && $resql2) {
  777. $this->db->commit();
  778. return 1;
  779. } else {
  780. $this->db->rollback();
  781. dol_print_error($this->db);
  782. return -1;
  783. }
  784. } else {
  785. return 0;
  786. }
  787. }
  788. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  789. /**
  790. * Load array this->attributes
  791. *
  792. * @param string $elementtype Type of element ('' = all or $object->table_element like 'adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...).
  793. * @param boolean $forceload Force load of extra fields whatever is status of cache.
  794. * @return array Array of attributes keys+label for all extra fields.
  795. */
  796. public function fetch_name_optionals_label($elementtype, $forceload = false)
  797. {
  798. // phpcs:enable
  799. global $conf;
  800. if (empty($elementtype)) {
  801. return array();
  802. }
  803. if ($elementtype == 'thirdparty') {
  804. $elementtype = 'societe';
  805. }
  806. if ($elementtype == 'contact') {
  807. $elementtype = 'socpeople';
  808. }
  809. if ($elementtype == 'order_supplier') {
  810. $elementtype = 'commande_fournisseur';
  811. }
  812. $array_name_label = array();
  813. // We should not have several time this request. If we have, there is some optimization to do by calling a simple $extrafields->fetch_optionals() in top of code and not into subcode
  814. $sql = "SELECT rowid, name, label, type, size, elementtype, fieldunique, fieldrequired, param, pos, alwayseditable, perms, langs, list, printable, totalizable, fielddefault, fieldcomputed, entity, enabled, help,";
  815. $sql .= " css, cssview, csslist";
  816. $sql .= " FROM ".$this->db->prefix()."extrafields";
  817. //$sql.= " WHERE entity IN (0,".$conf->entity.")"; // Filter is done later
  818. if ($elementtype) {
  819. $sql .= " WHERE elementtype = '".$this->db->escape($elementtype)."'"; // Filed with object->table_element
  820. }
  821. $sql .= " ORDER BY pos";
  822. $resql = $this->db->query($sql);
  823. if ($resql) {
  824. $count = 0;
  825. if ($this->db->num_rows($resql)) {
  826. while ($tab = $this->db->fetch_object($resql)) {
  827. if ($tab->entity != 0 && $tab->entity != $conf->entity) {
  828. // This field is not in current entity. We discard but before we save it into the array of mandatory fields if it is a mandatory field without default value
  829. if ($tab->fieldrequired && is_null($tab->fielddefault)) {
  830. $this->attributes[$tab->elementtype]['mandatoryfieldsofotherentities'][$tab->name] = $tab->type;
  831. }
  832. continue;
  833. }
  834. // We can add this attribute to object. TODO Remove this and return $this->attributes[$elementtype]['label']
  835. if ($tab->type != 'separate') {
  836. $array_name_label[$tab->name] = $tab->label;
  837. }
  838. // Old usage
  839. $this->attribute_type[$tab->name] = $tab->type;
  840. $this->attribute_label[$tab->name] = $tab->label;
  841. $this->attributes[$tab->elementtype]['type'][$tab->name] = $tab->type;
  842. $this->attributes[$tab->elementtype]['label'][$tab->name] = $tab->label;
  843. $this->attributes[$tab->elementtype]['size'][$tab->name] = $tab->size;
  844. $this->attributes[$tab->elementtype]['elementtype'][$tab->name] = $tab->elementtype;
  845. $this->attributes[$tab->elementtype]['default'][$tab->name] = $tab->fielddefault;
  846. $this->attributes[$tab->elementtype]['computed'][$tab->name] = $tab->fieldcomputed;
  847. $this->attributes[$tab->elementtype]['unique'][$tab->name] = $tab->fieldunique;
  848. $this->attributes[$tab->elementtype]['required'][$tab->name] = $tab->fieldrequired;
  849. $this->attributes[$tab->elementtype]['param'][$tab->name] = ($tab->param ? jsonOrUnserialize($tab->param) : '');
  850. $this->attributes[$tab->elementtype]['pos'][$tab->name] = $tab->pos;
  851. $this->attributes[$tab->elementtype]['alwayseditable'][$tab->name] = $tab->alwayseditable;
  852. $this->attributes[$tab->elementtype]['perms'][$tab->name] = ((is_null($tab->perms) || strlen($tab->perms) == 0) ? 1 : $tab->perms);
  853. $this->attributes[$tab->elementtype]['langfile'][$tab->name] = $tab->langs;
  854. $this->attributes[$tab->elementtype]['list'][$tab->name] = $tab->list;
  855. $this->attributes[$tab->elementtype]['printable'][$tab->name] = $tab->printable;
  856. $this->attributes[$tab->elementtype]['totalizable'][$tab->name] = ($tab->totalizable ? 1 : 0);
  857. $this->attributes[$tab->elementtype]['entityid'][$tab->name] = $tab->entity;
  858. $this->attributes[$tab->elementtype]['enabled'][$tab->name] = $tab->enabled;
  859. $this->attributes[$tab->elementtype]['help'][$tab->name] = $tab->help;
  860. $this->attributes[$tab->elementtype]['css'][$tab->name] = $tab->css;
  861. $this->attributes[$tab->elementtype]['cssview'][$tab->name] = $tab->cssview;
  862. $this->attributes[$tab->elementtype]['csslist'][$tab->name] = $tab->csslist;
  863. $this->attributes[$tab->elementtype]['loaded'] = 1;
  864. $count++;
  865. }
  866. }
  867. if ($elementtype) {
  868. $this->attributes[$elementtype]['loaded'] = 1; // If nothing found, we also save tag 'loaded'
  869. $this->attributes[$elementtype]['count'] = $count;
  870. }
  871. } else {
  872. $this->error = $this->db->lasterror();
  873. dol_syslog(get_class($this)."::fetch_name_optionals_label ".$this->error, LOG_ERR);
  874. }
  875. return $array_name_label;
  876. }
  877. /**
  878. * Return HTML string to put an input field into a page
  879. * Code very similar with showInputField of common object
  880. *
  881. * @param string $key Key of attribute
  882. * @param string|array $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); for dates in filter mode, a range array('start'=><timestamp>, 'end'=><timestamp>) should be provided
  883. * @param string $moreparam To add more parameters on html input tag
  884. * @param string $keysuffix Prefix string to add after name and id of field (can be used to avoid duplicate names)
  885. * @param string $keyprefix Suffix string to add before name and id of field (can be used to avoid duplicate names)
  886. * @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric)
  887. * @param int $objectid Current object id
  888. * @param string $extrafieldsobjectkey The key to use to store retreived data (for example $object->table_element)
  889. * @param string $mode 1=Used for search filters
  890. * @return string
  891. */
  892. public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $extrafieldsobjectkey = '', $mode = 0)
  893. {
  894. global $conf, $langs, $form;
  895. if (!is_object($form)) {
  896. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  897. $form = new Form($this->db);
  898. }
  899. $out = '';
  900. if (!preg_match('/options_$/', $keyprefix)) { // Because we work on extrafields, we add 'options_' to prefix if not already added
  901. $keyprefix = $keyprefix.'options_';
  902. }
  903. if (empty($extrafieldsobjectkey)) {
  904. dol_syslog(get_class($this).'::showInputField extrafieldsobjectkey required', LOG_ERR);
  905. return 'BadValueForParamExtraFieldsObjectKey';
  906. }
  907. if (!empty($extrafieldsobjectkey)) {
  908. $label = $this->attributes[$extrafieldsobjectkey]['label'][$key];
  909. $type = $this->attributes[$extrafieldsobjectkey]['type'][$key];
  910. $size = $this->attributes[$extrafieldsobjectkey]['size'][$key];
  911. $default = $this->attributes[$extrafieldsobjectkey]['default'][$key];
  912. $computed = $this->attributes[$extrafieldsobjectkey]['computed'][$key];
  913. $unique = $this->attributes[$extrafieldsobjectkey]['unique'][$key];
  914. $required = $this->attributes[$extrafieldsobjectkey]['required'][$key];
  915. $param = $this->attributes[$extrafieldsobjectkey]['param'][$key];
  916. $perms = dol_eval($this->attributes[$extrafieldsobjectkey]['perms'][$key], 1, 1, '1');
  917. $langfile = $this->attributes[$extrafieldsobjectkey]['langfile'][$key];
  918. $list = dol_eval($this->attributes[$extrafieldsobjectkey]['list'][$key], 1, 1, '1');
  919. $totalizable = $this->attributes[$extrafieldsobjectkey]['totalizable'][$key];
  920. $help = $this->attributes[$extrafieldsobjectkey]['help'][$key];
  921. $hidden = (empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
  922. } else {
  923. // Old usage
  924. $label = $this->attribute_label[$key];
  925. $type = $this->attribute_type[$key];
  926. $list = $this->attribute_list[$key];
  927. $hidden = (empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
  928. }
  929. if ($computed) {
  930. if (!preg_match('/^search_/', $keyprefix)) {
  931. return '<span class="opacitymedium">'.$langs->trans("AutomaticallyCalculated").'</span>';
  932. } else {
  933. return '';
  934. }
  935. }
  936. //
  937. // 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'css'=>'minwidth300 maxwidth500 widthcentpercentminusx', 'cssview'=>'wordbreak', 'csslist'=>'tdoverflowmax200'
  938. if (empty($morecss)) {
  939. // Add automatic css
  940. if ($type == 'date') {
  941. $morecss = 'minwidth100imp';
  942. } elseif ($type == 'datetime' || $type == 'link') {
  943. $morecss = 'minwidth200imp';
  944. } elseif (in_array($type, array('int', 'integer', 'double', 'price'))) {
  945. $morecss = 'maxwidth75';
  946. } elseif ($type == 'password') {
  947. $morecss = 'maxwidth100';
  948. } elseif ($type == 'url') {
  949. $morecss = 'minwidth400';
  950. } elseif ($type == 'boolean') {
  951. $morecss = '';
  952. } elseif ($type == 'radio') {
  953. $morecss = 'width25';
  954. } else {
  955. if (empty($size) || round($size) < 12) {
  956. $morecss = 'minwidth100';
  957. } elseif (round($size) <= 48) {
  958. $morecss = 'minwidth200';
  959. } else {
  960. $morecss = 'minwidth400';
  961. }
  962. }
  963. // If css forced in attribute, we use this one
  964. if (!empty($this->attributes[$extrafieldsobjectkey]['css'][$key])) {
  965. $morecss = $this->attributes[$extrafieldsobjectkey]['css'][$key];
  966. }
  967. }
  968. if (in_array($type, array('date'))) {
  969. $tmp = explode(',', $size);
  970. $newsize = $tmp[0];
  971. $showtime = 0;
  972. // Do not show current date when field not required (see selectDate() method)
  973. if (!$required && $value == '') {
  974. $value = '-1';
  975. }
  976. if ($mode == 1) {
  977. // search filter on a date extrafield shows two inputs to select a date range
  978. $prefill = array(
  979. 'start' => isset($value['start']) ? $value['start'] : '',
  980. 'end' => isset($value['end']) ? $value['end'] : ''
  981. );
  982. $out = '<div ' . ($moreparam ? $moreparam : '') . '><div class="nowrap">';
  983. $out .= $form->selectDate($prefill['start'], $keyprefix.$key.$keysuffix.'_start', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("From"));
  984. $out .= '</div><div class="nowrap">';
  985. $out .= $form->selectDate($prefill['end'], $keyprefix.$key.$keysuffix.'_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("to"));
  986. $out .= '</div></div>';
  987. } else {
  988. // TODO Must also support $moreparam
  989. $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $showtime, $required, '', 1, (($keyprefix != 'search_' && $keyprefix != 'search_options_') ? 1 : 0), 0, 1);
  990. }
  991. } elseif (in_array($type, array('datetime'))) {
  992. $tmp = explode(',', $size);
  993. $newsize = $tmp[0];
  994. $showtime = 1;
  995. // Do not show current date when field not required (see selectDate() method)
  996. if (!$required && $value == '') {
  997. $value = '-1';
  998. }
  999. if ($mode == 1) {
  1000. // search filter on a date extrafield shows two inputs to select a date range
  1001. $prefill = array(
  1002. 'start' => isset($value['start']) ? $value['start'] : '',
  1003. 'end' => isset($value['end']) ? $value['end'] : ''
  1004. );
  1005. $out = '<div ' . ($moreparam ? $moreparam : '') . '><div class="nowrap">';
  1006. $out .= $form->selectDate($prefill['start'], $keyprefix.$key.$keysuffix.'_start', 1, 1, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("From"), 'tzuserrel');
  1007. $out .= '</div><div class="nowrap">';
  1008. $out .= $form->selectDate($prefill['end'], $keyprefix.$key.$keysuffix.'_end', 1, 1, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("to"), 'tzuserrel');
  1009. $out .= '</div></div>';
  1010. } else {
  1011. // TODO Must also support $moreparam
  1012. $out = $form->selectDate($value, $keyprefix.$key.$keysuffix, $showtime, $showtime, $required, '', 1, (($keyprefix != 'search_' && $keyprefix != 'search_options_') ? 1 : 0), 0, 1, '', '', '', 1, '', '', 'tzuserrel');
  1013. }
  1014. } elseif (in_array($type, array('int', 'integer'))) {
  1015. $tmp = explode(',', $size);
  1016. $newsize = $tmp[0];
  1017. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" maxlength="'.$newsize.'" value="'.dol_escape_htmltag($value).'"'.($moreparam ? $moreparam : '').'>';
  1018. } elseif (preg_match('/varchar/', $type)) {
  1019. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" maxlength="'.$size.'" value="'.dol_escape_htmltag($value).'"'.($moreparam ? $moreparam : '').'>';
  1020. } elseif (in_array($type, array('mail', 'ip', 'phone', 'url'))) {
  1021. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.dol_escape_htmltag($value).'" '.($moreparam ? $moreparam : '').'>';
  1022. } elseif ($type == 'text') {
  1023. if (!preg_match('/search_/', $keyprefix)) { // If keyprefix is search_ or search_options_, we must just use a simple text field
  1024. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1025. $doleditor = new DolEditor($keyprefix.$key.$keysuffix, $value, '', 200, 'dolibarr_notes', 'In', false, false, false, ROWS_5, '90%');
  1026. $out = $doleditor->Create(1);
  1027. } else {
  1028. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.dol_escape_htmltag($value).'" '.($moreparam ? $moreparam : '').'>';
  1029. }
  1030. } elseif ($type == 'html') {
  1031. if (!preg_match('/search_/', $keyprefix)) { // If keyprefix is search_ or search_options_, we must just use a simple text field
  1032. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  1033. $doleditor = new DolEditor($keyprefix.$key.$keysuffix, $value, '', 200, 'dolibarr_notes', 'In', false, false, isModEnabled('fckeditor') && $conf->global->FCKEDITOR_ENABLE_SOCIETE, ROWS_5, '90%');
  1034. $out = $doleditor->Create(1);
  1035. } else {
  1036. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.dol_escape_htmltag($value).'" '.($moreparam ? $moreparam : '').'>';
  1037. }
  1038. } elseif ($type == 'boolean') {
  1039. if (empty($mode)) {
  1040. $checked = '';
  1041. if (!empty($value)) {
  1042. $checked = ' checked value="1" ';
  1043. } else {
  1044. $checked = ' value="1" ';
  1045. }
  1046. $out = '<input type="checkbox" class="flat valignmiddle'.($morecss ? ' '.$morecss : '').' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" '.$checked.' '.($moreparam ? $moreparam : '').'>';
  1047. } else {
  1048. $out .= $form->selectyesno($keyprefix.$key.$keysuffix, $value, 1, false, 1);
  1049. }
  1050. } elseif ($type == 'price') {
  1051. if (!empty($value)) { // $value in memory is a php numeric, we format it into user number format.
  1052. $value = price($value);
  1053. }
  1054. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.$value.'" '.($moreparam ? $moreparam : '').'> '.$langs->getCurrencySymbol($conf->currency);
  1055. } elseif ($type == 'pricecy') {
  1056. $currency = $conf->currency;
  1057. if (!empty($value)) {
  1058. // $value in memory is a php string like '10.01:USD'
  1059. $pricetmp = explode(':', $value);
  1060. $currency = !empty($pricetmp[1]) ? $pricetmp[1] : $conf->currency;
  1061. $value = price($pricetmp[0]);
  1062. }
  1063. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.$value.'" '.($moreparam ? $moreparam : '').'> ';
  1064. $out .= $form->selectCurrency($currency, $keyprefix.$key.$keysuffix.'currency_id');
  1065. } elseif ($type == 'double') {
  1066. if (!empty($value)) { // $value in memory is a php numeric, we format it into user number format.
  1067. $value = price($value);
  1068. }
  1069. $out = '<input type="text" class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.$value.'" '.($moreparam ? $moreparam : '').'> ';
  1070. } elseif ($type == 'select') {
  1071. $out = '';
  1072. if ($mode) {
  1073. $options = array();
  1074. foreach ($param['options'] as $okey => $val) {
  1075. if ((string) $okey == '') {
  1076. continue;
  1077. }
  1078. if ($langfile && $val) {
  1079. $options[$okey] = $langs->trans($val);
  1080. } else {
  1081. $options[$okey] = $val;
  1082. }
  1083. }
  1084. $selected = array();
  1085. if (!is_array($value)) {
  1086. $selected = explode(',', $value);
  1087. }
  1088. $out .= $form->multiselectarray($keyprefix.$key.$keysuffix, $options, $selected, 0, 0, $morecss, 0, 0, '', '', '', !empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_EXTRAFIELDS_DISABLE_SELECT2));
  1089. } else {
  1090. if (!empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_EXTRAFIELDS_DISABLE_SELECT2)) {
  1091. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  1092. $out .= ajax_combobox($keyprefix.$key.$keysuffix, array(), 0);
  1093. }
  1094. $out .= '<select class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" '.($moreparam ? $moreparam : '').'>';
  1095. $out .= '<option value="0">&nbsp;</option>';
  1096. foreach ($param['options'] as $key => $val) {
  1097. if ((string) $key == '') {
  1098. continue;
  1099. }
  1100. $valarray = explode('|', $val);
  1101. $val = $valarray[0];
  1102. $parent = '';
  1103. if (!empty($valarray[1])) {
  1104. $parent = $valarray[1];
  1105. }
  1106. $out .= '<option value="'.$key.'"';
  1107. $out .= (((string) $value == (string) $key) ? ' selected' : '');
  1108. $out .= (!empty($parent) ? ' parent="'.$parent.'"' : '');
  1109. $out .= '>';
  1110. if ($langfile && $val) {
  1111. $out .= $langs->trans($val);
  1112. } else {
  1113. $out .= $val;
  1114. }
  1115. $out .= '</option>';
  1116. }
  1117. $out .= '</select>';
  1118. }
  1119. } elseif ($type == 'sellist') {
  1120. $out = '';
  1121. if (!empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_EXTRAFIELDS_DISABLE_SELECT2)) {
  1122. include_once DOL_DOCUMENT_ROOT.'/core/lib/ajax.lib.php';
  1123. $out .= ajax_combobox($keyprefix.$key.$keysuffix, array(), 0);
  1124. }
  1125. $out .= '<select class="flat '.$morecss.' maxwidthonsmartphone" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" '.($moreparam ? $moreparam : '').'>';
  1126. if (is_array($param['options'])) {
  1127. $param_list = array_keys($param['options']);
  1128. $InfoFieldList = explode(":", $param_list[0]);
  1129. $parentName = '';
  1130. $parentField = '';
  1131. // 0 : tableName
  1132. // 1 : label field name
  1133. // 2 : key fields name (if differ of rowid)
  1134. // 3 : key field parent (for dependent lists)
  1135. // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
  1136. // 5 : id category type
  1137. // 6 : ids categories list separated by comma for category root
  1138. $keyList = (empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2].' as rowid');
  1139. if (count($InfoFieldList) > 4 && !empty($InfoFieldList[4])) {
  1140. if (strpos($InfoFieldList[4], 'extra.') !== false) {
  1141. $keyList = 'main.'.$InfoFieldList[2].' as rowid';
  1142. } else {
  1143. $keyList = $InfoFieldList[2].' as rowid';
  1144. }
  1145. }
  1146. if (count($InfoFieldList) > 3 && !empty($InfoFieldList[3])) {
  1147. list($parentName, $parentField) = explode('|', $InfoFieldList[3]);
  1148. $keyList .= ', '.$parentField;
  1149. }
  1150. $filter_categorie = false;
  1151. if (count($InfoFieldList) > 5) {
  1152. if ($InfoFieldList[0] == 'categorie') {
  1153. $filter_categorie = true;
  1154. }
  1155. }
  1156. if ($filter_categorie === false) {
  1157. $fields_label = explode('|', $InfoFieldList[1]);
  1158. if (is_array($fields_label)) {
  1159. $keyList .= ', ';
  1160. $keyList .= implode(', ', $fields_label);
  1161. }
  1162. $sqlwhere = '';
  1163. $sql = "SELECT ".$keyList;
  1164. $sql .= ' FROM '.$this->db->prefix().$InfoFieldList[0];
  1165. if (!empty($InfoFieldList[4])) {
  1166. // can use current entity filter
  1167. if (strpos($InfoFieldList[4], '$ENTITY$') !== false) {
  1168. $InfoFieldList[4] = str_replace('$ENTITY$', $conf->entity, $InfoFieldList[4]);
  1169. }
  1170. // can use SELECT request
  1171. if (strpos($InfoFieldList[4], '$SEL$') !== false) {
  1172. $InfoFieldList[4] = str_replace('$SEL$', 'SELECT', $InfoFieldList[4]);
  1173. }
  1174. // current object id can be use into filter
  1175. if (strpos($InfoFieldList[4], '$ID$') !== false && !empty($objectid)) {
  1176. $InfoFieldList[4] = str_replace('$ID$', $objectid, $InfoFieldList[4]);
  1177. } else {
  1178. $InfoFieldList[4] = str_replace('$ID$', '0', $InfoFieldList[4]);
  1179. }
  1180. //We have to join on extrafield table
  1181. if (strpos($InfoFieldList[4], 'extra.') !== false) {
  1182. $sql .= ' as main, '.$this->db->prefix().$InfoFieldList[0].'_extrafields as extra';
  1183. $sqlwhere .= " WHERE extra.fk_object=main.".$InfoFieldList[2]." AND ".$InfoFieldList[4];
  1184. } else {
  1185. $sqlwhere .= " WHERE ".$InfoFieldList[4];
  1186. }
  1187. } else {
  1188. $sqlwhere .= ' WHERE 1=1';
  1189. }
  1190. // Some tables may have field, some other not. For the moment we disable it.
  1191. if (in_array($InfoFieldList[0], array('tablewithentity'))) {
  1192. $sqlwhere .= ' AND entity = '.((int) $conf->entity);
  1193. }
  1194. $sql .= $sqlwhere;
  1195. //print $sql;
  1196. $sql .= ' ORDER BY '.implode(', ', $fields_label);
  1197. dol_syslog(get_class($this).'::showInputField type=sellist', LOG_DEBUG);
  1198. $resql = $this->db->query($sql);
  1199. if ($resql) {
  1200. $out .= '<option value="0">&nbsp;</option>';
  1201. $num = $this->db->num_rows($resql);
  1202. $i = 0;
  1203. while ($i < $num) {
  1204. $labeltoshow = '';
  1205. $obj = $this->db->fetch_object($resql);
  1206. // Several field into label (eq table:code|libelle:rowid)
  1207. $notrans = false;
  1208. $fields_label = explode('|', $InfoFieldList[1]);
  1209. if (is_array($fields_label) && count($fields_label) > 1) {
  1210. $notrans = true;
  1211. foreach ($fields_label as $field_toshow) {
  1212. $labeltoshow .= $obj->$field_toshow.' ';
  1213. }
  1214. } else {
  1215. $labeltoshow = $obj->{$InfoFieldList[1]};
  1216. }
  1217. $labeltoshow = $labeltoshow;
  1218. if ($value == $obj->rowid) {
  1219. if (!$notrans) {
  1220. foreach ($fields_label as $field_toshow) {
  1221. $translabel = $langs->trans($obj->$field_toshow);
  1222. $labeltoshow = $translabel.' ';
  1223. }
  1224. }
  1225. $out .= '<option value="'.$obj->rowid.'" selected>'.$labeltoshow.'</option>';
  1226. } else {
  1227. if (!$notrans) {
  1228. $translabel = $langs->trans($obj->{$InfoFieldList[1]});
  1229. $labeltoshow = $translabel;
  1230. }
  1231. if (empty($labeltoshow)) {
  1232. $labeltoshow = '(not defined)';
  1233. }
  1234. if (!empty($InfoFieldList[3]) && $parentField) {
  1235. $parent = $parentName.':'.$obj->{$parentField};
  1236. }
  1237. $out .= '<option value="'.$obj->rowid.'"';
  1238. $out .= ($value == $obj->rowid ? ' selected' : '');
  1239. $out .= (!empty($parent) ? ' parent="'.$parent.'"' : '');
  1240. $out .= '>'.$labeltoshow.'</option>';
  1241. }
  1242. $i++;
  1243. }
  1244. $this->db->free($resql);
  1245. } else {
  1246. print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.<br>';
  1247. }
  1248. } else {
  1249. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1250. $data = $form->select_all_categories(Categorie::$MAP_ID_TO_CODE[$InfoFieldList[5]], '', 'parent', 64, $InfoFieldList[6], 1, 1);
  1251. $out .= '<option value="0">&nbsp;</option>';
  1252. if (is_array($data)) {
  1253. foreach ($data as $data_key => $data_value) {
  1254. $out .= '<option value="'.$data_key.'"';
  1255. $out .= ($value == $data_key ? ' selected' : '');
  1256. $out .= '>'.$data_value.'</option>';
  1257. }
  1258. }
  1259. }
  1260. }
  1261. $out .= '</select>';
  1262. } elseif ($type == 'checkbox') {
  1263. $value_arr = $value;
  1264. if (!is_array($value)) {
  1265. $value_arr = explode(',', $value);
  1266. }
  1267. $out = $form->multiselectarray($keyprefix.$key.$keysuffix, (empty($param['options']) ?null:$param['options']), $value_arr, '', 0, '', 0, '100%');
  1268. } elseif ($type == 'radio') {
  1269. $out = '';
  1270. foreach ($param['options'] as $keyopt => $val) {
  1271. $out .= '<input class="flat '.$morecss.'" type="radio" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" '.($moreparam ? $moreparam : '');
  1272. $out .= ' value="'.$keyopt.'"';
  1273. $out .= ' id="'.$keyprefix.$key.$keysuffix.'_'.$keyopt.'"';
  1274. $out .= ($value == $keyopt ? 'checked' : '');
  1275. $out .= '/><label for="'.$keyprefix.$key.$keysuffix.'_'.$keyopt.'">'.$langs->trans($val).'</label><br>';
  1276. }
  1277. } elseif ($type == 'chkbxlst') {
  1278. if (is_array($value)) {
  1279. $value_arr = $value;
  1280. } else {
  1281. $value_arr = explode(',', $value);
  1282. }
  1283. if (is_array($param['options'])) {
  1284. $param_list = array_keys($param['options']);
  1285. $InfoFieldList = explode(":", $param_list[0]);
  1286. $parentName = '';
  1287. $parentField = '';
  1288. // 0 : tableName
  1289. // 1 : label field name
  1290. // 2 : key fields name (if differ of rowid)
  1291. // 3 : key field parent (for dependent lists)
  1292. // 4 : where clause filter on column or table extrafield, syntax field='value' or extra.field=value
  1293. // 5 : id category type
  1294. // 6 : ids categories list separated by comma for category root
  1295. $keyList = (empty($InfoFieldList[2]) ? 'rowid' : $InfoFieldList[2].' as rowid');
  1296. if (count($InfoFieldList) > 3 && !empty($InfoFieldList[3])) {
  1297. list ($parentName, $parentField) = explode('|', $InfoFieldList[3]);
  1298. $keyList .= ', '.$parentField;
  1299. }
  1300. if (count($InfoFieldList) > 4 && !empty($InfoFieldList[4])) {
  1301. if (strpos($InfoFieldList[4], 'extra.') !== false) {
  1302. $keyList = 'main.'.$InfoFieldList[2].' as rowid';
  1303. } else {
  1304. $keyList = $InfoFieldList[2].' as rowid';
  1305. }
  1306. }
  1307. $filter_categorie = false;
  1308. if (count($InfoFieldList) > 5) {
  1309. if ($InfoFieldList[0] == 'categorie') {
  1310. $filter_categorie = true;
  1311. }
  1312. }
  1313. if ($filter_categorie === false) {
  1314. $fields_label = explode('|', $InfoFieldList[1]);
  1315. if (is_array($fields_label)) {
  1316. $keyList .= ', ';
  1317. $keyList .= implode(', ', $fields_label);
  1318. }
  1319. $sqlwhere = '';
  1320. $sql = "SELECT ".$keyList;
  1321. $sql .= ' FROM '.$this->db->prefix().$InfoFieldList[0];
  1322. if (!empty($InfoFieldList[4])) {
  1323. // can use current entity filter
  1324. if (strpos($InfoFieldList[4], '$ENTITY$') !== false) {
  1325. $InfoFieldList[4] = str_replace('$ENTITY$', $conf->entity, $InfoFieldList[4]);
  1326. }
  1327. // can use SELECT request
  1328. if (strpos($InfoFieldList[4], '$SEL$') !== false) {
  1329. $InfoFieldList[4] = str_replace('$SEL$', 'SELECT', $InfoFieldList[4]);
  1330. }
  1331. // current object id can be use into filter
  1332. if (strpos($InfoFieldList[4], '$ID$') !== false && !empty($objectid)) {
  1333. $InfoFieldList[4] = str_replace('$ID$', $objectid, $InfoFieldList[4]);
  1334. } elseif (preg_match("#^.*list.php$#", $_SERVER["PHP_SELF"])) {
  1335. // Pattern for word=$ID$
  1336. $word = '\b[a-zA-Z0-9-\.-_]+\b=\$ID\$';
  1337. // Removing space arount =, ( and )
  1338. $InfoFieldList[4] = preg_replace('# *(=|\(|\)) *#', '$1', $InfoFieldList[4]);
  1339. $nbPreg = 1;
  1340. // While we have parenthesis
  1341. while ($nbPreg != 0) {
  1342. // Init des compteurs
  1343. $nbPregRepl = $nbPregSel = 0;
  1344. // On retire toutes les parenthèses sans = avant
  1345. $InfoFieldList[4] = preg_replace('#([^=])(\([^)^(]*('.$word.')[^)^(]*\))#', '$1 $3 ', $InfoFieldList[4], -1, $nbPregRepl);
  1346. // On retire les espaces autour des = et parenthèses
  1347. $InfoFieldList[4] = preg_replace('# *(=|\(|\)) *#', '$1', $InfoFieldList[4]);
  1348. // On retire toutes les parenthèses avec = avant
  1349. $InfoFieldList[4] = preg_replace('#\b[a-zA-Z0-9-\.-_]+\b=\([^)^(]*('.$word.')[^)^(]*\)#', '$1 ', $InfoFieldList[4], -1, $nbPregSel);
  1350. // On retire les espaces autour des = et parenthèses
  1351. $InfoFieldList[4] = preg_replace('# *(=|\(|\)) *#', '$1', $InfoFieldList[4]);
  1352. // Calcul du compteur général pour la boucle
  1353. $nbPreg = $nbPregRepl + $nbPregSel;
  1354. }
  1355. // Si l'on a un AND ou un OR, avant ou après
  1356. preg_match('#(AND|OR|) *('.$word.') *(AND|OR|)#', $InfoFieldList[4], $matchCondition);
  1357. while (!empty($matchCondition[0])) {
  1358. // If the two sides differ but are not empty
  1359. if (!empty($matchCondition[1]) && !empty($matchCondition[3]) && $matchCondition[1] != $matchCondition[3]) {
  1360. // Nobody sain would do that without parentheses
  1361. $InfoFieldList[4] = str_replace('$ID$', '0', $InfoFieldList[4]);
  1362. } else {
  1363. if (!empty($matchCondition[1])) {
  1364. $boolCond = (($matchCondition[1] == "AND") ? ' AND TRUE ' : ' OR FALSE ');
  1365. $InfoFieldList[4] = str_replace($matchCondition[0], $boolCond.$matchCondition[3], $InfoFieldList[4]);
  1366. } elseif (!empty($matchCondition[3])) {
  1367. $boolCond = (($matchCondition[3] == "AND") ? ' TRUE AND ' : ' FALSE OR');
  1368. $InfoFieldList[4] = str_replace($matchCondition[0], $boolCond, $InfoFieldList[4]);
  1369. } else {
  1370. $InfoFieldList[4] = " TRUE ";
  1371. }
  1372. }
  1373. // Si l'on a un AND ou un OR, avant ou après
  1374. preg_match('#(AND|OR|) *('.$word.') *(AND|OR|)#', $InfoFieldList[4], $matchCondition);
  1375. }
  1376. } else {
  1377. $InfoFieldList[4] = str_replace('$ID$', '0', $InfoFieldList[4]);
  1378. }
  1379. // We have to join on extrafield table
  1380. if (strpos($InfoFieldList[4], 'extra.') !== false) {
  1381. $sql .= ' as main, '.$this->db->prefix().$InfoFieldList[0].'_extrafields as extra';
  1382. $sqlwhere .= " WHERE extra.fk_object=main.".$InfoFieldList[2]." AND ".$InfoFieldList[4];
  1383. } else {
  1384. $sqlwhere .= " WHERE ".$InfoFieldList[4];
  1385. }
  1386. } else {
  1387. $sqlwhere .= ' WHERE 1=1';
  1388. }
  1389. // Some tables may have field, some other not. For the moment we disable it.
  1390. if (in_array($InfoFieldList[0], array('tablewithentity'))) {
  1391. $sqlwhere .= " AND entity = ".((int) $conf->entity);
  1392. }
  1393. // $sql.=preg_replace('/^ AND /','',$sqlwhere);
  1394. // print $sql;
  1395. $sql .= $sqlwhere;
  1396. dol_syslog(get_class($this).'::showInputField type=chkbxlst', LOG_DEBUG);
  1397. $resql = $this->db->query($sql);
  1398. if ($resql) {
  1399. $num = $this->db->num_rows($resql);
  1400. $i = 0;
  1401. $data = array();
  1402. while ($i < $num) {
  1403. $labeltoshow = '';
  1404. $obj = $this->db->fetch_object($resql);
  1405. $notrans = false;
  1406. // Several field into label (eq table:code|libelle:rowid)
  1407. $fields_label = explode('|', $InfoFieldList[1]);
  1408. if (is_array($fields_label)) {
  1409. $notrans = true;
  1410. foreach ($fields_label as $field_toshow) {
  1411. $labeltoshow .= $obj->$field_toshow.' ';
  1412. }
  1413. } else {
  1414. $labeltoshow = $obj->{$InfoFieldList[1]};
  1415. }
  1416. $labeltoshow = dol_trunc($labeltoshow, 45);
  1417. if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
  1418. $labeltoshow = '';
  1419. foreach ($fields_label as $field_toshow) {
  1420. $translabel = $langs->trans($obj->$field_toshow);
  1421. if ($translabel != $obj->$field_toshow) {
  1422. $labeltoshow .= ' '.dol_trunc($translabel, 18).' ';
  1423. } else {
  1424. $labeltoshow .= ' '.dol_trunc($obj->$field_toshow, 18).' ';
  1425. }
  1426. }
  1427. $data[$obj->rowid] = $labeltoshow;
  1428. } else {
  1429. if (!$notrans) {
  1430. $translabel = $langs->trans($obj->{$InfoFieldList[1]});
  1431. if ($translabel != $obj->{$InfoFieldList[1]}) {
  1432. $labeltoshow = dol_trunc($translabel, 18);
  1433. } else {
  1434. $labeltoshow = dol_trunc($obj->{$InfoFieldList[1]}, 18);
  1435. }
  1436. }
  1437. if (empty($labeltoshow)) {
  1438. $labeltoshow = '(not defined)';
  1439. }
  1440. if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
  1441. $data[$obj->rowid] = $labeltoshow;
  1442. }
  1443. if (!empty($InfoFieldList[3]) && $parentField) {
  1444. $parent = $parentName.':'.$obj->{$parentField};
  1445. }
  1446. $data[$obj->rowid] = $labeltoshow;
  1447. }
  1448. $i++;
  1449. }
  1450. $this->db->free($resql);
  1451. $out = $form->multiselectarray($keyprefix.$key.$keysuffix, $data, $value_arr, '', 0, '', 0, '100%');
  1452. } else {
  1453. print 'Error in request '.$sql.' '.$this->db->lasterror().'. Check setup of extra parameters.<br>';
  1454. }
  1455. } else {
  1456. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1457. $data = $form->select_all_categories(Categorie::$MAP_ID_TO_CODE[$InfoFieldList[5]], '', 'parent', 64, $InfoFieldList[6], 1, 1);
  1458. $out = $form->multiselectarray($keyprefix.$key.$keysuffix, $data, $value_arr, '', 0, '', 0, '100%');
  1459. }
  1460. }
  1461. } elseif ($type == 'link') {
  1462. $param_list = array_keys($param['options']); // $param_list='ObjectName:classPath'
  1463. $showempty = (($required && $default != '') ? 0 : 1);
  1464. $out = $form->selectForForms($param_list[0], $keyprefix.$key.$keysuffix, $value, $showempty, '', '', $morecss);
  1465. } elseif ($type == 'password') {
  1466. // If prefix is 'search_', field is used as a filter, we use a common text field.
  1467. $out = '<input style="display:none" type="text" name="fakeusernameremembered">'; // Hidden field to reduce impact of evil Google Chrome autopopulate bug.
  1468. $out .= '<input autocomplete="new-password" type="'.($keyprefix == 'search_' ? 'text' : 'password').'" class="flat '.$morecss.'" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'" value="'.$value.'" '.($moreparam ? $moreparam : '').'>';
  1469. }
  1470. if (!empty($hidden)) {
  1471. $out = '<input type="hidden" value="'.$value.'" name="'.$keyprefix.$key.$keysuffix.'" id="'.$keyprefix.$key.$keysuffix.'"/>';
  1472. }
  1473. /* Add comments
  1474. if ($type == 'date') $out.=' (YYYY-MM-DD)';
  1475. elseif ($type == 'datetime') $out.=' (YYYY-MM-DD HH:MM:SS)';
  1476. */
  1477. /*if (!empty($help) && $keyprefix != 'search_options_') {
  1478. $out .= $form->textwithpicto('', $help, 1, 'help', '', 0, 3);
  1479. }*/
  1480. return $out;
  1481. }
  1482. /**
  1483. * Return HTML string to put an output field into a page
  1484. *
  1485. * @param string $key Key of attribute
  1486. * @param string $value Value to show
  1487. * @param string $moreparam To add more parameters on html input tag (only checkbox use html input for output rendering)
  1488. * @param string $extrafieldsobjectkey Required (for example $object->table_element).
  1489. * @return string Formated value
  1490. */
  1491. public function showOutputField($key, $value, $moreparam = '', $extrafieldsobjectkey = '')
  1492. {
  1493. global $conf, $langs;
  1494. if (empty($extrafieldsobjectkey)) {
  1495. dol_syslog(get_class($this).'::showOutputField extrafieldsobjectkey required', LOG_ERR);
  1496. return 'BadValueForParamExtraFieldsObjectKey';
  1497. }
  1498. $label = $this->attributes[$extrafieldsobjectkey]['label'][$key];
  1499. $type = $this->attributes[$extrafieldsobjectkey]['type'][$key];
  1500. $size = $this->attributes[$extrafieldsobjectkey]['size'][$key]; // Can be '255', '24,8'...
  1501. $default = $this->attributes[$extrafieldsobjectkey]['default'][$key];
  1502. $computed = $this->attributes[$extrafieldsobjectkey]['computed'][$key];
  1503. $unique = $this->attributes[$extrafieldsobjectkey]['unique'][$key];
  1504. $required = $this->attributes[$extrafieldsobjectkey]['required'][$key];
  1505. $param = $this->attributes[$extrafieldsobjectkey]['param'][$key];
  1506. $perms = dol_eval($this->attributes[$extrafieldsobjectkey]['perms'][$key], 1, 1, '1');
  1507. $langfile = $this->attributes[$extrafieldsobjectkey]['langfile'][$key];
  1508. $list = dol_eval($this->attributes[$extrafieldsobjectkey]['list'][$key], 1, 1, '1');
  1509. $help = $this->attributes[$extrafieldsobjectkey]['help'][$key];
  1510. $hidden = (empty($list) ? 1 : 0); // If $list empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
  1511. if ($hidden) {
  1512. return ''; // This is a protection. If field is hidden, we should just not call this method.
  1513. }
  1514. //if ($computed) $value = // $value is already calculated into $value before calling this method
  1515. $showsize = 0;
  1516. if ($type == 'date') {
  1517. $showsize = 10;
  1518. if ($value !== '') {
  1519. $value = dol_print_date($value, 'day'); // For date without hour, date is always GMT for storage and output
  1520. }
  1521. } elseif ($type == 'datetime') {
  1522. $showsize = 19;
  1523. if ($value !== '') {
  1524. $value = dol_print_date($value, 'dayhour', 'tzuserrel');
  1525. }
  1526. } elseif ($type == 'int') {
  1527. $showsize = 10;
  1528. } elseif ($type == 'double') {
  1529. if (!empty($value)) {
  1530. //$value=price($value);
  1531. $sizeparts = explode(",", $size);
  1532. $number_decimals = array_key_exists(1, $sizeparts) ? $sizeparts[1] : 0;
  1533. $value = price($value, 0, $langs, 0, 0, $number_decimals, '');
  1534. }
  1535. } elseif ($type == 'boolean') {
  1536. $checked = '';
  1537. if (!empty($value)) {
  1538. $checked = ' checked ';
  1539. }
  1540. $value = '<input type="checkbox" '.$checked.' '.($moreparam ? $moreparam : '').' readonly disabled>';
  1541. } elseif ($type == 'mail') {
  1542. $value = dol_print_email($value, 0, 0, 0, 64, 1, 1);
  1543. } elseif ($type == 'ip') {
  1544. $value = dol_print_ip($value, 0);
  1545. } elseif ($type == 'url') {
  1546. $value = dol_print_url($value, '_blank', 32, 1);
  1547. } elseif ($type == 'phone') {
  1548. $value = dol_print_phone($value, '', 0, 0, '', '&nbsp;', 'phone');
  1549. } elseif ($type == 'price') {
  1550. //$value = price($value, 0, $langs, 0, 0, -1, $conf->currency);
  1551. if ($value || $value == '0') {
  1552. $value = price($value, 0, $langs, 0, $conf->global->MAIN_MAX_DECIMALS_TOT, -1).' '.$langs->getCurrencySymbol($conf->currency);
  1553. }
  1554. } elseif ($type == 'pricecy') {
  1555. $currency = $conf->currency;
  1556. if (!empty($value)) {
  1557. // $value in memory is a php string like '0.01:EUR'
  1558. $pricetmp = explode(':', $value);
  1559. $currency = !empty($pricetmp[1]) ? $pricetmp[1] : $conf->currency;
  1560. $value = $pricetmp[0];
  1561. }
  1562. if ($value || $value == '0') {
  1563. $value = price($value, 0, $langs, 0, $conf->global->MAIN_MAX_DECIMALS_TOT, -1, $currency);
  1564. }
  1565. } elseif ($type == 'select') {
  1566. $valstr = (!empty($param['options'][$value]) ? $param['options'][$value] : '');
  1567. if (($pos = strpos($valstr, "|")) !== false) {
  1568. $valstr = substr($valstr, 0, $pos);
  1569. }
  1570. if ($langfile && $valstr) {
  1571. $value = $langs->trans($valstr);
  1572. } else {
  1573. $value = $valstr;
  1574. }
  1575. } elseif ($type == 'sellist') {
  1576. $param_list = array_keys($param['options']);
  1577. $InfoFieldList = explode(":", $param_list[0]);
  1578. $selectkey = "rowid";
  1579. $keyList = 'rowid';
  1580. if (count($InfoFieldList) >= 3) {
  1581. $selectkey = $InfoFieldList[2];
  1582. $keyList = $InfoFieldList[2].' as rowid';
  1583. }
  1584. $fields_label = explode('|', $InfoFieldList[1]);
  1585. if (is_array($fields_label)) {
  1586. $keyList .= ', ';
  1587. $keyList .= implode(', ', $fields_label);
  1588. }
  1589. $filter_categorie = false;
  1590. if (count($InfoFieldList) > 5) {
  1591. if ($InfoFieldList[0] == 'categorie') {
  1592. $filter_categorie = true;
  1593. }
  1594. }
  1595. $sql = "SELECT ".$keyList;
  1596. $sql .= ' FROM '.$this->db->prefix().$InfoFieldList[0];
  1597. if (!empty($InfoFieldList[4]) && strpos($InfoFieldList[4], 'extra.') !== false) {
  1598. $sql .= ' as main';
  1599. }
  1600. if ($selectkey == 'rowid' && empty($value)) {
  1601. $sql .= " WHERE ".$selectkey." = 0";
  1602. } elseif ($selectkey == 'rowid') {
  1603. $sql .= " WHERE ".$selectkey." = ".((int) $value);
  1604. } else {
  1605. $sql .= " WHERE ".$selectkey." = '".$this->db->escape($value)."'";
  1606. }
  1607. //$sql.= ' AND entity = '.$conf->entity;
  1608. dol_syslog(get_class($this).':showOutputField:$type=sellist', LOG_DEBUG);
  1609. $resql = $this->db->query($sql);
  1610. if ($resql) {
  1611. if ($filter_categorie === false) {
  1612. $value = ''; // value was used, so now we reste it to use it to build final output
  1613. $obj = $this->db->fetch_object($resql);
  1614. // Several field into label (eq table:code|libelle:rowid)
  1615. $fields_label = explode('|', $InfoFieldList[1]);
  1616. if (is_array($fields_label) && count($fields_label) > 1) {
  1617. foreach ($fields_label as $field_toshow) {
  1618. $translabel = '';
  1619. if (!empty($obj->$field_toshow)) {
  1620. $translabel = $langs->trans($obj->$field_toshow);
  1621. }
  1622. if ($translabel != $obj->$field_toshow) {
  1623. $value .= dol_trunc($translabel, 24).' ';
  1624. } else {
  1625. $value .= $obj->$field_toshow.' ';
  1626. }
  1627. }
  1628. } else {
  1629. $translabel = '';
  1630. $tmppropname = $InfoFieldList[1];
  1631. //$obj->$tmppropname = '';
  1632. if (!empty(isset($obj->$tmppropname) ? $obj->$tmppropname : '')) {
  1633. $translabel = $langs->trans($obj->$tmppropname);
  1634. }
  1635. if ($translabel != (isset($obj->$tmppropname) ? $obj->$tmppropname : '')) {
  1636. $value = dol_trunc($translabel, 18);
  1637. } else {
  1638. $value = isset($obj->$tmppropname) ? $obj->$tmppropname : '';
  1639. }
  1640. }
  1641. } else {
  1642. $toprint = array();
  1643. $obj = $this->db->fetch_object($resql);
  1644. if ($obj->rowid) {
  1645. require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
  1646. $c = new Categorie($this->db);
  1647. $result = $c->fetch($obj->rowid);
  1648. if ($result > 0) {
  1649. $ways = $c->print_all_ways(); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
  1650. foreach ($ways as $way) {
  1651. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . img_object('', 'category') . ' ' . $way . '</li>';
  1652. }
  1653. }
  1654. }
  1655. $value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">'.implode(' ', $toprint).'</ul></div>';
  1656. }
  1657. } else {
  1658. dol_syslog(get_class($this).'::showOutputField error '.$this->db->lasterror(), LOG_WARNING);
  1659. }
  1660. } elseif ($type == 'radio') {
  1661. if (!isset($param['options'][$value])) {
  1662. $langs->load('errors');
  1663. $value = $langs->trans('ErrorNoValueForRadioType');
  1664. } else {
  1665. $value = $langs->trans($param['options'][$value]);
  1666. }
  1667. } elseif ($type == 'checkbox') {
  1668. $value_arr = explode(',', $value);
  1669. $value = '';
  1670. $toprint = array();
  1671. if (is_array($value_arr)) {
  1672. foreach ($value_arr as $keyval => $valueval) {
  1673. if (!empty($valueval)) {
  1674. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">'.$param['options'][$valueval].'</li>';
  1675. }
  1676. }
  1677. }
  1678. $value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">'.implode(' ', $toprint).'</ul></div>';
  1679. } elseif ($type == 'chkbxlst') {
  1680. $value_arr = explode(',', $value);
  1681. $param_list = array_keys($param['options']);
  1682. $InfoFieldList = explode(":", $param_list[0]);
  1683. $selectkey = "rowid";
  1684. $keyList = 'rowid';
  1685. if (count($InfoFieldList) >= 3) {
  1686. $selectkey = $InfoFieldList[2];
  1687. $keyList = $InfoFieldList[2].' as rowid';
  1688. }
  1689. $fields_label = explode('|', $InfoFieldList[1]);
  1690. if (is_array($fields_label)) {
  1691. $keyList .= ', ';
  1692. $keyList .= implode(', ', $fields_label);
  1693. }
  1694. $filter_categorie = false;
  1695. if (count($InfoFieldList) > 5) {
  1696. if ($InfoFieldList[0] == 'categorie') {
  1697. $filter_categorie = true;
  1698. }
  1699. }
  1700. $sql = "SELECT ".$keyList;
  1701. $sql .= " FROM ".$this->db->prefix().$InfoFieldList[0];
  1702. if (strpos($InfoFieldList[4], 'extra.') !== false) {
  1703. $sql .= ' as main';
  1704. }
  1705. // $sql.= " WHERE ".$selectkey."='".$this->db->escape($value)."'";
  1706. // $sql.= ' AND entity = '.$conf->entity;
  1707. dol_syslog(get_class($this).':showOutputField:$type=chkbxlst', LOG_DEBUG);
  1708. $resql = $this->db->query($sql);
  1709. if ($resql) {
  1710. if ($filter_categorie === false) {
  1711. $value = ''; // value was used, so now we reste it to use it to build final output
  1712. $toprint = array();
  1713. while ($obj = $this->db->fetch_object($resql)) {
  1714. // Several field into label (eq table:code|libelle:rowid)
  1715. $fields_label = explode('|', $InfoFieldList[1]);
  1716. if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
  1717. if (is_array($fields_label) && count($fields_label) > 1) {
  1718. $label = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">';
  1719. foreach ($fields_label as $field_toshow) {
  1720. $translabel = '';
  1721. if (!empty($obj->$field_toshow)) {
  1722. $translabel = $langs->trans($obj->$field_toshow);
  1723. }
  1724. if ($translabel != $field_toshow) {
  1725. $label .= ' '.dol_trunc($translabel, 18);
  1726. } else {
  1727. $label .= ' '.$obj->$field_toshow;
  1728. }
  1729. }
  1730. $label .= '</li>';
  1731. $toprint[] = $label;
  1732. } else {
  1733. $translabel = '';
  1734. if (!empty($obj->{$InfoFieldList[1]})) {
  1735. $translabel = $langs->trans($obj->{$InfoFieldList[1]});
  1736. }
  1737. if ($translabel != $obj->{$InfoFieldList[1]}) {
  1738. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">'.dol_trunc($translabel, 18).'</li>';
  1739. } else {
  1740. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #bbb">'.$obj->{$InfoFieldList[1]}.'</li>';
  1741. }
  1742. }
  1743. }
  1744. }
  1745. } else {
  1746. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1747. $toprint = array();
  1748. while ($obj = $this->db->fetch_object($resql)) {
  1749. if (is_array($value_arr) && in_array($obj->rowid, $value_arr)) {
  1750. $c = new Categorie($this->db);
  1751. $c->fetch($obj->rowid);
  1752. $ways = $c->print_all_ways(); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
  1753. foreach ($ways as $way) {
  1754. $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"'.($c->color ? ' style="background: #'.$c->color.';"' : ' style="background: #bbb"').'>'.img_object('', 'category').' '.$way.'</li>';
  1755. }
  1756. }
  1757. }
  1758. }
  1759. if (!empty($toprint)) $value = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">'.implode(' ', $toprint).'</ul></div>';
  1760. } else {
  1761. dol_syslog(get_class($this).'::showOutputField error '.$this->db->lasterror(), LOG_WARNING);
  1762. }
  1763. } elseif ($type == 'link') {
  1764. $out = '';
  1765. // Only if something to display (perf)
  1766. if ($value) { // If we have -1 here, pb is into insert, not into ouptut (fix insert instead of changing code here to compensate)
  1767. $param_list = array_keys($param['options']); // $param_list='ObjectName:classPath'
  1768. $InfoFieldList = explode(":", $param_list[0]);
  1769. $classname = $InfoFieldList[0];
  1770. $classpath = $InfoFieldList[1];
  1771. if (!empty($classpath)) {
  1772. dol_include_once($InfoFieldList[1]);
  1773. if ($classname && class_exists($classname)) {
  1774. $object = new $classname($this->db);
  1775. $object->fetch($value);
  1776. $value = $object->getNomUrl(3);
  1777. }
  1778. } else {
  1779. dol_syslog('Error bad setup of extrafield', LOG_WARNING);
  1780. return 'Error bad setup of extrafield';
  1781. }
  1782. }
  1783. } elseif ($type == 'text') {
  1784. $value = dol_htmlentitiesbr($value);
  1785. } elseif ($type == 'html') {
  1786. $value = dol_htmlentitiesbr($value);
  1787. } elseif ($type == 'password') {
  1788. $value = dol_trunc(preg_replace('/./i', '*', $value), 8, 'right', 'UTF-8', 1);
  1789. } else {
  1790. $showsize = round((float) $size);
  1791. if ($showsize > 48) {
  1792. $showsize = 48;
  1793. }
  1794. }
  1795. //print $type.'-'.$size;
  1796. $out = $value;
  1797. return $out;
  1798. }
  1799. /**
  1800. * Return the CSS to use for this extrafield into list
  1801. *
  1802. * @param string $key Key of attribute
  1803. * @param string $extrafieldsobjectkey If defined, use the new method to get extrafields data
  1804. * @return string Formated value
  1805. */
  1806. public function getAlignFlag($key, $extrafieldsobjectkey = '')
  1807. {
  1808. global $conf, $langs;
  1809. $type = 'varchar';
  1810. if (!empty($extrafieldsobjectkey)) {
  1811. $type = $this->attributes[$extrafieldsobjectkey]['type'][$key];
  1812. }
  1813. $cssstring = '';
  1814. if (in_array($type, array('date', 'datetime'))) {
  1815. $cssstring = "center";
  1816. } elseif (in_array($type, array('int', 'price', 'double'))) {
  1817. $cssstring = "right";
  1818. } elseif (in_array($type, array('boolean', 'radio', 'checkbox', 'ip'))) {
  1819. $cssstring = "center";
  1820. }
  1821. if (!empty($this->attributes[$extrafieldsobjectkey]['csslist'][$key])) {
  1822. $cssstring .= ($cssstring ? ' ' : '').$this->attributes[$extrafieldsobjectkey]['csslist'][$key];
  1823. } else {
  1824. if (in_array($type, array('ip'))) {
  1825. $cssstring .= ($cssstring ? ' ' : '').'tdoverflowmax150';
  1826. }
  1827. }
  1828. return $cssstring;
  1829. }
  1830. /**
  1831. * Return HTML string to print separator extrafield
  1832. *
  1833. * @param string $key Key of attribute
  1834. * @param object $object Object
  1835. * @param int $colspan Value of colspan to use (it must includes the first column with title)
  1836. * @param string $display_type "card" for form display, "line" for document line display (extrafields on propal line, order line, etc...)
  1837. * @param string $mode Show output ('view') or input ('create' or 'edit') for extrafield
  1838. * @return string HTML code with line for separator
  1839. */
  1840. public function showSeparator($key, $object, $colspan = 2, $display_type = 'card', $mode = '')
  1841. {
  1842. global $conf, $langs;
  1843. $tagtype='tr';
  1844. $tagtype_dyn='td';
  1845. if ($display_type=='line') {
  1846. $tagtype='div';
  1847. $tagtype_dyn='span';
  1848. $colspan=0;
  1849. }
  1850. $extrafield_param = $this->attributes[$object->table_element]['param'][$key];
  1851. $extrafield_param_list = array();
  1852. if (!empty($extrafield_param) && is_array($extrafield_param)) {
  1853. $extrafield_param_list = array_keys($extrafield_param['options']);
  1854. }
  1855. // Set $extrafield_collapse_display_value (do we have to collapse/expand the group after the separator)
  1856. $extrafield_collapse_display_value = -1;
  1857. $expand_display = false;
  1858. if (is_array($extrafield_param_list) && count($extrafield_param_list) > 0) {
  1859. $extrafield_collapse_display_value = intval($extrafield_param_list[0]);
  1860. $expand_display = ((isset($_COOKIE['DOLCOLLAPSE_'.$object->table_element.'_extrafields_'.$key]) || GETPOST('ignorecollapsesetup', 'int')) ? (empty($_COOKIE['DOLCOLLAPSE_'.$object->table_element.'_extrafields_'.$key]) ? false : true) : ($extrafield_collapse_display_value == 2 ? false : true));
  1861. }
  1862. if ($mode == 'create') {
  1863. $extrafield_collapse_display_value = 0;
  1864. }
  1865. $out = '<'.$tagtype.' id="trextrafieldseparator'.$key.(!empty($object->id)?'_'.$object->id:'').'" class="trextrafieldseparator trextrafieldseparator'.$key.(!empty($object->id)?'_'.$object->id:'').'">';
  1866. $out .= '<'.$tagtype_dyn.' '.(!empty($colspan)?'colspan="' . $colspan . '"':'').'>';
  1867. // Some js code will be injected here to manage the collapsing of extrafields
  1868. // Output the picto
  1869. $out .= '<span class="'.($extrafield_collapse_display_value ? 'cursorpointer ' : '').($extrafield_collapse_display_value == 0 ? 'fas fa-square opacitymedium' : 'far fa-'.(($expand_display ? 'minus' : 'plus').'-square')).'"></span>';
  1870. $out .= '&nbsp;';
  1871. $out .= '<strong>';
  1872. $out .= $langs->trans($this->attributes[$object->table_element]['label'][$key]);
  1873. $out .= '</strong>';
  1874. $out .= '</'.$tagtype_dyn.'>';
  1875. $out .= '</'.$tagtype.'>';
  1876. $collapse_group = $key.(!empty($object->id) ? '_'.$object->id : '');
  1877. //$extrafields_collapse_num = $this->attributes[$object->table_element]['pos'][$key].(!empty($object->id)?'_'.$object->id:'');
  1878. if ($extrafield_collapse_display_value == 1 || $extrafield_collapse_display_value == 2) {
  1879. // Set the collapse_display status to cookie in priority or if ignorecollapsesetup is 1, if cookie and ignorecollapsesetup not defined, use the setup.
  1880. $this->expand_display[$collapse_group] = $expand_display;
  1881. if (!empty($conf->use_javascript_ajax) && $mode != 'create') {
  1882. $out .= '<!-- Add js script to manage the collapse/uncollapse of extrafields separators '.$key.' -->'."\n";
  1883. $out .= '<script type="text/javascript">'."\n";
  1884. $out .= 'jQuery(document).ready(function(){'."\n";
  1885. if ($expand_display === false) {
  1886. $out .= ' console.log("Inject js for the collapsing of extrafield '.$key.' - hide");'."\n";
  1887. $out .= ' jQuery(".trextrafields_collapse'.$collapse_group.'").hide();'."\n";
  1888. } else {
  1889. $out .= ' console.log("Inject js for collapsing of extrafield '.$key.' - keep visible and set cookie");'."\n";
  1890. $out .= ' document.cookie = "DOLCOLLAPSE_'.$object->table_element.'_extrafields_'.$key.'=1; path='.$_SERVER["PHP_SELF"].'"'."\n";
  1891. }
  1892. $out .= ' jQuery("#trextrafieldseparator'.$key.(!empty($object->id)?'_'.$object->id:'').'").click(function(){'."\n";
  1893. $out .= ' console.log("We click on collapse/uncollapse .trextrafields_collapse'.$collapse_group.'");'."\n";
  1894. $out .= ' jQuery(".trextrafields_collapse'.$collapse_group.'").toggle(100, function(){'."\n";
  1895. $out .= ' if (jQuery(".trextrafields_collapse'.$collapse_group.'").is(":hidden")) {'."\n";
  1896. $out .= ' jQuery("#trextrafieldseparator'.$key.(!empty($object->id)?'_'.$object->id:'').' '.$tagtype_dyn.' span").addClass("fa-plus-square").removeClass("fa-minus-square");'."\n";
  1897. $out .= ' document.cookie = "DOLCOLLAPSE_'.$object->table_element.'_extrafields_'.$key.'=0; path='.$_SERVER["PHP_SELF"].'"'."\n";
  1898. $out .= ' } else {'."\n";
  1899. $out .= ' jQuery("#trextrafieldseparator'.$key.(!empty($object->id)?'_'.$object->id:'').' '.$tagtype_dyn.' span").addClass("fa-minus-square").removeClass("fa-plus-square");'."\n";
  1900. $out .= ' document.cookie = "DOLCOLLAPSE_'.$object->table_element.'_extrafields_'.$key.'=1; path='.$_SERVER["PHP_SELF"].'"'."\n";
  1901. $out .= ' }'."\n";
  1902. $out .= ' });'."\n";
  1903. $out .= ' });'."\n";
  1904. $out .= '});'."\n";
  1905. $out .= '</script>'."\n";
  1906. }
  1907. } else {
  1908. $this->expand_display[$collapse_group] = 1;
  1909. }
  1910. return $out;
  1911. }
  1912. /**
  1913. * Fill array_options property of object by extrafields value (using for data sent by forms)
  1914. *
  1915. * @param array $extralabels Deprecated (old $array of extrafields, now set this to null)
  1916. * @param object $object Object
  1917. * @param string $onlykey Only some keys are filled:
  1918. * 'string' => When we make update of only one extrafield ($action = 'update_extras'), calling page can set this to avoid to have other extrafields being reset.
  1919. * '@GETPOSTISSET' => When we make update of several extrafields ($action = 'update'), calling page can set this to avoid to have fields not into POST being reset.
  1920. * @param int $todefaultifmissing 1=Set value to the default value in database if value is mandatory and missing
  1921. * @return int 1 if array_options set, 0 if no value, -1 if error (field required missing for example)
  1922. */
  1923. public function setOptionalsFromPost($extralabels, &$object, $onlykey = '', $todefaultifmissing = 0)
  1924. {
  1925. global $_POST, $langs;
  1926. $nofillrequired = 0; // For error when required field left blank
  1927. $error_field_required = array();
  1928. if (isset($this->attributes[$object->table_element]['label']) && is_array($this->attributes[$object->table_element]['label'])) {
  1929. $extralabels = $this->attributes[$object->table_element]['label'];
  1930. }
  1931. if (is_array($extralabels)) {
  1932. // Get extra fields
  1933. foreach ($extralabels as $key => $value) {
  1934. if (!empty($onlykey) && $onlykey != '@GETPOSTISSET' && $key != $onlykey) {
  1935. continue;
  1936. }
  1937. if (!empty($onlykey) && $onlykey == '@GETPOSTISSET' && !GETPOSTISSET('options_'.$key) && (! in_array($this->attributes[$object->table_element]['type'][$key], array('boolean', 'chkbxlst')))) {
  1938. //when unticking boolean field, it's not set in POST
  1939. continue;
  1940. }
  1941. $key_type = $this->attributes[$object->table_element]['type'][$key];
  1942. if ($key_type == 'separate') {
  1943. continue;
  1944. }
  1945. $enabled = 1;
  1946. if (isset($this->attributes[$object->table_element]['enabled'][$key])) { // 'enabled' is often a condition on module enabled or not
  1947. $enabled = dol_eval($this->attributes[$object->table_element]['enabled'][$key], 1, 1, '1');
  1948. }
  1949. $visibility = 1;
  1950. if (isset($this->attributes[$object->table_element]['list'][$key])) { // 'list' is option for visibility
  1951. $visibility = intval(dol_eval($this->attributes[$object->table_element]['list'][$key], 1, 1, '1'));
  1952. }
  1953. $perms = 1;
  1954. if (isset($this->attributes[$object->table_element]['perms'][$key])) {
  1955. $perms = dol_eval($this->attributes[$object->table_element]['perms'][$key], 1, 1, '1');
  1956. }
  1957. if (empty($enabled)
  1958. || (
  1959. $onlykey === '@GETPOSTISSET'
  1960. && in_array($this->attributes[$object->table_element]['type'][$key], array('boolean', 'chkbxlst'))
  1961. && in_array(abs($enabled), array(2, 5))
  1962. && ! GETPOSTISSET('options_' . $key) // Update hidden checkboxes and multiselect only if they are provided
  1963. )
  1964. ) {
  1965. continue;
  1966. }
  1967. if (empty($visibility)) {
  1968. continue;
  1969. }
  1970. if (empty($perms)) {
  1971. continue;
  1972. }
  1973. if ($this->attributes[$object->table_element]['required'][$key]) { // Value is required
  1974. // Check if functionally empty without using GETPOST (depending on the type of extrafield, a
  1975. // technically non-empty value may be treated as empty functionally).
  1976. // value can be alpha, int, array, etc...
  1977. if ((!is_array($_POST["options_".$key]) && empty($_POST["options_".$key]) && $this->attributes[$object->table_element]['type'][$key] != 'select' && $_POST["options_".$key] != '0')
  1978. || (!is_array($_POST["options_".$key]) && empty($_POST["options_".$key]) && $this->attributes[$object->table_element]['type'][$key] == 'select')
  1979. || (!is_array($_POST["options_".$key]) && isset($_POST["options_".$key]) && $this->attributes[$object->table_element]['type'][$key] == 'sellist' && $_POST['options_'.$key] == '0')
  1980. || (is_array($_POST["options_".$key]) && empty($_POST["options_".$key]))) {
  1981. //print 'ccc'.$value.'-'.$this->attributes[$object->table_element]['required'][$key];
  1982. // Field is not defined. We mark this as a problem. We may fix it later if there is a default value and $todefaultifmissing is set.
  1983. $nofillrequired++;
  1984. $error_field_required[$key] = $langs->transnoentitiesnoconv($value);
  1985. }
  1986. }
  1987. if (in_array($key_type, array('date'))) {
  1988. // Clean parameters
  1989. $value_key = dol_mktime(12, 0, 0, GETPOST("options_".$key."month", 'int'), GETPOST("options_".$key."day", 'int'), GETPOST("options_".$key."year", 'int'));
  1990. } elseif (in_array($key_type, array('datetime'))) {
  1991. // Clean parameters
  1992. $value_key = dol_mktime(GETPOST("options_".$key."hour", 'int'), GETPOST("options_".$key."min", 'int'), GETPOST("options_".$key."sec", 'int'), GETPOST("options_".$key."month", 'int'), GETPOST("options_".$key."day", 'int'), GETPOST("options_".$key."year", 'int'), 'tzuserrel');
  1993. } elseif (in_array($key_type, array('checkbox', 'chkbxlst'))) {
  1994. $value_arr = GETPOST("options_".$key, 'array'); // check if an array
  1995. if (!empty($value_arr)) {
  1996. $value_key = implode(',', $value_arr);
  1997. } else {
  1998. $value_key = '';
  1999. }
  2000. } elseif (in_array($key_type, array('price', 'double'))) {
  2001. $value_arr = GETPOST("options_".$key, 'alpha');
  2002. $value_key = price2num($value_arr);
  2003. } elseif (in_array($key_type, array('pricecy', 'double'))) {
  2004. $value_key = price2num(GETPOST("options_".$key, 'alpha')).':'.GETPOST("options_".$key."currency_id", 'alpha');
  2005. } elseif (in_array($key_type, array('html'))) {
  2006. $value_key = GETPOST("options_".$key, 'restricthtml');
  2007. } elseif (in_array($key_type, array('text'))) {
  2008. $value_key = GETPOST("options_".$key, 'alphanohtml');
  2009. } else {
  2010. $value_key = GETPOST("options_".$key);
  2011. if (in_array($key_type, array('link')) && $value_key == '-1') {
  2012. $value_key = '';
  2013. }
  2014. }
  2015. if (!empty($error_field_required[$key]) && $todefaultifmissing) {
  2016. // Value is required but we have a default value and we asked to set empty value to the default value
  2017. if (!empty($this->attributes[$object->table_element]['default']) && !is_null($this->attributes[$object->table_element]['default'][$key])) {
  2018. $value_key = $this->attributes[$object->table_element]['default'][$key];
  2019. unset($error_field_required[$key]);
  2020. $nofillrequired--;
  2021. }
  2022. }
  2023. $object->array_options["options_".$key] = $value_key;
  2024. }
  2025. if ($nofillrequired) {
  2026. $langs->load('errors');
  2027. $this->error = $langs->trans('ErrorFieldsRequired').' : '.implode(', ', $error_field_required);
  2028. setEventMessages($this->error, null, 'errors');
  2029. return -1;
  2030. } else {
  2031. return 1;
  2032. }
  2033. } else {
  2034. return 0;
  2035. }
  2036. }
  2037. /**
  2038. * return array_options array of data of extrafields value of object sent by a search form
  2039. *
  2040. * @param array|string $extrafieldsobjectkey array of extrafields (old usage) or value of object->table_element (new usage)
  2041. * @param string $keyprefix Prefix string to add into name and id of field (can be used to avoid duplicate names)
  2042. * @param string $keysuffix Suffix string to add into name and id of field (can be used to avoid duplicate names)
  2043. * @return array|int array_options set or 0 if no value
  2044. */
  2045. public function getOptionalsFromPost($extrafieldsobjectkey, $keyprefix = '', $keysuffix = '')
  2046. {
  2047. global $_POST;
  2048. if (is_string($extrafieldsobjectkey) && !empty($this->attributes[$extrafieldsobjectkey]['label']) && is_array($this->attributes[$extrafieldsobjectkey]['label'])) {
  2049. $extralabels = $this->attributes[$extrafieldsobjectkey]['label'];
  2050. } else {
  2051. $extralabels = $extrafieldsobjectkey;
  2052. }
  2053. if (is_array($extralabels)) {
  2054. $array_options = array();
  2055. // Get extra fields
  2056. foreach ($extralabels as $key => $value) {
  2057. $key_type = '';
  2058. if (is_string($extrafieldsobjectkey)) {
  2059. $key_type = $this->attributes[$extrafieldsobjectkey]['type'][$key];
  2060. }
  2061. if (in_array($key_type, array('date'))) {
  2062. $dateparamname_start = $keysuffix . 'options_' . $key . $keyprefix . '_start';
  2063. $dateparamname_end = $keysuffix . 'options_' . $key . $keyprefix . '_end';
  2064. if (GETPOSTISSET($dateparamname_start . 'year') && GETPOSTISSET($dateparamname_end . 'year')) {
  2065. // values provided as a date pair (start date + end date), each date being broken down as year, month, day, etc.
  2066. $value_key = array(
  2067. 'start' => dol_mktime(0, 0, 0, GETPOST($dateparamname_start . 'month', 'int'), GETPOST($dateparamname_start . 'day', 'int'), GETPOST($dateparamname_start . 'year', 'int')),
  2068. 'end' => dol_mktime(23, 59, 59, GETPOST($dateparamname_end . 'month', 'int'), GETPOST($dateparamname_end . 'day', 'int'), GETPOST($dateparamname_end . 'year', 'int'))
  2069. );
  2070. } elseif (GETPOSTISSET($keysuffix."options_".$key.$keyprefix."year")) {
  2071. // Clean parameters
  2072. $value_key = dol_mktime(12, 0, 0, GETPOST($keysuffix."options_".$key.$keyprefix."month", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."day", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."year", 'int'));
  2073. } else {
  2074. continue; // Value was not provided, we should not set it.
  2075. }
  2076. } elseif (in_array($key_type, array('datetime'))) {
  2077. $dateparamname_start = $keysuffix . 'options_' . $key . $keyprefix . '_start';
  2078. $dateparamname_end = $keysuffix . 'options_' . $key . $keyprefix . '_end';
  2079. if (GETPOSTISSET($dateparamname_start . 'year') && GETPOSTISSET($dateparamname_end . 'year')) {
  2080. // values provided as a date pair (start date + end date), each date being broken down as year, month, day, etc.
  2081. $dateparamname_end_hour = GETPOST($dateparamname_end . 'hour', 'int') !='-1' ? GETPOST($dateparamname_end . 'hour', 'int') : '23';
  2082. $dateparamname_end_min = GETPOST($dateparamname_end . 'min', 'int') !='-1' ? GETPOST($dateparamname_end . 'min', 'int') : '59';
  2083. $dateparamname_end_sec = GETPOST($dateparamname_end . 'sec', 'int') !='-1' ? GETPOST($dateparamname_end . 'sec', 'int') : '59';
  2084. $value_key = array(
  2085. 'start' => dol_mktime(GETPOST($dateparamname_start . 'hour', 'int'), GETPOST($dateparamname_start . 'min', 'int'), GETPOST($dateparamname_start . 'sec', 'int'), GETPOST($dateparamname_start . 'month', 'int'), GETPOST($dateparamname_start . 'day', 'int'), GETPOST($dateparamname_start . 'year', 'int'), 'tzuserrel'),
  2086. 'end' => dol_mktime($dateparamname_end_hour, $dateparamname_end_min, $dateparamname_end_sec, GETPOST($dateparamname_end . 'month', 'int'), GETPOST($dateparamname_end . 'day', 'int'), GETPOST($dateparamname_end . 'year', 'int'), 'tzuserrel')
  2087. );
  2088. } elseif (GETPOSTISSET($keysuffix."options_".$key.$keyprefix."year")) {
  2089. // Clean parameters
  2090. $value_key = dol_mktime(GETPOST($keysuffix."options_".$key.$keyprefix."hour", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."min", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."sec", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."month", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."day", 'int'), GETPOST($keysuffix."options_".$key.$keyprefix."year", 'int'), 'tzuserrel');
  2091. } else {
  2092. continue; // Value was not provided, we should not set it.
  2093. }
  2094. } elseif ($key_type == 'select') {
  2095. // to detect if we are in search context
  2096. if (GETPOSTISARRAY($keysuffix."options_".$key.$keyprefix)) {
  2097. $value_arr = GETPOST($keysuffix."options_".$key.$keyprefix, 'array:aZ09');
  2098. // Make sure we get an array even if there's only one selected
  2099. $value_arr = (array) $value_arr;
  2100. $value_key = implode(',', $value_arr);
  2101. } else {
  2102. $value_key = GETPOST($keysuffix."options_".$key.$keyprefix);
  2103. }
  2104. } elseif (in_array($key_type, array('checkbox', 'chkbxlst'))) {
  2105. if (!GETPOSTISSET($keysuffix."options_".$key.$keyprefix)) {
  2106. continue; // Value was not provided, we should not set it.
  2107. }
  2108. $value_arr = GETPOST($keysuffix."options_".$key.$keyprefix);
  2109. // Make sure we get an array even if there's only one checkbox
  2110. $value_arr = (array) $value_arr;
  2111. $value_key = implode(',', $value_arr);
  2112. } elseif (in_array($key_type, array('price', 'double', 'int'))) {
  2113. if (!GETPOSTISSET($keysuffix."options_".$key.$keyprefix)) {
  2114. continue; // Value was not provided, we should not set it.
  2115. }
  2116. $value_arr = GETPOST($keysuffix."options_".$key.$keyprefix);
  2117. if ($keysuffix != 'search_') { // If value is for a search, we must keep complex string like '>100 <=150'
  2118. $value_key = price2num($value_arr);
  2119. } else {
  2120. $value_key = $value_arr;
  2121. }
  2122. } elseif (in_array($key_type, array('boolean'))) {
  2123. if (!GETPOSTISSET($keysuffix."options_".$key.$keyprefix)) {
  2124. $value_key = '';
  2125. } else {
  2126. $value_arr = GETPOST($keysuffix."options_".$key.$keyprefix);
  2127. $value_key = $value_arr;
  2128. }
  2129. } else {
  2130. if (!GETPOSTISSET($keysuffix."options_".$key.$keyprefix)) {
  2131. continue; // Value was not provided, we should not set it.
  2132. }
  2133. $value_key = GETPOST($keysuffix."options_".$key.$keyprefix);
  2134. }
  2135. $array_options[$keysuffix."options_".$key] = $value_key; // No keyprefix here. keyprefix is used only for read.
  2136. }
  2137. return $array_options;
  2138. }
  2139. return 0;
  2140. }
  2141. }