Просмотр исходного кода

Ticket #4148 - Build-in API support: Browse profile/context connections.

AntonLV 3 лет назад
Родитель
Сommit
b4eed3dc68

+ 13 - 8
inc/classes/BxDolSearch.php

@@ -501,20 +501,25 @@ class BxDolSearchResult implements iBxDolReplaceable
 
     function processingAPI () 
     {
+        $sModule = 'system';
         $sUnitType = 'content';
-        if(method_exists($this->oModule, 'serviceActAsProfile') && $this->oModule->serviceActAsProfile())
-             $sUnitType = 'profile';
-        if(method_exists($this->oModule, 'serviceIsGroupProfile') && $this->oModule->serviceIsGroupProfile())
-             $sUnitType = 'context';
+        if(!empty($this->oModule)) {
+            $sModule = $this->oModule->getName();
+
+            if(method_exists($this->oModule, 'serviceActAsProfile') && $this->oModule->serviceActAsProfile())
+                 $sUnitType = 'profile';
+            if(method_exists($this->oModule, 'serviceIsGroupProfile') && $this->oModule->serviceIsGroupProfile())
+                 $sUnitType = 'context';
+        }
 
         $sUnit =  'list';
-        if ($this->sUnitViewDefault == 'showcase' || $this->sUnitViewDefault == 'gallery')
+        if(in_array($this->sUnitViewDefault, ['showcase', 'gallery']))
             $sUnit = 'card';
-        
+
         return [
-            'module' => $this->oModule->getName(),
+            'module' => $sModule,
             'unit' => 'general-' . $sUnitType . '-' . $sUnit,
-            'request_url' => '/api.php?r=' . $this->oModule->getName() . '/browse/&params[]=',
+            'request_url' => '/api.php?r=' . $sModule . '/browse/&params[]=',
             'data' => $this->decodeData($this->getSearchData()),
             'params' => [
                 'per_page' => $this->aCurrent['paginate']['perPage'],

+ 13 - 0
modules/base/groups/classes/BxBaseModGroupsModule.php

@@ -506,6 +506,19 @@ class BxBaseModGroupsModule extends BxBaseModProfileModule
     {
         $CNF = &$this->_oConfig->CNF;
 
+        if(bx_is_api()) {
+            $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
+            if(!$iProfileId)
+                return [];
+
+            return bx_srv('system', 'browse_connections', [
+                'profile_id' => $iProfileId,
+                'connection' => $CNF['OBJECT_CONNECTIONS'],
+                'type' => 'content',
+                'mutual' => 1
+            ], 'TemplServiceProfiles');
+        }
+
         $oGrid = BxDolGrid::getObjectInstance($CNF['OBJECT_GRID_CONNECTIONS']);
         if(!$oGrid)
             return false;

+ 0 - 14
modules/base/profile/classes/BxBaseModProfileModule.php

@@ -813,20 +813,6 @@ class BxBaseModProfileModule extends BxBaseModGeneralModule implements iBxDolCon
             ], $iDesignBox
         );
     }
-    
-    public function serviceBrowseConnectionsEverywhere ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iDesignBox = BX_DB_PADDING_DEF, $iProfileId2 = 0)
-    {
-        return $this->_serviceBrowse (
-            'connections', [
-                'object' => $sObjectConnections,
-                'type' => $sConnectionsType,
-                'mutual' => $iMutual,
-                'profile' => (int)$iProfileId,
-                'profile2' => (int)$iProfileId2,
-                'everywhere' => true
-            ], $iDesignBox
-        );
-    }
 
     public function serviceBrowseRelationsQuick ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iProfileId2 = 0)
     {

+ 0 - 6
modules/base/profile/classes/BxBaseModProfileSearchResult.php

@@ -124,12 +124,6 @@ class BxBaseModProfileSearchResult extends BxBaseModGeneralSearchResult
         $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $a['restriction']);
         $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $a['join']);
 
-        if($this->bConnectionsEverywhere) {
-            $this->aCurrent['ownFields'] = ['id', 'account_id', 'type', 'content_id'];
-            
-            unset($this->aCurrent['restriction']['perofileType'], $this->aCurrent['join']['profile']);
-        }
-
         return true;
     }
     

+ 150 - 0
template/scripts/BxBaseProfileSearchResult.php

