BxBaseFormView.php 95 KB

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