BxBaseFormView.php 95 KB

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