@@ -0,0 +1,150 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+class BxBaseProfileSearchResult extends BxTemplSearchResult
+{
+    protected $_bIsApi;
+
+    protected $aUnitViews;
+    protected $sUnitViewDefault;
+
+    public function __construct($sMode = '', $aParams = [])
+    {
+        parent::__construct();
+
+        $this->_bIsApi = bx_is_api();
+
+        $this->aUnitViews = [
+            'unit' => 'unit_wo_cover.html', 
+            'gallery' => 'unit_with_cover.html'
+        ];
+        if(!empty($aParams['unit_views']) && is_array($aParams['unit_views']))
+            $this->aUnitViews = array_merge($this->aUnitViews, $aParams['unit_views']);
+
+        $this->sUnitViewDefault = 'gallery';
+        if(!empty($aParams['unit_view']))
+            $this->sUnitViewDefault = $aParams['unit_view'];
+
+        $this->sUnitTemplate = $this->aUnitViews[$this->sUnitViewDefault];
+
+        $this->addContainerClass (array(
+            'bx-base-pofile-units-wrapper', 
+            'bx-base-puw-' . str_replace('_' ,  '-', $this->sUnitViewDefault),
+            'bx-def-margin-sec-neg'
+        ));
+
+        $this->aCurrent = [
+            'name' => 'sys_search',
+            'module_name' => 'system',
+            'title' => '',
+            'table' => 'sys_profiles',
+            'ownFields' => ['id', 'account_id', 'type', 'content_id'],
+            'searchFields' => [],
+            'restriction' => [
+                'account_id' => ['value' => '', 'field' => 'account_id', 'operator' => '='],
+                'perofileStatus' => ['value' => 'active', 'field' => 'status', 'operator' => '='],
+                'perofileType' => ['value' => '', 'field' => 'type', 'operator' => '='],
+            ],
+            'join' => [
+                'account' => [
+                    'type' => 'INNER',
+                    'table' => 'sys_accounts',
+                    'mainField' => 'account_id',
+                    'onField' => 'id',
+                    'joinFields' => [],
+                ],
+            ],
+            'paginate' => ['start' => 0],
+            'sorting' => 'last',
+            'ident' => 'id'
+        ];
+
+        switch ($sMode) {
+            case 'connections':
+                if (!$this->_setConnectionsConditions($aParams)) 
+                    break;
+
+                $oProfile = BxDolProfile::getInstance($aParams['profile']);
+                $oProfile2 = isset($aParams['profile2']) ? BxDolProfile::getInstance($aParams['profile2']) : null;
+
+                $bCommon = isset($aParams['type']) && $aParams['type'] == 'common';
+                if($bCommon && $oProfile && $oProfile2)
+                    $this->aCurrent['title'] = _t('_sys_page_title_browse_connections_mutual', $oProfile->getDisplayName(), $oProfile2->getDisplayName());
+                else if(!$bCommon && $oProfile)
+                    $this->aCurrent['title'] = _t('_sys_page_title_browse_connections', $oProfile->getDisplayName());
+                break;
+        }
+    }
+
+    function displaySearchUnit ($aData)
+    {
+        $oProfile = BxDolProfile::getInstance($aData['id']);
+        if(!$oProfile)
+            return '';
+
+        if($this->_bIsApi)
+            return $oProfile->getUnitAPI();
+
+        return $oProfile->getUnit(0, ['template' => substr($this->sUnitTemplate, 0, -5)]);
+    }
+
+    function getAlterOrder()
+    {
+        $sType = is_array($this->aCurrent['sorting']) ? $this->aCurrent['sorting']['type'] : $this->aCurrent['sorting'];
+
+        switch($sType) {
+            case 'none':
+                return [];
+
+            case 'active':
+                return ['order' => ' ORDER BY `sys_accounts`.`logged` DESC '];
+
+            case 'last_connected':
+                return ['order' => ' ORDER BY `' . $this->aCurrent['sorting']['table'] . '`.`added` DESC '];
+
+            case 'last':
+            default:
+                return ['order' => ' ORDER BY `sys_accounts`.`added` DESC '];
+        }
+    }
+
+    function decodeData ($a)
+    {
+        $aResult = [];
+
+        foreach ($a as $index => $aItem) 
+            if(($oProfile = BxDolProfile::getInstance($aItem['id'])) !== false)
+                $aResult[] = $oProfile->getUnitAPI();
+
+        return $aResult;
+    }
+
+    protected function _setConnectionsConditions ($aParams)
+    {
+        $oConnection = isset($aParams['object']) ? BxDolConnection::getObjectInstance($aParams['object']) : false;
+        if(!$oConnection || empty($aParams['profile']))
+            return false;
+
+        $sContentType = isset($aParams['type']) ? $aParams['type'] : BX_CONNECTIONS_CONTENT_TYPE_CONTENT;
+        $isMutual = isset($aParams['mutual']) ? $aParams['mutual'] : false;
+        $aConnection = $oConnection->getConnectionsAsCondition($sContentType, 'id', (int)$aParams['profile'], (int)$aParams['profile2'], $isMutual);
+
+        $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aConnection['restriction']);
+        $this->aCurrent['join'] = array_merge($this->aCurrent['join'], $aConnection['join']);
+
+        $this->aCurrent['sorting'] = [
+            'type' => 'last_connected',
+            'table' => current($aConnection['join'])['table']
+        ];
+
+        return true;
+    }
+}
+
+/** @} */

