From 528b1c15016c938540a70c5591e298232f441f35 Mon Sep 17 00:00:00 2001 From: "roman.lesnikov@gmail.com" Date: Wed, 22 Nov 2023 10:08:01 +0200 Subject: [PATCH] #4558 - Build-in API support (grids, locations) --- .../BxBaseModGeneralGridAdministration.php | 16 +++- .../classes/BxBaseModProfileTemplate.php | 3 +- .../BxBaseModTextGridAdministration.php | 21 +++++ .../timeline/classes/BxTimelineModule.php | 9 +++ template/scripts/BxBaseCmtsServices.php | 3 +- template/scripts/BxBaseFormView.php | 14 ++++ template/scripts/BxBaseGrid.php | 78 ++++++++++++++++++- template/scripts/BxBaseMetatags.php | 4 + template/scripts/BxBasePage.php | 12 ++- template/scripts/BxBaseServices.php | 5 ++ 10 files changed, 154 insertions(+), 11 deletions(-) diff --git a/modules/base/general/classes/BxBaseModGeneralGridAdministration.php b/modules/base/general/classes/BxBaseModGeneralGridAdministration.php index dbf87c0627..6f51cf5411 100644 --- a/modules/base/general/classes/BxBaseModGeneralGridAdministration.php +++ b/modules/base/general/classes/BxBaseModGeneralGridAdministration.php @@ -68,8 +68,11 @@ public function performActionDelete($aParams = array()) $aIdsAffected[] = $iId; $iAffected++; } - - echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_delete']))); + $mRv = $iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_delete'])); + if (bx_is_api()){ + return $mRv; + } + echoJson($mRv); } public function performActionClearReports($aParams = array()) @@ -127,6 +130,12 @@ protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisa return ''; } + if (bx_is_api()){ + $a['name'] = $sKey; + $a['type'] = 'callback'; + return $a; + } + return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow); } @@ -283,6 +292,9 @@ protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow) { $CNF = &$this->_oModule->_oConfig->CNF; + if (bx_is_api()){ + return ['type' => 'switcher', 'data' => $aRow[$this->_sStatusField], 'fld' => $this->_sStatusField]; + } if(isset($aRow[$this->_sStatusField]) && !in_array($aRow[$this->_sStatusField], $this->_aStatusValues)) { $sStatusKey = '_sys_status_' . $aRow[$this->_sStatusField]; if(!empty($CNF['T']['txt_status_' . $aRow[$this->_sStatusField]])) diff --git a/modules/base/profile/classes/BxBaseModProfileTemplate.php b/modules/base/profile/classes/BxBaseModProfileTemplate.php index c2386b9194..bfae9da53d 100644 --- a/modules/base/profile/classes/BxBaseModProfileTemplate.php +++ b/modules/base/profile/classes/BxBaseModProfileTemplate.php @@ -622,7 +622,8 @@ function urlThumb ($aData, $bSubstituteNoImage = true) function urlAvatar ($aData, $bSubstituteNoImage = true) { $CNF = &$this->_oConfig->CNF; - return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_AVATAR'], 'no-picture-preview.png', $aData, $bSubstituteNoImage); + $sDefault = bx_is_api() ? '' : 'no-picture-preview.png'; + return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_AVATAR'], $sDefault, $aData, $bSubstituteNoImage); } /** diff --git a/modules/base/text/classes/BxBaseModTextGridAdministration.php b/modules/base/text/classes/BxBaseModTextGridAdministration.php index 003eb388ba..ad07de6bc6 100644 --- a/modules/base/text/classes/BxBaseModTextGridAdministration.php +++ b/modules/base/text/classes/BxBaseModTextGridAdministration.php @@ -137,16 +137,30 @@ protected function _getCellTitle($mixedValue, $sKey, $aField, $aRow) if ($sTitle == '') $sTitle = _t('_sys_txt_no_title'); + if (bx_is_api()){ + return ['type' => 'link', 'data' => [ + 'text' => $aRow[$CNF['FIELD_TITLE']], + 'url' => $sUrl = bx_api_get_relative_url(bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aRow[$CNF['FIELD_ID']]))) + ]]; + } + return parent::_getCellDefault($this->_getEntryLink($sTitle, $aRow), $sKey, $aField, $aRow); } protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow) { + if (bx_is_api()){ + return ['type' => 'time', 'data' => $mixedValue]; + } return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow); } protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow) { + if (bx_is_api()){ + return ['type' => 'profile', 'data' => BxDolProfile::getData($mixedValue)]; + } + $oProfile = $this->_getProfileObject($aRow['author']); $sProfile = $oProfile->getDisplayName(); @@ -182,6 +196,13 @@ protected function _getActionEdit($sType, $sKey, $a, $isSmall = false, $isDisabl $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_EDIT_ENTRY'] . '&id=' . $aRow[$CNF['FIELD_ID']])); + if (bx_is_api()){ + $a['type'] = 'link'; + $a['name'] = $sKey; + $a['url'] = bx_api_get_relative_url($sUrl); + return $a; + } + $a['attr'] = array_merge($a['attr'], array( "onclick" => "window.open('" . $sUrl . "','_self');" )); diff --git a/modules/boonex/timeline/classes/BxTimelineModule.php b/modules/boonex/timeline/classes/BxTimelineModule.php index fe38f0366d..fd9b3a44f6 100644 --- a/modules/boonex/timeline/classes/BxTimelineModule.php +++ b/modules/boonex/timeline/classes/BxTimelineModule.php @@ -3264,6 +3264,11 @@ public function serviceGetOptionsAttachmentsLayout() public function serviceGetLiveUpdate($aBrowseParams, $iProfileId, $iValue = 0, $iInit = 0) { $CNF = &$this->_oConfig->CNF; + + if (!is_array($aBrowseParams)){ + $aTmp = json_decode($aBrowseParams, true); + $aBrowseParams = $aTmp['params']; + } $sKey = $this->_oConfig->getLiveUpdateKey($aBrowseParams); @@ -3296,6 +3301,10 @@ public function serviceGetLiveUpdate($aBrowseParams, $iProfileId, $iValue = 0, $ if($iValueNew == $iValue) return false; + if(bx_is_api()){ + return $iValueNew; + } + if((int)$iInit != 0) return array('count' => $iValueNew); diff --git a/template/scripts/BxBaseCmtsServices.php b/template/scripts/BxBaseCmtsServices.php index fdb82bd97b..251a417e69 100644 --- a/template/scripts/BxBaseCmtsServices.php +++ b/template/scripts/BxBaseCmtsServices.php @@ -630,7 +630,6 @@ public function serviceGetCommentsApi($oCmts, $aParams) $aCmts = array_slice($aCmts, 0, $aBp['per_view']); $aParams['start_from'] = $aBp['start'] + $aBp['per_view']; } - $aCmtsRv = []; foreach ($aCmts as $aCmt) { $oCmt = $oCmts->getCommentStructure($aCmt['cmt_id'], $aBp, $aDp); @@ -650,7 +649,7 @@ public function serviceGetCommentsApi($oCmts, $aParams) 'start' => $aParams['start_from'], 'count' => count($aCmts), 'per_view' => $aBp['per_view'], - 'total_count' => $aBp['count'], + 'total_count' => $oCmts->getCommentsCount(), 'order' => $aParams['order_way'], 'view' => $aDp['type'], 'module' => $oCmts->getSystemName(), diff --git a/template/scripts/BxBaseFormView.php b/template/scripts/BxBaseFormView.php index aa3ae348f7..d1414be1d2 100644 --- a/template/scripts/BxBaseFormView.php +++ b/template/scripts/BxBaseFormView.php @@ -366,6 +366,20 @@ function getCodeAPI() $aInput['values'] = ''; $aInput['values_src'] = ''; } + + if (isset($aInput['type']) && 'location' == $aInput['type']){ + $aLocationIndexes = BxDolForm::$LOCATION_INDEXES; + $aVars = []; + $o = BxDolLocationField::getObjectInstance(getParam('sys_location_field_default')); + foreach ($aLocationIndexes as $sKey) + $aVars[$sKey] = $o->getLocationVal($aInput, $sKey, $this); + if ($aVars['country']) { + $aCountries = BxDolFormQuery::getDataItems('Country'); + $sLocationString = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']]; + $aVars['location_string'] = $sLocationString; + } + $aInput['value'] = $aVars; + } } return ['inputs' => $this->aInputs, 'attrs' => $this->aFormAttrs, 'params' => $this->aParams]; diff --git a/template/scripts/BxBaseGrid.php b/template/scripts/BxBaseGrid.php index ad954086ea..d2b4a46f86 100644 --- a/template/scripts/BxBaseGrid.php +++ b/template/scripts/BxBaseGrid.php @@ -51,6 +51,11 @@ public function __construct ($aOptions, $oTemplate) public function performActionDisplay() { + + if (bx_is_api()){ + return $this->getCodeAPI(); + } + require_once(BX_DIRECTORY_PATH_INC . "design.inc.php"); echoJson(array( @@ -112,6 +117,11 @@ public function performActionEnable($mixedChecked = null) $aAffectedIds[] = preg_match("/^[\d\w]+$/", $mixedId) ? $mixedId : (int)$mixedId; $sAction = $iChecked ? 'enable' : 'disable'; + + if (bx_is_api()){ + return $aAffectedIds; + } + echo echoJson(array($sAction => $aAffectedIds)); } @@ -305,16 +315,58 @@ public function getCodeAPI() else if($this->_aOptions['paginate_per_page']) $iPerPage = (int)$this->_aOptions['paginate_per_page']; + $aData = $this->_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage); + if(!empty($aData) && is_array($aData)) + $aData = $this->decodeDataAPI($aData); + + $aFilter = []; + $CNF = &$this->_oModule->_oConfig->CNF; + $aFilter[''] = _t($CNF['T']['filter_item_select_one_' . $this->_sFilter1Name]); + $aFilter = array_merge($aFilter, $this->_aFilter1Values); + foreach($aFilter as $iKey => &$aRow) { + $aRow = _t($aRow); + } + + return [ 'header' => $this->_getRowHeadAPI(), - 'data' => $this->_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage), + 'settings' => [ + 'object' => $this->_aOptions['object'], + 'field_id' => $this->_aOptions['field_id'], + 'start' => $iStart, + 'per_page' => $iPerPage, + 'filter1' => $aFilter + ], + 'data' => $aData, 'actions' => [ 'independent' => $this->_getActionsAPI('independent'), 'bulk' => $this->_getActionsAPI('bulk'), + 'single' => $this->_getActionsAPI('single'), ] ]; } + public function decodeDataAPI($aData) + { + $aDataRv = []; + foreach($aData as $iKey => $aRow) { + $aDataRv[$iKey] = []; + foreach($this->_aOptions['fields'] as $sKey => $aField){ + $aDataRv[$iKey]['id'] = $aData[$iKey]['id']; + $sMethod = '_getCellDefault'; + $sCustomMethod = '_getCell' . $this->_genMethodName($sKey); + + if (method_exists($this, $sCustomMethod)){ + $sMethod = $sCustomMethod; + $s = $this->$sMethod($aData[$iKey][$sKey], $sKey, $aField, $aRow); + $aDataRv[$iKey][$sKey] = $s; + } + + } + } + return $aDataRv; + } + /** * Reset query params, like filter and page number */ @@ -346,7 +398,9 @@ protected function _getRowHeadAPI() $aHeader = []; foreach($this->_aOptions['fields'] as $sKey => $aField) $aHeader[] = [ - 'title' => bx_process_output($aField['title']) + 'name' => bx_process_output($aField['name']), + 'title' => bx_process_output($aField['title']), + 'width' => $aField['width'] ]; return $aHeader; @@ -533,6 +587,10 @@ protected function _getCellDesign($sKey, $aField, $aRow) protected function _getCellCheckbox ($mixedValue, $sKey, $aField, $aRow) { + if (bx_is_api()){ + return ['type' => 'checkbox', 'data' => $aRow[$this->_aOptions['field_id']]]; + } + $sAttr = $this->_convertAttrs( $aField, 'attr_cell', 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes @@ -590,12 +648,20 @@ protected function _getCellActions ($mixedValue, $sKey, $aField, $aRow) $mixedDisabledBehavior = $this->_getActionsDisabledBehavior($aRow); $sActions = $this->_getActions('single', $aRow[$this->_aOptions['field_id']], false, null === $mixedDisabledBehavior ? $this->_isRowDisabled($aRow) : $mixedDisabledBehavior, null !== $mixedDisabledBehavior, $aRow); + + if (bx_is_api()){ + $b = $sActions; + return ['type' => 'actions', 'data' => $b]; + } return '
' . $sActions . '
'; } protected function _getCellDefault ($mixedValue, $sKey, $aField, $aRow) { + if(bx_is_api()){ + return ['type' => 'text', 'value'=> $mixedValue]; + } $sAttr = $this->_convertAttrs( $aField, 'attr_cell', 'bx-def-padding-sec-bottom bx-def-padding-sec-top', // add default classes @@ -630,10 +696,14 @@ protected function _getActions ($sType, $sActionData = false, $isSmall = false, if ($isPermanentState && !isset($a['attr']['bx_grid_permanent_state'])) $a['attr']['bx_grid_permanent_state'] = 1; - + if (bx_is_api()){ + $aRet[] = $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow); + } + else{ $sRet .= $this->$sFunc($sType, $sKey, $a, $isSmall, $isDisabled, $aRow); + } } - return $sRet; + return bx_is_api() ? $aRet : $sRet; } protected function _getActionsAPI ($sType) diff --git a/template/scripts/BxBaseMetatags.php b/template/scripts/BxBaseMetatags.php index 19aadc78a2..a941f68ec6 100644 --- a/template/scripts/BxBaseMetatags.php +++ b/template/scripts/BxBaseMetatags.php @@ -114,6 +114,10 @@ public function getLocationsMap($iId, $aParams = array()) if(!$aLocation) return ''; + if (bx_is_api()){ + return [bx_api_get_block('map', ['location' => $aLocation, 'caption' => $this->locationsString($iId, false)])]; + } + $sLocationHtml = $this->locationsString($iId); if(!$sLocationHtml) return ''; diff --git a/template/scripts/BxBasePage.php b/template/scripts/BxBasePage.php index a8f1dd285c..4d99aa4100 100644 --- a/template/scripts/BxBasePage.php +++ b/template/scripts/BxBasePage.php @@ -441,7 +441,8 @@ public function getPageAPI ($aBlocks = []) if (BxDolCover::getInstance($this)->isCover() && isset($this->_aProfileInfo)) { $oModule = BxDolModule::getInstance($this->getModule()); - + $aContentInfo = bx_srv($this->getModule(), 'get_info', [$this->_aProfileInfo['content_id'], false]); + $a['cover_block'] = [ 'profile' => BxDolProfile::getData($this->_aProfileInfo['id'], [ 'get_avatar' => 'getAvatarBig', @@ -450,7 +451,7 @@ public function getPageAPI ($aBlocks = []) 'actions_menu' => '', 'meta_menu' => '', 'cover' => $oModule->serviceGetCover($this->_aProfileInfo['content_id']), - 'allow_edit' => $oModule->checkAllowedChangeCover($this->_aProfileInfo['id']) === CHECK_ACTION_RESULT_ALLOWED + 'allow_edit' => $oModule->checkAllowedChangeCover($aContentInfo) === CHECK_ACTION_RESULT_ALLOWED ]; $CNF = $oModule->_oConfig->CNF; @@ -515,6 +516,13 @@ public function getPageAPI ($aBlocks = []) if($oPayments->isActive()) $a['user']['cart'] = $oPayments->getCartItemsCount(); } + + $aExtras = [ + 'page' => $this, + 'blocks' => $aBlocks, + 'data' => &$a, + ]; + bx_alert('system', 'get_page_api', 0, 0, $aExtras); return $a; } diff --git a/template/scripts/BxBaseServices.php b/template/scripts/BxBaseServices.php index cf52027f7c..d40ecea33e 100644 --- a/template/scripts/BxBaseServices.php +++ b/template/scripts/BxBaseServices.php @@ -54,6 +54,7 @@ public function serviceGetSafeServices() 'AccountSettingsPassword' => 'BxBaseServiceAccount', 'ForgotPassword' => 'BxBaseServiceAccount', 'SwitchProfile' => 'BxBaseServiceAccount', + 'AccountProfileSwitcher' => 'BxBaseServiceAccount', 'EmailConfirmation' => 'BxBaseServiceAccount', 'CategoriesList' => 'BxBaseServiceCategory', @@ -96,6 +97,10 @@ public function serviceGetSafeServices() 'GetDataApi' => 'BxBaseCmtsServices', 'GetDataApi' => 'BxBaseUploaderServices', + + 'GetStatBlock' => 'BxBaseDashboardServices', + + 'PerfomActionApi' => 'BxBaseServiceGrid', ); }