BxBaseFormView.php 91 KB

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