BxBaseFormView.php 76 KB

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