BxBaseFormView.php 97 KB

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