BxBaseFormView.php 107 KB

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