BxBaseFormView.php 78 KB

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