BxBaseFormView.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  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. default:
  630. if(isset($aInput['value']) && '' !== $aInput['value']) {
  631. $sValue = $aInput['value'];
  632. if(get_mb_substr($sValue, 0, 1) == '_')
  633. $sValue = _t($sValue);
  634. $sValue = bx_process_output($sValue);
  635. }
  636. else
  637. $sValue = null;
  638. }
  639. return $sValue;
  640. }
  641. function genViewRowValueForSelect(&$aInput)
  642. {
  643. if (!isset($aInput['value']) || !$aInput['value'])
  644. return null;
  645. $s = isset($aInput['value']) && isset($aInput['values'][$aInput['value']]) ? $aInput['values'][$aInput['value']] : null;
  646. if (isset($aInput['values_list_name']) && ($oCategory = BxDolCategory::getObjectInstanceByFormAndList($this->aFormAttrs['name'], $aInput['values_list_name'])) !== false)
  647. return $oCategory->getCategoryLink($s, $aInput['value']);
  648. return $s;
  649. }
  650. protected function genCustomRowBirthday(&$aInput)
  651. {
  652. if(!$this->_bViewMode)
  653. return $this->genRowStandard($aInput);
  654. $aInput['caption_src'] = '_sys_form_input_age';
  655. $aInput['caption'] = _t($aInput['caption_src']);
  656. return $this->genViewRowWrapped($aInput);
  657. }
  658. protected function genCustomViewRowValueBirthday(&$aInput)
  659. {
  660. if(!isset($aInput['value']) || !$aInput['value'] || in_array($aInput['value'], array('0000-00-00', '0000-00-00 00:00:00')))
  661. return null;
  662. $sValue = $aInput['value'];
  663. $iPosSpace = strpos($sValue, ' ');
  664. if($iPosSpace !== false)
  665. $sValue = trim(substr($sValue, 0, $iPosSpace));
  666. $aDate = explode('-', $sValue);
  667. $iCdYear = (int)date('Y');
  668. $iCdMonth = (int)date('n');
  669. $iCdDay = (int)date('j');
  670. $iResult = $iCdYear - (int)$aDate[0];
  671. if($iCdMonth < (int)$aDate[1] || ($iCdMonth == (int)$aDate[1] && $iCdDay < (int)$aDate[2]))
  672. $iResult -= 1;
  673. return $iResult;
  674. }
  675. /**
  676. * Generate standard row
  677. *
  678. * @param array $aInput
  679. * @return string
  680. */
  681. function genRowStandard(&$aInput, $isOneLine = false)
  682. {
  683. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption'], BX_DATA_HTML) : '';
  684. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  685. $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> ' : '';
  686. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  687. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  688. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  689. $sClassWrapper = 'bx-form-element-wrapper';
  690. if($isOneLine)
  691. $sClassWrapper .= ' ' . $sClassWrapper . '-oneline';
  692. $sClassWrapper .= ' bx-def-margin-top-auto';
  693. if (isset($aInput['name']))
  694. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  695. $sTrAttrs = bx_convert_array2attrs(isset($aInput['tr_attrs']) && is_array($aInput['tr_attrs']) ? $aInput['tr_attrs'] : array(), $sClassWrapper);
  696. $sClassOneLineCaption = '';
  697. $sClassOneLineValue = '';
  698. if ($isOneLine) {
  699. $sClassOneLineCaption = ' bx-form-caption-oneline bx-form-caption-oneline-' . $aInput['type'] . ' bx-def-margin-sec-left';
  700. $sClassOneLineValue = ' bx-form-value-oneline bx-form-value-oneline-' . $aInput['type'];
  701. $aInput['attrs']['id'] = $this->getInputId($aInput);
  702. if ($sCaption)
  703. $sCaption = '<label for="' . $aInput['attrs']['id'] . '">' . $sCaption . '</label>';
  704. }
  705. $sInput = $this->genInput($aInput);
  706. if(isset($aInput['error_updated']) && $aInput['error_updated'] === true)
  707. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  708. if(empty($sErrorIcon))
  709. $sErrorIcon = '';
  710. $aTmplVarsRow = array(
  711. 'bx_if:show_caption' => array(
  712. 'condition' => !empty($sCaption),
  713. 'content' => array(
  714. 'class_caption' => $sClassOneLineCaption,
  715. 'caption' => $sCaption,
  716. 'required' => $sRequired,
  717. 'help' => $sHelp,
  718. )
  719. ),
  720. 'class_value' => $sClassAdd . $sClassOneLineValue,
  721. 'value' => $this->genWrapperInput($aInput, $sInput),
  722. 'info' => $sInfoIcon,
  723. 'error' => $sErrorIcon,
  724. );
  725. return $this->oTemplate->parseHtmlByName('form_row_standard.html', array(
  726. 'tr_attrs' => $sTrAttrs,
  727. 'bx_if:show_one_line' => array(
  728. 'condition' => $isOneLine,
  729. 'content' => $aTmplVarsRow
  730. ),
  731. 'bx_if:show_lined' => array(
  732. 'condition' => !$isOneLine,
  733. 'content' => $aTmplVarsRow
  734. ),
  735. 'info' => $sInfoIcon,
  736. 'error' => $sErrorIcon
  737. ));
  738. }
  739. function genWrapperInput($aInput, $sContent)
  740. {
  741. $aAttrs = $this->_genWrapperInputAttrs($aInput);
  742. $sClass = "bx-form-input-wrapper bx-form-input-wrapper-{$aInput['type']}";
  743. if(isset($aInput['html']) && $aInput['html'] && $this->isHtmlEditor($aInput['html'], $aInput))
  744. $sClass .= ' bx-form-input-wrapper-html';
  745. return $this->oTemplate->parseHtmlByName('form_input_wrapper.html', array(
  746. 'attrs' => bx_convert_array2attrs($aAttrs, $sClass),
  747. 'content' => $sContent
  748. ));
  749. }
  750. protected function _genWrapperInputAttrs(&$aInput)
  751. {
  752. return isset($aInput['attrs_wrapper']) && is_array($aInput['attrs_wrapper']) ? $aInput['attrs_wrapper'] : [];
  753. }
  754. /**
  755. * Generate custom row
  756. *
  757. * @param array $aInput
  758. * @param string $sCustomMethod custom method to generate code for input
  759. * @return string
  760. */
  761. function genRowCustom(&$aInput, $sCustomMethod)
  762. {
  763. $sCaption = isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '';
  764. $sRequired = !empty($aInput['required']) ? '<span class="bx-form-required">*</span>' : '';
  765. $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> ' : '';
  766. $sClassAdd = !empty($aInput['error']) ? ' bx-form-error' : '';
  767. $sInfoIcon = !empty($aInput['info']) ? $this->genInfoIcon($aInput['info']) : '';
  768. $sErrorIcon = $this->genErrorIcon(empty($aInput['error']) ? '' : $aInput['error']);
  769. $sInput = $this->$sCustomMethod($aInput, $sInfoIcon, $sErrorIcon);
  770. if (isset($aInput['name']))
  771. $aInput['tr_attrs']['id'] = "bx-form-element-" . $aInput['name'];
  772. $sTrAttrs = bx_convert_array2attrs(empty($aInput['tr_attrs']) ? array() : $aInput['tr_attrs'], "bx-form-element-wrapper bx-def-margin-top");
  773. return $this->oTemplate->parseHtmlByName('form_row_custom.html', array(
  774. 'tr_attrs' => $sTrAttrs,
  775. 'bx_if:show_caption' => array(
  776. 'condition' => !empty($sCaption),
  777. 'content' => array(
  778. 'caption' => $sCaption,
  779. 'required' => $sRequired,
  780. 'help' => $sHelp
  781. )
  782. ),
  783. 'class_value' => $sClassAdd,
  784. 'value' => $sInput,
  785. ));
  786. }
  787. /**
  788. * Generate Block Headers row
  789. *
  790. * @param array $aInput
  791. * @return string
  792. */
  793. function genRowBlockHeader(&$aInput)
  794. {
  795. $aAttrs = empty($aInput['attrs']) ? '' : $aInput['attrs'];
  796. // if there is no caption - show divider only
  797. if (empty($aInput['caption'])) {
  798. $sCode = $this->{$this->_sSectionClose}();
  799. $sCode .= $this->{$this->_sSectionOpen}($aAttrs);
  800. return $sCode;
  801. }
  802. // if section is collapsed by default, add necessary code
  803. $sClassAddCollapsable = 'bx-form-collapsable';
  804. if (isset($aInput['collapsed']) and $aInput['collapsed'])
  805. $sClassAddCollapsable .= ' bx-form-collapsed bx-form-section-hidden';
  806. // display section with caption
  807. $sCode = $this->{$this->_sSectionClose}();
  808. if (empty($aAttrs))
  809. $aAttrs = array('class' => 'bx-form-collapsable ' . $sClassAddCollapsable);
  810. else
  811. $aAttrs['class'] .= ' bx-form-collapsable ' . $sClassAddCollapsable;
  812. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode'])
  813. $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>';
  814. else
  815. $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>';
  816. $sCode .= $this->{$this->_sSectionOpen}($aAttrs, $sLegend);
  817. return $sCode;
  818. }
  819. function genBlockEnd()
  820. {
  821. $aNextTbodyAdd = false; // need to have some default
  822. $sCode = '';
  823. $sCode .= $this->{$this->_sSectionClose}();
  824. $sCode .= $this->{$this->_sSectionOpen}($aNextTbodyAdd);
  825. return $sCode;
  826. }
  827. /**
  828. * Generate HTML Input Element
  829. *
  830. * @param array $aInput
  831. * @return string Output HTML Code
  832. */
  833. function genInput(&$aInput)
  834. {
  835. if (!empty($aInput['name'])) {
  836. $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']);
  837. if (method_exists($this, $sCustomMethod))
  838. return $this->$sCustomMethod($aInput);
  839. }
  840. switch ($aInput['type']) {
  841. case 'datepicker':
  842. if (!isset($aInput['attrs']))
  843. $aInput['attrs'] = array();
  844. $aInput['attrs']['autosuggestion'] = 'off';
  845. $sInput = $this->genInputStandard($aInput);
  846. break;
  847. // standard inputs (and non-standard, interpreted as standard)
  848. case 'text':
  849. case 'datepicker':
  850. case 'dateselect':
  851. case 'date_time':
  852. case 'datetime':
  853. case 'number':
  854. case 'time':
  855. case 'checkbox':
  856. case 'radio':
  857. case 'image':
  858. case 'slider':
  859. case 'doublerange':
  860. case 'hidden':
  861. $sInput = $this->genInputStandard($aInput);
  862. break;
  863. case 'password':
  864. $sInput = $this->genInputPassword($aInput);
  865. break;
  866. case 'file':
  867. $sInput = $this->genInputFile($aInput);
  868. break;
  869. case 'rgb':
  870. case 'rgba':
  871. $sClass = 'bx-form-input-' . $aInput['type'];
  872. $aInput['attrs']['class'] = !empty($aInput['attrs']['class']) ? $aInput['attrs']['class'] . ' ' . $sClass : $sClass;
  873. $aInput['type'] = 'text';
  874. $sInput = $this->genInputStandard($aInput);
  875. break;
  876. case 'switcher':
  877. $sInput = $this->genInputSwitcher($aInput);
  878. break;
  879. case 'button':
  880. case 'reset':
  881. case 'submit':
  882. $sInput = $this->genInputButton($aInput);
  883. break;
  884. case 'textarea':
  885. $sInput = $this->genInputTextarea($aInput);
  886. break;
  887. case 'select':
  888. $sInput = $this->genInputSelect($aInput);
  889. break;
  890. case 'select_multiple':
  891. $sInput = $this->genInputSelectMultiple($aInput);
  892. break;
  893. case 'checkbox_set':
  894. $sInput = $this->genInputCheckboxSet($aInput);
  895. break;
  896. case 'radio_set':
  897. $sInput = $this->genInputRadioSet($aInput);
  898. break;
  899. case 'input_set': // numeric array of inputs
  900. $sInput = '';
  901. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : ' ';
  902. foreach ($aInput as $iKey => $aSubInput) {
  903. if (!is_int($iKey) or !$aSubInput)
  904. continue; // parse only integer keys and existing values
  905. $sInput .= $this->genInput($aSubInput); // recursive call
  906. $sInput .= $sDivider;
  907. }
  908. $sInput .= '<div class="bx-clear"></div>';
  909. break;
  910. case 'custom':
  911. $sInput = isset($aInput['content']) ? $aInput['content'] : '';
  912. break;
  913. case 'captcha':
  914. $sInput = $this->genInputCaptcha($aInput);
  915. break;
  916. case 'location':
  917. $sInput = $this->genInputLocation($aInput);
  918. break;
  919. case 'nested_form':
  920. $sInput = $this->genInputNestedForm($aInput);
  921. break;
  922. case 'value':
  923. $sInput = '';
  924. if(!isset($aInput['value']))
  925. break;
  926. $sInput = $aInput['value'];
  927. if(get_mb_substr($sInput, 0, 1) == '_')
  928. $sInput = _t($sInput);
  929. break;
  930. default:
  931. //unknown control type
  932. $sInput = 'Unknown control type';
  933. }
  934. // create input label
  935. $sInput .= $this->genLabel($aInput);
  936. // create input privacy group chooser
  937. $sInput .= $this->genPrivacyGroupChooser($aInput);
  938. return $sInput;
  939. }
  940. /**
  941. * Generate new Input Element id
  942. *
  943. * @param array $aInput
  944. * @return string
  945. */
  946. function getInputId(&$aInput)
  947. {
  948. if (isset($aInput['id']))
  949. return $aInput['id'];
  950. $sName = md5($aInput['name']);
  951. $sID = $this->id . '_input_' . $sName;
  952. if ( // multiple elements cause identical id's
  953. (
  954. (
  955. $aInput['type'] == 'checkbox' and
  956. substr($aInput['name'], -2) == '[]' // it is multiple element
  957. ) or
  958. $aInput['type'] == 'radio' // it is always multiple (i think so... hm)
  959. ) and
  960. isset($aInput['value']) // if we can make difference
  961. ) {
  962. $sValue = md5($aInput['value']);
  963. // add value
  964. $sID .= '_' . $sValue;
  965. }
  966. $sID = trim($sID, '_');
  967. $aInput['id'] = $sID; // just for repeated calls
  968. return $sID;
  969. }
  970. /**
  971. * Generate standard Input Element
  972. *
  973. * @param array $aInput
  974. * @return string
  975. */
  976. function genInputStandard(&$aInput)
  977. {
  978. $aAttrs = $this->_genInputStandardAttrs($aInput);
  979. return $this->oTemplate->parseHtmlByName('form_field_standard.html', [
  980. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}")
  981. ]);
  982. }
  983. protected function _genInputStandardAttrs(&$aInput)
  984. {
  985. // clone attributes for system use ;)
  986. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  987. // add default className to attributes
  988. $aAttrs['type'] = $aInput['type'];
  989. if ('datetime' == $aAttrs['type'])
  990. $aAttrs['type'] = 'date_time';
  991. switch($aAttrs['type']) {
  992. case 'date_time':
  993. $this->addCssJsUi ();
  994. $this->addCssJsTimepicker ();
  995. break;
  996. case 'slider':
  997. case 'doublerange':
  998. case 'datepicker':
  999. $this->addCssJsUi ();
  1000. break;
  1001. case 'dateselect':
  1002. $this->_addJs(array(
  1003. 'combodate/combodate.js',
  1004. ), "'undefined' === typeof($.combodate)");
  1005. break;
  1006. case 'text':
  1007. if (isset($aAttrs['class']) && false !== strpos($aAttrs['class'], 'bx-form-input-rgb'))
  1008. $this->addCssJsMinicolors ();
  1009. break;
  1010. }
  1011. if ('doublerange' == $aAttrs['type'] && (!isset($aInput['value']) || !$aInput['value']) && !empty($aAttrs['min']) && !empty($aAttrs['max'])) {
  1012. $aInput['value'] = $aAttrs['min'] . '-' . $aAttrs['max'];
  1013. }
  1014. if (isset($aInput['name'])) $aAttrs['name'] = $aInput['name'];
  1015. if (isset($aInput['value'])) $aAttrs['value'] = $aInput['value'];
  1016. if (isset($aInput['db']['pass']) && ('DateUtc' == $aInput['db']['pass'] || 'DateTimeUtc' == $aInput['db']['pass'])) $aAttrs['data-utc'] = 1;
  1017. // for inputs with labels generate id
  1018. if (isset($aInput['label']))
  1019. $aAttrs['id'] = $this->getInputId($aInput);
  1020. // for checkboxes
  1021. if (isset($aInput['checked']) and $aInput['checked'])
  1022. $aAttrs['checked'] = 'checked';
  1023. return $aAttrs;
  1024. }
  1025. /**
  1026. * Generate Switcher Input Element (based on checkbox)
  1027. *
  1028. * @param array $aInput
  1029. * @return string
  1030. */
  1031. function genInputSwitcher(&$aInput)
  1032. {
  1033. $aInput['type'] = 'checkbox';
  1034. $sCheckbox = $this->genInputStandard($aInput);
  1035. $aInput['type'] = 'switcher';
  1036. $sClass = 'off';
  1037. if(isset($aInput['checked']) && $aInput['checked'])
  1038. $sClass = 'on';
  1039. return $this->oTemplate->parseHtmlByName('form_field_switcher.html', [
  1040. 'class' => $sClass,
  1041. 'checkbox' => $sCheckbox
  1042. ]);
  1043. }
  1044. /**
  1045. * Generate Button Input Element
  1046. *
  1047. * @param array $aInput
  1048. * @return string
  1049. */
  1050. function genInputButton(&$aInput)
  1051. {
  1052. $aAttrs = $this->_genInputButtonAttrs($aInput);
  1053. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']} bx-btn";
  1054. if($aInput['type'] == 'submit')
  1055. $sClassAdd .= ' bx-btn-primary';
  1056. return $this->oTemplate->parseHtmlByName('form_field_button.html', [
  1057. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1058. 'value' => $aInput['value']
  1059. ]);
  1060. }
  1061. protected function _genInputButtonAttrs(&$aInput)
  1062. {
  1063. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1064. // add default className to attributes
  1065. $aAttrs['type'] = $aInput['type'];
  1066. if(isset($aInput['value']))
  1067. $aAttrs['value'] = $aInput['value'];
  1068. if(isset($aInput['name']))
  1069. $aAttrs['name'] = $aInput['name'];
  1070. return $aAttrs;
  1071. }
  1072. /**
  1073. * Generate Textarea Element
  1074. *
  1075. * @param array $aInput
  1076. * @return string
  1077. */
  1078. function genInputTextarea(&$aInput)
  1079. {
  1080. $aAttrs = $this->_genInputTextareaAttrs($aInput);
  1081. $sClassAdd = "bx-def-font-inputs bx-form-input-{$aInput['type']}";
  1082. if(isset($aInput['html']) && $aInput['html'] && $this->addHtmlEditor($aInput['html'], $aInput))
  1083. $sClassAdd .= ' bx-form-input-html';
  1084. $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')) : '';
  1085. return $this->oTemplate->parseHtmlByName('form_field_textarea.html', [
  1086. 'attrs' => bx_convert_array2attrs($aAttrs, $sClassAdd),
  1087. 'value' => $sValue
  1088. ]);
  1089. }
  1090. protected function _genInputTextareaAttrs(&$aInput)
  1091. {
  1092. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1093. $aAttrs['name'] = $aInput['name'];
  1094. // for inputs with labels generate id
  1095. if (isset($aInput['label']))
  1096. $aAttrs['id'] = $this->getInputId($aInput);
  1097. return $aAttrs;
  1098. }
  1099. function isHtmlEditor($iViewMode, &$aInput)
  1100. {
  1101. return BxDolEditor::getObjectInstance(false, $this->oTemplate) !== false;
  1102. }
  1103. function addHtmlEditor($iViewMode, &$aInput)
  1104. {
  1105. $oEditor = BxDolEditor::getObjectInstance(false, $this->oTemplate);
  1106. if (!$oEditor)
  1107. return false;
  1108. $this->_sCodeAdd .= $oEditor->attachEditor ('#' . $this->aFormAttrs['id'] . ' [name='.$aInput['name'].']', $iViewMode, $this->_bDynamicMode, ['form_id' => $this->aFormAttrs['id'], 'element_name' => $aInput['name']]);
  1109. return true;
  1110. }
  1111. /**
  1112. * Generate Select Box Element
  1113. *
  1114. * @param array $aInput
  1115. * @return string
  1116. */
  1117. function genInputSelectBox(&$aInput, $sInfo = '', $sError = '')
  1118. {
  1119. $aNewInput = $aInput;
  1120. $aNewInput['type'] = 'select';
  1121. $aNewInput['name'] .= '[]';
  1122. $sInput = $this->genInput($aNewInput);
  1123. return <<<BLAH
  1124. <div class="bx-form-input-wrapper input-wrapper-{$aInput['type']}">
  1125. $sInput
  1126. </div>
  1127. $sInfo
  1128. $sError
  1129. BLAH;
  1130. }
  1131. /**
  1132. * Generate Browse File Element
  1133. *
  1134. * @param array $aInput
  1135. * @return string
  1136. */
  1137. function genInputFile(&$aInput)
  1138. {
  1139. $sOnChange = "$(this).parents('.bx-form-input-wrapper-file:first').find('.bx-fif-value').html($(this).val());";
  1140. if(!empty($aInput['attrs']['onchange']))
  1141. $aInput['attrs']['onchange'] = $sOnChange . $aInput['attrs']['onchange'];
  1142. else
  1143. $aInput['attrs']['onchange'] = $sOnChange;
  1144. $sInput = $this->genInputStandard($aInput);
  1145. 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>';
  1146. }
  1147. /**
  1148. * Generate Select Box Element
  1149. *
  1150. * @param array $aInput
  1151. * @return string
  1152. */
  1153. function genInputFiles(&$aInput, $sInfo = '', $sError = '')
  1154. {
  1155. if(empty($aInput['uploaders']) || !is_array($aInput['uploaders']))
  1156. return '';
  1157. $sUniqId = !empty($aInput['uploaders_id']) ? $aInput['uploaders_id'] : genRndPwd (8, false);
  1158. $sUploaders = '';
  1159. $oUploader = null;
  1160. foreach ($aInput['uploaders'] as $sUploaderObject) {
  1161. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $aInput['storage_object'], $sUniqId, $this->oTemplate);
  1162. if (!$oUploader)
  1163. continue;
  1164. $sGhostTemplate = $this->genGhostTemplate($aInput);
  1165. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : array();
  1166. if (empty($aInput['attrs']['disabled']))
  1167. $aAttrs = array_merge($aAttrs, array('onclick' => $oUploader->getNameJsInstanceUploader() . '.showUploaderForm();'));
  1168. $aParams = array(
  1169. 'button_title' => bx_js_string($oUploader->getUploaderButtonTitle(isset($aInput['upload_buttons_titles']) ? $aInput['upload_buttons_titles'] : false)),
  1170. 'content_id' => isset($aInput['content_id']) ? $aInput['content_id'] : '',
  1171. 'storage_private' => isset($aInput['storage_private']) ? $aInput['storage_private'] : '1',
  1172. 'attrs' => bx_convert_array2attrs($aAttrs),
  1173. 'btn_class' => !empty($aInput['attrs']['disabled']) ? 'bx-btn-disabled' : '',
  1174. );
  1175. if (isset($aInput['images_transcoder']) && $aInput['images_transcoder'])
  1176. $aParams['images_transcoder'] = bx_js_string($aInput['images_transcoder']);
  1177. $sUploaders .= $oUploader->getUploaderButton($sGhostTemplate, isset($aInput['multiple']) ? $aInput['multiple'] : true, $aParams, $this->_bDynamicMode);
  1178. }
  1179. if(!$oUploader)
  1180. return '';
  1181. $bInitReordering = !empty($aInput['init_reordering']);
  1182. if($bInitReordering) {
  1183. $this->addCssJsUi();
  1184. $this->addCssJsUiSortable();
  1185. }
  1186. return $this->oTemplate->parseHtmlByName('form_field_uploader.html', array(
  1187. 'uploaders_buttons' => $sUploaders,
  1188. 'info' => $sInfo,
  1189. 'error' => $sError,
  1190. 'id_container_errors' => $oUploader ? $oUploader->getIdContainerErrors() : '',
  1191. 'id_container_result' => $oUploader ? $oUploader->getIdContainerResult() : '',
  1192. 'uploader_instance_name' => $oUploader ? $oUploader->getNameJsInstanceUploader() : '',
  1193. 'is_init_ghosts' => isset($aInput['init_ghosts']) && !$aInput['init_ghosts'] ? 0 : 1,
  1194. 'is_init_reordering' => !$bInitReordering ? 0 : 1
  1195. ));
  1196. }
  1197. protected function genGhostTemplate(&$aInput)
  1198. {
  1199. $sGhostTemplate = false;
  1200. if (isset($aInput['ghost_template']) && is_object($aInput['ghost_template'])) { // form is not submitted and ghost template is BxDolFormNested object
  1201. $oFormNested = $aInput['ghost_template'];
  1202. if ($oFormNested instanceof BxDolFormNestedGhost)
  1203. $sGhostTemplate = $oFormNested->genForm();
  1204. } 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
  1205. $oFormNested = new BxDolFormNestedGhost($aInput['name'], $aInput['ghost_template'], $this->aParams['db']['submit_name'], $this->oTemplate);
  1206. $sGhostTemplate = $oFormNested->getCode();
  1207. } elseif (isset($aInput['ghost_template']) && is_array($aInput['ghost_template']) && $aInput['ghost_template']) { // form is submitted and ghost template is array of BxDolFormNested objects
  1208. $sGhostTemplate = array ();
  1209. foreach ($aInput['ghost_template'] as $iFileId => $oFormNested)
  1210. if (is_object($oFormNested) && $oFormNested instanceof BxDolFormNestedGhost)
  1211. $sGhostTemplate[$iFileId] = $oFormNested->genForm();
  1212. } elseif (isset($aInput['ghost_template']) && is_string($aInput['ghost_template'])) { // ghost template is just string template, without nested form
  1213. $sGhostTemplate = $aInput['ghost_template'];
  1214. }
  1215. return $sGhostTemplate;
  1216. }
  1217. protected function genCustomInputUsernamesSuggestions ($aInput)
  1218. {
  1219. $bDisabled = isset($aInput['attrs']['disabled']) && $aInput['attrs']['disabled'] == 'disabled';
  1220. $aAttrs = $this->_genCustomInputUsernamesSuggestionsAttrs($aInput, $bDisabled);
  1221. $aTmplVarsVals = [];
  1222. if(!empty($aInput['value'])) {
  1223. if(is_array($aInput['value'])) {
  1224. foreach($aInput['value'] as $sVal) {
  1225. if(!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  1226. continue;
  1227. $aTmplVarsVals[] = [
  1228. 'item_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  1229. 'item_name' => $oProfile->getDisplayName(),
  1230. 'name' => $aInput['name'],
  1231. 'value' => $sVal
  1232. ];
  1233. }
  1234. }
  1235. else if(is_string($aInput['value']))
  1236. $aTmplVarsVals = $aInput['value'];
  1237. }
  1238. $aTmplVarsInputText = [];
  1239. if(!$bDisabled) {
  1240. $aInputText = $aInput;
  1241. $aInputText['type'] = 'text';
  1242. $aInputText['value'] = '';
  1243. $aInputText['attrs'] = $this->_genCustomInputUsernamesSuggestionsTextAttrs($aInputText, $bDisabled);
  1244. unset($aInputText['name']);
  1245. $aTmplVarsInputText['input'] = $this->genInputStandard($aInputText);
  1246. }
  1247. $this->addCssJsUi();
  1248. return $this->oTemplate->parseHtmlByName('form_field_custom_suggestions.html', array(
  1249. 'name' => $aInput['name'],
  1250. 'attrs' => bx_convert_array2attrs($aAttrs),
  1251. 'bx_repeat:vals' => $aTmplVarsVals,
  1252. 'bx_if:input' => array(
  1253. 'condition' => !$bDisabled,
  1254. 'content' => $aTmplVarsInputText
  1255. ),
  1256. 'bx_if:init' => array(
  1257. 'condition' => !$bDisabled,
  1258. 'content' => array(
  1259. 'id' => $aAttrs['id'],
  1260. 'name' => $aInput['name'],
  1261. 'url_get_recipients' => $aInput['ajax_get_suggestions'],
  1262. 'b_img' => isset($aInput['custom']['b_img']) ? (int)$aInput['custom']['b_img'] : 1,
  1263. 'only_once' => isset($aInput['custom']['only_once']) ? 1 : 0,
  1264. 'on_select' => isset($aInput['custom']['on_select']) ? $aInput['custom']['on_select']: 'null',
  1265. 'placeholder' => bx_html_attribute(isset($aInput['placeholder']) ? $aInput['placeholder'] : _t('_sys_form_paceholder_profiles_suggestions'), BX_ESCAPE_STR_QUOTE),
  1266. )
  1267. )
  1268. ));
  1269. }
  1270. protected function _genCustomInputUsernamesSuggestionsAttrs (&$aInput, $bDisabled = false)
  1271. {
  1272. $aAttrs = ['id' => $aInput['name'] . time() . mt_rand(0, 100)];
  1273. $aAttrs['class'] = 'bx-form-input-autotoken bx-form-input-text bx-def-font-inputs';
  1274. if($bDisabled)
  1275. $aAttrs['class'] .= ' bx-form-input-disabled';
  1276. if(!empty($aInput['attrs']['class']))
  1277. $aAttrs['class'] .= ' ' . $aInput['attrs']['class'];
  1278. return $aAttrs;
  1279. }
  1280. protected function _genCustomInputUsernamesSuggestionsTextAttrs (&$aInput, $bDisabled = false)
  1281. {
  1282. $aAttrs = ['value' => '', 'autocomplete' => 'off', 'autocapitalize' => 'off', 'autocorrect' => 'off'];
  1283. if(!empty($aInput['attrs']) && is_array($aInput['attrs'])) {
  1284. if(isset($aInput['attrs']['class']))
  1285. unset($aInput['attrs']['class']);
  1286. $aAttrs = array_merge($aAttrs, $aInput['attrs']);
  1287. }
  1288. return $aAttrs;
  1289. }
  1290. protected function genCustomViewRowValueLabels ($aInput)
  1291. {
  1292. if (empty($aInput['value']) || !($oMetatags = BxDolMetatags::getObjectInstance($aInput['meta_object'])) || !$oMetatags->keywordsIsEnabled())
  1293. return null;
  1294. if (!empty($aInput['value']) && !is_array($aInput['value']))
  1295. $aInput['value'] = unserialize($aInput['value']);
  1296. $s = '';
  1297. foreach ($aInput['value'] as $sLabel)
  1298. $s .= '<a href="' . $oMetatags->keywordsGetHashTagUrl($sLabel, $aInput['content_id']) . '"><b class="bx-def-label val">' . trim($sLabel) . '</b></a>';
  1299. return $this->oTemplate->parseHtmlByName('form_field_labels_view.html', array(
  1300. 'values' => $s,
  1301. ));
  1302. }
  1303. protected function genCustomInputLabels ($aInput)
  1304. {
  1305. $oLabel = BxDolLabel::getInstance();
  1306. $sName = !empty($aInput['name']) ? $aInput['name'] : 'labels';
  1307. $sValue = '';
  1308. if(!empty($aInput['value'])) {
  1309. if(!is_array($aInput['value']))
  1310. $aInput['value'] = unserialize($aInput['value']);
  1311. if(is_array($aInput['value']))
  1312. foreach($aInput['value'] as $sLabel)
  1313. $sValue .= $oLabel->getLabel($sName, $sLabel);
  1314. }
  1315. $sKeyPlaceholder = $aInput['caption_src'] . '_placeholder';
  1316. if(strcmp($sKeyPlaceholder, _t($sKeyPlaceholder)) != 0)
  1317. $sValue .= $oLabel->getLabelPlaceholder($sKeyPlaceholder);
  1318. $aInputLabels = array(
  1319. 'type' => 'custom',
  1320. 'name' => $sName,
  1321. 'caption' => '',
  1322. 'value' => $sValue,
  1323. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . bx_append_url_params('label.php', array(
  1324. 'action' => 'labels_list',
  1325. )),
  1326. 'attrs' => array(
  1327. 'class' => 'bx-form-input-labels',
  1328. 'disabled' => 'disabled'
  1329. )
  1330. );
  1331. return $this->oTemplate->parseHtmlByName('label_select_field.html', array(
  1332. 'js_object' => $oLabel->getJsObjectName(),
  1333. 'js_code' => $oLabel->getJsCodeForm(),
  1334. 'html_id' => $oLabel->getFormFieldId($aInput),
  1335. 'name' => $sName,
  1336. 'input_labels' => $this->genCustomInputUsernamesSuggestions($aInputLabels)
  1337. ));
  1338. }
  1339. /**
  1340. * Generate Select Element
  1341. *
  1342. * @param array $aInput
  1343. * @return string
  1344. */
  1345. function genInputSelect(&$aInput)
  1346. {
  1347. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1348. return $this->_genInputSelect($aInput, false, $sCurValue, '_isSelected');
  1349. }
  1350. /**
  1351. * Generate Multiple Select Element
  1352. *
  1353. * @param array $aInput
  1354. * @return string
  1355. */
  1356. function genInputSelectMultiple(&$aInput)
  1357. {
  1358. $aCurValues = array();
  1359. if (isset($aInput['value']) && $aInput['value'])
  1360. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1361. return $this->_genInputSelect($aInput, true, $aCurValues, '_isSelectedMultiple');
  1362. }
  1363. /**
  1364. * Generate Checkbox Set Element
  1365. *
  1366. * @param array $aInput
  1367. * @return string
  1368. */
  1369. function genInputCheckboxSet(&$aInput)
  1370. {
  1371. $aCurValues = array();
  1372. if (isset($aInput['value']) && $aInput['value'])
  1373. $aCurValues = is_array($aInput['value']) ? $aInput['value'] : array();
  1374. return $this->_genInputsSet($aInput, 'checkbox', $aCurValues, '_isSelectedMultiple', '[]');
  1375. }
  1376. /**
  1377. * Generate Radiobuttons Set Element
  1378. *
  1379. * @param array $aInput
  1380. * @return string
  1381. */
  1382. function genInputRadioSet(&$aInput)
  1383. {
  1384. $sCurValue = isset($aInput['value']) ? $aInput['value'] : '';
  1385. return $this->_genInputsSet($aInput, 'radio', $sCurValue, '_isSelected');
  1386. }
  1387. function _isSelected ($sValue, $sCurValue)
  1388. {
  1389. return ((string)$sValue === (string)$sCurValue);
  1390. }
  1391. function _isSelectedMultiple ($sValue, $aCurValues)
  1392. {
  1393. return in_array($sValue, $aCurValues);
  1394. }
  1395. function _genInputSelect(&$aInput, $isMultiple, $mixedCurrentVal, $sIsSelectedFunc)
  1396. {
  1397. $aOptions = [];
  1398. if(isset($aInput['values']) && is_array($aInput['values'])) {
  1399. foreach ($aInput['values'] as $sOptValue => $sOptTitle) {
  1400. $aOption = array(
  1401. 'bx_if:show_option_group_begin' => [
  1402. 'condition' => false,
  1403. 'content' => []
  1404. ],
  1405. 'bx_if:show_option_group_end' => [
  1406. 'condition' => false,
  1407. 'content' => []
  1408. ],
  1409. 'bx_if:show_option' => [
  1410. 'condition' => false,
  1411. 'content' => []
  1412. ]
  1413. );
  1414. $sOptAttrs = "";
  1415. if(is_array($sOptTitle)) {
  1416. if(isset($sOptTitle['type'])) {
  1417. switch($sOptTitle['type']) {
  1418. case 'group_header':
  1419. $aOption = array_merge($aOption, [
  1420. 'bx_if:show_option_group_begin' => [
  1421. 'condition' => true,
  1422. 'content' => [
  1423. 'opt_label' => bx_process_output($sOptTitle['value'])
  1424. ]
  1425. ]
  1426. ]);
  1427. $aOptions[] = $aOption;
  1428. break;
  1429. case 'group_end':
  1430. $aOption = array_merge($aOption, [
  1431. 'bx_if:show_option_group_end' => [
  1432. 'condition' => true,
  1433. 'content' => []
  1434. ]
  1435. ]);
  1436. $aOptions[] = $aOption;
  1437. break;
  1438. }
  1439. continue;
  1440. }
  1441. $aOptAttrs = $this->_genInputSelectOptionAttrs($sOptTitle);
  1442. if(!empty($aOptAttrs))
  1443. $sOptAttrs = bx_convert_array2attrs($aOptAttrs);
  1444. $sOptValue = $sOptTitle['key'];
  1445. $sOptTitle = $sOptTitle['value'];
  1446. }
  1447. $aOption = array_merge($aOption, [
  1448. 'bx_if:show_option' => [
  1449. 'condition' => true,
  1450. 'content' => [
  1451. 'opt_title' => bx_process_output($sOptTitle),
  1452. 'opt_value' => bx_html_attribute($sOptValue),
  1453. 'opt_attrs' => $sOptAttrs,
  1454. 'bx_if:show_selected' => [
  1455. 'condition' => $this->$sIsSelectedFunc($sOptValue, $mixedCurrentVal),
  1456. 'content' => []
  1457. ]
  1458. ]
  1459. ]
  1460. ]);
  1461. $aOptions[] = $aOption;
  1462. }
  1463. }
  1464. $aAttrs = $this->_genInputSelectAttrs($aInput, $isMultiple);
  1465. return $this->_parseInputSelect('form_field_select.html', [
  1466. 'attrs' => bx_convert_array2attrs($aAttrs, "bx-def-font-inputs bx-form-input-{$aInput['type']}"),
  1467. 'bx_repeat:options' => $aOptions,
  1468. 'content' => !empty($aInput['content']) ? $aInput['content'] : ''
  1469. ]);
  1470. }
  1471. protected function _genInputSelectAttrs(&$aInput, $isMultiple)
  1472. {
  1473. $aAttrs = !empty($aInput['attrs']) ? $aInput['attrs'] : [];
  1474. $aAttrs['name'] = $aInput['name'];
  1475. if ($isMultiple) {
  1476. $aAttrs['name'] .= '[]';
  1477. $aAttrs['multiple'] = 'multiple';
  1478. }
  1479. // for inputs with labels generate id
  1480. if (isset($aInput['label']))
  1481. $aAttrs['id'] = $this->getInputId($aInput);
  1482. return $aAttrs;
  1483. }
  1484. protected function _genInputSelectOptionAttrs(&$aOption)
  1485. {
  1486. $aAttrs = [];
  1487. if(!empty($aOption['attrs']) && is_array($aOption['attrs']))
  1488. $aAttrs = $aOption['attrs'];
  1489. if(isset($aOption['class']))
  1490. $aAttrs['class'] = !empty($aAttrs['class']) ? $aAttrs['class'] . ' ' . $aOption['class'] : $aOption['class'];
  1491. if(isset($aOption['style']))
  1492. $aAttrs['style'] = !empty($aAttrs['style']) ? $aAttrs['style'] . ' ' . $aOption['style'] : $aOption['style'];
  1493. return $aAttrs;
  1494. }
  1495. protected function _parseInputSelect($sTmplName, $aTmplVars)
  1496. {
  1497. return $this->oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  1498. }
  1499. function _genInputsSet(&$aInput, $sType, $mixedCurrentVal, $sIsCheckedFunc, $sNameAppend = '')
  1500. {
  1501. $aAttrs = empty($aInput['attrs']) || 'radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type'] ? array() : $aInput['attrs'];
  1502. $aAttrs['name'] = $aInput['name'];
  1503. // for inputs with labels generate id
  1504. if (isset($aInput['label']))
  1505. $aAttrs['id'] = $this->getInputId($aInput);
  1506. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1507. // generate options
  1508. $sDivider = isset($aInput['dv']) ? $aInput['dv'] : $this->_sDivider;
  1509. $iDividerThreshold = !empty($aInput['dv_thd']) ? (int)$aInput['dv_thd'] : 3;
  1510. $sOptions = '';
  1511. if (isset($aInput['values']) && is_array($aInput['values'])) {
  1512. if (count($aInput['values']) > $iDividerThreshold && $sDivider == $this->_sDivider)
  1513. $sDivider = $this->_sDividerAlt;
  1514. // generate complex input using simple standard inputs
  1515. foreach ($aInput['values'] as $sValue => $sLabel) {
  1516. if(is_array($sLabel)) {
  1517. $sValue = $sLabel['key'];
  1518. $sLabel = $sLabel['value'];
  1519. }
  1520. // create new simple input
  1521. $aNewInput = array(
  1522. 'type' => $sType,
  1523. 'name' => $aInput['name'] . $sNameAppend,
  1524. 'value' => $sValue,
  1525. 'checked' => $this->$sIsCheckedFunc($sValue, $mixedCurrentVal),
  1526. 'label' => $sLabel,
  1527. 'attrs' => !empty($aInput['attrs']) && ('radio_set' == $aInput['type'] || 'checkbox_set' == $aInput['type']) ? $aInput['attrs'] : false,
  1528. );
  1529. if (isset($aInput['label_as_html']))
  1530. $aNewInput['label_as_html'] = $aInput['label_as_html'];
  1531. $sNewInput = $this->genInput($aNewInput);
  1532. // attach new input to complex
  1533. $sOptions .= ($sNewInput . $sDivider);
  1534. }
  1535. }
  1536. // generate element
  1537. $sCode = <<<BLAH
  1538. <div $sAttrs>
  1539. $sOptions
  1540. </div>
  1541. BLAH;
  1542. return $sCode;
  1543. }
  1544. function genInputLocation(&$aInput)
  1545. {
  1546. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1547. if (!$o)
  1548. return _t('_sys_txt_error_occured');
  1549. return $o->genInputLocation($aInput, $this);
  1550. }
  1551. public function setLocationVals ($aInput, $aVals)
  1552. {
  1553. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1554. if (!$o)
  1555. return false;
  1556. return $o->setLocationVals ($aInput, $aVals, $this);
  1557. }
  1558. public function setLocationVal ($aInput, $sIndex, $sVal)
  1559. {
  1560. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1561. if (!$o)
  1562. return false;
  1563. return $o->setLocationVal ($aInput, $sIndex, $sVal, $this);
  1564. }
  1565. protected function getLocationVal ($aInput, $sIndex)
  1566. {
  1567. $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default'));
  1568. if (!$o)
  1569. return false;
  1570. return $o->getLocationVal ($aInput, $sIndex, $this);
  1571. }
  1572. function genInputPassword(&$aInput)
  1573. {
  1574. return $this->oTemplate->parseHtmlByName('form_field_password.html', array('input' => $this->genInputStandard($aInput)));
  1575. }
  1576. function genInputNestedForm(&$aInput)
  1577. {
  1578. return '';
  1579. }
  1580. function genNestedForm(&$aInput)
  1581. {
  1582. return '';
  1583. }
  1584. function genInputCaptcha(&$aInput)
  1585. {
  1586. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  1587. // for inputs with labels generate id
  1588. if (isset($aInput['label']))
  1589. $aAttrs['id'] = $this->getInputId($aInput);
  1590. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-input-{$aInput['type']}");
  1591. $oCaptcha = BxDolCaptcha::getObjectInstance();
  1592. return "<div $sAttrs>" . ($oCaptcha ? $oCaptcha->display($this->_bDynamicMode) : _t('_sys_txt_captcha_not_available')) . "</div>";
  1593. }
  1594. /**
  1595. * Generate Label Element
  1596. *
  1597. * @param string $aInput input array
  1598. * @return string HTML code
  1599. */
  1600. function genLabel(&$aInput)
  1601. {
  1602. if (!isset($aInput['label']) || empty($aInput['label']))
  1603. return '';
  1604. $sInputID = $this->getInputId($aInput);
  1605. $sLabel = bx_process_output($aInput['label']);
  1606. if (isset($aInput['label_as_html']) && $aInput['label_as_html'] == true)
  1607. $sLabel = $aInput['label'];
  1608. return '<label for="' . $sInputID . '">' . $sLabel . '</label>';
  1609. }
  1610. function genPrivacyGroupChooser(&$aInput, $sPrivacyObject = '')
  1611. {
  1612. if(!isset($aInput['privacy']) || empty($aInput['privacy']))
  1613. return '';
  1614. $iAuthorId = bx_get_logged_profile_id();
  1615. $mixedResult = checkActionModule($iAuthorId, 'set form fields privacy', 'system');
  1616. if($mixedResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  1617. return '';
  1618. if(empty($sPrivacyObject))
  1619. $sPrivacyObject = $this->_sPrivacyObjectView;
  1620. $iInputId = (int)$aInput['id'];
  1621. $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId);
  1622. if(!isset($aInput['attrs_wrapper']['class']))
  1623. $aInput['attrs_wrapper']['class'] = '';
  1624. $aInput['attrs_wrapper']['class'] .= ' bx-form-input-wrapper-pgc';
  1625. return $this->oTemplate->parseHtmlByName('form_field_privacy.html', array(
  1626. 'js_object' => $this->getJsObjectName(),
  1627. 'html_id' => $this->_aHtmlIds['pgc'] . $iInputId,
  1628. 'id' => $iInputId,
  1629. 'privacy_object' => $sPrivacyObject,
  1630. 'icon' => $this->_getPrivacyIcon($mixedPrivacyGroup)
  1631. ));
  1632. }
  1633. function genInfoIcon($sInfo)
  1634. {
  1635. return '<div class="bx-form-info bx-def-font-grayed bx-def-font-small">' . bx_process_output($sInfo, BX_DATA_HTML) . '</div>';
  1636. }
  1637. function genErrorIcon( $sError = '' )
  1638. {
  1639. if ($this->bEnableErrorIcon) {
  1640. $sStyle = '';
  1641. if (!$sError)
  1642. $sStyle = 'style="display:none;"';
  1643. return '<div class="bx-form-warn" ' . $sStyle . '>' . $sError . '</div>';
  1644. }
  1645. }
  1646. function getOpenSection($aAttrs = array(), $sLegend = '')
  1647. {
  1648. if (!$this->_isSectionOpened) {
  1649. if (!$aAttrs || !is_array($aAttrs))
  1650. $aAttrs = array();
  1651. if ($sLegend)
  1652. $sClassesAdd = "bx-form-section-header";
  1653. else
  1654. $sClassesAdd = "bx-form-section-divider";
  1655. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1656. $this->_isSectionOpened = true;
  1657. 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";
  1658. } else {
  1659. return '';
  1660. }
  1661. }
  1662. function getCloseSection()
  1663. {
  1664. if ($this->_isSectionOpened) {
  1665. $this->_isSectionOpened = false;
  1666. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1667. } else {
  1668. return '';
  1669. }
  1670. }
  1671. function getOpenSectionViewMode($aAttrs = array(), $sLegend = '')
  1672. {
  1673. if (!$this->_isSectionOpened) {
  1674. if (!$aAttrs || !is_array($aAttrs))
  1675. $aAttrs = array();
  1676. if ($sLegend)
  1677. $sClassesAdd = "bx-form-section-header";
  1678. else
  1679. $sClassesAdd = "bx-form-section-divider";
  1680. $sAttrs = bx_convert_array2attrs($aAttrs, "bx-form-section bx-form-view-section bx-def-padding-sec-top bx-def-border-top " . $sClassesAdd);
  1681. $this->_isSectionOpened = true;
  1682. 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";
  1683. } else {
  1684. return '';
  1685. }
  1686. }
  1687. function getCloseSectionViewMode()
  1688. {
  1689. if ($this->_isSectionOpened) {
  1690. $this->_isSectionOpened = false;
  1691. return "</div> </div> </div> \n<!-- form header content ends -->\n";
  1692. } else {
  1693. return '';
  1694. }
  1695. }
  1696. static function getJsUiLangs ()
  1697. {
  1698. 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);
  1699. }
  1700. function addCssJsUi ()
  1701. {
  1702. if (self::$_isCssJsUiAdded)
  1703. return;
  1704. $aUiLangs = $this->getJsUiLangs ();
  1705. $sUiLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aUiLangs);
  1706. $this->_addJs(array(
  1707. 'jquery-ui/jquery-ui.custom.min.js',
  1708. 'jquery-ui/i18n/jquery.ui.datepicker-' . $sUiLang . '.min.js',
  1709. ), "'undefined' === typeof($.datepicker)");
  1710. $this->_addCss('jquery-ui/jquery-ui.css');
  1711. self::$_isCssJsUiAdded = true;
  1712. }
  1713. function addCssJsUiSortable ()
  1714. {
  1715. if (self::$_isCssJsUiSortableAdded)
  1716. return;
  1717. $this->_addJs(array(
  1718. 'jquery-ui/jquery.ui.sortable.min.js'
  1719. ), "'undefined' === typeof($.sortable)");
  1720. self::$_isCssJsUiSortableAdded = true;
  1721. }
  1722. function addCssJsTimepicker ()
  1723. {
  1724. if (self::$_isCssJsTimepickerAdded)
  1725. return;
  1726. $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);
  1727. $sCalendarLang = BxDolLanguages::getInstance()->detectLanguageFromArray ($aCalendarLangs);
  1728. $this->_addCss('timepicker-addon/jquery-ui-timepicker-addon.css');
  1729. $this->_addJs(array(
  1730. 'timepicker-addon/jquery-ui-timepicker-addon.min.js',
  1731. 'timepicker-addon/jquery-ui-sliderAccess.js',
  1732. 'timepicker-addon/i18n/jquery-ui-timepicker-' . $sCalendarLang . '.js',
  1733. 'jquery.ui.touch-punch.min.js',
  1734. ), "'undefined' === typeof($.fn.datetimepicker)");
  1735. self::$_isCssJsTimepickerAdded = true;
  1736. }
  1737. function addCssJsMinicolors ()
  1738. {
  1739. if (self::$_isCssJsMinicolorsAdded)
  1740. return;
  1741. $this->_addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'jquery-minicolors/|jquery.minicolors.css');
  1742. $this->_addJs('jquery-minicolors/jquery.minicolors.min.js', "'undefined' === typeof($.minicolors)");
  1743. self::$_isCssJsMinicolorsAdded = true;
  1744. }
  1745. function addCssJsViewMode ()
  1746. {
  1747. if (self::$_isCssJsAddedViewMode)
  1748. return;
  1749. $this->_addCss('forms.css');
  1750. self::$_isCssJsAddedViewMode = true;
  1751. }
  1752. function addCssJsCore ()
  1753. {
  1754. if (self::$_isCssJsAdded)
  1755. return;
  1756. $this->_addCss('forms.css');
  1757. $this->_addJs('jquery.webForms.js', "'undefined' === typeof($.fn.addWebForms)");
  1758. $this->_addJs(array('BxDolForm.js', 'BxDolNestedForm.js'), "true");
  1759. self::$_isCssJsAdded = true;
  1760. }
  1761. function addCssJs ()
  1762. {
  1763. if (isset($this->aParams['view_mode']) && $this->aParams['view_mode']) {
  1764. $this->addCssJsViewMode ();
  1765. } else {
  1766. $this->addCssJsCore();
  1767. }
  1768. }
  1769. protected function _processCssJs()
  1770. {
  1771. $sRet = '';
  1772. if ($this->_bDynamicMode) {
  1773. $sRet .= $this->oTemplate->addCss($this->_aCss, true);
  1774. $sRet .= "\n<script>\n";
  1775. $sRet .= "\nglJsLoadOnaddWebForms = [];\n";
  1776. foreach ($this->_aJs as $sCondition => $aJs) {
  1777. $sJs = $this->oTemplate->addJs($aJs, true);
  1778. if (!$sJs)
  1779. continue;
  1780. $sRet .= "if ($sCondition)\n";
  1781. if (preg_match_all("/src=\"(.*?)\"/", $sJs, $aMatches))
  1782. $sRet .= "\tglJsLoadOnaddWebForms = glJsLoadOnaddWebForms.concat(" . json_encode($aMatches[1]) . ");\n";
  1783. }
  1784. $sRet .= "\n</script>\n";
  1785. }
  1786. else {
  1787. $this->oTemplate->addCss($this->_aCss);
  1788. $aJs = array();
  1789. foreach ($this->_aJs as $sCondition => $a)
  1790. $aJs = array_merge($aJs, $a);
  1791. $this->oTemplate->addJs($aJs);
  1792. }
  1793. return $sRet;
  1794. }
  1795. protected function _addJs($mixed, $sJsCondition)
  1796. {
  1797. if (!is_array($mixed))
  1798. $mixed = array($mixed);
  1799. $this->_aJs[$sJsCondition] = $mixed;
  1800. }
  1801. protected function _addCss($mixed)
  1802. {
  1803. if (!is_array($mixed))
  1804. $mixed = array($mixed);
  1805. foreach ($mixed as $s)
  1806. $this->_aCss[$s] = $s;
  1807. }
  1808. }
  1809. /** @} */