BxBaseFormView.php 97 KB

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