BxBaseFormView.php 96 KB

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