extrafields.class.php 99 KB

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