BxBaseFormView.php 91 KB

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