1
0

BxBaseFormView.php 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxBaseFormView extends BxDolForm
  10. {
  11. protected static $_isToggleJsAdded = false;
  12. protected static $_isCssJsAdded = false;
  13. protected static $_isCssJsUiAdded = false;
  14. protected static $_isCssJsUiSortableAdded = false;
  15. protected static $_isCssJsMinicolorsAdded = false;
  16. protected static $_isCssJsCodeMirrorAdded = false;
  17. protected static $_isCssJsLabelsAdded = false;
  18. protected static $_isCssJsTimepickerAdded = false;
  19. protected static $_isCssJsAddedViewMode = false;
  20. /**
  21. * Enable or disable error message displaying
  22. */
  23. protected $bEnableErrorIcon = true;
  24. /**
  25. * HTML Code of this form
  26. */
  27. protected $sCode;
  28. /**
  29. * Code which will be added to the beginning of the form.
  30. * For example, hidden inputs.
  31. * For internal use only
  32. */
  33. protected $_sCodeAdd = '';
  34. /**
  35. * for internal use only
  36. */
  37. protected $_isSectionOpened = false;
  38. /**
  39. * Default divider for several inputs
  40. */
  41. protected $_sDivider = '<span class="bx-def-margin-left"></span>';
  42. /**
  43. * Alternative divider for several inputs
  44. */
  45. protected $_sDividerAlt = '<div class="bx-form-input-dv-nl"></div>';
  46. /**
  47. * Form is added dynamically.
  48. */
  49. protected $_bDynamicMode = false;
  50. /**
  51. * Form is submitted dynamically (using Ajax Submit).
  52. */
  53. protected $_bAjaxMode = false;
  54. /**
  55. * Use absolute Action URL which is needed in Ajax Mode.
  56. */
  57. protected $_bAbsoluteActionUrl = false;
  58. /**
  59. * Form is displayed in view mode.
  60. */
  61. protected $_bViewMode = false;
  62. /**
  63. * Cached CSRF check for revealing a secret field in the current request.
  64. */
  65. protected $_bSecretRevealCsrfValid = null;
  66. /**
  67. * Show or not sections which have no fileds
  68. */
  69. protected $_bShowEmptySections = false;
  70. /**
  71. * Function name for generation close form section HTML.
  72. */
  73. protected $_sSectionClose = 'getCloseSection';
  74. /**
  75. * Function name for generation open form section HTML.
  76. */
  77. protected $_sSectionOpen = 'getOpenSection';
  78. /**
  79. * JS files list for form
  80. */
  81. protected $_aJs = array();
  82. /**
  83. * CSS files list for form
  84. */
  85. protected $_aCss = array();
  86. protected $_sJsClassName;
  87. protected $_sJsObjectName;
  88. protected $_aHtmlIds;
  89. protected $_aAgentsFormObject;
  90. protected $_sAgentFormObject;
  91. /**
  92. * Constructor
  93. *
  94. * @param array $aInfo Form contents
  95. *
  96. * $aInfo['params'] = array(
  97. * 'remove_form' => true|false,
  98. * );
  99. *
  100. * @return BxBaseFormView
  101. */
  102. function __construct($aInfo, $oTemplate)
  103. {
  104. parent::__construct($aInfo, $oTemplate);
  105. $this->_bAjaxMode = isset($this->aParams['ajax_mode']) && $this->aParams['ajax_mode'];
  106. $this->_bViewMode = isset($this->aParams['view_mode']) && $this->aParams['view_mode'];
  107. if($this->_bViewMode) {
  108. $this->_sSectionClose = 'getCloseSectionViewMode';
  109. $this->_sSectionOpen = 'getOpenSectionViewMode';
  110. }
  111. $this->_sJsClassName = 'BxDolForm';
  112. $sName = !empty($aInfo['params']['display']) ? $aInfo['params']['display'] : '';
  113. if(empty($sName) && !empty($aInfo['params']['object']))
  114. $sName = $aInfo['params']['object'];
  115. if(empty($sName) && !empty($aInfo['form_attrs']['id']))
  116. $sName = $aInfo['form_attrs']['id'];
  117. if(empty($sName) && !empty($aInfo['form_attrs']['name']))
  118. $sName = $aInfo['form_attrs']['name'];
  119. $this->_sJsObjectName = 'oForm' . bx_gen_method_name($sName, array('_' , '-'));
  120. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sName);
  121. $this->_aHtmlIds = array(
  122. 'help_popup' => $sHtmlId . '-help-popup-',
  123. 'pgc' => $sHtmlId . '-pgc-',
  124. 'pgc_popup' => $sHtmlId . '-pgc-popup-',
  125. 'pgc_form' => $sHtmlId . '-pgc-form-',
  126. );
  127. $this->_sAgentFormObject = $aInfo['params']['object'] ?? $this->id;
  128. $oAi = BxDolAI::getInstance();
  129. $this->_aAgentsFormObject = $oAi ? $oAi->getAgentsByFormObject($this->_sAgentFormObject) : [];
  130. if ($this->_aAgentsFormObject && is_array($this->_aAgentsFormObject)) {
  131. $this->_aAgentsFormObject = array_filter($this->_aAgentsFormObject, function ($a) use ($oAi) {
  132. return $oAi->canInteract($a);
  133. });
  134. }
  135. }
  136. public function performActionGetHelp()
  137. {
  138. $sInput = bx_process_input(bx_get('input'));
  139. if(empty($sInput) || empty($this->aInputs[$sInput]['help']))
  140. return;
  141. echo $this->oTemplate->parseHtmlByName('form_field_help_popup.html', array(
  142. 'content' => _t($this->aInputs[$sInput]['help']),
  143. 'bx_if:show_edit' => array(
  144. 'condition' => isAdmin(),
  145. 'content' => array(
  146. 'link' => BX_DOL_URL_STUDIO . bx_append_url_params('builder_forms.php', array(
  147. 'page' => 'fields',
  148. 'module' => $this->aParams['module'],
  149. 'object' => $this->aParams['object'],
  150. 'display' => $this->aParams['display']
  151. ))
  152. )
  153. )
  154. ));
  155. }
  156. public function performActionChangePrivacyGroup()
  157. {
  158. $iAuthorId = (int)bx_get_logged_profile_id();
  159. if(empty($iAuthorId))
  160. return echoJson(array());
  161. $iInputId = bx_process_input(bx_get('input_id'), BX_DATA_INT);
  162. if(empty($iInputId))
  163. return echoJson(array());
  164. $sPrivacyObject = bx_process_input(bx_get('privacy_object'));
  165. $sPrivacyField = BxDolPrivacy::getFieldName($sPrivacyObject);
  166. if(empty($sPrivacyField))
  167. return echoJson(array());
  168. $sPrivacyGroup = bx_process_input(bx_get($sPrivacyField));
  169. if(empty($sPrivacyField))
  170. return echoJson(array());
  171. if(!BxDolFormQuery::setInputPrivacy($iInputId, $iAuthorId, $sPrivacyField, $sPrivacyGroup))
  172. return echoJson(array());
  173. return echoJson(array(
  174. 'code' => 0,
  175. 'form_id' => $this->getId(),
  176. 'chooser_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  177. 'icon' => $this->_getPrivacyIcon($sPrivacyGroup),
  178. 'eval' => $this->getJsObjectName() . '.pgcOnSelectGroup(oData);'
  179. ));
  180. }
  181. public function performActionGetPrivacyGroupChooser()
  182. {
  183. $iInputId = bx_process_input(bx_get('input_id'), BX_DATA_INT);
  184. $sPrivacyObject = bx_process_input(bx_get('privacy_object'));
  185. if(empty($iInputId) || empty($sPrivacyObject))
  186. return '';
  187. $sFormId = $this->_aHtmlIds['pgc_form'] . $iInputId;
  188. $aFormActionParams = array();
  189. if(!empty($this->aParams['object']) && !empty($this->aParams['display']))
  190. $aFormActionParams = array(
  191. 'o' => $this->aParams['object'],
  192. 'd' => $this->aParams['display']
  193. );
  194. $aFormActionParams['a'] = 'change_privacy_group';
  195. $iAuthorId = bx_get_logged_profile_id();
  196. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  197. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  198. $aInputPgc = $oPrivacy->getGroupChooser($sPrivacyObject, $iAuthorId);
  199. $aInputPgc['value'] = $mixedPrivacyGroup;
  200. if(!isset($aInputPgc['content']))
  201. $aInputPgc['content'] = '';
  202. $aInputPgc['content'] .= $oPrivacy->addCssJs(true);
  203. $aInputPgc['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iAuthorId, array(
  204. 'content_id' => $iInputId,
  205. 'group_id' => $mixedPrivacyGroup,
  206. 'html_ids' => array(
  207. 'form' => $sFormId
  208. )
  209. ));
  210. $aFormInfo = array(
  211. 'form_attrs' => array(
  212. 'id' => $sFormId,
  213. 'action' => BX_DOL_URL_ROOT . bx_append_url_params('form.php', $aFormActionParams),
  214. 'method' => 'post'
  215. ),
  216. 'inputs' => array (
  217. 'input_id' => array(
  218. 'type' => 'hidden',
  219. 'name' => 'input_id',
  220. 'value' => $iInputId,
  221. 'db' => array (
  222. 'pass' => 'Int',
  223. ),
  224. ),
  225. 'privacy_object' => array(
  226. 'type' => 'hidden',
  227. 'name' => 'privacy_object',
  228. 'value' => $sPrivacyObject,
  229. 'db' => array (
  230. 'pass' => 'Xss',
  231. ),
  232. ),
  233. 'privacy_group_chooser' => $aInputPgc,
  234. 'controls' => array(
  235. 'name' => 'controls',
  236. 'type' => 'input_set',
  237. array(
  238. 'type' => 'button',
  239. 'name' => 'do_submit',
  240. 'value' => _t('_sys_submit'),
  241. 'attrs' => array(
  242. 'onclick' => "$('#" . $sFormId . "').submit()",
  243. ),
  244. ),
  245. array (
  246. 'type' => 'reset',
  247. 'name' => 'do_cancel',
  248. 'value' => _t('_Cancel'),
  249. 'attrs' => array(
  250. 'onclick' => "$(this).parents('.bx-popup-applied:visible:first').dolPopupHide()",
  251. 'class' => 'bx-def-margin-sec-left',
  252. ),
  253. )
  254. )
  255. )
  256. );
  257. $oForm = new BxTemplFormView($aFormInfo);
  258. echo $this->oTemplate->parseHtmlByName('form_field_privacy_popup.html', array(
  259. 'form_id' => $oForm->getId(),
  260. 'form' => $oForm->getCode(true),
  261. ));
  262. }
  263. function setShowEmptySections($b)
  264. {
  265. $this->_bShowEmptySections = $b;
  266. }
  267. function setAjaxMode($bAjaxMode)
  268. {
  269. $this->_bAjaxMode = (bool)$bAjaxMode;
  270. }
  271. function isAjaxMode()
  272. {
  273. return $this->_bAjaxMode;
  274. }
  275. function isViewMode()
  276. {
  277. return (bool)($this->aParams['view_mode'] ?? 0);
  278. }
  279. function setAbsoluteActionUrl($sUrl)
  280. {
  281. if(empty($sUrl))
  282. return;
  283. $this->aFormAttrs['action'] = $sUrl;
  284. $this->_bAbsoluteActionUrl = true;
  285. }
  286. /**
  287. * Return Form code
  288. * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup.
  289. * @return string
  290. */
  291. function getCode($bDynamicMode = false)
  292. {
  293. if(!$bDynamicMode && bx_is_dynamic_request())
  294. $bDynamicMode = true;
  295. $this->_bDynamicMode = $bDynamicMode;
  296. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  297. $sInclude = '';
  298. $this->sCode = false;
  299. /**
  300. * @hooks
  301. * @hookdef hook-system-form_output 'system', 'form_output' - hook to override form object and/or code to be output
  302. * - $unit_name - equals `system`
  303. * - $action - equals `form_output`
  304. * - $object_id - not used
  305. * - $sender_id - not used
  306. * - $extra_params - array of additional params with the following array keys:
  307. * - `dynamic` - [boolean] is dynamic mode
  308. * - `object` - [object] by ref, an instance of form class, @see BxDolForm, can be overridden in hook processing
  309. * - `code` - [boolean] or [string] by ref, when false the default generation mechanism will be used, can be overridden in hook processing
  310. * - `include` - [string] by ref, additional data to be attached to output, can be overridden in hook processing
  311. * @hook @ref hook-system-form_output
  312. */
  313. bx_alert('system', 'form_output', 0, 0, [
  314. 'dynamic' => $this->_bDynamicMode,
  315. 'object' => &$this,
  316. 'code' => &$this->sCode,
  317. 'include' => &$sInclude,
  318. 'code_ref' => &$this->sCode,
  319. 'include_ref' => &$sInclude,
  320. 'form_inputs_ref' => &$this->aInputs,
  321. ]);
  322. if($this->sCode === false)
  323. $this->sCode = $this->genForm();
  324. $this->addCssJs();
  325. $sDynamicCssJs = $this->_processCssJs();
  326. return $sInclude . $sDynamicCssJs . $this->sCode;
  327. }
  328. function getCodeAPI()
  329. {
  330. $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs);
  331. $this->sCode = false;
  332. /**
  333. * @hooks
  334. * @hookdef hook-system-form_output_api 'system', 'form_output_api' - hook to override form object and/or code to be output. Is used in API calls.
  335. * - $unit_name - equals `system`
  336. * - $action - equals `form_output_api`
  337. * - $object_id - not used
  338. * - $sender_id - not used
  339. * - $extra_params - array of additional params with the following array keys:
  340. * - `dynamic` - [boolean] is dynamic mode
  341. * - `object` - [object] by ref, an instance of form class, @see BxDolForm, can be overridden in hook processing
  342. * - `code` - [boolean] by ref, when false the default generation mechanism will be used, can be overridden in hook processing
  343. * @hook @ref hook-system-form_output_api
  344. */
  345. bx_alert('system', 'form_output_api', 0, 0, [
  346. 'dynamic' => $this->_bDynamicMode,
  347. 'object' => &$this,
  348. 'code' => &$this->sCode,
  349. 'code_ref' => &$this->sCode,
  350. 'form_inputs_ref' => &$this->aInputs,
  351. ]);
  352. if($this->sCode === false) {
  353. if(!$this->genForm() && (bool)($this->aParams['view_mode'] ?? 0))
  354. return [];
  355. }
  356. $oIconset = BxDolIconset::getObjectInstance();
  357. $oFunctions = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate);
  358. // TODO: process inputs to translate titles, alerts, etc
  359. $aInputs = [];
  360. foreach($this->aInputs as $sKey => $aInput) {
  361. if(isset($aInput['visible_for_levels']) && !self::isVisible($aInput))
  362. continue;
  363. if(isset($aInput['type']) && in_array($aInput['name'], ['captcha']))
  364. continue;
  365. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aInput['icon'] ?? '');
  366. $aInput['icon'] = $sIcon ? $oIconset->getIcon($sIcon) : ($sIconHtml ? $sIconHtml : '');
  367. if(!empty($aInput['info']))
  368. $aInput['info'] = strip_tags($aInput['info']);
  369. if(!empty($aInput['help']))
  370. $aInput['help'] = strip_tags($aInput['help']);
  371. if(!empty($aInput['error']))
  372. $aInput['error'] = strip_tags($aInput['error']);
  373. $bType = isset($aInput['type']);
  374. if ($bType && 'select' == $aInput['type']) {
  375. if(($sK1 = 'value') && empty($aInput[$sK1]) && ($aValues = $aInput['values'] ?? false) && is_array($aValues))
  376. $aInput[$sK1] = reset($aValues)['key'] ?? key($aValues);
  377. }
  378. if ($bType && 'files' == $aInput['type']) {
  379. $oStorage = BxDolStorage::getObjectInstance($aInput['storage_object']);
  380. $aInput['ext_allow'] = $oStorage->getObjectData()['ext_allow'];
  381. $aInput['ext_deny'] = $oStorage->getObjectData()['ext_deny'];
  382. $aInput['ghost_template'] = '';
  383. $aInput['value'] = '';
  384. $aInput['values'] = '';
  385. $aInput['values_src'] = '';
  386. if(!empty($aInput['content_id']) && ($sUpr = reset($aInput['uploaders'])) && ($sStg = $aInput['storage_object'] ?? '') && ($oUploader = BxDolUploader::getObjectInstance($sUpr, $sStg, genRndPwd(8))) !== false) {
  387. $oUploader->setMultiple($aInput['multiple'] ?? true);
  388. if(($sMethod = 'setSubmitted') && method_exists($oUploader, $sMethod))
  389. $oUploader->$sMethod($this->isSubmitted());
  390. $sData = $oUploader->getGhostsWithOrder((int)bx_get_logged_profile_id(), 'json', $aInput['images_transcoder'], $aInput['content_id']);
  391. if($sData && ($aData = json_decode($sData, true)) && !empty($aData['g']) && is_array($aData['g'])) {
  392. $aInput['values_src'] = array_values($aData['g']);
  393. $aGhostIds = [];
  394. array_walk($aData['g'], function($aGhost) use (&$aGhostIds) {
  395. $aGhostIds[] = $aGhost['file_id'];
  396. });
  397. $aInput['value'] = implode(',', $aGhostIds);
  398. }
  399. }
  400. }
  401. if ($bType && 'custom' == $aInput['type']){
  402. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  403. if (method_exists($this, $sCustomMethod)) {
  404. $mixed = $this->$sCustomMethod($aInput);
  405. $aInput = is_array($mixed) ? $mixed : ['type' => 'custom', 'content' => $mixed];
  406. }
  407. }
  408. if ($bType && 'block_header' == $aInput['type']){
  409. $aInput['name'] = $sKey;
  410. }
  411. if ($bType && 'block_header' == $aInput['type']){
  412. $aInput['name'] = $sKey;
  413. }
  414. if ($bType && 'location' == $aInput['type']){
  415. $oLocation = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  416. $aVars = [];
  417. foreach(BxDolForm::$LOCATION_INDEXES as $sKey)
  418. $aVars[$sKey] = $oLocation->getLocationVal($aInput, $sKey, $this);
  419. if(($sLocationString = bx_api_get_location_string($aVars)))
  420. $aVars['location_string'] = $sLocationString;
  421. $aInput['value'] = $aVars;
  422. }
  423. if ($bType && 'textarea' == $aInput['type']) {
  424. $iTaHtml = (int)($aInput['html'] ?? 0);
  425. if(($sTaValue = $aInput['value'] ?? false) && $iTaHtml == 0)
  426. $aInput['value'] = strip_tags($sTaValue);
  427. if($iTaHtml != 0)
  428. $aInput['mentions'] = $this->getMentionsQueryParams($aInput);
  429. }
  430. if ($bType && 'price' == $aInput['type']) {
  431. $aInput['value'] = [
  432. 'value' => $aInput['value'],
  433. 'currency' => BxDolPayments::getInstance()->getOption('default_currency_sign') ?: getParam('currency_sign')
  434. ];
  435. }
  436. if ($this->_isSecretInput($aInput) && isset($aInput['value']) && $aInput['value'] !== '' && $aInput['value'] !== null && !is_array($aInput['value'])) {
  437. if (!$this->isViewMode() || !$this->_isSecretRevealed($aInput['name']))
  438. $aInput['value'] = bx_gen_secret((string)$aInput['value']);
  439. }
  440. /**
  441. * Everytime when [key => value] is used it should be converted to [key => ..., value => ...]
  442. */
  443. if (($sVt = $aInput['type'] ?? false) && in_array($sVt, ['select', 'select_multiple', 'checkbox_set', 'radio_set']) && is_array(($aVv = $aInput[($sKv = 'values')] ?? false)) && !(is_array(($aVv1 = reset($aVv))) && isset($aVv1['key'], $aVv1['value']))) {
  444. $aInput[$sKv] = array_map(function($sKey) use($aInput, $sKv) {
  445. return ['key' => $sKey, 'value' => $aInput[$sKv][$sKey]];
  446. }, array_keys($aInput[$sKv]));
  447. }
  448. if(isset($aInput['name']) && in_array($aInput['name'], ['allow_view_to', 'object_privacy_view'])) {
  449. $aParams = bx_get('params');
  450. if(empty($aParams) || !is_array($aParams))
  451. $aParams = [];
  452. if(!isset($aParams['context_id']) || ($iContextId = (int)$aParams['context_id']) >= 0) {
  453. $oProfile = BxDolProfile::getInstance();
  454. foreach($aInput['values'] as $aValue) {
  455. //--- Selected friends
  456. if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_FRIENDS_SELECTED) {
  457. $aIds = BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContent($oProfile->id(), true, 0, 20);
  458. if(!empty($aIds) && is_array($aIds)) {
  459. $aInput['values_friends'] = [];
  460. foreach($aIds as $iId)
  461. $aInput['values_friends'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)];
  462. }
  463. }
  464. //--- Selected relations
  465. if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_RELATIONS_SELECTED) {
  466. $aIds = BxDolConnection::getObjectInstance('sys_profiles_relations')->getConnectedContent($oProfile->id(), true, 0, 20);
  467. if(!empty($aIds) && is_array($aIds)) {
  468. $aInput['values_relations'] = [];
  469. foreach($aIds as $iId)
  470. $aInput['values_relations'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)];
  471. }
  472. }
  473. //--- Selected memberships
  474. if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_MEMBERSHIPS_SELECTED) {
  475. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true);
  476. if(!empty($aLevels) && is_array($aLevels)) {
  477. $aInput['values_memberships'] = [];
  478. foreach($aLevels as $iId => $sTitle)
  479. $aInput['values_memberships'][] = ['key' => $iId, 'value' => $sTitle];
  480. }
  481. }
  482. }
  483. }
  484. else
  485. $aInput = array_merge($aInput, [
  486. 'type' => 'hidden',
  487. 'value' => $iContextId,
  488. 'owner_info' => BxDolProfile::getData(abs($iContextId))
  489. ]);
  490. }
  491. $aInputs[$sKey] = $aInput;
  492. }
  493. return [
  494. 'inputs' => $aInputs,
  495. 'attrs' => $this->aFormAttrs,
  496. 'params' => $this->aParams
  497. ];
  498. }
  499. public function getJsClassName()
  500. {
  501. return $this->_sJsClassName;
  502. }
  503. public function getJsObjectName()
  504. {
  505. return $this->_sJsObjectName;
  506. }
  507. public function getJsScript($bWrap = false)
  508. {
  509. $sJsObjName = $this->getJsObjectName();
  510. $sJsObjClass = $this->getJsClassName();
  511. $sCode = "if(window['" . $sJsObjName . "'] == undefined) window['" . $sJsObjName . "'] = new " . $sJsObjClass . "(" . json_encode(array(
  512. 'sObjName' => $sJsObjName,
  513. 'sId' => $this->getId(),
  514. 'sName' => $this->getName(),
  515. 'sObject' => isset($this->aParams['object']) ? $this->aParams['object'] : '',
  516. 'sDisplay' => isset($this->aParams['display']) ? $this->aParams['display'] : '',
  517. 'sRootUrl' => BX_DOL_URL_ROOT,
  518. 'aHtmlIds' => $this->_aHtmlIds,
  519. 'bInitCodeMirror' => self::$_isCssJsCodeMirrorAdded === true,
  520. 'bLeavePageConfirmation' => getParam('sys_form_lpc_enable') == 'on',
  521. 'sTxtLeavePageConfirmation' => _t('_sys_leave_page_confirmation')
  522. )) . "); window['" . $sJsObjName . "'].init();";
  523. return $bWrap ? $this->oTemplate->_wrapInTagJsCode($sCode) : $sCode;
  524. }
  525. /**
  526. * Generate the whole form
  527. *
  528. * @return string
  529. */
  530. function genForm()
  531. {
  532. $this->_sCodeAdd = '';
  533. $bViewMode = $this->isViewMode();
  534. $sTable = $this->genRows();
  535. if($bViewMode && !$sTable)
  536. return '';
  537. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  538. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  539. if (!empty($this->aParams['remove_form']) || $bViewMode) {
  540. $sForm = <<<BLAH
  541. $sHtmlBefore
  542. {$this->_sCodeAdd}
  543. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  544. $sTable
  545. </div>
  546. $sHtmlAfter
  547. BLAH;
  548. } else {
  549. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  550. $sAjaxFormJs = '';
  551. if($this->_bAjaxMode)
  552. $sAjaxFormJs = <<<BLAH
  553. $("form#{$this->id}").ajaxForm({
  554. dataType: "json",
  555. beforeSubmit: function (formData, jqForm, options) {
  556. bx_loading($("form#{$this->id}"), true);
  557. },
  558. success: function (oData) {
  559. bx_loading($("form#{$this->id}"), false);
  560. processJsonData(oData);
  561. }
  562. });
  563. BLAH;
  564. $sJsCodeDynamic = '';
  565. if($this->_bDynamicMode)
  566. $sJsCodeDynamic = 'function() {' . $this->getJsScript() . '}';
  567. $sForm = <<<BLAH
  568. $sHtmlBefore
  569. <form $sFormAttrs>
  570. {$this->_sCodeAdd}
  571. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  572. $sTable
  573. </div>
  574. </form>
  575. <script>
  576. $(document).ready(function() {
  577. $(this).addWebForms($sJsCodeDynamic);
  578. });
  579. $sAjaxFormJs
  580. </script>
  581. $sHtmlAfter
  582. BLAH;
  583. }
  584. if(!$this->_bDynamicMode)
  585. $sForm = $sForm . $this->getJsScript(true);
  586. return $sForm;
  587. }
  588. /**
  589. * Generate Table HTML code
  590. *
  591. * @return string
  592. */
  593. function genRows()
  594. {
  595. // add CSRF token if it's needed.
  596. if (!(isset($this->aParams['view_mode']) && $this->aParams['view_mode']) && getParam('sys_security_form_token_enable') == 'on' && (!isset($this->aParams['csrf']['disable']) || (isset($this->aParams['csrf']['disable']) && $this->aParams['csrf']['disable'] !== true)) && ($mixedCsrfToken = BxDolForm::getCsrfToken()) !== false) {
  597. $this->aInputs['csrf_token'] = array(
  598. 'type' => 'hidden',
  599. 'name' => 'csrf_token',
  600. 'value' => $mixedCsrfToken,
  601. 'db' => array ('pass' => 'Xss'),
  602. 'visible_for_levels' => PHP_INT_MAX,
  603. );
  604. }
  605. // add 'Ajax Mode' flag
  606. if($this->_bAjaxMode)
  607. $this->aInputs['ajax_mode'] = array(
  608. 'type' => 'hidden',
  609. 'name' => 'ajax_mode',
  610. 'value' => 1,
  611. 'db' => array ('pass' => 'Int'),
  612. 'visible_for_levels' => PHP_INT_MAX,
  613. );
  614. // add 'Absolute Action Url' flag
  615. if($this->_bAbsoluteActionUrl)
  616. $this->aInputs['absolute_action_url'] = array(
  617. 'type' => 'hidden',
  618. 'name' => 'absolute_action_url',
  619. 'value' => 1,
  620. 'db' => array ('pass' => 'Int'),
  621. 'visible_for_levels' => PHP_INT_MAX,
  622. );
  623. // check if we need to generate open section clause
  624. $sOpenSection = '';
  625. foreach ($this->aInputs as $aInput) {
  626. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  627. continue;
  628. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  629. $sOpenSection = $this->{$this->_sSectionOpen}();
  630. break;
  631. }
  632. // generate rows contents
  633. $sCont = '';
  634. $sContHeader = '';
  635. $sContFields = '';
  636. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  637. foreach ($this->aInputs as &$aInput) {
  638. if (!isset($aInput['visible_for_levels']) || self::isVisible($aInput)) {
  639. if ((isset($aInput['type']) && 'block_header' == $aInput['type']) && !$this->_bShowEmptySections) {
  640. // don't show section with no fields or with empty fields
  641. if ($sContHeader) {
  642. if ($sContFields)
  643. $sCont .= $sContHeader . $sContFields;
  644. else
  645. $sContHeader = '';
  646. $sContFields = '';
  647. }
  648. else {
  649. $sCont .= $sContFields;
  650. $sContFields = '';
  651. }
  652. $sContHeader = $this->$sFuncGenRow($aInput);
  653. } else {
  654. $sContFields .= $this->$sFuncGenRow($aInput);
  655. }
  656. }
  657. }
  658. $sCont .= $sContHeader . $sContFields;
  659. $sCloseSection = $this->{$this->_sSectionClose}();
  660. return $this->isViewMode() && !$sCont ? '' : $sOpenSection . $sCont . $sCloseSection;
  661. }
  662. /**
  663. * Generate single Table Row
  664. *
  665. * @param array $aInput
  666. * @return string
  667. */
  668. function genRow(&$aInput)
  669. {
  670. if (!isset($aInput['type']))
  671. $aInput['type'] = false;
  672. if (!empty($aInput['name'])) {
  673. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  674. if (method_exists($this, $sCustomMethod))
  675. return $this->$sCustomMethod($aInput);
  676. }
  677. switch ($aInput['type']) {
  678. case 'block_header':
  679. $sRow = $this->genRowBlockHeader($aInput);
  680. break;
  681. case 'block_end':
  682. $sRow = $this->genBlockEnd($aInput);
  683. break;
  684. case 'hidden':
  685. // do not generate row for hidden inputs
  686. $sRow = '';
  687. $this->_sCodeAdd .= $this->genInput($aInput);
  688. break;
  689. case 'select_box':
  690. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  691. break;
  692. case 'files':
  693. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  694. break;
  695. case 'switcher':
  696. case 'checkbox':
  697. $sRow = $this->genRowStandard($aInput, true);
  698. break;
  699. default:
  700. $sRow = $this->genRowStandard($aInput);
  701. }
  702. return $sRow;
  703. }
  704. public function isInputVisible($mixedInput)
  705. {
  706. if(!is_array($mixedInput) && isset($this->aInputs[$mixedInput]))
  707. $mixedInput = $this->aInputs[$mixedInput];
  708. if(empty($mixedInput) || !is_array($mixedInput))
  709. $mixedInput = BxDolFormQuery::getInputByName($this->aParams['object'], $mixedInput);
  710. if(empty($mixedInput) || !is_array($mixedInput))
  711. return false;
  712. if(!empty($mixedInput['privacy']) && !empty($this->_iAuthorId) && !$this->_isInputVisibleByPrivacy($mixedInput))
  713. return false;
  714. return true;
  715. }
  716. protected function _isInputVisibleByPrivacy($aInput)
  717. {
  718. $mixedResult = checkActionModule($this->_iAuthorId, 'set form fields privacy', 'system');
  719. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  720. return true;
  721. $aInputPrivacy = BxDolFormQuery::getInputPrivacy($aInput['id'], $this->_iAuthorId);
  722. if(empty($aInputPrivacy) || !is_array($aInputPrivacy))
  723. if(BxDolFormQuery::setInputPrivacy($aInput['id'], $this->_iAuthorId, BxDolPrivacy::getFieldName($this->_sPrivacyObjectView), $this->_sPrivacyGroupDefault))
  724. $aInputPrivacy = BxDolFormQuery::getInputPrivacy($aInput['id'], $this->_iAuthorId);
  725. if((empty($aInputPrivacy) || !is_array($aInputPrivacy)) && $this->_sPrivacyGroupDefault != BX_DOL_PG_ALL)
  726. return false;
  727. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_sPrivacyObjectView);
  728. if($oPrivacy && !$oPrivacy->check($aInputPrivacy['id']))
  729. return false;
  730. return true;
  731. }
  732. /**
  733. * Generate single Table Row for view mode
  734. *
  735. * @param array $aInput
  736. * @return string
  737. */
  738. function genViewRow(&$aInput)
  739. {
  740. if (!isset($aInput['type']))
  741. $aInput['type'] = false;
  742. if(!$this->isInputVisible($aInput))
  743. return '';
  744. if (!empty($aInput['name'])) {
  745. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  746. if (method_exists($this, $sCustomMethod))
  747. return $this->$sCustomMethod($aInput);
  748. }
  749. switch ($aInput['type']) {
  750. case 'block_header':
  751. $sRow = $this->genRowBlockHeader($aInput);
  752. break;
  753. case 'block_end':
  754. $sRow = $this->genBlockEnd($aInput);
  755. break;
  756. case 'nested_form':
  757. $sRow = $this->genNestedForm($aInput);
  758. break;
  759. case 'textarea':
  760. $sRow = $this->{'genViewRowWrapped' . ($aInput['html'] != 0 ? 'Html' : '')}($aInput);
  761. break;
  762. default:
  763. $sRow = $this->genViewRowWrapped($aInput);
  764. }
  765. return $sRow;
  766. }
  767. /**
  768. * Generate complete wrapped row for view mode form
  769. *
  770. * @param array $aInput
  771. * @return string
  772. */
  773. function genViewRowWrapped(&$aInput)
  774. {
  775. return $this->_genViewRowWrapped($aInput);
  776. }
  777. function genViewRowWrappedHtml(&$aInput)
  778. {
  779. return $this->_genViewRowWrapped($aInput, [
  780. 'class_value' => 'bx-def-vanilla-html max-w-none'
  781. ]);
  782. }
  783. protected function _genViewRowWrapped(&$aInput, $aParams = [])
  784. {
  785. $sValue = $this->genViewRowValue($aInput);
  786. if (null === $sValue)
  787. return '';
  788. $sClass = !empty($aParams['class']) ? $aParams['class'] : '';
  789. $sClassValue = !empty($aParams['class_value']) ? $aParams['class_value'] : '';
  790. if ($this->_isSecretInput($aInput)) {
  791. $sValue = $this->_wrapSecretViewRowValue($aInput, $sValue, $this->_isSecretRevealed($aInput['name']));
  792. $sClassValue .= ($sClassValue ? ' ' : '') . 'bx-form-row-view-value-secret';
  793. }
  794. $aTmplVarsIcon = [];
  795. if(!empty($aInput['icon']))
  796. $aTmplVarsIcon = [
  797. 'icon' => $this->genIcon($aInput['icon'])
  798. ];
  799. return $this->oTemplate->parseHtmlByName('form_view_row.html', [
  800. 'type' => $aInput['type'],
  801. 'class' => $sClass,
  802. 'bx_if:show_icon' => [
  803. 'condition' => !empty($aTmplVarsIcon),
  804. 'content' => $aTmplVarsIcon
  805. ],
  806. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  807. 'class_value' => $sClassValue,
  808. 'value' => $sValue
  809. ]);
  810. }
  811. /**
  812. * Generate value for view mode row
  813. *
  814. * @param array $aInput
  815. * @return string
  816. */
  817. function genViewRowValue(&$aInput)
  818. {
  819. if ($this->_isSecretInput($aInput) && !$this->_isSecretRevealed($aInput['name'])) {
  820. $sRaw = $this->_getSecretRawValue($aInput);
  821. return $sRaw !== '' ? bx_process_output(bx_gen_secret($sRaw)) : null;
  822. }
  823. if (!empty($aInput['name'])) {
  824. $sCustomMethod = 'genCustomViewRowValue' . $this->_genMethodName($aInput['name']);
  825. if (method_exists($this, $sCustomMethod))
  826. return $this->$sCustomMethod($aInput);
  827. }
  828. switch ($aInput['type']) {
  829. case 'hidden':
  830. $sValue = null;
  831. break;
  832. case 'select':
  833. case 'radio_set':
  834. $sValue = $this->genViewRowValueForSelect($aInput);
  835. break;
  836. case 'dateselect':
  837. case 'datepicker':
  838. $sValue = null;
  839. if (empty($aInput['value']) || !$aInput['value'] || '0000-00-00' == $aInput['value'])
  840. break;
  841. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass']) && !is_numeric($aInput['value']))
  842. $sValue = BxTemplFunctions::getInstance()->timeForJsFullDate($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format']: BX_FORMAT_DATE, true, true);
  843. else
  844. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  845. break;
  846. case 'date_time':
  847. case 'datetime':
  848. $sValue = null;
  849. if(empty($aInput['value']) || !$aInput['value'] || '0000-00-00 00:00:00' == $aInput['value'] || '0000-00-00 00:00' == $aInput['value'])
  850. break;
  851. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass']) && !is_numeric($aInput['value']))
  852. $sValue = BxTemplFunctions::getInstance()->timeForJsFullDate($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true, true);
  853. else
  854. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  855. break;
  856. case 'checkbox_set':
  857. case 'select_multiple':
  858. $sValue = null;
  859. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  860. $sValue = '';
  861. foreach ($aInput['value'] as $sVal)
  862. $sValue .= $aInput['values'][$sVal] . ', ';
  863. $sValue = trim ($sValue, ', ');
  864. }
  865. break;
  866. case 'checkbox':
  867. case 'switcher':
  868. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  869. break;
  870. case 'textarea':
  871. if (isset($aInput['value']) && '' !== $aInput['value'])
  872. $sValue = (isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : bx_process_output($aInput['value'], BX_DATA_TEXT_MULTILINE);
  873. else
  874. $sValue = null;
  875. break;
  876. case 'text':
  877. if(isset($aInput['value']) && '' !== $aInput['value'])
  878. $sValue = bx_linkify(bx_process_output($aInput['value']));
  879. else
  880. $sValue = null;
  881. break;
  882. case 'price':
  883. if(isset($aInput['value']) && $aInput['value'] !== '')
  884. $sValue = _t_format_currency(bx_process_output($aInput['value']));
  885. else
  886. $sValue = null;
  887. break;
  888. default:
  889. if(isset($aInput['value']) && '' !== $aInput['value']) {
  890. $sValue = $aInput['value'];
  891. if(get_mb_substr($sValue, 0, 1) == '_')
  892. $sValue = _t($sValue);
  893. $sValue = bx_process_output($sValue);
  894. }
  895. else
  896. $sValue = null;
  897. }
  898. return $sValue;
  899. }
  900. function genViewRowValueForSelect(&$aInput)
  901. {
  902. if (!isset($aInput['value']) || !$aInput['value'])
  903. return null;
  904. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  905. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])) !== false)
  906. return $oCategory->getCategoryLink($s, $aInput['value']);
  907. return $s;
  908. }
  909. protected function _isSecretInput($aInput)
  910. {
  911. return !empty($aInput['secret']) && !empty($aInput['name']);
  912. }
  913. protected function _isSecretRevealed($sName)
  914. {
  915. $mixed = bx_get('form_secret_reveal');
  916. if ($mixed === false || $sName === '' || $sName === null)
  917. return false;
  918. if (is_array($mixed)) {
  919. if (!in_array((string)$sName, array_map('strval', $mixed), true))
  920. return false;
  921. } else if ((string)$mixed !== (string)$sName) {
  922. return false;
  923. }
  924. if ($this->_bSecretRevealCsrfValid === null) {
  925. $mixedToken = bx_get('csrf_token');
  926. $this->_bSecretRevealCsrfValid = BxDolForm::isCsrfTokenValid($mixedToken === false ? '' : $mixedToken);
  927. }
  928. return $this->_bSecretRevealCsrfValid;
  929. }
  930. protected function _getSecretRawValue(&$aInput)
  931. {
  932. if (!isset($aInput['value']) || $aInput['value'] === '' || $aInput['value'] === null || is_array($aInput['value']))
  933. return '';
  934. return (string)$aInput['value'];
  935. }
  936. protected function _wrapSecretViewRowValue(&$aInput, $sValue, $bRevealed = false)
  937. {
  938. $mixedToken = !$bRevealed ? BxDolForm::getCsrfToken() : false;
  939. return $this->oTemplate->parseHtmlByName('form_view_secret.html', [
  940. 'class' => $bRevealed ? 'bx-form-secret-shown' : '',
  941. 'value' => $sValue,
  942. 'title' => bx_html_attribute(_t($bRevealed ? '_sys_form_secret_hide' : '_sys_form_secret_reveal')),
  943. 'name' => bx_html_attribute($aInput['name']),
  944. 'csrf' => $mixedToken ? bx_html_attribute($mixedToken) : '',
  945. 'icon' => $bRevealed ? 'eye-slash' : 'eye',
  946. ]);
  947. }
  948. protected function genCustomRowBirthday(&$aInput)
  949. {
  950. if(!$this->_bViewMode)
  951. return $this->genRowStandard($aInput);
  952. $sTxtAge = '_sys_form_input_age';
  953. $aInput = array_merge($aInput, [
  954. 'caption_src' => $sTxtAge,
  955. 'caption' => _t($sTxtAge),
  956. ]);
  957. if(bx_is_api())
  958. $aInput = array_merge($aInput, [
  959. 'type' => 'text',
  960. 'value' => bx_birthday2age($aInput['value'])
  961. ]);
  962. return $this->genViewRowWrapped($aInput);
  963. }
  964. protected function genCustomRowCf(&$aInput)
  965. {
  966. $aInput = BxDolContentFilter::getInstance()->getInput($aInput);
  967. if($aInput['type'] == 'hidden') {
  968. $this->_sCodeAdd .= $this->genInput($aInput);
  969. return '';
  970. }
  971. return $this->genRowStandard($aInput);
  972. }
  973. protected function genCustomViewRowValueBirthday(&$aInput)
  974. {
  975. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00', '0000-00-00Z', '0000-00-00 00:00:00Z')))
  976. return null;
  977. return bx_birthday2age($aInput['value']);
  978. }
  979. /**
  980. * Generate standard row
  981. *
  982. * @param array $aInput
  983. * @return string
  984. */
  985. function genRowStandard(&$aInput, $isOneLine = false)
  986. {
  987. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  988. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  989. $sHelp = !empty($aInput['help']) ? ' <a href="javascript:void(0)" onclick="javascript:' . $this->getJsObjectName() . '.showHelp(this, \'' . $aInput['name'] . '\')"><i class="sys-icon question-circle"></i></a> ' : '';
  990. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  991. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  992. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  993. $sClassWrapper = 'bx-form-element-wrapper';
  994. if($isOneLine)
  995. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  996. $sClassWrapper .= ' bx-def-margin-top-auto';
  997. if (isset($aInput['name'])) {
  998. if (!isset($aInput['tr_attrs']) || !is_array($aInput['tr_attrs']))
  999. $aInput['tr_attrs'] = [];
  1000. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  1001. }
  1002. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  1003. $sClassOneLineCaption = '';
  1004. $sClassOneLineValue = '';
  1005. if ($isOneLine) {
  1006. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' ltr:ml-4 rtl:mr-4';
  1007. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  1008. if (!isset($aInput['attrs']) || !is_array($aInput['attrs']))
  1009. $aInput['attrs'] = [];
  1010. $aInput['attrs']['id'] = $this->getInputId($aInput);
  1011. if ($sCaption)
  1012. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  1013. }
  1014. $sInput = $this->genInput($aInput);
  1015. if(isset($aInput['error_updated']) && $aInput['error_updated'] === true)
  1016. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  1017. if(empty($sErrorIcon))
  1018. $sErrorIcon = '';
  1019. $aTmplVarsRow = array(
  1020. 'bx_if:show_caption' => array(
  1021. 'condition' => !empty($sCaption),
  1022. 'content' => array(
  1023. 'class_caption' => $sClassOneLineCaption,
  1024. 'caption' => $sCaption,
  1025. 'required' => $sRequired,
  1026. 'help' => $sHelp,
  1027. )
  1028. ),
  1029. 'class_value' => $sClassAdd . $sClassOneLineValue,
  1030. 'value' => $this->genWrapperInput($aInput, $sInput),
  1031. 'info' => $sInfoIcon,
  1032. 'error' => $sErrorIcon,
  1033. );
  1034. return $this->oTemplate->parseHtmlByName('form_row_standard.html', array(
  1035. 'tr_attrs' => $sTrAttrs,
  1036. 'bx_if:show_one_line' => array(
  1037. 'condition' => $isOneLine,
  1038. 'content' => $aTmplVarsRow
  1039. ),
  1040. 'bx_if:show_lined' => array(
  1041. 'condition' => !$isOneLine,
  1042. 'content' => $aTmplVarsRow
  1043. ),
  1044. 'info' => $sInfoIcon,
  1045. 'error' => $sErrorIcon
  1046. ));
  1047. }
  1048. function genWrapperInput($aInput, $sContent)
  1049. {
  1050. $aAttrs = $this->_genWrapperInputAttrs($aInput);
  1051. $sClass = "bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}";
  1052. if(isset($aInput['html']) && $aInput['html'] && $this->isHtmlEditor($aInput['html'], $aInput))
  1053. $sClass .= ' bx-form-input-wrapper-html';
  1054. return $this->oTemplate->parseHtmlByName('form_input_wrapper.html', array(
  1055. 'attrs' => bx_convert_array2attrs($aAttrs, $sClass),
  1056. 'content' => $sContent
  1057. ));
  1058. }
  1059. protected function _genWrapperInputAttrs(&$aInput)
  1060. {
  1061. return isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? $aInput['attrs_wrapper'] : [];
  1062. }
  1063. /**
  1064. * Generate custom row
  1065. *
  1066. * @param array $aInput
  1067. * @param string $sCustomMethod custom method to generate code for input
  1068. * @return string
  1069. */
  1070. function genRowCustom(&$aInput, $sCustomMethod)
  1071. {
  1072. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  1073. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  1074. $sHelp = !empty($aInput['help']) ? ' <a href="javascript:void(0)" onclick="javascript:' . $this->getJsObjectName() . '.showHelp(this, \'' . $aInput['name'] . '\')"><i class="sys-icon question-circle"></i></a> ' : '';
  1075. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  1076. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  1077. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  1078. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  1079. if (isset($aInput['name'])) {
  1080. if (!isset($aInput['tr_attrs']) || !is_array($aInput['tr_attrs']))
  1081. $aInput['tr_attrs'] = [];
  1082. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  1083. }
  1084. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  1085. return $this->oTemplate->parseHtmlByName('form_row_custom.html', array(
  1086. 'tr_attrs' => $sTrAttrs,
  1087. 'bx_if:show_caption' => array(
  1088. 'condition' => !empty($sCaption),
  1089. 'content' => array(
  1090. 'class_caption' => '',
  1091. 'caption' => $sCaption,
  1092. 'required' => $sRequired,
  1093. 'help' => $sHelp
  1094. )
  1095. ),
  1096. 'class_value' => $sClassAdd,
  1097. 'value' => $sInput,
  1098. ));
  1099. }
  1100. /**
  1101. * Generate Block Headers row
  1102. *
  1103. * @param array $aInput
  1104. * @return string
  1105. */
  1106. function genRowBlockHeader(&$aInput)
  1107. {
  1108. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  1109. // if there is no caption - show divider only
  1110. if (empty($aInput['caption']))
  1111. return $this->{$this->_sSectionOpen}($aAttrs);
  1112. // if section is collapsed by default, add necessary code
  1113. $sClassAddCollapsable = 'bx-form-collapsable';
  1114. if(isset($aInput['collapsed']) && $aInput['collapsed'])
  1115. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  1116. // display section with caption
  1117. if(empty($aAttrs))
  1118. $aAttrs = ['class' => $sClassAddCollapsable];
  1119. else
  1120. $aAttrs['class'] .= ' ' . $sClassAddCollapsable;
  1121. $bViewMode = !empty($this->aParams['view_mode']);
  1122. $sCaption = bx_process_output($aInput['caption'], BX_DATA_HTML);
  1123. $sInfo = !empty($aInput['info']) ? bx_process_output($aInput['info']) : '';
  1124. $bInfo = !empty($sInfo);
  1125. $sTitle = $this->oTemplate->parseHtmlByName('form_field_section_title.html', [
  1126. 'bx_if:show_view' => [
  1127. 'condition' => $bViewMode,
  1128. 'content' => [
  1129. 'caption' => $sCaption,
  1130. 'bx_if:show_view_info' => [
  1131. 'condition' => $bInfo,
  1132. 'content' => [
  1133. 'info' => $sInfo
  1134. ]
  1135. ]
  1136. ]
  1137. ],
  1138. 'bx_if:show_default' => [
  1139. 'condition' => !$bViewMode,
  1140. 'content' => [
  1141. 'caption' => $sCaption,
  1142. 'bx_if:show_default_info' => [
  1143. 'condition' => $bInfo,
  1144. 'content' => [
  1145. 'info' => $sInfo
  1146. ]
  1147. ]
  1148. ]
  1149. ]
  1150. ]);
  1151. if (isset($aInput['name'])) {
  1152. if (!isset($aInput['tr_attrs']) || !is_array($aInput['tr_attrs']))
  1153. $aInput['tr_attrs'] = [];
  1154. $aInput['tr_attrs']['id'] = "bx-form-section-" . $aInput['name'];
  1155. }
  1156. return $this->{$this->_sSectionOpen}($aAttrs, $sTitle, !empty($aInput['tr_attrs']) ? $aInput['tr_attrs'] : []);
  1157. }
  1158. function genBlockEnd()
  1159. {
  1160. return $this->{$this->_sSectionClose}();
  1161. }
  1162. /**
  1163. * Generate HTML Input Element
  1164. *
  1165. * @param array $aInput
  1166. * @return string Output HTML Code
  1167. */
  1168. function genInput(&$aInput)
  1169. {
  1170. if (!empty($aInput['name'])) {
  1171. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  1172. if (method_exists($this, $sCustomMethod))
  1173. return $this->$sCustomMethod($aInput);
  1174. }
  1175. switch ($aInput['type']) {
  1176. case 'datepicker':
  1177. if (!isset($aInput['attrs']))
  1178. $aInput['attrs'] = array();
  1179. $aInput['attrs']['autosuggestion'] = 'off';
  1180. $sInput = $this->genInputStandard($aInput);
  1181. break;
  1182. // standard inputs (and non-standard, interpreted as standard)
  1183. case 'text':
  1184. case 'datepicker':
  1185. case 'dateselect':
  1186. case 'date_time':
  1187. case 'datetime':
  1188. case 'number':
  1189. case 'time':
  1190. case 'checkbox':
  1191. case 'radio':
  1192. case 'image':
  1193. case 'slider':
  1194. case 'doublerange':
  1195. case 'hidden':
  1196. $sInput = $this->genInputStandard($aInput);
  1197. break;
  1198. case 'phone':
  1199. $aInput['type'] = 'text';
  1200. $sInput = $this->genInputStandard($aInput);
  1201. break;
  1202. case 'password':
  1203. $sInput = $this->genInputPassword($aInput);
  1204. break;
  1205. case 'price':
  1206. $sInput = $this->genInputPrice($aInput);
  1207. break;
  1208. case 'file':
  1209. $sInput = $this->genInputFile($aInput);
  1210. break;
  1211. case 'rgb':
  1212. case 'rgba':
  1213. $sClass = 'bx-form-input-' . $aInput['type'];
  1214. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  1215. $aInput['type'] = 'text';
  1216. $sInput = $this->genInputStandard($aInput);
  1217. break;
  1218. case 'rgb-list':
  1219. $sInput = $this->genInputSelectRgb($aInput);
  1220. break;
  1221. case 'switcher':
  1222. $sInput = $this->genInputSwitcher($aInput);
  1223. break;
  1224. case 'button':
  1225. case 'reset':
  1226. case 'submit':
  1227. $sInput = $this->genInputButton($aInput);
  1228. break;
  1229. case 'textarea':
  1230. $sInput = $this->genInputTextarea($aInput);
  1231. break;
  1232. case 'select':
  1233. $sInput = $this->genInputSelect($aInput);
  1234. break;
  1235. case 'select_multiple':
  1236. $sInput = $this->genInputSelectMultiple($aInput);
  1237. break;
  1238. case 'checkbox_set':
  1239. $sInput = $this->genInputCheckboxSet($aInput);
  1240. break;
  1241. case 'radio_set':
  1242. $sInput = $this->genInputRadioSet($aInput);
  1243. break;
  1244. case 'input_set': // numeric array of inputs
  1245. $sInput = '';
  1246. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  1247. foreach ($aInput as $iKey => $aSubInput) {
  1248. if (!is_int($iKey) or !$aSubInput)
  1249. continue; // parse only integer keys and existing values
  1250. $sInput .= $this->genInput($aSubInput); // recursive call
  1251. $sInput .= $sDivider;
  1252. }
  1253. $sInput .= '<div class="bx-clear"></div>';
  1254. break;
  1255. case 'custom':
  1256. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  1257. break;
  1258. case 'captcha':
  1259. $sInput = $this->genInputCaptcha($aInput);
  1260. break;
  1261. case 'location':
  1262. $sInput = $this->genInputLocation($aInput);
  1263. break;
  1264. case 'nested_form':
  1265. $sInput = $this->genInputNestedForm($aInput);
  1266. break;
  1267. case 'value':
  1268. $sInput = '';
  1269. if(!isset($aInput['value']))
  1270. break;
  1271. $sInput = $aInput['value'];
  1272. if(get_mb_substr($sInput, 0, 1) == '_')
  1273. $sInput = _t($sInput);
  1274. break;
  1275. default:
  1276. //unknown control type
  1277. $sInput = 'Unknown control type';
  1278. }
  1279. // create input label
  1280. $sInput .= $this->genLabel($aInput);
  1281. // create input privacy group chooser
  1282. $sInput .= $this->genPrivacyGroupChooser($aInput);
  1283. return $sInput;
  1284. }
  1285. /**
  1286. * Generate new Input Element id
  1287. *
  1288. * @param array $aInput
  1289. * @return string
  1290. */
  1291. function getInputId(&$aInput)
  1292. {
  1293. if (isset($aInput['id']))
  1294. return $aInput['id'];
  1295. $sName = md5($aInput['name']);
  1296. $sID = $this->id . '_input_' . $sName;
  1297. if ( // multiple elements cause identical id's
  1298. (
  1299. (
  1300. $aInput['type'] == 'checkbox' and
  1301. substr($aInput['name'], -2) == '[]' // it is multiple element
  1302. ) or
  1303. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  1304. ) and
  1305. isset($aInput['value']) // if we can make difference
  1306. ) {
  1307. $sValue = md5($aInput['value']);
  1308. // add value
  1309. $sID .= '_' . $sValue;
  1310. }
  1311. $sID = trim($sID, '_');
  1312. $aInput['id'] = $sID; // just for repeated calls
  1313. return $sID;
  1314. }
  1315. /**
  1316. * Generate standard Input Element
  1317. *
  1318. * @param array $aInput
  1319. * @return string
  1320. */
  1321. function genInputStandard(&$aInput)
  1322. {
  1323. $aAttrs = $this->_genInputStandardAttrs($aInput);
  1324. $aAgent = [];
  1325. if(!empty($this->_aAgentsFormObject) && isset($aInput['name'])) {
  1326. foreach($this->_aAgentsFormObject as $a) {
  1327. if($a['form_object'] == $this->_sAgentFormObject && $a['form_input'] == $aInput['name']) {
  1328. $aAgent = $a;
  1329. break;
  1330. }
  1331. }
  1332. }
  1333. $sAgentButton = '';
  1334. if($aInput['type'] == 'text' && !empty($aAgent)) {
  1335. $sAgentButton = $this->oTemplate->parseHtmlByName('form_field_agent_button.html', [
  1336. 'input' => 'input',
  1337. 'name' => $aInput['name'],
  1338. 'agent_id' => $aAgent['id'] ?? 0,
  1339. 'form_id' => $this->getId(),
  1340. 'html' => $aInput['html'] ?? 0,
  1341. 'popup_text' => $aInput['agent_popup_text'] ?? _t('_sys_agents_agent_form_input_popup_text'),
  1342. ]);
  1343. }
  1344. return $this->oTemplate->parseHtmlByName('form_field_standard.html', [
  1345. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
  1346. 'agent_button' => $sAgentButton,
  1347. ]);
  1348. }
  1349. protected function _genInputStandardAttrs(&$aInput)
  1350. {
  1351. // clone attributes for system use ;)
  1352. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1353. // add default className to attributes
  1354. $aAttrs['type'] = $aInput['type'];
  1355. if ('datetime' == $aAttrs['type'])
  1356. $aAttrs['type'] = 'date_time';
  1357. switch($aAttrs['type']) {
  1358. case 'datepicker':
  1359. case 'date_time':
  1360. $this->addCssJsTimepicker ();
  1361. break;
  1362. case 'slider':
  1363. case 'doublerange':
  1364. $this->addCssJsUi ();
  1365. break;
  1366. case 'dateselect':
  1367. if(!empty($aInput['value']) && strpos($aInput['value'], ' ') !== false)
  1368. list($aInput['value']) = explode(' ', $aInput['value']);
  1369. $this->_addJs(array(
  1370. 'combodate/combodate.js',
  1371. ), "'undefined' === typeof($.combodate)");
  1372. break;
  1373. case 'text':
  1374. if (isset($aAttrs['class']) && false !== strpos($aAttrs['class'], 'bx-form-input-rgb'))
  1375. $this->addCssJsMinicolors ();
  1376. break;
  1377. }
  1378. if ('doublerange' == $aAttrs['type'] && (!isset($aInput['value']) || !$aInput['value']) && !empty($aAttrs['min']) && !empty($aAttrs['max'])) {
  1379. $aInput['value'] = $aAttrs['min'] . '-' . $aAttrs['max'];
  1380. }
  1381. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  1382. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  1383. if ($aInput['type'] == 'text' && ($aInput['secret'] ?? false) && ($sValue = $aAttrs['value'] ?? false))
  1384. $aAttrs['value'] = bx_gen_secret($sValue);
  1385. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  1386. if (!isset($aAttrs['data-format-24h']))
  1387. $aAttrs['data-frmt-24h'] = getParam('sys_format_input_24h');
  1388. if (!isset($aAttrs['data-format-date']))
  1389. $aAttrs['data-frmt-date'] = getParam('sys_format_date');
  1390. if (!isset($aAttrs['data-format-datetime']))
  1391. $aAttrs['data-frmt-datetime'] = getParam('sys_format_datetime');
  1392. if(($sAreaLabel = $this->_genInputAreaLabel($aInput) ?: bx_html_attribute($this->_genInputCaption($aInput))))
  1393. $aAttrs['area-label'] = $sAreaLabel;
  1394. // for inputs with labels generate id
  1395. if (isset($aInput['label']))
  1396. $aAttrs['id'] = $this->getInputId($aInput);
  1397. // for checkboxes
  1398. if (isset($aInput['checked']) and $aInput['checked'])
  1399. $aAttrs['checked'] = 'checked';
  1400. return $aAttrs;
  1401. }
  1402. /**
  1403. * Generate Switcher Input Element (based on checkbox)
  1404. *
  1405. * @param array $aInput
  1406. * @return string
  1407. */
  1408. function genInputSwitcher(&$aInput)
  1409. {
  1410. $aInput['type'] = 'checkbox';
  1411. $sCheckbox = $this->genInputStandard($aInput);
  1412. $aInput['type'] = 'switcher';
  1413. $sClass = 'off';
  1414. if(isset($aInput['checked']) && $aInput['checked'])
  1415. $sClass = 'on';
  1416. return $this->oTemplate->parseHtmlByName('form_field_switcher.html', [
  1417. 'class' => $sClass,
  1418. 'checkbox' => $sCheckbox
  1419. ]);
  1420. }
  1421. function genInputCheckbox(&$aInput, $bWrapped = false)
  1422. {
  1423. $sInput = $this->genInputStandard($aInput);
  1424. return $bWrapped ? $this->genWrapperInput($aInput, $sInput) : $sInput;
  1425. }
  1426. /**
  1427. * Generate Button Input Element
  1428. *
  1429. * @param array $aInput
  1430. * @return string
  1431. */
  1432. function genInputButton(&$aInput)
  1433. {
  1434. $aAttrs = $this->_genInputButtonAttrs($aInput);
  1435. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn";
  1436. if($aInput['type'] == 'submit')
  1437. $sClassAdd .= ' bx-btn-primary';
  1438. $aTmplVarsIcon = [];
  1439. if(isset($aInput['icon']) && ($sIcon = $this->genIcon($aInput['icon'])))
  1440. $aTmplVarsIcon = ['icon' => $sIcon];
  1441. return $this->oTemplate->parseHtmlByName('form_field_button.html', [
  1442. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1443. 'bx_if:show_icon' => [
  1444. 'condition' => !empty($aTmplVarsIcon),
  1445. 'content' => $aTmplVarsIcon
  1446. ],
  1447. 'value' => $aInput['value']
  1448. ]);
  1449. }
  1450. protected function _genInputButtonAttrs(&$aInput)
  1451. {
  1452. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1453. // add default className to attributes
  1454. $aAttrs['type'] = $aInput['type'];
  1455. if(isset($aInput['value']))
  1456. $aAttrs['value'] = $aInput['value'];
  1457. if(isset($aInput['name']))
  1458. $aAttrs['name'] = $aInput['name'];
  1459. if(($sAreaLabel = $this->_genInputAreaLabel($aInput) ?: bx_html_attribute($aInput['value'])))
  1460. $aAttrs['area-label'] = $sAreaLabel;
  1461. return $aAttrs;
  1462. }
  1463. /**
  1464. * Generate Textarea Element
  1465. *
  1466. * @param array $aInput
  1467. * @return string
  1468. */
  1469. function genInputTextarea(&$aInput)
  1470. {
  1471. $aAttrs = $this->_genInputTextareaAttrs($aInput);
  1472. $sUniq = genRndPwd(10, false);
  1473. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']}";
  1474. $bHtml = $bHtmlEditor = false;
  1475. if(($bHtmlEditor = ($bHtml = (bool)($aInput['html'] ?? false)) && $this->addHtmlEditor($aInput['html'], $aInput, $sUniq))) {
  1476. $sClassAdd .= ' bx-form-input-html ' . $sUniq;
  1477. }
  1478. $bCode = $bCodeEditor = false;
  1479. if(($bCodeEditor = ($bCode = (bool)($aInput['code'] ?? false)) && $this->addCodeEditor($aInput['code'], $aInput, $sUniq))) {
  1480. $sClassAdd .= ' bx-form-input-code ' . $sUniq;
  1481. }
  1482. $aTmplVarsSwitcher = [];
  1483. if(($aInput['html_toggle'] ?? false) && $bHtmlEditor && ($aHtmlEditor = $this->getHtmlEditorInfo($aInput['html'], $aInput, $sUniq))) {
  1484. if(($sJsInit = ($aHtmlEditor['js_call_init'] ?? false)) && ($sJsDestroy = ($aHtmlEditor['js_call_destroy'] ?? false))) {
  1485. $aInputSwitcher = [
  1486. 'type' => 'switcher',
  1487. 'name' => $aInput['name'] . '_switcher',
  1488. 'checked' => true,
  1489. 'attrs' => [
  1490. 'onchange' => "if($(this).prop('checked')) " . $sJsInit . "; else " . $sJsDestroy . ";",
  1491. ],
  1492. ];
  1493. $aTmplVarsSwitcher = [
  1494. 'class' => 'bx-form-input-' . $aInput['type'] . '-switcher',
  1495. 'title' => _t('_sys_form_input_textarea_editor'),
  1496. 'switcher' => $this->genInputSwitcher($aInputSwitcher)
  1497. ];
  1498. }
  1499. }
  1500. $bTagsAllowed = $bHtml || $bCode || ($aInput['html_tags'] ?? false);
  1501. $sValue = isset($aInput['value']) ? bx_process_output($bTagsAllowed ? $aInput['value'] : strip_tags($aInput['value']), BX_DATA_TEXT, ['no_process_macros']) : '';
  1502. if(($aInput['secret'] ?? false) && $sValue)
  1503. $sValue = bx_gen_secret($sValue);
  1504. $aAgent = [];
  1505. if(!empty($this->_aAgentsFormObject)) {
  1506. foreach($this->_aAgentsFormObject as $a) {
  1507. if($a['form_object'] == $this->_sAgentFormObject && $a['form_input'] == $aInput['name']) {
  1508. $aAgent = $a;
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. $sAgentButton = '';
  1514. if(!empty($aAgent)) {
  1515. $sAgentButton = $this->oTemplate->parseHtmlByName('form_field_agent_button.html', [
  1516. 'input' => 'textarea',
  1517. 'name' => $aInput['name'],
  1518. 'agent_id' => $aAgent['id'] ?? 0,
  1519. 'form_id' => $this->getId(),
  1520. 'html' => $aInput['html'] ?? 0,
  1521. 'popup_text' => $aInput['agent_popup_text'] ?? _t('_sys_agents_agent_form_input_popup_text'),
  1522. ]);
  1523. }
  1524. return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
  1525. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1526. 'value' => $sValue,
  1527. 'bx_if:show_switcher' => [
  1528. 'condition' => !empty($aTmplVarsSwitcher),
  1529. 'content' => $aTmplVarsSwitcher
  1530. ],
  1531. 'agent_button' => $sAgentButton,
  1532. ]);
  1533. }
  1534. protected function _genInputTextareaAttrs(&$aInput)
  1535. {
  1536. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1537. $aAttrs['name'] = $aInput['name'];
  1538. if(($sAreaLabel = $this->_genInputAreaLabel($aInput) ?: bx_html_attribute($this->_genInputCaption($aInput))))
  1539. $aAttrs['area-label'] = $sAreaLabel;
  1540. // for inputs with labels generate id
  1541. if (isset($aInput['label']))
  1542. $aAttrs['id'] = $this->getInputId($aInput);
  1543. return $aAttrs;
  1544. }
  1545. function isHtmlEditor($iViewMode, &$aInput)
  1546. {
  1547. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  1548. }
  1549. function addHtmlEditor($iViewMode, &$aInput, $sUniq)
  1550. {
  1551. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1552. if(!$oEditor)
  1553. return false;
  1554. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name=' . $aInput['name'] . ']', $iViewMode, $this->_bDynamicMode, [
  1555. 'form_id' => $this->aFormAttrs['id'],
  1556. 'element_name' => $aInput['name'],
  1557. 'query_params' => $this->getHtmlEditorQueryParams($aInput),
  1558. 'uniq' => $sUniq
  1559. ]);
  1560. return true;
  1561. }
  1562. function getHtmlEditorInfo($iViewMode, &$aInput, $sUniq)
  1563. {
  1564. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1565. if(!$oEditor)
  1566. return false;
  1567. return $oEditor->getEditorInfo('#' . $this->aFormAttrs['id'] . ' [name=' . $aInput['name'] . ']', $iViewMode, $this->_bDynamicMode, [
  1568. 'form_id' => $this->aFormAttrs['id'],
  1569. 'element_name' => $aInput['name'],
  1570. 'uniq' => $sUniq
  1571. ]);
  1572. }
  1573. function addCodeEditor($iViewMode, &$aInput, $sUniq)
  1574. {
  1575. $this->addCssJsCodeMirror();
  1576. return true;
  1577. }
  1578. function getHtmlEditorQueryParams($aInput)
  1579. {
  1580. $aQueryParams = ['i' => $aInput['name'], 'f' => $this->aFormAttrs['id'], 'fi' => ''];
  1581. /**
  1582. * @hooks
  1583. * @hookdef hook-system-editor_query_params 'system', 'editor_query_params' - hook to override http(s) request's query params, which is used in HTML editor
  1584. * - $unit_name - equals `system`
  1585. * - $action - equals `editor_query_params`
  1586. * - $object_id - not used
  1587. * - $sender_id - not used
  1588. * - $extra_params - array of additional params with the following array keys:
  1589. * - `form` - [object] an instance of form, @see BxDolForm
  1590. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  1591. * @hook @ref hook-system-editor_query_params
  1592. */
  1593. bx_alert('system', 'editor_query_params', 0, 0, [
  1594. 'form' => $this,
  1595. 'override_result' => &$aQueryParams
  1596. ]);
  1597. return $aQueryParams;
  1598. }
  1599. function getMentionsQueryParams($aInput)
  1600. {
  1601. return [];
  1602. }
  1603. /**
  1604. * Generate Select Box Element
  1605. *
  1606. * @param array $aInput
  1607. * @return string
  1608. */
  1609. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  1610. {
  1611. $aNewInput = $aInput;
  1612. $aNewInput['type'] = 'select';
  1613. $aNewInput['name'] .= '[]';
  1614. $sInput = $this->genInput($aNewInput);
  1615. return <<<BLAH
  1616. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  1617. $sInput
  1618. </div>
  1619. $sInfo
  1620. $sError
  1621. BLAH;
  1622. }
  1623. /**
  1624. * Generate Browse File Element
  1625. *
  1626. * @param array $aInput
  1627. * @return string
  1628. */
  1629. function genInputFile(&$aInput)
  1630. {
  1631. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  1632. if(!empty($aInput['attrs']['onchange']))
  1633. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  1634. else
  1635. $aInput['attrs']['onchange'] = $sOnChange;
  1636. $sInput = $this->genInputStandard($aInput);
  1637. return '<label class="bx-btn">' . $sInput . '<span class="bx-fif-label">' . _t('_sys_form_txt_select_file') . '</span></label><span class="bx-fif-value bx-def-margin-thd-left"></span>';
  1638. }
  1639. /**
  1640. * Generate Select Box Element
  1641. *
  1642. * @param array $aInput
  1643. * @return string
  1644. */
  1645. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  1646. {
  1647. if(empty($aInput['uploaders']) || !is_array($aInput['uploaders']))
  1648. return '';
  1649. $sUniqId = !empty($aInput['uploaders_id']) ? $aInput['uploaders_id'] : genRndPwd (8, false);
  1650. $bMultiple = $aInput['multiple'] ?? true;
  1651. $bInitGhosts = isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1;
  1652. $bInitReordering = empty($aInput['init_reordering']) ? 0 : 1;
  1653. $oUploader = null;
  1654. $sUploadersButtons = $sUploadersJs = '';
  1655. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1656. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  1657. if(!$oUploader)
  1658. continue;
  1659. $oUploader->setMultiple($bMultiple);
  1660. //--- Get Button code.
  1661. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1662. if(empty($aInput['attrs']['disabled']))
  1663. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  1664. $aParamsButtons = [
  1665. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1666. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1667. 'attrs' => bx_convert_array2attrs($aAttrs),
  1668. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  1669. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  1670. ];
  1671. $sUploadersButtons .= $oUploader->getUploaderButton($aParamsButtons);
  1672. //--- Get JS code.
  1673. $sGhostTemplate = $this->genGhostTemplate($aInput);
  1674. $aParamsJs = array_merge($oUploader->getUploaderJsParams(),
  1675. [
  1676. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1677. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1678. 'is_init_ghosts' => $bInitGhosts,
  1679. 'is_init_reordering' => $bInitReordering
  1680. ]
  1681. );
  1682. if(isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  1683. $aParamsJs['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  1684. $sUploadersJs .= $oUploader->getUploaderJs($sGhostTemplate, $bMultiple, $aParamsJs, $this->_bDynamicMode);
  1685. }
  1686. if(!$oUploader)
  1687. return '';
  1688. if($bInitReordering) {
  1689. $this->addCssJsUi();
  1690. $this->addCssJsUiSortable();
  1691. }
  1692. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  1693. 'uploaders_buttons' => $sUploadersButtons,
  1694. 'info' => $sInfo,
  1695. 'error' => $sError,
  1696. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  1697. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  1698. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  1699. 'uploaders_js' => $sUploadersJs,
  1700. ));
  1701. }
  1702. protected function genGhostTemplate(&$aInput)
  1703. {
  1704. $sGhostTemplate = false;
  1705. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  1706. $oFormNested = $aInput['ghost_template'];
  1707. if ($oFormNested instanceof BxDolFormNestedGhost)
  1708. $sGhostTemplate = $oFormNested->genForm();
  1709. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && isset($aInput['ghost_template']['inputs'])) { // form is not submitted and ghost template is form array
  1710. $oFormNested = new BxDolFormNestedGhost($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  1711. $sGhostTemplate = $oFormNested->getCode();
  1712. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  1713. $sGhostTemplate = array ();
  1714. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  1715. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNestedGhost)
  1716. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  1717. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  1718. $sGhostTemplate = $aInput['ghost_template'];
  1719. }
  1720. return $sGhostTemplate;
  1721. }
  1722. protected function genCustomInputUsernamesSuggestions ($aInput)
  1723. {
  1724. $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
  1725. $aAttrs = $this->_genCustomInputUsernamesSuggestionsAttrs($aInput, $bDisabled);
  1726. $aTmplVarsVals = [];
  1727. $sValue = '';
  1728. if(!empty($aInput['value'])) {
  1729. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']) . 'Value';
  1730. if(method_exists($this, $sCustomMethod))
  1731. $aTmplVarsVals = $this->$sCustomMethod($aInput);
  1732. else if(is_array($aInput['value']) || (is_numeric($aInput['value']))) {
  1733. if (!is_array($aInput['value']))
  1734. $aInput['value'] = [$aInput['value']];
  1735. foreach($aInput['value'] as $sVal) {
  1736. if(!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  1737. continue;
  1738. $aTmplVarsVals[] = [
  1739. 'item_unit' => $oProfile->getUnit(0, ['template' => ['name' => 'unit_wo_info', 'size' => 'icon']]),
  1740. 'item_name' => $oProfile->getDisplayName(),
  1741. 'name' => $aInput['name'] . (isset($aInput['custom']['only_once']) && $aInput['custom']['only_once'] == 1 ? '' : '[]'),
  1742. 'value' => $sVal
  1743. ];
  1744. }
  1745. }
  1746. else if(is_string($aInput['value']))
  1747. $sValue = $aInput['value'];
  1748. }
  1749. $aTmplVarsInputText = [];
  1750. if(!$bDisabled) {
  1751. $aInputText = $aInput;
  1752. $aInputText['type'] = 'text';
  1753. $aInputText['value'] = '';
  1754. $aInputText['attrs'] = $this->_genCustomInputUsernamesSuggestionsTextAttrs($aInputText, $bDisabled);
  1755. unset($aInputText['name']);
  1756. $aTmplVarsInputText['input'] = $this->genInputStandard($aInputText);
  1757. }
  1758. if (bx_is_api()){
  1759. $aInput['type'] = 'initial_members';
  1760. return $aInput;
  1761. }
  1762. $this->addCssJsUi();
  1763. $sJsCode = '';
  1764. if(!$bDisabled) {
  1765. $sJsCode = $this->oTemplate->parseHtmlByName('form_field_custom_suggestions_js.html', [
  1766. 'id' => $aAttrs['id'],
  1767. 'name' => $aInput['name'],
  1768. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  1769. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  1770. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  1771. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  1772. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  1773. ]);
  1774. if($this->_bDynamicMode)
  1775. $sJsCode = $this->oTemplate->addJsPreloadedWrapped([
  1776. 'jquery-ui/jquery-ui.min.js'
  1777. ], $sJsCode);
  1778. else
  1779. $sJsCode = $this->oTemplate->addJsCodeOnLoadWrapped($sJsCode);
  1780. }
  1781. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  1782. 'name' => $aInput['name'],
  1783. 'attrs' => bx_convert_array2attrs($aAttrs),
  1784. 'bx_repeat:vals' => $aTmplVarsVals,
  1785. 'value' => $sValue,
  1786. 'bx_if:input' => array(
  1787. 'condition' => !$bDisabled,
  1788. 'content' => $aTmplVarsInputText
  1789. ),
  1790. 'js_code' => $sJsCode
  1791. ));
  1792. }
  1793. protected function _genCustomInputUsernamesSuggestionsAttrs (&$aInput, $bDisabled = false)
  1794. {
  1795. $aAttrs = ['id' => $aInput['name'] . time() . mt_rand(0, 100)];
  1796. $aAttrs['class'] = 'bx-form-input-autotoken bx-form-input-text bx-def-font-inputs';
  1797. if($bDisabled)
  1798. $aAttrs['class'] .= ' bx-form-input-disabled';
  1799. if(!empty($aInput['attrs']['class']))
  1800. $aAttrs['class'] .= ' ' . $aInput['attrs']['class'];
  1801. return $aAttrs;
  1802. }
  1803. protected function _genCustomInputUsernamesSuggestionsTextAttrs (&$aInput, $bDisabled = false)
  1804. {
  1805. $aAttrs = ['value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off'];
  1806. if(!empty($aInput['attrs']) && is_array($aInput['attrs'])) {
  1807. if(isset($aInput['attrs']['class']))
  1808. unset($aInput['attrs']['class']);
  1809. $aAttrs = array_merge($aAttrs, $aInput['attrs']);
  1810. }
  1811. return $aAttrs;
  1812. }
  1813. protected function genCustomViewRowValueLabels ($aInput)
  1814. {
  1815. if (empty($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  1816. return null;
  1817. if (!empty($aInput['value']) && !is_array($aInput['value']))
  1818. $aInput['value'] = unserialize($aInput['value']);
  1819. $s = '';
  1820. foreach ($aInput['value'] as $sLabel)
  1821. $s .= '<a href="' . $oMetatags->keywordsGetHashTagUrl($sLabel, $aInput['content_id']) . '"><b class="bx-def-label val">' . trim($sLabel) . '</b></a>';
  1822. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  1823. 'values' => $s,
  1824. ));
  1825. }
  1826. protected function genCustomInputLabels (&$aInput)
  1827. {
  1828. $oLabel = BxDolLabel::getInstance();
  1829. $sName = !empty($aInput['name']) ? $aInput['name'] : 'labels';
  1830. $sValue = '';
  1831. if(!empty($aInput['value'])) {
  1832. if(!is_array($aInput['value']))
  1833. $aInput['value'] = unserialize($aInput['value']);
  1834. if(is_array($aInput['value']))
  1835. foreach($aInput['value'] as $sLabel)
  1836. $sValue .= $oLabel->getLabel($sName, $sLabel);
  1837. }
  1838. $sKeyPlaceholder = $aInput['caption_src'] . '_placeholder';
  1839. if(strcmp($sKeyPlaceholder, _t($sKeyPlaceholder)) != 0)
  1840. $sValue .= $oLabel->getLabelPlaceholder($sKeyPlaceholder);
  1841. $aInputLabels = array(
  1842. 'type' => 'custom',
  1843. 'name' => $sName,
  1844. 'caption' => '',
  1845. 'value' => $sValue,
  1846. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . bx_append_url_params('label.php', array(
  1847. 'action' => 'labels_list',
  1848. )),
  1849. 'attrs' => array(
  1850. 'class' => 'bx-form-input-labels',
  1851. 'disabled' => 'disabled'
  1852. )
  1853. );
  1854. if (bx_is_api()) {
  1855. return array_merge($aInput, [
  1856. 'type' => 'labels',
  1857. 'values' => bx_srv('system', 'get_labels', [], 'TemplLabelsServices')
  1858. ]);
  1859. }
  1860. return $this->oTemplate->parseHtmlByName('label_select_field.html', array(
  1861. 'js_object' => $oLabel->getJsObjectName(),
  1862. 'js_code' => $oLabel->getJsCodeForm(),
  1863. 'html_id' => $oLabel->getFormFieldId($aInput),
  1864. 'name' => $sName,
  1865. 'input_labels' => $this->genCustomInputUsernamesSuggestions($aInputLabels)
  1866. ));
  1867. }
  1868. /**
  1869. * Generate Select Element
  1870. *
  1871. * @param array $aInput
  1872. * @return string
  1873. */
  1874. function genInputSelect(&$aInput)
  1875. {
  1876. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1877. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1878. }
  1879. /**
  1880. * Generate Select RGB Element
  1881. *
  1882. * @param array $aInput
  1883. * @return string
  1884. */
  1885. function genInputSelectRgb(&$aInput)
  1886. {
  1887. $aInput['type'] = 'select';
  1888. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1889. $aInput['values_list_name'] = 'sys_colors';
  1890. $aInput['values'] = self::getDataItems('sys_colors', isset(self::$TYPES_SET[$aInput['type']]));
  1891. $aInput['attrs'] = ['class' => 'bx-form-input-rgb-list'];
  1892. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1893. $aInput['values'][$sOptValue] = ['key' => $sOptValue, 'value' => $sOptTitle, 'class' => 'bg-' . $sOptValue, 'style' => 'color:#fff'];
  1894. }
  1895. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1896. }
  1897. /**
  1898. * Generate Multiple Select Element
  1899. *
  1900. * @param array $aInput
  1901. * @return string
  1902. */
  1903. function genInputSelectMultiple(&$aInput)
  1904. {
  1905. $aCurValues = array();
  1906. if (isset($aInput['value']) && $aInput['value'])
  1907. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1908. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  1909. }
  1910. /**
  1911. * Generate Checkbox Set Element
  1912. *
  1913. * @param array $aInput
  1914. * @return string
  1915. */
  1916. function genInputCheckboxSet(&$aInput)
  1917. {
  1918. $aCurValues = array();
  1919. if (isset($aInput['value']) && $aInput['value'])
  1920. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1921. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  1922. }
  1923. /**
  1924. * Generate Radiobuttons Set Element
  1925. *
  1926. * @param array $aInput
  1927. * @return string
  1928. */
  1929. function genInputRadioSet(&$aInput)
  1930. {
  1931. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1932. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  1933. }
  1934. function _isSelected ($sValue, $sCurValue)
  1935. {
  1936. return ((string)$sValue === (string)$sCurValue);
  1937. }
  1938. function _isSelectedMultiple ($sValue, $aCurValues)
  1939. {
  1940. return in_array($sValue, $aCurValues);
  1941. }
  1942. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  1943. {
  1944. $aOptions = [];
  1945. if(isset($aInput['values']) && is_array($aInput['values'])) {
  1946. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1947. $aOption = array(
  1948. 'bx_if:show_option_group_begin' => [
  1949. 'condition' => false,
  1950. 'content' => []
  1951. ],
  1952. 'bx_if:show_option_group_end' => [
  1953. 'condition' => false,
  1954. 'content' => []
  1955. ],
  1956. 'bx_if:show_option' => [
  1957. 'condition' => false,
  1958. 'content' => []
  1959. ]
  1960. );
  1961. $sOptAttrs = "";
  1962. if(is_array($sOptTitle)) {
  1963. if(isset($sOptTitle['type'])) {
  1964. switch($sOptTitle['type']) {
  1965. case 'group_header':
  1966. $aOption = array_merge($aOption, [
  1967. 'bx_if:show_option_group_begin' => [
  1968. 'condition' => true,
  1969. 'content' => [
  1970. 'opt_label' => bx_process_output($sOptTitle['value'])
  1971. ]
  1972. ]
  1973. ]);
  1974. $aOptions[] = $aOption;
  1975. break;
  1976. case 'group_end':
  1977. $aOption = array_merge($aOption, [
  1978. 'bx_if:show_option_group_end' => [
  1979. 'condition' => true,
  1980. 'content' => []
  1981. ]
  1982. ]);
  1983. $aOptions[] = $aOption;
  1984. break;
  1985. }
  1986. continue;
  1987. }
  1988. $aOptAttrs = $this->_genInputSelectOptionAttrs($sOptTitle);
  1989. if(!empty($aOptAttrs))
  1990. $sOptAttrs = bx_convert_array2attrs($aOptAttrs);
  1991. $sOptValue = $sOptTitle['key'];
  1992. $sOptTitle = $sOptTitle['value'];
  1993. }
  1994. $aOption = array_merge($aOption, [
  1995. 'bx_if:show_option' => [
  1996. 'condition' => true,
  1997. 'content' => [
  1998. 'opt_title' => bx_process_output($sOptTitle),
  1999. 'opt_value' => !empty($sOptValue) ? bx_html_attribute($sOptValue) : '',
  2000. 'opt_attrs' => $sOptAttrs,
  2001. 'bx_if:show_selected' => [
  2002. 'condition' => $this->$sIsSelectedFunc($sOptValue, $mixedCurrentVal),
  2003. 'content' => []
  2004. ]
  2005. ]
  2006. ]
  2007. ]);
  2008. $aOptions[] = $aOption;
  2009. }
  2010. }
  2011. $aAttrs = $this->_genInputSelectAttrs($aInput, $isMultiple);
  2012. return $this->_parseInputSelect('form_field_select.html', [
  2013. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
  2014. 'bx_repeat:options' => $aOptions,
  2015. 'content' => !empty($aInput['content']) ? $aInput['content'] : ''
  2016. ]);
  2017. }
  2018. protected function _genInputSelectAttrs(&$aInput, $isMultiple)
  2019. {
  2020. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  2021. $aAttrs['name'] = $aInput['name'];
  2022. if ($isMultiple) {
  2023. $aAttrs['name'] .= '[]';
  2024. $aAttrs['multiple'] = 'multiple';
  2025. }
  2026. if(($sAreaLabel = $this->_genInputAreaLabel($aInput) ?: bx_html_attribute($this->_genInputCaption($aInput))))
  2027. $aAttrs['area-label'] = $sAreaLabel;
  2028. // for inputs with labels generate id
  2029. if (isset($aInput['label']))
  2030. $aAttrs['id'] = $this->getInputId($aInput);
  2031. return $aAttrs;
  2032. }
  2033. protected function _genInputSelectOptionAttrs(&$aOption)
  2034. {
  2035. $aAttrs = [];
  2036. if(!empty($aOption['attrs']) && is_array($aOption['attrs']))
  2037. $aAttrs = $aOption['attrs'];
  2038. if(isset($aOption['class']))
  2039. $aAttrs['class'] = !empty($aAttrs['class']) ? $aAttrs['class'] . ' ' . $aOption['class'] : $aOption['class'];
  2040. if(isset($aOption['style']))
  2041. $aAttrs['style'] = !empty($aAttrs['style']) ? $aAttrs['style'] . ' ' . $aOption['style'] : $aOption['style'];
  2042. return $aAttrs;
  2043. }
  2044. protected function _parseInputSelect($sTmplName, $aTmplVars)
  2045. {
  2046. return $this->oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  2047. }
  2048. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  2049. {
  2050. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  2051. $aAttrs['name'] = $aInput['name'];
  2052. if(($sAreaLabel = $this->_genInputAreaLabel($aInput) ?: bx_html_attribute($this->_genInputCaption($aInput))))
  2053. $aAttrs['area-label'] = $sAreaLabel;
  2054. // for inputs with labels generate id
  2055. if (isset($aInput['label']))
  2056. $aAttrs['id'] = $this->getInputId($aInput);
  2057. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  2058. // generate options
  2059. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  2060. $iDividerThreshold = !empty($aInput['dv_thd']) ? (int)$aInput['dv_thd'] : 3;
  2061. $sOptions = '';
  2062. $aAdditionalFieldInfo = [];
  2063. if (in_array($aInput['type'], ['radio_set', 'checkbox_set']) && !empty($aInput['values_list_name']))
  2064. $aAdditionalFieldInfo = BxDolFormQuery::getDataItems($aInput['values_list_name'], false, BX_DATA_VALUES_ALL);
  2065. if (isset($aInput['values']) && is_array($aInput['values'])) {
  2066. if (count($aInput['values']) > $iDividerThreshold && $sDivider == $this->_sDivider)
  2067. $sDivider = $this->_sDividerAlt;
  2068. // generate complex input using simple standard inputs
  2069. foreach ($aInput['values'] as $sValue => $sLabel) {
  2070. if(is_array($sLabel)) {
  2071. $sValue = $sLabel['key'];
  2072. $sLabel = $sLabel['value'];
  2073. }
  2074. // create new simple input
  2075. $aNewInput = array(
  2076. 'type' => $sType,
  2077. 'name' => $aInput['name'] . $sNameAppend,
  2078. 'value' => $sValue,
  2079. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  2080. 'label' => $sLabel,
  2081. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  2082. );
  2083. if (isset($aInput['label_as_html']))
  2084. $aNewInput['label_as_html'] = $aInput['label_as_html'];
  2085. $sNewInput = $this->genInputCheckbox($aNewInput, true) . $this->genLabel($aNewInput);
  2086. // add additional info for the field
  2087. if (isset($aAdditionalFieldInfo[$sValue]) && !empty($aAdditionalFieldInfo[$sValue]['LKey2']))
  2088. $sNewInput = $this->genFiledItemInfoWrapper($sNewInput, _t($aAdditionalFieldInfo[$sValue]['LKey2']));
  2089. // attach new input to complex
  2090. $sOptions .= ($sNewInput . $sDivider);
  2091. }
  2092. }
  2093. // generate element
  2094. $sCode = <<<BLAH
  2095. <div $sAttrs>
  2096. $sOptions
  2097. </div>
  2098. BLAH;
  2099. return $sCode;
  2100. }
  2101. function genInputLocation(&$aInput)
  2102. {
  2103. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  2104. if (!$o)
  2105. return _t('_sys_txt_error_occured');
  2106. return $o->genInputLocation($aInput, $this);
  2107. }
  2108. public function setLocationVals ($aInput, $aVals)
  2109. {
  2110. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  2111. if (!$o)
  2112. return false;
  2113. return $o->setLocationVals ($aInput, $aVals, $this);
  2114. }
  2115. public function setLocationVal ($aInput, $sIndex, $sVal)
  2116. {
  2117. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  2118. if (!$o)
  2119. return false;
  2120. return $o->setLocationVal ($aInput, $sIndex, $sVal, $this);
  2121. }
  2122. protected function getLocationVal ($aInput, $sIndex)
  2123. {
  2124. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  2125. if (!$o)
  2126. return false;
  2127. return $o->getLocationVal ($aInput, $sIndex, $this);
  2128. }
  2129. function genInputPassword(&$aInput)
  2130. {
  2131. return $this->oTemplate->parseHtmlByName('form_field_password.html', array('input' => $this->genInputStandard($aInput)));
  2132. }
  2133. function genInputPrice(&$aInput)
  2134. {
  2135. $sCurrency = '';
  2136. if(!isset($aInput['value_currency'])) {
  2137. $sCurrency = BxDolPayments::getInstance()->getOption('default_currency_code');
  2138. if(empty($sCurrency))
  2139. $sCurrency = getParam('currency_code');
  2140. }
  2141. else
  2142. $sCurrency = $aInput['value_currency'];
  2143. return $this->oTemplate->parseHtmlByName('form_field_price.html', array(
  2144. 'bx_if:show_currency' => [
  2145. 'condition' => !empty($sCurrency),
  2146. 'content' => [
  2147. 'currency' => $sCurrency,
  2148. ]
  2149. ],
  2150. 'input' => $this->genInputStandard($aInput)
  2151. ));
  2152. }
  2153. function genInputNestedForm(&$aInput)
  2154. {
  2155. return '';
  2156. }
  2157. function genNestedForm(&$aInput)
  2158. {
  2159. return '';
  2160. }
  2161. function genInputCaptcha(&$aInput)
  2162. {
  2163. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  2164. // for inputs with labels generate id
  2165. if (isset($aInput['label']))
  2166. $aAttrs['id'] = $this->getInputId($aInput);
  2167. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  2168. $oCaptcha = BxDolCaptcha::getObjectInstance();
  2169. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  2170. }
  2171. /**
  2172. * Generate Label Element
  2173. *
  2174. * @param string $aInput input array
  2175. * @return string HTML code
  2176. */
  2177. function genLabel(&$aInput)
  2178. {
  2179. if (!isset($aInput['label']) || empty($aInput['label']))
  2180. return '';
  2181. $sInputID = $this->getInputId($aInput);
  2182. $sLabel = bx_process_output($aInput['label']);
  2183. if (isset($aInput['label_as_html']) && $aInput['label_as_html'] == true)
  2184. $sLabel = $aInput['label'];
  2185. return '<label for="' . $sInputID . '">' . $sLabel . '</label>';
  2186. }
  2187. function genPrivacyGroupChooser(&$aInput, $sPrivacyObject = '')
  2188. {
  2189. if(!isset($aInput['privacy']) || empty($aInput['privacy']))
  2190. return '';
  2191. $iAuthorId = bx_get_logged_profile_id();
  2192. $mixedResult = checkActionModule($iAuthorId, 'set form fields privacy', 'system');
  2193. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  2194. return '';
  2195. if(empty($sPrivacyObject))
  2196. $sPrivacyObject = $this->_sPrivacyObjectView;
  2197. $iInputId = (int)$aInput['id'];
  2198. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  2199. if(!isset($aInput['attrs_wrapper']['class']))
  2200. $aInput['attrs_wrapper']['class'] = '';
  2201. $aInput['attrs_wrapper']['class'] .= ' bx-form-input-wrapper-pgc';
  2202. return $this->oTemplate->parseHtmlByName('form_field_privacy.html', array(
  2203. 'js_object' => $this->getJsObjectName(),
  2204. 'html_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  2205. 'id' => $iInputId,
  2206. 'privacy_object' => $sPrivacyObject,
  2207. 'icon' => $this->_getPrivacyIcon($mixedPrivacyGroup)
  2208. ));
  2209. }
  2210. function genFiledItemInfoWrapper($sInput, $sInfoValue)
  2211. {
  2212. $sInfo = $this->genInfoIcon($sInfoValue) ;
  2213. return $this->oTemplate->parseHtmlByName('form_field_option_info.html', [
  2214. 'input' => $sInput,
  2215. 'info' => $sInfo
  2216. ]);
  2217. }
  2218. function genIcon($sIcon)
  2219. {
  2220. if(!$sIcon)
  2221. return '';
  2222. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate)->getIcon($sIcon);
  2223. return $this->oTemplate->parseHtmlByName('form_input_icon.html', [
  2224. 'bx_if:icon' => [
  2225. 'condition' => (bool)$sIcon,
  2226. 'content' => ['icon' => $sIcon],
  2227. ],
  2228. 'bx_if:icon-html' => [
  2229. 'condition' => (bool)$sIconHtml,
  2230. 'content' => ['icon' => $sIconHtml],
  2231. ],
  2232. 'bx_if:image_inline' => [
  2233. 'condition' => false,
  2234. 'content' => ['image' => ''],
  2235. ]
  2236. ]);
  2237. }
  2238. function genInfoIcon($sInfo)
  2239. {
  2240. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small mt-1">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  2241. }
  2242. function genErrorIcon( $sError = '' )
  2243. {
  2244. if ($this->bEnableErrorIcon) {
  2245. $sStyle = '';
  2246. if (!$sError)
  2247. $sStyle = 'style="display:none;"';
  2248. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  2249. }
  2250. }
  2251. function getOpenSection($aAttrs = [], $sTitle = '', $aWrapperAttrs = [])
  2252. {
  2253. $sClose = '';
  2254. if($this->_isSectionOpened)
  2255. $sClose = $this->{$this->_sSectionClose}();
  2256. $sWrapperAttrs = bx_convert_array2attrs($aWrapperAttrs, "bx-form-section-wrapper my-4");
  2257. if(!$aAttrs || !is_array($aAttrs))
  2258. $aAttrs = [];
  2259. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-section-" . ($sTitle ? "header" : "divider"));
  2260. $this->_isSectionOpened = true;
  2261. return $sClose . "<!-- form header content begins -->\n <div $sWrapperAttrs> <div $sAttrs> $sTitle <div class=\"bx-form-section-content pt-4 pb-6" . ($sTitle ? ' px-6' : '') . "\">\n";
  2262. }
  2263. function getCloseSection()
  2264. {
  2265. if(!$this->_isSectionOpened)
  2266. return '';
  2267. $this->_isSectionOpened = false;
  2268. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  2269. }
  2270. function getOpenSectionViewMode($aAttrs = array(), $sTitle = '', $aWrapperAttrs = [])
  2271. {
  2272. if (!$this->_isSectionOpened) {
  2273. if (!$aAttrs || !is_array($aAttrs))
  2274. $aAttrs = array();
  2275. if ($sTitle)
  2276. $sClassesAdd = "bx-form-section-header";
  2277. else
  2278. $sClassesAdd = "bx-form-section-divider";
  2279. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  2280. $sWrapperAttrs = bx_convert_array2attrs($aWrapperAttrs, "bx-form-section-wrapper bx-def-margin-top");
  2281. $this->_isSectionOpened = true;
  2282. return "<!-- form header content begins -->\n <div $sWrapperAttrs> <div $sAttrs> $sTitle <div class=\"bx-form-section-content bx-def-padding-top\">\n";
  2283. } else {
  2284. return '';
  2285. }
  2286. }
  2287. function getCloseSectionViewMode()
  2288. {
  2289. if ($this->_isSectionOpened) {
  2290. $this->_isSectionOpened = false;
  2291. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  2292. } else {
  2293. return '';
  2294. }
  2295. }
  2296. protected function _genInputCaption($aInput)
  2297. {
  2298. return ($sCaption = $aInput['caption'] ?? '') ? bx_process_output($sCaption, BX_DATA_HTML) : '';
  2299. }
  2300. protected function _genInputAreaLabel($aInput)
  2301. {
  2302. return ($sAreaLabel = $aInput['area_label'] ?? '') && ($sAreaLabel = strip_tags(_t($sAreaLabel))) ? bx_html_attribute($sAreaLabel) : '';
  2303. }
  2304. public static function getJsCalendarLangs ()
  2305. {
  2306. return array ('ar-dz' => 1, 'ar' => 1, 'at' => 1, 'az' => 1, 'be' => 1, 'bg' => 1, 'bn' => 1, 'bs' => 1, 'cat' => 1, 'ckb' => 1, 'cs' => 1, 'cy' => 1, 'da' => 1, 'de' => 1, 'default' => 1, 'eo' => 1, 'es' => 1, 'et' => 1, 'fa' => 1, 'fi' => 1, 'fo' => 1, 'fr' => 1, 'ga' => 1, 'gr' => 1, 'he' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'index' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'ka' => 1, 'km' => 1, 'ko' => 1, 'kz' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'mn' => 1, 'ms' => 1, 'my' => 1, 'nl' => 1, 'nn' => 1, 'no' => 1, 'pa' => 1, 'pl' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'si' => 1, 'sk' => 1, 'sl' => 1, 'sq' => 1, 'sr-cyr' => 1, 'sr' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'uz' => 1, 'uz_latn' => 1, 'vn' => 1, 'zh-tw' => 1, 'zh' => 1);
  2307. }
  2308. public static function getCssJsCalendar()
  2309. {
  2310. $aLangs = self::getJsCalendarLangs();
  2311. $sLang = BxDolLanguages::getInstance()->detectLanguageFromArray($aLangs);
  2312. return [[
  2313. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flatpickr/dist/|flatpickr.min.css'
  2314. ], [
  2315. 'flatpickr/dist/flatpickr.min.js',
  2316. 'flatpickr/dist/l10n/' . $sLang . '.js',
  2317. ]
  2318. ];
  2319. }
  2320. function addCssJsUi ()
  2321. {
  2322. if (self::$_isCssJsUiAdded)
  2323. return;
  2324. $this->_addJs(array(
  2325. 'jquery-ui/jquery-ui.min.js',
  2326. ), "'undefined' === typeof(jQuery.ui.position)");
  2327. $this->_addCss('jquery-ui/jquery-ui.min.css');
  2328. self::$_isCssJsUiAdded = true;
  2329. }
  2330. function addCssJsUiSortable ()
  2331. {
  2332. if (self::$_isCssJsUiSortableAdded)
  2333. return;
  2334. $this->_addJs(array(
  2335. 'jquery-ui/jquery-ui.min.js'
  2336. ), "'undefined' === typeof(jQuery.ui.position)");
  2337. self::$_isCssJsUiSortableAdded = true;
  2338. }
  2339. function addCssJsTimepicker ()
  2340. {
  2341. if (self::$_isCssJsTimepickerAdded)
  2342. return;
  2343. list($aCss, $aJs) = self::getCssJsCalendar();
  2344. $this->_addCss($aCss);
  2345. $this->_addJs($aJs, "'undefined' === typeof(flatpickr)");
  2346. self::$_isCssJsTimepickerAdded = true;
  2347. }
  2348. function addCssJsMinicolors ()
  2349. {
  2350. if (self::$_isCssJsMinicolorsAdded)
  2351. return;
  2352. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  2353. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  2354. self::$_isCssJsMinicolorsAdded = true;
  2355. }
  2356. function addCssJsCodeMirror ()
  2357. {
  2358. if (self::$_isCssJsCodeMirrorAdded)
  2359. return;
  2360. $this->_addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'codemirror/|codemirror.css']);
  2361. $this->_addJs(['codemirror/codemirror-ext.min.js'], "'undefined' === typeof(CodeMirror)");
  2362. self::$_isCssJsCodeMirrorAdded = true;
  2363. }
  2364. function addCssJsViewMode ()
  2365. {
  2366. if (self::$_isCssJsAddedViewMode)
  2367. return;
  2368. $this->_addCss('forms.css');
  2369. self::$_isCssJsAddedViewMode = true;
  2370. }
  2371. function addCssJsCore ()
  2372. {
  2373. if (self::$_isCssJsAdded)
  2374. return;
  2375. $this->_addCss('forms.css');
  2376. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  2377. $this->_addJs(array('BxDolForm.js', 'BxDolNestedForm.js'), "true");
  2378. self::$_isCssJsAdded = true;
  2379. }
  2380. function addCssJs ()
  2381. {
  2382. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  2383. $this->addCssJsViewMode ();
  2384. } else {
  2385. $this->addCssJsCore();
  2386. }
  2387. }
  2388. protected function _processCssJs()
  2389. {
  2390. $sRet = '';
  2391. if ($this->_bDynamicMode) {
  2392. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  2393. $sRet .= "\n<script>\n";
  2394. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  2395. foreach ($this->_aJs as $sCondition => $aJs) {
  2396. $sJs = $this->oTemplate->addJs($aJs, true);
  2397. if (!$sJs)
  2398. continue;
  2399. $sRet .= "if ($sCondition)\n";
  2400. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  2401. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  2402. }
  2403. $sRet .= "\n</script>\n";
  2404. }
  2405. else {
  2406. $this->oTemplate->addCss($this->_aCss);
  2407. $aJs = array();
  2408. foreach ($this->_aJs as $sCondition => $a)
  2409. $aJs = array_merge($aJs, $a);
  2410. $this->oTemplate->addJs($aJs);
  2411. }
  2412. return $sRet;
  2413. }
  2414. protected function _addJs($mixed, $sJsCondition)
  2415. {
  2416. if (!is_array($mixed))
  2417. $mixed = array($mixed);
  2418. $this->_aJs[$sJsCondition] = $mixed;
  2419. }
  2420. protected function _addCss($mixed)
  2421. {
  2422. if (!is_array($mixed))
  2423. $mixed = array($mixed);
  2424. foreach ($mixed as $s)
  2425. $this->_aCss[$s] = $s;
  2426. }
  2427. }
  2428. /** @} */