BxBaseFormView.php 96 KB

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