BxBaseFormView.php 100 KB

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