BxBaseFormView.php 96 KB

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