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

Ticket #4148 - Build-in API support: Connections.

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

+ 10 - 2
modules/base/profile/classes/BxBaseModProfileMenuSnippetMeta.php

@@ -173,9 +173,17 @@ class BxBaseModProfileMenuSnippetMeta extends BxBaseModGeneralMenuSnippetMeta
             return false;
 
         if($this->_bIsApi)
-            return $this->_getMenuItemAPI($aItem, ['display' => 'button', 'content' => 'text'], [
+            return $this->_getMenuItemAPI($aItem, ['display' => 'element'], [
                 'title' => $sTitle,
-                'primary' => !empty($aItem['primary'])
+                'data' => [
+                    'type' => 'connections',
+                    'o' => $sConnection,
+                    'a' => $sAction,
+                    'iid' => bx_get_logged_profile_id(),
+                    'cid' => $iContentProfile,
+                    'title' => $sTitle,
+                    'primary' => !empty($aItem['primary']),
+                ]
             ]);
 
         return [

+ 101 - 0
template/scripts/BxBaseServiceConnections.php

@@ -17,6 +17,107 @@ class BxBaseServiceConnections extends BxDol
         parent::__construct();
     }
 
+    /**
+     * @page service Service Calls
+     * @section bx_system_general System Services 
+     * @subsection bx_system_general-connections Connections
+     * @subsubsection bx_system_general-perform perform
+     * 
+     * @code bx_srv('system', 'perform', [[...]], 'TemplVoteServices'); @endcode
+     * @code {{~system:perform:TemplVoteServices[[...]]~}} @endcode
+     * 
+     * Performs Perform an action (add, remove, etc) with connection object.
+     * @param $aParams an array with necessary parameters 
+     * 
+     * @see BxBaseServiceConnections::servicePerform
+     */
+    /** 
+     * @ref bx_system_general-perform "perform"
+     * @api @ref bx_system_general-perform "perform"
+     */
+    public function servicePerform($aParams)
+    {
+        if(is_string($aParams))
+            $aParams = json_decode($aParams, true);
+
+        if(!$aParams['o'] || !$aParams['a'] || !$aParams['iid'] || !$aParams['cid'])
+            return ['code' => 1];
+
+        $oConnection = BxDolConnection::getObjectInstance($aParams['o']);
+        if(!$oConnection)
+            return ['code' => 2];
+
+        $sMethod = 'action' . bx_gen_method_name($aParams['a']);
+        if(!method_exists($oConnection, $sMethod))
+            return ['code' => 2];
+
+        $aResult = $oConnection->$sMethod($aParams['cid'], $aParams['iid']);
+        if($aResult['err'] !== false)
+            return ['code' => 3, 'message' => $aResult['msg']];
+
+        $aFlip = ['add' => 'remove', 'remove' => 'add'];
+
+        return [
+            'a' => $aFlip[$aParams['a']],
+            'title' => $this->serviceGetActionTitle($aParams['o'], $aParams['a'], $aParams['iid'], $aParams['cid'], true),
+        ];
+    }
+
+    public function serviceGetActionTitle($sObject, $sAction, $iInitiatorId, $iContentId, $bFlip = false)
+    {
+        if(empty($sAction))
+            return '';
+
+        $oConnection = BxDolConnection::getObjectInstance($sObject);
+        if(!$oConnection)
+            return '';
+
+        $aResult = [];
+        switch($sObject) {
+            case 'sys_profiles_friends':
+                if($oConnection->isConnectedNotMutual($iInitiatorId, $iContentId))
+                    $aResult = [
+                        'add' => '',
+                        'remove' => '_sys_menu_item_title_sm_unfriend_cancel',
+                    ];
+                else if($oConnection->isConnectedNotMutual($iContentId, $iInitiatorId))
+                    $aResult = [
+                        'add' => '_sys_menu_item_title_sm_befriend_confirm',
+                        'remove' => '_sys_menu_item_title_sm_unfriend_reject',
+                    ];
+                else if($oConnection->isConnected($iInitiatorId, $iContentId, true))
+                    $aResult = [
+                        'add' => '',
+                        'remove' => '_sys_menu_item_title_sm_unfriend',
+                    ];
+                else
+                    $aResult = [
+                        'add' => '_sys_menu_item_title_sm_befriend',
+                        'remove' => '',
+                    ];
+                break;
+
+            case 'sys_profiles_subscriptions':
+                if($oConnection->isConnected($iInitiatorId, $iContentId))
+                    $aResult = [
+                        'add' => '',
+                        'remove' => '_sys_menu_item_title_sm_unsubscribe',
+                    ];
+                else
+                    $aResult = [
+                        'add' => '_sys_menu_item_title_sm_subscribe',
+                        'remove' => '',
+                    ];
+                break;
+        }
+
+        $aFlip = ['add' => 'remove', 'remove' => 'add'];
+        if($bFlip)
+            $sAction = $aFlip[$sAction];
+
+        return !empty($aResult[$sAction]) ? _t($aResult[$sAction]) : '';
+    }       
+
     /**
      * get grid with friends connections
      */

+ 2 - 3
template/scripts/BxBaseServices.php

@@ -57,7 +57,6 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
             'MemberAuthCode' => 'BxBaseServiceLogin',
             'LoginForm' => 'BxBaseServiceLogin',
             'LoginFormOnly' => 'BxBaseServiceLogin',
-            'Logout' => 'BxBaseServiceLogin',
         
             'KeywordsCloud' => 'BxBaseServiceMetatags',
 
@@ -73,8 +72,8 @@ class BxBaseServices extends BxDol implements iBxDolProfileService
 
             'Do' => 'BxBaseVoteServices',
             'GetPerformedBy' => 'BxBaseVoteServices',
-            
-            'GetDataApi' => 'BxBaseCmtsServices'
+
+            'Perform' => 'BxBaseServiceConnections'
         );
     }