extrafields.class.php 96 KB

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