'; /** * Alternative divider for several inputs */ protected $_sDividerAlt = '
'; /** * Form is added dynamically. */ protected $_bDynamicMode = false; /** * Form is submitted dynamically (using Ajax Submit). */ protected $_bAjaxMode = false; /** * Use absolute Action URL which is needed in Ajax Mode. */ protected $_bAbsoluteActionUrl = false; /** * Form is displayed in view mode. */ protected $_bViewMode = false; /** * Cached CSRF check for revealing a secret field in the current request. */ protected $_bSecretRevealCsrfValid = null; /** * Show or not sections which have no fileds */ protected $_bShowEmptySections = false; /** * Function name for generation close form section HTML. */ protected $_sSectionClose = 'getCloseSection'; /** * Function name for generation open form section HTML. */ protected $_sSectionOpen = 'getOpenSection'; /** * JS files list for form */ protected $_aJs = array(); /** * CSS files list for form */ protected $_aCss = array(); protected $_sJsClassName; protected $_sJsObjectName; protected $_aHtmlIds; protected $_aAgentsFormObject; protected $_sAgentFormObject; /** * Constructor * * @param array $aInfo Form contents * * $aInfo['params'] = array( * 'remove_form' => true|false, * ); * * @return BxBaseFormView */ function __construct($aInfo, $oTemplate) { parent::__construct($aInfo, $oTemplate); $this->_bAjaxMode = isset($this->aParams['ajax_mode']) && $this->aParams['ajax_mode']; $this->_bViewMode = isset($this->aParams['view_mode']) && $this->aParams['view_mode']; if($this->_bViewMode) { $this->_sSectionClose = 'getCloseSectionViewMode'; $this->_sSectionOpen = 'getOpenSectionViewMode'; } $this->_sJsClassName = 'BxDolForm'; $sName = !empty($aInfo['params']['display']) ? $aInfo['params']['display'] : ''; if(empty($sName) && !empty($aInfo['params']['object'])) $sName = $aInfo['params']['object']; if(empty($sName) && !empty($aInfo['form_attrs']['id'])) $sName = $aInfo['form_attrs']['id']; if(empty($sName) && !empty($aInfo['form_attrs']['name'])) $sName = $aInfo['form_attrs']['name']; $this->_sJsObjectName = 'oForm' . bx_gen_method_name($sName, array('_' , '-')); $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $sName); $this->_aHtmlIds = array( 'help_popup' => $sHtmlId . '-help-popup-', 'pgc' => $sHtmlId . '-pgc-', 'pgc_popup' => $sHtmlId . '-pgc-popup-', 'pgc_form' => $sHtmlId . '-pgc-form-', ); $this->_sAgentFormObject = $aInfo['params']['object'] ?? $this->id; $oAi = BxDolAI::getInstance(); $this->_aAgentsFormObject = $oAi ? $oAi->getAgentsByFormObject($this->_sAgentFormObject) : []; if ($this->_aAgentsFormObject && is_array($this->_aAgentsFormObject)) { $this->_aAgentsFormObject = array_filter($this->_aAgentsFormObject, function ($a) use ($oAi) { return $oAi->canInteract($a); }); } } public function performActionGetHelp() { $sInput = bx_process_input(bx_get('input')); if(empty($sInput) || empty($this->aInputs[$sInput]['help'])) return; echo $this->oTemplate->parseHtmlByName('form_field_help_popup.html', array( 'content' => _t($this->aInputs[$sInput]['help']), 'bx_if:show_edit' => array( 'condition' => isAdmin(), 'content' => array( 'link' => BX_DOL_URL_STUDIO . bx_append_url_params('builder_forms.php', array( 'page' => 'fields', 'module' => $this->aParams['module'], 'object' => $this->aParams['object'], 'display' => $this->aParams['display'] )) ) ) )); } public function performActionChangePrivacyGroup() { $iAuthorId = (int)bx_get_logged_profile_id(); if(empty($iAuthorId)) return echoJson(array()); $iInputId = bx_process_input(bx_get('input_id'), BX_DATA_INT); if(empty($iInputId)) return echoJson(array()); $sPrivacyObject = bx_process_input(bx_get('privacy_object')); $sPrivacyField = BxDolPrivacy::getFieldName($sPrivacyObject); if(empty($sPrivacyField)) return echoJson(array()); $sPrivacyGroup = bx_process_input(bx_get($sPrivacyField)); if(empty($sPrivacyField)) return echoJson(array()); if(!BxDolFormQuery::setInputPrivacy($iInputId, $iAuthorId, $sPrivacyField, $sPrivacyGroup)) return echoJson(array()); return echoJson(array( 'code' => 0, 'form_id' => $this->getId(), 'chooser_id' => $this->_aHtmlIds['pgc'] . $iInputId, 'icon' => $this->_getPrivacyIcon($sPrivacyGroup), 'eval' => $this->getJsObjectName() . '.pgcOnSelectGroup(oData);' )); } public function performActionGetPrivacyGroupChooser() { $iInputId = bx_process_input(bx_get('input_id'), BX_DATA_INT); $sPrivacyObject = bx_process_input(bx_get('privacy_object')); if(empty($iInputId) || empty($sPrivacyObject)) return ''; $sFormId = $this->_aHtmlIds['pgc_form'] . $iInputId; $aFormActionParams = array(); if(!empty($this->aParams['object']) && !empty($this->aParams['display'])) $aFormActionParams = array( 'o' => $this->aParams['object'], 'd' => $this->aParams['display'] ); $aFormActionParams['a'] = 'change_privacy_group'; $iAuthorId = bx_get_logged_profile_id(); $mixedPrivacyGroup = $this->_getPrivacyGroup($sPrivacyObject, $iInputId, $iAuthorId); $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject); $aInputPgc = $oPrivacy->getGroupChooser($sPrivacyObject, $iAuthorId); $aInputPgc['value'] = $mixedPrivacyGroup; if(!isset($aInputPgc['content'])) $aInputPgc['content'] = ''; $aInputPgc['content'] .= $oPrivacy->addCssJs(true); $aInputPgc['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iAuthorId, array( 'content_id' => $iInputId, 'group_id' => $mixedPrivacyGroup, 'html_ids' => array( 'form' => $sFormId ) )); $aFormInfo = array( 'form_attrs' => array( 'id' => $sFormId, 'action' => BX_DOL_URL_ROOT . bx_append_url_params('form.php', $aFormActionParams), 'method' => 'post' ), 'inputs' => array ( 'input_id' => array( 'type' => 'hidden', 'name' => 'input_id', 'value' => $iInputId, 'db' => array ( 'pass' => 'Int', ), ), 'privacy_object' => array( 'type' => 'hidden', 'name' => 'privacy_object', 'value' => $sPrivacyObject, 'db' => array ( 'pass' => 'Xss', ), ), 'privacy_group_chooser' => $aInputPgc, 'controls' => array( 'name' => 'controls', 'type' => 'input_set', array( 'type' => 'button', 'name' => 'do_submit', 'value' => _t('_sys_submit'), 'attrs' => array( 'onclick' => "$('#" . $sFormId . "').submit()", ), ), array ( 'type' => 'reset', 'name' => 'do_cancel', 'value' => _t('_Cancel'), 'attrs' => array( 'onclick' => "$(this).parents('.bx-popup-applied:visible:first').dolPopupHide()", 'class' => 'bx-def-margin-sec-left', ), ) ) ) ); $oForm = new BxTemplFormView($aFormInfo); echo $this->oTemplate->parseHtmlByName('form_field_privacy_popup.html', array( 'form_id' => $oForm->getId(), 'form' => $oForm->getCode(true), )); } function setShowEmptySections($b) { $this->_bShowEmptySections = $b; } function setAjaxMode($bAjaxMode) { $this->_bAjaxMode = (bool)$bAjaxMode; } function isAjaxMode() { return $this->_bAjaxMode; } function isViewMode() { return (bool)($this->aParams['view_mode'] ?? 0); } function setAbsoluteActionUrl($sUrl) { if(empty($sUrl)) return; $this->aFormAttrs['action'] = $sUrl; $this->_bAbsoluteActionUrl = true; } /** * Return Form code * @param $bDynamicMode - set it to true if form is added via JS/AJAX call, for example form in AJAX popup. * @return string */ function getCode($bDynamicMode = false) { if(!$bDynamicMode && bx_is_dynamic_request()) $bDynamicMode = true; $this->_bDynamicMode = $bDynamicMode; $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs); $sInclude = ''; $this->sCode = false; /** * @hooks * @hookdef hook-system-form_output 'system', 'form_output' - hook to override form object and/or code to be output * - $unit_name - equals `system` * - $action - equals `form_output` * - $object_id - not used * - $sender_id - not used * - $extra_params - array of additional params with the following array keys: * - `dynamic` - [boolean] is dynamic mode * - `object` - [object] by ref, an instance of form class, @see BxDolForm, can be overridden in hook processing * - `code` - [boolean] or [string] by ref, when false the default generation mechanism will be used, can be overridden in hook processing * - `include` - [string] by ref, additional data to be attached to output, can be overridden in hook processing * @hook @ref hook-system-form_output */ bx_alert('system', 'form_output', 0, 0, [ 'dynamic' => $this->_bDynamicMode, 'object' => &$this, 'code' => &$this->sCode, 'include' => &$sInclude, 'code_ref' => &$this->sCode, 'include_ref' => &$sInclude, 'form_inputs_ref' => &$this->aInputs, ]); if($this->sCode === false) $this->sCode = $this->genForm(); $this->addCssJs(); $sDynamicCssJs = $this->_processCssJs(); return $sInclude . $sDynamicCssJs . $this->sCode; } function getCodeAPI() { $this->aFormAttrs = $this->_replaceMarkers($this->aFormAttrs); $this->sCode = false; /** * @hooks * @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. * - $unit_name - equals `system` * - $action - equals `form_output_api` * - $object_id - not used * - $sender_id - not used * - $extra_params - array of additional params with the following array keys: * - `dynamic` - [boolean] is dynamic mode * - `object` - [object] by ref, an instance of form class, @see BxDolForm, can be overridden in hook processing * - `code` - [boolean] by ref, when false the default generation mechanism will be used, can be overridden in hook processing * @hook @ref hook-system-form_output_api */ bx_alert('system', 'form_output_api', 0, 0, [ 'dynamic' => $this->_bDynamicMode, 'object' => &$this, 'code' => &$this->sCode, 'code_ref' => &$this->sCode, 'form_inputs_ref' => &$this->aInputs, ]); if($this->sCode === false) { if(!$this->genForm() && (bool)($this->aParams['view_mode'] ?? 0)) return []; } $oIconset = BxDolIconset::getObjectInstance(); $oFunctions = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate); // TODO: process inputs to translate titles, alerts, etc $aInputs = []; foreach($this->aInputs as $sKey => $aInput) { if(isset($aInput['visible_for_levels']) && !self::isVisible($aInput)) continue; if(isset($aInput['type']) && in_array($aInput['name'], ['captcha'])) continue; list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $oFunctions->getIcon($aInput['icon'] ?? ''); $aInput['icon'] = $sIcon ? $oIconset->getIcon($sIcon) : ($sIconHtml ? $sIconHtml : ''); if(!empty($aInput['info'])) $aInput['info'] = strip_tags($aInput['info']); if(!empty($aInput['help'])) $aInput['help'] = strip_tags($aInput['help']); if(!empty($aInput['error'])) $aInput['error'] = strip_tags($aInput['error']); $bType = isset($aInput['type']); if ($bType && 'select' == $aInput['type']) { if(($sK1 = 'value') && empty($aInput[$sK1]) && ($aValues = $aInput['values'] ?? false) && is_array($aValues)) $aInput[$sK1] = reset($aValues)['key'] ?? key($aValues); } if ($bType && 'files' == $aInput['type']) { $oStorage = BxDolStorage::getObjectInstance($aInput['storage_object']); $aInput['ext_allow'] = $oStorage->getObjectData()['ext_allow']; $aInput['ext_deny'] = $oStorage->getObjectData()['ext_deny']; $aInput['ghost_template'] = ''; $aInput['value'] = ''; $aInput['values'] = ''; $aInput['values_src'] = ''; if(!empty($aInput['content_id']) && ($sUpr = reset($aInput['uploaders'])) && ($sStg = $aInput['storage_object'] ?? '') && ($oUploader = BxDolUploader::getObjectInstance($sUpr, $sStg, genRndPwd(8))) !== false) { $oUploader->setMultiple($aInput['multiple'] ?? true); if(($sMethod = 'setSubmitted') && method_exists($oUploader, $sMethod)) $oUploader->$sMethod($this->isSubmitted()); $sData = $oUploader->getGhostsWithOrder((int)bx_get_logged_profile_id(), 'json', $aInput['images_transcoder'], $aInput['content_id']); if($sData && ($aData = json_decode($sData, true)) && !empty($aData['g']) && is_array($aData['g'])) { $aInput['values_src'] = array_values($aData['g']); $aGhostIds = []; array_walk($aData['g'], function($aGhost) use (&$aGhostIds) { $aGhostIds[] = $aGhost['file_id']; }); $aInput['value'] = implode(',', $aGhostIds); } } } if ($bType && 'custom' == $aInput['type']){ $sCustomMethod = 'genCustomInput' . $this->_genMethodName($aInput['name']); if (method_exists($this, $sCustomMethod)) { $mixed = $this->$sCustomMethod($aInput); $aInput = is_array($mixed) ? $mixed : ['type' => 'custom', 'content' => $mixed]; } } if ($bType && 'block_header' == $aInput['type']){ $aInput['name'] = $sKey; } if ($bType && 'block_header' == $aInput['type']){ $aInput['name'] = $sKey; } if ($bType && 'location' == $aInput['type']){ $oLocation = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default')); $aVars = []; foreach(BxDolForm::$LOCATION_INDEXES as $sKey) $aVars[$sKey] = $oLocation->getLocationVal($aInput, $sKey, $this); if(($sLocationString = bx_api_get_location_string($aVars))) $aVars['location_string'] = $sLocationString; $aInput['value'] = $aVars; } if ($bType && 'textarea' == $aInput['type']) { $iTaHtml = (int)($aInput['html'] ?? 0); if(($sTaValue = $aInput['value'] ?? false) && $iTaHtml == 0) $aInput['value'] = strip_tags($sTaValue); if($iTaHtml != 0) $aInput['mentions'] = $this->getMentionsQueryParams($aInput); } if ($bType && 'price' == $aInput['type']) { $aInput['value'] = [ 'value' => $aInput['value'], 'currency' => BxDolPayments::getInstance()->getOption('default_currency_sign') ?: getParam('currency_sign') ]; } if ($this->_isSecretInput($aInput) && isset($aInput['value']) && $aInput['value'] !== '' && $aInput['value'] !== null && !is_array($aInput['value'])) { if (!$this->isViewMode() || !$this->_isSecretRevealed($aInput['name'])) $aInput['value'] = bx_gen_secret((string)$aInput['value']); } /** * Everytime when [key => value] is used it should be converted to [key => ..., value => ...] */ if (($sVt = $aInput['type'] ?? false) && in_array($sVt, ['select', 'select_multiple', 'checkbox_set', 'radio_set']) && is_array(($aVv = $aInput[($sKv = 'values')] ?? false)) && !(is_array(($aVv1 = reset($aVv))) && isset($aVv1['key'], $aVv1['value']))) { $aInput[$sKv] = array_map(function($sKey) use($aInput, $sKv) { return ['key' => $sKey, 'value' => $aInput[$sKv][$sKey]]; }, array_keys($aInput[$sKv])); } if(isset($aInput['name']) && in_array($aInput['name'], ['allow_view_to', 'object_privacy_view'])) { $aParams = bx_get('params'); if(empty($aParams) || !is_array($aParams)) $aParams = []; if(!isset($aParams['context_id']) || ($iContextId = (int)$aParams['context_id']) >= 0) { $oProfile = BxDolProfile::getInstance(); foreach($aInput['values'] as $aValue) { //--- Selected friends if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_FRIENDS_SELECTED) { $aIds = BxDolConnection::getObjectInstance('sys_profiles_friends')->getConnectedContent($oProfile->id(), true, 0, 20); if(!empty($aIds) && is_array($aIds)) { $aInput['values_friends'] = []; foreach($aIds as $iId) $aInput['values_friends'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)]; } } //--- Selected relations if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_RELATIONS_SELECTED) { $aIds = BxDolConnection::getObjectInstance('sys_profiles_relations')->getConnectedContent($oProfile->id(), true, 0, 20); if(!empty($aIds) && is_array($aIds)) { $aInput['values_relations'] = []; foreach($aIds as $iId) $aInput['values_relations'][] = ['key' => $iId, 'value' => BxDolProfile::getData($iId)]; } } //--- Selected memberships if(isset($aValue['key']) && (int)$aValue['key'] == BX_DOL_PG_MEMBERSHIPS_SELECTED) { $aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true); if(!empty($aLevels) && is_array($aLevels)) { $aInput['values_memberships'] = []; foreach($aLevels as $iId => $sTitle) $aInput['values_memberships'][] = ['key' => $iId, 'value' => $sTitle]; } } } } else $aInput = array_merge($aInput, [ 'type' => 'hidden', 'value' => $iContextId, 'owner_info' => BxDolProfile::getData(abs($iContextId)) ]); } $aInputs[$sKey] = $aInput; } return [ 'inputs' => $aInputs, 'attrs' => $this->aFormAttrs, 'params' => $this->aParams ]; } public function getJsClassName() { return $this->_sJsClassName; } public function getJsObjectName() { return $this->_sJsObjectName; } public function getJsScript($bWrap = false) { $sJsObjName = $this->getJsObjectName(); $sJsObjClass = $this->getJsClassName(); $sCode = "if(window['" . $sJsObjName . "'] == undefined) window['" . $sJsObjName . "'] = new " . $sJsObjClass . "(" . json_encode(array( 'sObjName' => $sJsObjName, 'sId' => $this->getId(), 'sName' => $this->getName(), 'sObject' => isset($this->aParams['object']) ? $this->aParams['object'] : '', 'sDisplay' => isset($this->aParams['display']) ? $this->aParams['display'] : '', 'sRootUrl' => BX_DOL_URL_ROOT, 'aHtmlIds' => $this->_aHtmlIds, 'bInitCodeMirror' => self::$_isCssJsCodeMirrorAdded === true, 'bLeavePageConfirmation' => getParam('sys_form_lpc_enable') == 'on', 'sTxtLeavePageConfirmation' => _t('_sys_leave_page_confirmation') )) . "); window['" . $sJsObjName . "'].init();"; return $bWrap ? $this->oTemplate->_wrapInTagJsCode($sCode) : $sCode; } /** * Generate the whole form * * @return string */ function genForm() { $this->_sCodeAdd = ''; $bViewMode = $this->isViewMode(); $sTable = $this->genRows(); if($bViewMode && !$sTable) return ''; $sHtmlBefore = isset($this->aParams['html_before']) ? $this->aParams['html_before'] : ''; $sHtmlAfter = isset($this->aParams['html_after']) ? $this->aParams['html_after'] : ''; if (!empty($this->aParams['remove_form']) || $bViewMode) { $sForm = <<