BxBaseFormView.php 80 KB

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