BxBaseFormView.php 81 KB

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