Pārlūkot izejas kodu

#4558 - Build-in API support (grids, locations)

roman.lesnikov@gmail.com 2 gadi atpakaļ
vecāks
revīzija
528b1c1501

+ 14 - 2
modules/base/general/classes/BxBaseModGeneralGridAdministration.php

@@ -68,8 +68,11 @@ class BxBaseModGeneralGridAdministration extends BxTemplGrid
             $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 @@ class BxBaseModGeneralGridAdministration extends BxTemplGrid
                 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 @@ class BxBaseModGeneralGridAdministration extends BxTemplGrid
     {
         $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]]))

+ 2 - 1
modules/base/profile/classes/BxBaseModProfileTemplate.php

@@ -622,7 +622,8 @@ class BxBaseModProfileTemplate extends BxBaseModGeneralTemplate
     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);
     }
 
     /**

+ 21 - 0
modules/base/text/classes/BxBaseModTextGridAdministration.php

@@ -137,16 +137,30 @@ class BxBaseModTextGridAdministration extends BxBaseModGeneralGridAdministration
         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 @@ class BxBaseModTextGridAdministration extends BxBaseModGeneralGridAdministration
 
         $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');"
     	));

+ 9 - 0
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -3264,6 +3264,11 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
     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 @@ class BxTimelineModule extends BxBaseModNotificationsModule implements iBxDolCon
         if($iValueNew == $iValue)
             return false;
 
+        if(bx_is_api()){
+            return $iValueNew;
+        }
+        
         if((int)$iInit != 0)
             return array('count' => $iValueNew);
 

+ 1 - 2
template/scripts/BxBaseCmtsServices.php

@@ -630,7 +630,6 @@ class BxBaseCmtsServices extends BxDol
             $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 @@ class BxBaseCmtsServices extends BxDol
             '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(), 

+ 14 - 0
template/scripts/BxBaseFormView.php

@@ -366,6 +366,20 @@ class BxBaseFormView extends BxDolForm
                 $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];

+ 74 - 4
template/scripts/BxBaseGrid.php

@@ -51,6 +51,11 @@ class BxBaseGrid extends BxDolGrid
 
     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 @@ class BxBaseGrid extends BxDolGrid
                 $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 @@ class BxBaseGrid extends BxDolGrid
         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 @@ class BxBaseGrid extends BxDolGrid
         $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 @@ class BxBaseGrid extends BxDolGrid
 
     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 @@ class BxBaseGrid extends BxDolGrid
 
         $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
@@ -630,10 +696,14 @@ class BxBaseGrid extends BxDolGrid
 
             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)

+ 4 - 0
template/scripts/BxBaseMetatags.php

@@ -114,6 +114,10 @@ class BxBaseMetatags extends BxDolMetatags
         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 '';

+ 10 - 2
template/scripts/BxBasePage.php

@@ -441,7 +441,8 @@ class BxBasePage extends BxDolPage
         
         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 @@ class BxBasePage extends BxDolPage
                 '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 @@ class BxBasePage extends BxDolPage
             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;
     }

+ 5 - 0
template/scripts/BxBaseServices.php

@@ -54,6 +54,7 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             'AccountSettingsPassword' => 'BxBaseServiceAccount',
             'ForgotPassword' => 'BxBaseServiceAccount',
             'SwitchProfile' => 'BxBaseServiceAccount',
+            'AccountProfileSwitcher' => 'BxBaseServiceAccount',
             'EmailConfirmation' => 'BxBaseServiceAccount',
 
             'CategoriesList' => 'BxBaseServiceCategory',
@@ -96,6 +97,10 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             'GetDataApi' => 'BxBaseCmtsServices',
 
             'GetDataApi' => 'BxBaseUploaderServices',
+            
+            'GetStatBlock' => 'BxBaseDashboardServices',
+            
+            'PerfomActionApi' => 'BxBaseServiceGrid',
         );
     }