Skip to content

Commit

Permalink
#4558 - Build-in API support (grids, locations)
Browse files Browse the repository at this point in the history
  • Loading branch information
romanlesnikov committed Nov 22, 2023
1 parent 40b0565 commit 528b1c1
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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]]))
Expand Down
3 changes: 2 additions & 1 deletion modules/base/profile/classes/BxBaseModProfileTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions modules/base/text/classes/BxBaseModTextGridAdministration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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');"
));
Expand Down
9 changes: 9 additions & 0 deletions modules/boonex/timeline/classes/BxTimelineModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions template/scripts/BxBaseCmtsServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(),
Expand Down
14 changes: 14 additions & 0 deletions template/scripts/BxBaseFormView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
78 changes: 74 additions & 4 deletions template/scripts/BxBaseGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 '<td ' . $sAttr . '><div class="bx-grid-cell-single-actions-wrapper bx-def-margin-thd-neg">' . $sActions . '</div></td>';
}

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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions template/scripts/BxBaseMetatags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down
12 changes: 10 additions & 2 deletions template/scripts/BxBasePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions template/scripts/BxBaseServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function serviceGetSafeServices()
'AccountSettingsPassword' => 'BxBaseServiceAccount',
'ForgotPassword' => 'BxBaseServiceAccount',
'SwitchProfile' => 'BxBaseServiceAccount',
'AccountProfileSwitcher' => 'BxBaseServiceAccount',
'EmailConfirmation' => 'BxBaseServiceAccount',

'CategoriesList' => 'BxBaseServiceCategory',
Expand Down Expand Up @@ -96,6 +97,10 @@ public function serviceGetSafeServices()
'GetDataApi' => 'BxBaseCmtsServices',

'GetDataApi' => 'BxBaseUploaderServices',

'GetStatBlock' => 'BxBaseDashboardServices',

'PerfomActionApi' => 'BxBaseServiceGrid',
);
}

Expand Down

0 comments on commit 528b1c1

Please sign in to comment.