BxBaseFormView.php 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  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 = '<br />';
  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. bx_alert('system', 'form_output', 0, 0, array(
  277. 'dynamic' => $this->_bDynamicMode,
  278. 'object' => &$this,
  279. 'code' => &$this->sCode,
  280. 'include' => &$sInclude
  281. ));
  282. if($this->sCode === false)
  283. $this->sCode = $this->genForm();
  284. $this->addCssJs();
  285. $sDynamicCssJs = $this->_processCssJs();
  286. return $sInclude . $sDynamicCssJs . $this->sCode;
  287. }
  288. public function getJsClassName()
  289. {
  290. return $this->_sJsClassName;
  291. }
  292. public function getJsObjectName()
  293. {
  294. return $this->_sJsObjectName;
  295. }
  296. public function getJsScript($bWrap = false)
  297. {
  298. $sJsObjName = $this->getJsObjectName();
  299. $sJsObjClass = $this->getJsClassName();
  300. $sCode = "if(window['" . $sJsObjName . "'] == undefined) window['" . $sJsObjName . "'] = new " . $sJsObjClass . "(" . json_encode(array(
  301. 'sObjName' => $sJsObjName,
  302. 'sObject' => isset($this->aParams['object']) ? $this->aParams['object'] : '',
  303. 'sDisplay' => isset($this->aParams['display']) ? $this->aParams['display'] : '',
  304. 'sRootUrl' => BX_DOL_URL_ROOT,
  305. 'aHtmlIds' => $this->_aHtmlIds,
  306. )) . ");";
  307. return $bWrap ? $this->oTemplate->_wrapInTagJsCode($sCode) : $sCode;
  308. }
  309. /**
  310. * Generate the whole form
  311. *
  312. * @return string
  313. */
  314. function genForm()
  315. {
  316. $this->_sCodeAdd = '';
  317. $sTable = $this->genRows();
  318. $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : '';
  319. $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : '';
  320. if (!empty($this->aParams['remove_form']) || (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])) {
  321. $sForm = <<<BLAH
  322. $sHtmlBefore
  323. {$this->_sCodeAdd}
  324. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  325. $sTable
  326. </div>
  327. $sHtmlAfter
  328. BLAH;
  329. } else {
  330. $sFormAttrs = bx_convert_array2attrs($this->aFormAttrs, 'bx-form-advanced');
  331. $sAjaxFormJs = '';
  332. if($this->_bAjaxMode)
  333. $sAjaxFormJs = <<<BLAH
  334. $("form#{$this->id}").ajaxForm({
  335. dataType: "json",
  336. beforeSubmit: function (formData, jqForm, options) {
  337. bx_loading($("form#{$this->id}"), true);
  338. },
  339. success: function (oData) {
  340. bx_loading($("form#{$this->id}"), false);
  341. processJsonData(oData);
  342. }
  343. });
  344. BLAH;
  345. $sJsCodeDynamic = '';
  346. if($this->_bDynamicMode)
  347. $sJsCodeDynamic = 'function() {' . $this->getJsScript() . '}';
  348. $sForm = <<<BLAH
  349. $sHtmlBefore
  350. <form $sFormAttrs>
  351. {$this->_sCodeAdd}
  352. <div class="bx-form-advanced-wrapper {$this->id}_wrapper">
  353. $sTable
  354. </div>
  355. </form>
  356. <script>
  357. $(document).ready(function() {
  358. $(this).addWebForms($sJsCodeDynamic);
  359. });
  360. $sAjaxFormJs
  361. </script>
  362. $sHtmlAfter
  363. BLAH;
  364. }
  365. if(!$this->_bDynamicMode)
  366. $sForm = $this->getJsScript(true) . $sForm;
  367. return $sForm;
  368. }
  369. /**
  370. * Generate Table HTML code
  371. *
  372. * @return string
  373. */
  374. function genRows()
  375. {
  376. // add CSRF token if it's needed.
  377. 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) {
  378. $this->aInputs['csrf_token'] = array(
  379. 'type' => 'hidden',
  380. 'name' => 'csrf_token',
  381. 'value' => $mixedCsrfToken,
  382. 'db' => array ('pass' => 'Xss'),
  383. 'visible_for_levels' => PHP_INT_MAX,
  384. );
  385. }
  386. // add 'Ajax Mode' flag
  387. if($this->_bAjaxMode)
  388. $this->aInputs['ajax_mode'] = array(
  389. 'type' => 'hidden',
  390. 'name' => 'ajax_mode',
  391. 'value' => 1,
  392. 'db' => array ('pass' => 'Int'),
  393. 'visible_for_levels' => PHP_INT_MAX,
  394. );
  395. // add 'Absolute Action Url' flag
  396. if($this->_bAbsoluteActionUrl)
  397. $this->aInputs['absolute_action_url'] = array(
  398. 'type' => 'hidden',
  399. 'name' => 'absolute_action_url',
  400. 'value' => 1,
  401. 'db' => array ('pass' => 'Int'),
  402. 'visible_for_levels' => PHP_INT_MAX,
  403. );
  404. // check if we need to generate open section clause
  405. $sOpenSection = '';
  406. foreach ($this->aInputs as $aInput) {
  407. if (isset($aInput['type']) && 'hidden' == $aInput['type'])
  408. continue;
  409. if (isset($aInput['type']) && 'block_header' != $aInput['type'])
  410. $sOpenSection = $this->{$this->_sSectionOpen}();
  411. break;
  412. }
  413. // generate rows contents
  414. $sCont = '';
  415. $sContHeader = '';
  416. $sContFields = '';
  417. $sFuncGenRow = isset($this->aParams['view_mode']) && $this->aParams['view_mode'] ? 'genViewRow' : 'genRow';
  418. foreach ($this->aInputs as $aInput) {
  419. if (!isset($aInput['visible_for_levels']) || self::isVisible($aInput)) {
  420. if ('block_header' == $aInput['type'] && !$this->_bShowEmptySections) {
  421. // don't show section with no fields or with empty fields
  422. if ($sContHeader) {
  423. if ($sContFields)
  424. $sCont .= $sContHeader . $sContFields;
  425. else
  426. $sContHeader = '';
  427. $sContFields = '';
  428. }
  429. else {
  430. $sCont .= $sContFields;
  431. $sContFields = '';
  432. }
  433. $sContHeader = $this->$sFuncGenRow($aInput);
  434. } else {
  435. $sContFields .= $this->$sFuncGenRow($aInput);
  436. }
  437. }
  438. }
  439. $sCont .= $sContHeader . $sContFields;
  440. $sCloseSection = $this->{$this->_sSectionClose}();
  441. return $sOpenSection . $sCont . $sCloseSection;
  442. }
  443. /**
  444. * Generate single Table Row
  445. *
  446. * @param array $aInput
  447. * @return string
  448. */
  449. function genRow(&$aInput)
  450. {
  451. if (!isset($aInput['type']))
  452. $aInput['type'] = false;
  453. if (!empty($aInput['name'])) {
  454. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  455. if (method_exists($this, $sCustomMethod))
  456. return $this->$sCustomMethod($aInput);
  457. }
  458. switch ($aInput['type']) {
  459. case 'block_header':
  460. $sRow = $this->genRowBlockHeader($aInput);
  461. break;
  462. case 'block_end':
  463. $sRow = $this->genBlockEnd($aInput);
  464. break;
  465. case 'hidden':
  466. // do not generate row for hidden inputs
  467. $sRow = '';
  468. $this->_sCodeAdd .= $this->genInput($aInput);
  469. break;
  470. case 'select_box':
  471. $sRow = $this->genRowCustom($aInput, 'genInputSelectBox');
  472. break;
  473. case 'files':
  474. $sRow = $this->genRowCustom($aInput, 'genInputFiles');
  475. break;
  476. case 'switcher':
  477. case 'checkbox':
  478. $sRow = $this->genRowStandard($aInput, true);
  479. break;
  480. default:
  481. $sRow = $this->genRowStandard($aInput);
  482. }
  483. return $sRow;
  484. }
  485. public function isInputVisible($mixedInput)
  486. {
  487. if(!is_array($mixedInput) && isset($this->aInputs[$mixedInput]))
  488. $mixedInput = $this->aInputs[$mixedInput];
  489. if(empty($mixedInput) || !is_array($mixedInput))
  490. $mixedInput = BxDolFormQuery::getInputByName($this->aParams['object'], $mixedInput);
  491. if(empty($mixedInput) || !is_array($mixedInput))
  492. return false;
  493. if(!empty($mixedInput['privacy']) && !empty($this->_iAuthorId) && !$this->_isInputVisibleByPrivacy($mixedInput))
  494. return false;
  495. return true;
  496. }
  497. protected function _isInputVisibleByPrivacy($aInput)
  498. {
  499. $mixedResult = checkActionModule($this->_iAuthorId, 'set form fields privacy', 'system');
  500. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  501. return true;
  502. $aInputPrivacy = BxDolFormQuery::getInputPrivacy($aInput['id'], $this->_iAuthorId);
  503. if(empty($aInputPrivacy) || !is_array($aInputPrivacy))
  504. if(BxDolFormQuery::setInputPrivacy($aInput['id'], $this->_iAuthorId, BxDolPrivacy::getFieldName($this->_sPrivacyObjectView), $this->_sPrivacyGroupDefault))
  505. $aInputPrivacy = BxDolFormQuery::getInputPrivacy($aInput['id'], $this->_iAuthorId);
  506. if((empty($aInputPrivacy) || !is_array($aInputPrivacy)) && $this->_sPrivacyGroupDefault != BX_DOL_PG_ALL)
  507. return false;
  508. $oPrivacy = BxDolPrivacy::getObjectInstance($this->_sPrivacyObjectView);
  509. if($oPrivacy && !$oPrivacy->check($aInputPrivacy['id']))
  510. return false;
  511. return true;
  512. }
  513. /**
  514. * Generate single Table Row for view mode
  515. *
  516. * @param array $aInput
  517. * @return string
  518. */
  519. function genViewRow(&$aInput)
  520. {
  521. if (!isset($aInput['type']))
  522. $aInput['type'] = false;
  523. if(!$this->isInputVisible($aInput))
  524. return '';
  525. if (!empty($aInput['name'])) {
  526. $sCustomMethod = 'genCustomRow' . $this->_genMethodName($aInput['name']);
  527. if (method_exists($this, $sCustomMethod))
  528. return $this->$sCustomMethod($aInput);
  529. }
  530. switch ($aInput['type']) {
  531. case 'block_header':
  532. $sRow = $this->genRowBlockHeader($aInput);
  533. break;
  534. case 'block_end':
  535. $sRow = $this->genBlockEnd($aInput);
  536. break;
  537. case 'nested_form':
  538. $sRow = $this->genNestedForm($aInput);
  539. break;
  540. default:
  541. $sRow = $this->genViewRowWrapped($aInput);
  542. }
  543. return $sRow;
  544. }
  545. /**
  546. * Generate complete wrapped row for view mode form
  547. *
  548. * @param array $aInput
  549. * @return string
  550. */
  551. function genViewRowWrapped(&$aInput)
  552. {
  553. $sValue = $this->genViewRowValue($aInput);
  554. if (null === $sValue)
  555. return '';
  556. return $this->oTemplate->parseHtmlByName('form_view_row.html', array(
  557. 'type' => $aInput['type'],
  558. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  559. 'value' => $sValue
  560. ));
  561. }
  562. /**
  563. * Generate value for view mode row
  564. *
  565. * @param array $aInput
  566. * @return string
  567. */
  568. function genViewRowValue(&$aInput)
  569. {
  570. if (!empty($aInput['name'])) {
  571. $sCustomMethod = 'genCustomViewRowValue' . $this->_genMethodName($aInput['name']);
  572. if (method_exists($this, $sCustomMethod))
  573. return $this->$sCustomMethod($aInput);
  574. }
  575. switch ($aInput['type']) {
  576. case 'hidden':
  577. $sValue = null;
  578. break;
  579. case 'select':
  580. case 'radio_set':
  581. $sValue = $this->genViewRowValueForSelect($aInput);
  582. break;
  583. case 'dateselect':
  584. case 'datepicker':
  585. $sValue = null;
  586. if (empty($aInput['value']) || !$aInput['value'] || '0000-00-00' == $aInput['value'])
  587. break;
  588. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass']) && !is_numeric($aInput['value']))
  589. $sValue = BxTemplFunctions::getInstance()->timeForJsFullDate($aInput['value'] . " Z", isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  590. else
  591. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE, true);
  592. break;
  593. case 'date_time':
  594. case 'datetime':
  595. $sValue = null;
  596. if(empty($aInput['value']) || !$aInput['value'] || '0000-00-00 00:00:00' == $aInput['value'] || '0000-00-00 00:00' == $aInput['value'])
  597. break;
  598. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass']) && !is_numeric($aInput['value']))
  599. $sValue = BxTemplFunctions::getInstance()->timeForJsFullDate($aInput['value'] . " Z", isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  600. else
  601. $sValue = BxTemplFunctions::getInstance()->{is_numeric($aInput['value']) ? 'timeForJs' : 'timeForJsFullDate'}($aInput['value'], isset($aInput['date_format']) ? $aInput['date_format'] : BX_FORMAT_DATE_TIME, true);
  602. break;
  603. case 'checkbox_set':
  604. case 'select_multiple':
  605. $sValue = null;
  606. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  607. $sValue = '';
  608. foreach ($aInput['value'] as $sVal)
  609. $sValue .= $aInput['values'][$sVal] . ', ';
  610. $sValue = trim ($sValue, ', ');
  611. }
  612. break;
  613. case 'checkbox':
  614. case 'switcher':
  615. $sValue = isset($aInput['checked']) && $aInput['checked'] ? _t('_sys_form_checkbox_value_on') : _t('_sys_form_checkbox_value_off');
  616. break;
  617. case 'textarea':
  618. if (isset($aInput['value']) && '' !== $aInput['value'])
  619. $sValue = (isset($aInput['html']) && $aInput['html']) || (isset($aInput['code']) && $aInput['code']) ? $aInput['value'] : bx_process_output($aInput['value'], BX_DATA_TEXT_MULTILINE);
  620. else
  621. $sValue = null;
  622. break;
  623. case 'text':
  624. if(isset($aInput['value']) && '' !== $aInput['value'])
  625. $sValue = bx_linkify(bx_process_output($aInput['value']));
  626. else
  627. $sValue = null;
  628. break;
  629. case 'price':
  630. if(isset($aInput['value']) && $aInput['value'] !== '')
  631. $sValue = _t_format_currency(bx_process_output($aInput['value']));
  632. else
  633. $sValue = null;
  634. break;
  635. default:
  636. if(isset($aInput['value']) && '' !== $aInput['value']) {
  637. $sValue = $aInput['value'];
  638. if(get_mb_substr($sValue, 0, 1) == '_')
  639. $sValue = _t($sValue);
  640. $sValue = bx_process_output($sValue);
  641. }
  642. else
  643. $sValue = null;
  644. }
  645. return $sValue;
  646. }
  647. function genViewRowValueForSelect(&$aInput)
  648. {
  649. if (!isset($aInput['value']) || !$aInput['value'])
  650. return null;
  651. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  652. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])) !== false)
  653. return $oCategory->getCategoryLink($s, $aInput['value']);
  654. return $s;
  655. }
  656. protected function genCustomRowBirthday(&$aInput)
  657. {
  658. if(!$this->_bViewMode)
  659. return $this->genRowStandard($aInput);
  660. $aInput['caption_src'] = '_sys_form_input_age';
  661. $aInput['caption'] = _t($aInput['caption_src']);
  662. return $this->genViewRowWrapped($aInput);
  663. }
  664. protected function genCustomViewRowValueBirthday(&$aInput)
  665. {
  666. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00')))
  667. return null;
  668. $sValue = $aInput['value'];
  669. $iPosSpace = strpos($sValue, ' ');
  670. if($iPosSpace !== false)
  671. $sValue = trim(substr($sValue, 0, $iPosSpace));
  672. $aDate = explode('-', $sValue);
  673. $iCdYear = (int)date('Y');
  674. $iCdMonth = (int)date('n');
  675. $iCdDay = (int)date('j');
  676. $iResult = $iCdYear - (int)$aDate[0];
  677. if($iCdMonth < (int)$aDate[1] || ($iCdMonth == (int)$aDate[1] && $iCdDay < (int)$aDate[2]))
  678. $iResult -= 1;
  679. return $iResult;
  680. }
  681. /**
  682. * Generate standard row
  683. *
  684. * @param array $aInput
  685. * @return string
  686. */
  687. function genRowStandard(&$aInput, $isOneLine = false)
  688. {
  689. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  690. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  691. $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> ' : '';
  692. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  693. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  694. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  695. $sClassWrapper = 'bx-form-element-wrapper';
  696. if($isOneLine)
  697. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  698. $sClassWrapper .= ' bx-def-margin-top-auto';
  699. if (isset($aInput['name']))
  700. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  701. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  702. $sClassOneLineCaption = '';
  703. $sClassOneLineValue = '';
  704. if ($isOneLine) {
  705. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  706. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  707. $aInput['attrs']['id'] = $this->getInputId($aInput);
  708. if ($sCaption)
  709. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  710. }
  711. $sInput = $this->genInput($aInput);
  712. if(isset($aInput['error_updated']) && $aInput['error_updated'] === true)
  713. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  714. if(empty($sErrorIcon))
  715. $sErrorIcon = '';
  716. $aTmplVarsRow = array(
  717. 'bx_if:show_caption' => array(
  718. 'condition' => !empty($sCaption),
  719. 'content' => array(
  720. 'class_caption' => $sClassOneLineCaption,
  721. 'caption' => $sCaption,
  722. 'required' => $sRequired,
  723. 'help' => $sHelp,
  724. )
  725. ),
  726. 'class_value' => $sClassAdd . $sClassOneLineValue,
  727. 'value' => $this->genWrapperInput($aInput, $sInput),
  728. 'info' => $sInfoIcon,
  729. 'error' => $sErrorIcon,
  730. );
  731. return $this->oTemplate->parseHtmlByName('form_row_standard.html', array(
  732. 'tr_attrs' => $sTrAttrs,
  733. 'bx_if:show_one_line' => array(
  734. 'condition' => $isOneLine,
  735. 'content' => $aTmplVarsRow
  736. ),
  737. 'bx_if:show_lined' => array(
  738. 'condition' => !$isOneLine,
  739. 'content' => $aTmplVarsRow
  740. ),
  741. 'info' => $sInfoIcon,
  742. 'error' => $sErrorIcon
  743. ));
  744. }
  745. function genWrapperInput($aInput, $sContent)
  746. {
  747. $aAttrs = $this->_genWrapperInputAttrs($aInput);
  748. $sClass = "bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}";
  749. if(isset($aInput['html']) && $aInput['html'] && $this->isHtmlEditor($aInput['html'], $aInput))
  750. $sClass .= ' bx-form-input-wrapper-html';
  751. return $this->oTemplate->parseHtmlByName('form_input_wrapper.html', array(
  752. 'attrs' => bx_convert_array2attrs($aAttrs, $sClass),
  753. 'content' => $sContent
  754. ));
  755. }
  756. protected function _genWrapperInputAttrs(&$aInput)
  757. {
  758. return isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? $aInput['attrs_wrapper'] : [];
  759. }
  760. /**
  761. * Generate custom row
  762. *
  763. * @param array $aInput
  764. * @param string $sCustomMethod custom method to generate code for input
  765. * @return string
  766. */
  767. function genRowCustom(&$aInput, $sCustomMethod)
  768. {
  769. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  770. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  771. $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> ' : '';
  772. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  773. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  774. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  775. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  776. if (isset($aInput['name']))
  777. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  778. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  779. return $this->oTemplate->parseHtmlByName('form_row_custom.html', array(
  780. 'tr_attrs' => $sTrAttrs,
  781. 'bx_if:show_caption' => array(
  782. 'condition' => !empty($sCaption),
  783. 'content' => array(
  784. 'caption' => $sCaption,
  785. 'required' => $sRequired,
  786. 'help' => $sHelp
  787. )
  788. ),
  789. 'class_value' => $sClassAdd,
  790. 'value' => $sInput,
  791. ));
  792. }
  793. /**
  794. * Generate Block Headers row
  795. *
  796. * @param array $aInput
  797. * @return string
  798. */
  799. function genRowBlockHeader(&$aInput)
  800. {
  801. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  802. // if there is no caption - show divider only
  803. if (empty($aInput['caption'])) {
  804. $sCode = $this->{$this->_sSectionClose}();
  805. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  806. return $sCode;
  807. }
  808. // if section is collapsed by default, add necessary code
  809. $sClassAddCollapsable = 'bx-form-collapsable';
  810. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  811. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  812. // display section with caption
  813. $sCode = $this->{$this->_sSectionClose}();
  814. if (empty($aAttrs))
  815. $aAttrs = array('class' => 'bx-form-collapsable ' . $sClassAddCollapsable);
  816. else
  817. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  818. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  819. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-large">' . bx_process_output($aInput['caption']) . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  820. else
  821. $sLegend = '<legend class="bx-def-padding-left bx-def-padding-sec-right bx-def-font-grayed bx-def-font-large"><a href="javascript:void(0);">' . bx_process_output($aInput['caption']) . '</a>' . (!empty($aInput['info']) ? '<br /><span>' . bx_process_output($aInput['info']) . '</span>' : '') . '</legend>';
  822. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  823. return $sCode;
  824. }
  825. function genBlockEnd()
  826. {
  827. $aNextTbodyAdd = false; // need to have some default
  828. $sCode = '';
  829. $sCode .= $this->{$this->_sSectionClose}();
  830. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  831. return $sCode;
  832. }
  833. /**
  834. * Generate HTML Input Element
  835. *
  836. * @param array $aInput
  837. * @return string Output HTML Code
  838. */
  839. function genInput(&$aInput)
  840. {
  841. if (!empty($aInput['name'])) {
  842. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  843. if (method_exists($this, $sCustomMethod))
  844. return $this->$sCustomMethod($aInput);
  845. }
  846. switch ($aInput['type']) {
  847. case 'datepicker':
  848. if (!isset($aInput['attrs']))
  849. $aInput['attrs'] = array();
  850. $aInput['attrs']['autosuggestion'] = 'off';
  851. $sInput = $this->genInputStandard($aInput);
  852. break;
  853. // standard inputs (and non-standard, interpreted as standard)
  854. case 'text':
  855. case 'datepicker':
  856. case 'dateselect':
  857. case 'date_time':
  858. case 'datetime':
  859. case 'number':
  860. case 'time':
  861. case 'checkbox':
  862. case 'radio':
  863. case 'image':
  864. case 'slider':
  865. case 'doublerange':
  866. case 'hidden':
  867. $sInput = $this->genInputStandard($aInput);
  868. break;
  869. case 'password':
  870. $sInput = $this->genInputPassword($aInput);
  871. break;
  872. case 'price':
  873. $sInput = $this->genInputPrice($aInput);
  874. break;
  875. case 'file':
  876. $sInput = $this->genInputFile($aInput);
  877. break;
  878. case 'rgb':
  879. case 'rgba':
  880. $sClass = 'bx-form-input-' . $aInput['type'];
  881. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  882. $aInput['type'] = 'text';
  883. $sInput = $this->genInputStandard($aInput);
  884. break;
  885. case 'switcher':
  886. $sInput = $this->genInputSwitcher($aInput);
  887. break;
  888. case 'button':
  889. case 'reset':
  890. case 'submit':
  891. $sInput = $this->genInputButton($aInput);
  892. break;
  893. case 'textarea':
  894. $sInput = $this->genInputTextarea($aInput);
  895. break;
  896. case 'select':
  897. $sInput = $this->genInputSelect($aInput);
  898. break;
  899. case 'select_multiple':
  900. $sInput = $this->genInputSelectMultiple($aInput);
  901. break;
  902. case 'checkbox_set':
  903. $sInput = $this->genInputCheckboxSet($aInput);
  904. break;
  905. case 'radio_set':
  906. $sInput = $this->genInputRadioSet($aInput);
  907. break;
  908. case 'input_set': // numeric array of inputs
  909. $sInput = '';
  910. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  911. foreach ($aInput as $iKey => $aSubInput) {
  912. if (!is_int($iKey) or !$aSubInput)
  913. continue; // parse only integer keys and existing values
  914. $sInput .= $this->genInput($aSubInput); // recursive call
  915. $sInput .= $sDivider;
  916. }
  917. $sInput .= '<div class="bx-clear"></div>';
  918. break;
  919. case 'custom':
  920. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  921. break;
  922. case 'captcha':
  923. $sInput = $this->genInputCaptcha($aInput);
  924. break;
  925. case 'location':
  926. $sInput = $this->genInputLocation($aInput);
  927. break;
  928. case 'nested_form':
  929. $sInput = $this->genInputNestedForm($aInput);
  930. break;
  931. case 'value':
  932. $sInput = '';
  933. if(!isset($aInput['value']))
  934. break;
  935. $sInput = $aInput['value'];
  936. if(get_mb_substr($sInput, 0, 1) == '_')
  937. $sInput = _t($sInput);
  938. break;
  939. default:
  940. //unknown control type
  941. $sInput = 'Unknown control type';
  942. }
  943. // create input label
  944. $sInput .= $this->genLabel($aInput);
  945. // create input privacy group chooser
  946. $sInput .= $this->genPrivacyGroupChooser($aInput);
  947. return $sInput;
  948. }
  949. /**
  950. * Generate new Input Element id
  951. *
  952. * @param array $aInput
  953. * @return string
  954. */
  955. function getInputId(&$aInput)
  956. {
  957. if (isset($aInput['id']))
  958. return $aInput['id'];
  959. $sName = md5($aInput['name']);
  960. $sID = $this->id . '_input_' . $sName;
  961. if ( // multiple elements cause identical id's
  962. (
  963. (
  964. $aInput['type'] == 'checkbox' and
  965. substr($aInput['name'], -2) == '[]' // it is multiple element
  966. ) or
  967. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  968. ) and
  969. isset($aInput['value']) // if we can make difference
  970. ) {
  971. $sValue = md5($aInput['value']);
  972. // add value
  973. $sID .= '_' . $sValue;
  974. }
  975. $sID = trim($sID, '_');
  976. $aInput['id'] = $sID; // just for repeated calls
  977. return $sID;
  978. }
  979. /**
  980. * Generate standard Input Element
  981. *
  982. * @param array $aInput
  983. * @return string
  984. */
  985. function genInputStandard(&$aInput)
  986. {
  987. $aAttrs = $this->_genInputStandardAttrs($aInput);
  988. return $this->oTemplate->parseHtmlByName('form_field_standard.html', [
  989. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}")
  990. ]);
  991. }
  992. protected function _genInputStandardAttrs(&$aInput)
  993. {
  994. // clone attributes for system use ;)
  995. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  996. // add default className to attributes
  997. $aAttrs['type'] = $aInput['type'];
  998. if ('datetime' == $aAttrs['type'])
  999. $aAttrs['type'] = 'date_time';
  1000. switch($aAttrs['type']) {
  1001. case 'date_time':
  1002. $this->addCssJsUi ();
  1003. $this->addCssJsTimepicker ();
  1004. break;
  1005. case 'slider':
  1006. case 'doublerange':
  1007. case 'datepicker':
  1008. $this->addCssJsUi ();
  1009. break;
  1010. case 'dateselect':
  1011. $this->_addJs(array(
  1012. 'combodate/combodate.js',
  1013. ), "'undefined' === typeof($.combodate)");
  1014. break;
  1015. case 'text':
  1016. if (isset($aAttrs['class']) && false !== strpos($aAttrs['class'], 'bx-form-input-rgb'))
  1017. $this->addCssJsMinicolors ();
  1018. break;
  1019. }
  1020. if ('doublerange' == $aAttrs['type'] && (!isset($aInput['value']) || !$aInput['value']) && !empty($aAttrs['min']) && !empty($aAttrs['max'])) {
  1021. $aInput['value'] = $aAttrs['min'] . '-' . $aAttrs['max'];
  1022. }
  1023. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  1024. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  1025. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  1026. // for inputs with labels generate id
  1027. if (isset($aInput['label']))
  1028. $aAttrs['id'] = $this->getInputId($aInput);
  1029. // for checkboxes
  1030. if (isset($aInput['checked']) and $aInput['checked'])
  1031. $aAttrs['checked'] = 'checked';
  1032. return $aAttrs;
  1033. }
  1034. /**
  1035. * Generate Switcher Input Element (based on checkbox)
  1036. *
  1037. * @param array $aInput
  1038. * @return string
  1039. */
  1040. function genInputSwitcher(&$aInput)
  1041. {
  1042. $aInput['type'] = 'checkbox';
  1043. $sCheckbox = $this->genInputStandard($aInput);
  1044. $aInput['type'] = 'switcher';
  1045. $sClass = 'off';
  1046. if(isset($aInput['checked']) && $aInput['checked'])
  1047. $sClass = 'on';
  1048. return $this->oTemplate->parseHtmlByName('form_field_switcher.html', [
  1049. 'class' => $sClass,
  1050. 'checkbox' => $sCheckbox
  1051. ]);
  1052. }
  1053. /**
  1054. * Generate Button Input Element
  1055. *
  1056. * @param array $aInput
  1057. * @return string
  1058. */
  1059. function genInputButton(&$aInput)
  1060. {
  1061. $aAttrs = $this->_genInputButtonAttrs($aInput);
  1062. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn";
  1063. if($aInput['type'] == 'submit')
  1064. $sClassAdd .= ' bx-btn-primary';
  1065. return $this->oTemplate->parseHtmlByName('form_field_button.html', [
  1066. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1067. 'value' => $aInput['value']
  1068. ]);
  1069. }
  1070. protected function _genInputButtonAttrs(&$aInput)
  1071. {
  1072. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1073. // add default className to attributes
  1074. $aAttrs['type'] = $aInput['type'];
  1075. if(isset($aInput['value']))
  1076. $aAttrs['value'] = $aInput['value'];
  1077. if(isset($aInput['name']))
  1078. $aAttrs['name'] = $aInput['name'];
  1079. return $aAttrs;
  1080. }
  1081. /**
  1082. * Generate Textarea Element
  1083. *
  1084. * @param array $aInput
  1085. * @return string
  1086. */
  1087. function genInputTextarea(&$aInput)
  1088. {
  1089. $aAttrs = $this->_genInputTextareaAttrs($aInput);
  1090. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']}";
  1091. if(isset($aInput['html']) && $aInput['html'] && $this->addHtmlEditor($aInput['html'], $aInput))
  1092. $sClassAdd .= ' bx-form-input-html';
  1093. $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')) : '';
  1094. return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
  1095. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1096. 'value' => $sValue
  1097. ]);
  1098. }
  1099. protected function _genInputTextareaAttrs(&$aInput)
  1100. {
  1101. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1102. $aAttrs['name'] = $aInput['name'];
  1103. // for inputs with labels generate id
  1104. if (isset($aInput['label']))
  1105. $aAttrs['id'] = $this->getInputId($aInput);
  1106. return $aAttrs;
  1107. }
  1108. function isHtmlEditor($iViewMode, &$aInput)
  1109. {
  1110. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  1111. }
  1112. function addHtmlEditor($iViewMode, &$aInput)
  1113. {
  1114. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1115. if (!$oEditor)
  1116. return false;
  1117. $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)]);
  1118. return true;
  1119. }
  1120. function getHtmlEditorQueryParams($aInput)
  1121. {
  1122. return ['i' => $aInput['name'], 'f' => $this->aFormAttrs['id']];
  1123. }
  1124. /**
  1125. * Generate Select Box Element
  1126. *
  1127. * @param array $aInput
  1128. * @return string
  1129. */
  1130. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  1131. {
  1132. $aNewInput = $aInput;
  1133. $aNewInput['type'] = 'select';
  1134. $aNewInput['name'] .= '[]';
  1135. $sInput = $this->genInput($aNewInput);
  1136. return <<<BLAH
  1137. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  1138. $sInput
  1139. </div>
  1140. $sInfo
  1141. $sError
  1142. BLAH;
  1143. }
  1144. /**
  1145. * Generate Browse File Element
  1146. *
  1147. * @param array $aInput
  1148. * @return string
  1149. */
  1150. function genInputFile(&$aInput)
  1151. {
  1152. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  1153. if(!empty($aInput['attrs']['onchange']))
  1154. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  1155. else
  1156. $aInput['attrs']['onchange'] = $sOnChange;
  1157. $sInput = $this->genInputStandard($aInput);
  1158. 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>';
  1159. }
  1160. /**
  1161. * Generate Select Box Element
  1162. *
  1163. * @param array $aInput
  1164. * @return string
  1165. */
  1166. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  1167. {
  1168. if(empty($aInput['uploaders']) || !is_array($aInput['uploaders']))
  1169. return '';
  1170. $sUniqId = !empty($aInput['uploaders_id']) ? $aInput['uploaders_id'] : genRndPwd (8, false);
  1171. $bInitGhosts = isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1;
  1172. $bInitReordering = empty($aInput['init_reordering']) ? 0 : 1;
  1173. $oUploader = null;
  1174. $sUploadersButtons = $sUploadersJs = '';
  1175. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1176. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  1177. if(!$oUploader)
  1178. continue;
  1179. //--- Get Button code.
  1180. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1181. if(empty($aInput['attrs']['disabled']))
  1182. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  1183. $aParamsButtons = [
  1184. 'attrs' => bx_convert_array2attrs($aAttrs),
  1185. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  1186. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  1187. ];
  1188. $sUploadersButtons .= $oUploader->getUploaderButton($aParamsButtons);
  1189. //--- Get JS code.
  1190. $sGhostTemplate = $this->genGhostTemplate($aInput);
  1191. $aParamsJs = [
  1192. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1193. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1194. 'is_init_ghosts' => $bInitGhosts,
  1195. 'is_init_reordering' => $bInitReordering
  1196. ];
  1197. if(isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  1198. $aParamsJs['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  1199. $sUploadersJs .= $oUploader->getUploaderJs($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParamsJs, $this->_bDynamicMode);
  1200. }
  1201. if(!$oUploader)
  1202. return '';
  1203. if($bInitReordering) {
  1204. $this->addCssJsUi();
  1205. $this->addCssJsUiSortable();
  1206. }
  1207. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  1208. 'uploaders_buttons' => $sUploadersButtons,
  1209. 'info' => $sInfo,
  1210. 'error' => $sError,
  1211. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  1212. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  1213. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  1214. 'uploaders_js' => $sUploadersJs,
  1215. ));
  1216. }
  1217. protected function genGhostTemplate(&$aInput)
  1218. {
  1219. $sGhostTemplate = false;
  1220. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  1221. $oFormNested = $aInput['ghost_template'];
  1222. if ($oFormNested instanceof BxDolFormNestedGhost)
  1223. $sGhostTemplate = $oFormNested->genForm();
  1224. } 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
  1225. $oFormNested = new BxDolFormNestedGhost($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  1226. $sGhostTemplate = $oFormNested->getCode();
  1227. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  1228. $sGhostTemplate = array ();
  1229. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  1230. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNestedGhost)
  1231. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  1232. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  1233. $sGhostTemplate = $aInput['ghost_template'];
  1234. }
  1235. return $sGhostTemplate;
  1236. }
  1237. protected function genCustomInputUsernamesSuggestions ($aInput)
  1238. {
  1239. $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
  1240. $aAttrs = $this->_genCustomInputUsernamesSuggestionsAttrs($aInput, $bDisabled);
  1241. $aTmplVarsVals = [];
  1242. if(!empty($aInput['value'])) {
  1243. if(is_array($aInput['value']) || (is_numeric($aInput['value']))) {
  1244. if (!is_array($aInput['value']))
  1245. $aInput['value'] = [$aInput['value']];
  1246. foreach($aInput['value'] as $sVal) {
  1247. if(!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  1248. continue;
  1249. $aTmplVarsVals[] = [
  1250. 'item_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  1251. 'item_name' => $oProfile->getDisplayName(),
  1252. 'name' => $aInput['name'] . (isset($aInput['custom']['only_once']) && $aInput['custom']['only_once'] == 1 ? '' : '[]'),
  1253. 'value' => $sVal
  1254. ];
  1255. }
  1256. }
  1257. else if(is_string($aInput['value']))
  1258. $aTmplVarsVals = $aInput['value'];
  1259. }
  1260. $aTmplVarsInputText = [];
  1261. if(!$bDisabled) {
  1262. $aInputText = $aInput;
  1263. $aInputText['type'] = 'text';
  1264. $aInputText['value'] = '';
  1265. $aInputText['attrs'] = $this->_genCustomInputUsernamesSuggestionsTextAttrs($aInputText, $bDisabled);
  1266. unset($aInputText['name']);
  1267. $aTmplVarsInputText['input'] = $this->genInputStandard($aInputText);
  1268. }
  1269. $this->addCssJsUi();
  1270. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  1271. 'name' => $aInput['name'],
  1272. 'attrs' => bx_convert_array2attrs($aAttrs),
  1273. 'bx_repeat:vals' => $aTmplVarsVals,
  1274. 'bx_if:input' => array(
  1275. 'condition' => !$bDisabled,
  1276. 'content' => $aTmplVarsInputText
  1277. ),
  1278. 'bx_if:init' => array(
  1279. 'condition' => !$bDisabled,
  1280. 'content' => array(
  1281. 'id' => $aAttrs['id'],
  1282. 'name' => $aInput['name'],
  1283. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  1284. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  1285. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  1286. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  1287. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  1288. )
  1289. )
  1290. ));
  1291. }
  1292. protected function _genCustomInputUsernamesSuggestionsAttrs (&$aInput, $bDisabled = false)
  1293. {
  1294. $aAttrs = ['id' => $aInput['name'] . time() . mt_rand(0, 100)];
  1295. $aAttrs['class'] = 'bx-form-input-autotoken bx-form-input-text bx-def-font-inputs';
  1296. if($bDisabled)
  1297. $aAttrs['class'] .= ' bx-form-input-disabled';
  1298. if(!empty($aInput['attrs']['class']))
  1299. $aAttrs['class'] .= ' ' . $aInput['attrs']['class'];
  1300. return $aAttrs;
  1301. }
  1302. protected function _genCustomInputUsernamesSuggestionsTextAttrs (&$aInput, $bDisabled = false)
  1303. {
  1304. $aAttrs = ['value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off'];
  1305. if(!empty($aInput['attrs']) && is_array($aInput['attrs'])) {
  1306. if(isset($aInput['attrs']['class']))
  1307. unset($aInput['attrs']['class']);
  1308. $aAttrs = array_merge($aAttrs, $aInput['attrs']);
  1309. }
  1310. return $aAttrs;
  1311. }
  1312. protected function genCustomViewRowValueLabels ($aInput)
  1313. {
  1314. if (empty($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  1315. return null;
  1316. if (!empty($aInput['value']) && !is_array($aInput['value']))
  1317. $aInput['value'] = unserialize($aInput['value']);
  1318. $s = '';
  1319. foreach ($aInput['value'] as $sLabel)
  1320. $s .= '<a href="' . $oMetatags->keywordsGetHashTagUrl($sLabel, $aInput['content_id']) . '"><b class="bx-def-label val">' . trim($sLabel) . '</b></a>';
  1321. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  1322. 'values' => $s,
  1323. ));
  1324. }
  1325. protected function genCustomInputLabels ($aInput)
  1326. {
  1327. $oLabel = BxDolLabel::getInstance();
  1328. $sName = !empty($aInput['name']) ? $aInput['name'] : 'labels';
  1329. $sValue = '';
  1330. if(!empty($aInput['value'])) {
  1331. if(!is_array($aInput['value']))
  1332. $aInput['value'] = unserialize($aInput['value']);
  1333. if(is_array($aInput['value']))
  1334. foreach($aInput['value'] as $sLabel)
  1335. $sValue .= $oLabel->getLabel($sName, $sLabel);
  1336. }
  1337. $sKeyPlaceholder = $aInput['caption_src'] . '_placeholder';
  1338. if(strcmp($sKeyPlaceholder, _t($sKeyPlaceholder)) != 0)
  1339. $sValue .= $oLabel->getLabelPlaceholder($sKeyPlaceholder);
  1340. $aInputLabels = array(
  1341. 'type' => 'custom',
  1342. 'name' => $sName,
  1343. 'caption' => '',
  1344. 'value' => $sValue,
  1345. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . bx_append_url_params('label.php', array(
  1346. 'action' => 'labels_list',
  1347. )),
  1348. 'attrs' => array(
  1349. 'class' => 'bx-form-input-labels',
  1350. 'disabled' => 'disabled'
  1351. )
  1352. );
  1353. return $this->oTemplate->parseHtmlByName('label_select_field.html', array(
  1354. 'js_object' => $oLabel->getJsObjectName(),
  1355. 'js_code' => $oLabel->getJsCodeForm(),
  1356. 'html_id' => $oLabel->getFormFieldId($aInput),
  1357. 'name' => $sName,
  1358. 'input_labels' => $this->genCustomInputUsernamesSuggestions($aInputLabels)
  1359. ));
  1360. }
  1361. /**
  1362. * Generate Select Element
  1363. *
  1364. * @param array $aInput
  1365. * @return string
  1366. */
  1367. function genInputSelect(&$aInput)
  1368. {
  1369. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1370. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1371. }
  1372. /**
  1373. * Generate Multiple Select Element
  1374. *
  1375. * @param array $aInput
  1376. * @return string
  1377. */
  1378. function genInputSelectMultiple(&$aInput)
  1379. {
  1380. $aCurValues = array();
  1381. if (isset($aInput['value']) && $aInput['value'])
  1382. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1383. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  1384. }
  1385. /**
  1386. * Generate Checkbox Set Element
  1387. *
  1388. * @param array $aInput
  1389. * @return string
  1390. */
  1391. function genInputCheckboxSet(&$aInput)
  1392. {
  1393. $aCurValues = array();
  1394. if (isset($aInput['value']) && $aInput['value'])
  1395. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1396. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  1397. }
  1398. /**
  1399. * Generate Radiobuttons Set Element
  1400. *
  1401. * @param array $aInput
  1402. * @return string
  1403. */
  1404. function genInputRadioSet(&$aInput)
  1405. {
  1406. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1407. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  1408. }
  1409. function _isSelected ($sValue, $sCurValue)
  1410. {
  1411. return ((string)$sValue === (string)$sCurValue);
  1412. }
  1413. function _isSelectedMultiple ($sValue, $aCurValues)
  1414. {
  1415. return in_array($sValue, $aCurValues);
  1416. }
  1417. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  1418. {
  1419. $aOptions = [];
  1420. if(isset($aInput['values']) && is_array($aInput['values'])) {
  1421. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1422. $aOption = array(
  1423. 'bx_if:show_option_group_begin' => [
  1424. 'condition' => false,
  1425. 'content' => []
  1426. ],
  1427. 'bx_if:show_option_group_end' => [
  1428. 'condition' => false,
  1429. 'content' => []
  1430. ],
  1431. 'bx_if:show_option' => [
  1432. 'condition' => false,
  1433. 'content' => []
  1434. ]
  1435. );
  1436. $sOptAttrs = "";
  1437. if(is_array($sOptTitle)) {
  1438. if(isset($sOptTitle['type'])) {
  1439. switch($sOptTitle['type']) {
  1440. case 'group_header':
  1441. $aOption = array_merge($aOption, [
  1442. 'bx_if:show_option_group_begin' => [
  1443. 'condition' => true,
  1444. 'content' => [
  1445. 'opt_label' => bx_process_output($sOptTitle['value'])
  1446. ]
  1447. ]
  1448. ]);
  1449. $aOptions[] = $aOption;
  1450. break;
  1451. case 'group_end':
  1452. $aOption = array_merge($aOption, [
  1453. 'bx_if:show_option_group_end' => [
  1454. 'condition' => true,
  1455. 'content' => []
  1456. ]
  1457. ]);
  1458. $aOptions[] = $aOption;
  1459. break;
  1460. }
  1461. continue;
  1462. }
  1463. $aOptAttrs = $this->_genInputSelectOptionAttrs($sOptTitle);
  1464. if(!empty($aOptAttrs))
  1465. $sOptAttrs = bx_convert_array2attrs($aOptAttrs);
  1466. $sOptValue = $sOptTitle['key'];
  1467. $sOptTitle = $sOptTitle['value'];
  1468. }
  1469. $aOption = array_merge($aOption, [
  1470. 'bx_if:show_option' => [
  1471. 'condition' => true,
  1472. 'content' => [
  1473. 'opt_title' => bx_process_output($sOptTitle),
  1474. 'opt_value' => bx_html_attribute($sOptValue),
  1475. 'opt_attrs' => $sOptAttrs,
  1476. 'bx_if:show_selected' => [
  1477. 'condition' => $this->$sIsSelectedFunc($sOptValue, $mixedCurrentVal),
  1478. 'content' => []
  1479. ]
  1480. ]
  1481. ]
  1482. ]);
  1483. $aOptions[] = $aOption;
  1484. }
  1485. }
  1486. $aAttrs = $this->_genInputSelectAttrs($aInput, $isMultiple);
  1487. return $this->_parseInputSelect('form_field_select.html', [
  1488. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
  1489. 'bx_repeat:options' => $aOptions,
  1490. 'content' => !empty($aInput['content']) ? $aInput['content'] : ''
  1491. ]);
  1492. }
  1493. protected function _genInputSelectAttrs(&$aInput, $isMultiple)
  1494. {
  1495. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1496. $aAttrs['name'] = $aInput['name'];
  1497. if ($isMultiple) {
  1498. $aAttrs['name'] .= '[]';
  1499. $aAttrs['multiple'] = 'multiple';
  1500. }
  1501. // for inputs with labels generate id
  1502. if (isset($aInput['label']))
  1503. $aAttrs['id'] = $this->getInputId($aInput);
  1504. return $aAttrs;
  1505. }
  1506. protected function _genInputSelectOptionAttrs(&$aOption)
  1507. {
  1508. $aAttrs = [];
  1509. if(!empty($aOption['attrs']) && is_array($aOption['attrs']))
  1510. $aAttrs = $aOption['attrs'];
  1511. if(isset($aOption['class']))
  1512. $aAttrs['class'] = !empty($aAttrs['class']) ? $aAttrs['class'] . ' ' . $aOption['class'] : $aOption['class'];
  1513. if(isset($aOption['style']))
  1514. $aAttrs['style'] = !empty($aAttrs['style']) ? $aAttrs['style'] . ' ' . $aOption['style'] : $aOption['style'];
  1515. return $aAttrs;
  1516. }
  1517. protected function _parseInputSelect($sTmplName, $aTmplVars)
  1518. {
  1519. return $this->oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  1520. }
  1521. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  1522. {
  1523. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  1524. $aAttrs['name'] = $aInput['name'];
  1525. // for inputs with labels generate id
  1526. if (isset($aInput['label']))
  1527. $aAttrs['id'] = $this->getInputId($aInput);
  1528. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1529. // generate options
  1530. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1531. $iDividerThreshold = !empty($aInput['dv_thd']) ? (int)$aInput['dv_thd'] : 3;
  1532. $sOptions = '';
  1533. if (isset($aInput['values']) && is_array($aInput['values'])) {
  1534. if (count($aInput['values']) > $iDividerThreshold && $sDivider == $this->_sDivider)
  1535. $sDivider = $this->_sDividerAlt;
  1536. // generate complex input using simple standard inputs
  1537. foreach ($aInput['values'] as $sValue => $sLabel) {
  1538. if(is_array($sLabel)) {
  1539. $sValue = $sLabel['key'];
  1540. $sLabel = $sLabel['value'];
  1541. }
  1542. // create new simple input
  1543. $aNewInput = array(
  1544. 'type' => $sType,
  1545. 'name' => $aInput['name'] . $sNameAppend,
  1546. 'value' => $sValue,
  1547. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1548. 'label' => $sLabel,
  1549. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1550. );
  1551. if (isset($aInput['label_as_html']))
  1552. $aNewInput['label_as_html'] = $aInput['label_as_html'];
  1553. $sNewInput = $this->genInput($aNewInput);
  1554. // attach new input to complex
  1555. $sOptions .= ($sNewInput . $sDivider);
  1556. }
  1557. }
  1558. // generate element
  1559. $sCode = <<<BLAH
  1560. <div $sAttrs>
  1561. $sOptions
  1562. </div>
  1563. BLAH;
  1564. return $sCode;
  1565. }
  1566. function genInputLocation(&$aInput)
  1567. {
  1568. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1569. if (!$o)
  1570. return _t('_sys_txt_error_occured');
  1571. return $o->genInputLocation($aInput, $this);
  1572. }
  1573. public function setLocationVals ($aInput, $aVals)
  1574. {
  1575. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1576. if (!$o)
  1577. return false;
  1578. return $o->setLocationVals ($aInput, $aVals, $this);
  1579. }
  1580. public function setLocationVal ($aInput, $sIndex, $sVal)
  1581. {
  1582. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1583. if (!$o)
  1584. return false;
  1585. return $o->setLocationVal ($aInput, $sIndex, $sVal, $this);
  1586. }
  1587. protected function getLocationVal ($aInput, $sIndex)
  1588. {
  1589. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1590. if (!$o)
  1591. return false;
  1592. return $o->getLocationVal ($aInput, $sIndex, $this);
  1593. }
  1594. function genInputPassword(&$aInput)
  1595. {
  1596. return $this->oTemplate->parseHtmlByName('form_field_password.html', array('input' => $this->genInputStandard($aInput)));
  1597. }
  1598. function genInputPrice(&$aInput)
  1599. {
  1600. /*
  1601. if(!isset($aInput['attrs_wrapper']['class'])) {
  1602. if(!isset($aInput['attrs_wrapper']))
  1603. $aInput['attrs_wrapper'] = [];
  1604. $aInput['attrs_wrapper']['class'] = '';
  1605. }
  1606. $aInput['attrs_wrapper']['class'] .= 'bx-form-input-wrapper-text';
  1607. if(!isset($aInput['attrs']['class'])) {
  1608. if(!isset($aInput['attrs']))
  1609. $aInput['attrs'] = [];
  1610. $aInput['attrs']['class'] = '';
  1611. }
  1612. $aInput['attrs']['class'] .= 'bx-form-input-text';
  1613. */
  1614. $sCurrency = '';
  1615. if(!isset($aInput['value_currency'])) {
  1616. $sCurrency = BxDolPayments::getInstance()->getOption('default_currency_code');
  1617. if(empty($sCurrency))
  1618. $sCurrency = getParam('currency_code');
  1619. }
  1620. else
  1621. $sCurrency = $aInput['value_currency'];
  1622. return $this->oTemplate->parseHtmlByName('form_field_price.html', array(
  1623. 'bx_if:show_currency' => [
  1624. 'condition' => !empty($sCurrency),
  1625. 'content' => [
  1626. 'currency' => $sCurrency,
  1627. ]
  1628. ],
  1629. 'input' => $this->genInputStandard($aInput)
  1630. ));
  1631. }
  1632. function genInputNestedForm(&$aInput)
  1633. {
  1634. return '';
  1635. }
  1636. function genNestedForm(&$aInput)
  1637. {
  1638. return '';
  1639. }
  1640. function genInputCaptcha(&$aInput)
  1641. {
  1642. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1643. // for inputs with labels generate id
  1644. if (isset($aInput['label']))
  1645. $aAttrs['id'] = $this->getInputId($aInput);
  1646. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1647. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1648. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1649. }
  1650. /**
  1651. * Generate Label Element
  1652. *
  1653. * @param string $aInput input array
  1654. * @return string HTML code
  1655. */
  1656. function genLabel(&$aInput)
  1657. {
  1658. if (!isset($aInput['label']) || empty($aInput['label']))
  1659. return '';
  1660. $sInputID = $this->getInputId($aInput);
  1661. $sLabel = bx_process_output($aInput['label']);
  1662. if (isset($aInput['label_as_html']) && $aInput['label_as_html'] == true)
  1663. $sLabel = $aInput['label'];
  1664. return '<label for="' . $sInputID . '">' . $sLabel . '</label>';
  1665. }
  1666. function genPrivacyGroupChooser(&$aInput, $sPrivacyObject = '')
  1667. {
  1668. if(!isset($aInput['privacy']) || empty($aInput['privacy']))
  1669. return '';
  1670. $iAuthorId = bx_get_logged_profile_id();
  1671. $mixedResult = checkActionModule($iAuthorId, 'set form fields privacy', 'system');
  1672. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1673. return '';
  1674. if(empty($sPrivacyObject))
  1675. $sPrivacyObject = $this->_sPrivacyObjectView;
  1676. $iInputId = (int)$aInput['id'];
  1677. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  1678. if(!isset($aInput['attrs_wrapper']['class']))
  1679. $aInput['attrs_wrapper']['class'] = '';
  1680. $aInput['attrs_wrapper']['class'] .= ' bx-form-input-wrapper-pgc';
  1681. return $this->oTemplate->parseHtmlByName('form_field_privacy.html', array(
  1682. 'js_object' => $this->getJsObjectName(),
  1683. 'html_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  1684. 'id' => $iInputId,
  1685. 'privacy_object' => $sPrivacyObject,
  1686. 'icon' => $this->_getPrivacyIcon($mixedPrivacyGroup)
  1687. ));
  1688. }
  1689. function genInfoIcon($sInfo)
  1690. {
  1691. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1692. }
  1693. function genErrorIcon( $sError = '' )
  1694. {
  1695. if ($this->bEnableErrorIcon) {
  1696. $sStyle = '';
  1697. if (!$sError)
  1698. $sStyle = 'style="display:none;"';
  1699. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1700. }
  1701. }
  1702. function getOpenSection($aAttrs = array(), $sLegend = '')
  1703. {
  1704. if (!$this->_isSectionOpened) {
  1705. if (!$aAttrs || !is_array($aAttrs))
  1706. $aAttrs = array();
  1707. if ($sLegend)
  1708. $sClassesAdd = "bx-form-section-header";
  1709. else
  1710. $sClassesAdd = "bx-form-section-divider";
  1711. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1712. $this->_isSectionOpened = true;
  1713. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <div $sAttrs> $sLegend <div class=\"bx-form-section-content bx-def-padding-top bx-def-padding-bottom" . ($sLegend ? ' bx-def-padding-left bx-def-padding-right' : '') . "\">\n";
  1714. } else {
  1715. return '';
  1716. }
  1717. }
  1718. function getCloseSection()
  1719. {
  1720. if ($this->_isSectionOpened) {
  1721. $this->_isSectionOpened = false;
  1722. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1723. } else {
  1724. return '';
  1725. }
  1726. }
  1727. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1728. {
  1729. if (!$this->_isSectionOpened) {
  1730. if (!$aAttrs || !is_array($aAttrs))
  1731. $aAttrs = array();
  1732. if ($sLegend)
  1733. $sClassesAdd = "bx-form-section-header";
  1734. else
  1735. $sClassesAdd = "bx-form-section-divider";
  1736. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1737. $this->_isSectionOpened = true;
  1738. return "<!-- form header content begins -->\n <div class=\"bx-form-section-wrapper bx-def-margin-top\"> <div $sAttrs> $sLegend <div class=\"bx-form-section-content bx-def-padding-top\">\n";
  1739. } else {
  1740. return '';
  1741. }
  1742. }
  1743. function getCloseSectionViewMode()
  1744. {
  1745. if ($this->_isSectionOpened) {
  1746. $this->_isSectionOpened = false;
  1747. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1748. } else {
  1749. return '';
  1750. }
  1751. }
  1752. static function getJsUiLangs ()
  1753. {
  1754. return array ('af' => 1, 'ar-DZ' => 1, 'ar' => 1, 'az' => 1, 'be' => 1, 'bg' => 1, 'bs' => 1, 'ca' => 1, 'cs' => 1, 'cy-GB' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'en-AU' => 1, 'en-GB' => 1, 'en-NZ' => 1, 'en' => 1, 'eo' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fo' => 1, 'fr-CA' => 1, 'fr-CH' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hi' => 1, 'hr' => 1, 'hu' => 1, 'hy' => 1, 'id' => 1, 'is' => 1, 'it' => 1, 'ja' => 1, 'ka' => 1, 'kk' => 1, 'km' => 1, 'ko' => 1, 'ky' => 1, 'lb' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'ml' => 1, 'ms' => 1, 'nb' => 1, 'nl-BE' => 1, 'nl' => 1, 'nn' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'rm' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sq' => 1, 'sr-SR' => 1, 'sr' => 1, 'sv' => 1, 'ta' => 1, 'th' => 1, 'tj' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-HK' => 1, 'zh-TW' => 1);
  1755. }
  1756. function addCssJsUi ()
  1757. {
  1758. if (self::$_isCssJsUiAdded)
  1759. return;
  1760. $aUiLangs = $this->getJsUiLangs ();
  1761. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1762. $this->_addJs(array(
  1763. 'jquery-ui/jquery-ui.custom.min.js',
  1764. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1765. ), "'undefined' === typeof($.datepicker)");
  1766. $this->_addCss('jquery-ui/jquery-ui.css');
  1767. self::$_isCssJsUiAdded = true;
  1768. }
  1769. function addCssJsUiSortable ()
  1770. {
  1771. if (self::$_isCssJsUiSortableAdded)
  1772. return;
  1773. $this->_addJs(array(
  1774. 'jquery-ui/jquery.ui.sortable.min.js'
  1775. ), "'undefined' === typeof($.sortable)");
  1776. self::$_isCssJsUiSortableAdded = true;
  1777. }
  1778. function addCssJsTimepicker ()
  1779. {
  1780. if (self::$_isCssJsTimepickerAdded)
  1781. return;
  1782. $aCalendarLangs = array ('af' => 1, 'am' => 1, 'bg' => 1, 'ca' => 1, 'cs' => 1, 'da' => 1, 'de' => 1, 'el' => 1, 'es' => 1, 'et' => 1, 'eu' => 1, 'fa' => 1, 'fi' => 1, 'fr' => 1, 'gl' => 1, 'he' => 1, 'hr' => 1, 'hu' => 1, 'id' => 1, 'it' => 1, 'ja' => 1, 'ko' => 1, 'lt' => 1, 'lv' => 1, 'mk' => 1, 'nl' => 1, 'no' => 1, 'pl' => 1, 'pt-BR' => 1, 'pt' => 1, 'ro' => 1, 'ru' => 1, 'sk' => 1, 'sl' => 1, 'sr-RS' => 1, 'sr-YU' => 1, 'sv' => 1, 'th' => 1, 'tr' => 1, 'uk' => 1, 'vi' => 1, 'zh-CN' => 1, 'zh-TW' => 1);
  1783. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1784. $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
  1785. $this->_addJs(array(
  1786. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1787. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1788. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1789. 'jquery.ui.touch-punch.min.js',
  1790. ), "'undefined' === typeof($.fn.datetimepicker)");
  1791. self::$_isCssJsTimepickerAdded = true;
  1792. }
  1793. function addCssJsMinicolors ()
  1794. {
  1795. if (self::$_isCssJsMinicolorsAdded)
  1796. return;
  1797. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  1798. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  1799. self::$_isCssJsMinicolorsAdded = true;
  1800. }
  1801. function addCssJsViewMode ()
  1802. {
  1803. if (self::$_isCssJsAddedViewMode)
  1804. return;
  1805. $this->_addCss('forms.css');
  1806. self::$_isCssJsAddedViewMode = true;
  1807. }
  1808. function addCssJsCore ()
  1809. {
  1810. if (self::$_isCssJsAdded)
  1811. return;
  1812. $this->_addCss('forms.css');
  1813. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  1814. $this->_addJs(array('BxDolForm.js', 'BxDolNestedForm.js'), "true");
  1815. self::$_isCssJsAdded = true;
  1816. }
  1817. function addCssJs ()
  1818. {
  1819. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1820. $this->addCssJsViewMode ();
  1821. } else {
  1822. $this->addCssJsCore();
  1823. }
  1824. }
  1825. protected function _processCssJs()
  1826. {
  1827. $sRet = '';
  1828. if ($this->_bDynamicMode) {
  1829. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1830. $sRet .= "\n<script>\n";
  1831. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  1832. foreach ($this->_aJs as $sCondition => $aJs) {
  1833. $sJs = $this->oTemplate->addJs($aJs, true);
  1834. if (!$sJs)
  1835. continue;
  1836. $sRet .= "if ($sCondition)\n";
  1837. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1838. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  1839. }
  1840. $sRet .= "\n</script>\n";
  1841. }
  1842. else {
  1843. $this->oTemplate->addCss($this->_aCss);
  1844. $aJs = array();
  1845. foreach ($this->_aJs as $sCondition => $a)
  1846. $aJs = array_merge($aJs, $a);
  1847. $this->oTemplate->addJs($aJs);
  1848. }
  1849. return $sRet;
  1850. }
  1851. protected function _addJs($mixed, $sJsCondition)
  1852. {
  1853. if (!is_array($mixed))
  1854. $mixed = array($mixed);
  1855. $this->_aJs[$sJsCondition] = $mixed;
  1856. }
  1857. protected function _addCss($mixed)
  1858. {
  1859. if (!is_array($mixed))
  1860. $mixed = array($mixed);
  1861. foreach ($mixed as $s)
  1862. $this->_aCss[$s] = $s;
  1863. }
  1864. }
  1865. /** @} */