BxBaseFormView.php 100 KB

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