+ 26 - 2
template/scripts/BxBaseServiceConnections.php

@@ -135,6 +135,14 @@ class BxBaseServiceConnections extends BxDol
         if(!$iProfileId)
             return false;
 
+        if(bx_is_api())
+            return bx_srv('system', 'browse_connections', [
+                'profile_id' => $iProfileId,
+                'connection' => 'sys_profiles_friends',
+                'type' => 'content',
+                'mutual' => 1,
+            ], 'TemplServiceProfiles');
+
         $oGrid = BxDolGrid::getObjectInstance('sys_grid_connections');
         if(!$oGrid)
             return false;
@@ -259,10 +267,18 @@ class BxBaseServiceConnections extends BxDol
         if(!$iProfileId && bx_get('profile_id') !== false)
             $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
 
-        $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
+        $oProfile = BxDolProfile::getInstance($iProfileId);
+        $aProfile = $oProfile->getInfo();
         if(empty($aProfile) || !is_array($aProfile))
             return false;
 
+        if(bx_is_api())
+            return bx_srv('system', 'browse_connections', [
+                'profile_id' => $iProfileId,
+                'connection' => 'sys_profiles_subscriptions',
+                'type' => 'content'
+            ], 'TemplServiceProfiles');
+
         $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
         if(getParam($CNF['PARAM_PUBLIC_SBSN']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
             return false;
@@ -290,10 +306,18 @@ class BxBaseServiceConnections extends BxDol
         if(!$iProfileId && bx_get('profile_id') !== false)
             $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
 
-        $aProfile = BxDolProfile::getInstance($iProfileId)->getInfo();
+        $oProfile = BxDolProfile::getInstance($iProfileId);
+        $aProfile = $oProfile->getInfo();
         if(empty($aProfile) || !is_array($aProfile))
             return false;
 
+        if(bx_is_api())
+            return bx_srv('system', 'browse_connections', [
+                'profile_id' => $iProfileId,
+                'connection' => 'sys_profiles_subscriptions',
+                'type' => 'initiators'
+            ], 'TemplServiceProfiles');
+
         $CNF = &BxDolModule::getInstance($aProfile['type'])->_oConfig->CNF;
         if(getParam($CNF['PARAM_PUBLIC_SBSD']) != 'on' && $aProfile['id'] != bx_get_logged_profile_id())
             return false;

+ 25 - 0
template/scripts/BxBaseServiceProfiles.php

@@ -470,6 +470,31 @@ class BxBaseServiceProfiles extends BxDol
 
         return $s . '<div class="bx-clear"></div>';
     }
+    
+    public function serviceBrowseConnections ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iProfileId2 = 0, $iDesignBox = BX_DB_PADDING_DEF, $bDisplayEmptyMsg = false, $bAjaxPaginate = true)
+    {
+        $sClass = 'BxTemplProfileSearchResult';
+        bx_import($sClass);
+        $o = new $sClass('connections', [
+            'object' => $sObjectConnections,
+            'type' => $sConnectionsType,
+            'mutual' => $iMutual,
+            'profile' => (int)$iProfileId,
+            'profile2' => (int)$iProfileId2
+        ]);
+
+        $o->setDesignBoxTemplateId($iDesignBox);
+        $o->setDisplayEmptyMsg($bDisplayEmptyMsg);
+        $o->setAjaxPaginate($bAjaxPaginate);
+
+        if ($o->isError)
+            return '';
+
+        if ($s = $o->processing())
+            return bx_is_api() ? [bx_api_get_block('browse', $s)] : $s;
+        else
+            return '';
+    }
 
     public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
     {

+ 18 - 0
template/scripts_templ/BxTemplProfileSearchResult.php

@@ -0,0 +1,18 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+class BxTemplProfileSearchResult extends BxBaseProfileSearchResult
+{
+    public function __construct($sMode = '', $aParams = [])
+    {
+        parent::__construct($sMode, $aParams);
+    }
+}
+
+/** @} */