BxBaseFormView.php 96 KB

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