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

Ticket #4993 - Group based: Add members counter the same as comments, votes, etc.

AntonLV 1 год назад
Родитель
Сommit
b6a14fa0af

+ 64 - 14
inc/classes/BxDolConnection.php

@@ -63,6 +63,18 @@ define('BX_CONNECTIONS_CONTENT_TYPE_INITIATORS', 'initiators');
  */
 define('BX_CONNECTIONS_CONTENT_TYPE_COMMON', 'common');
 
+/**
+ * Connections trigger type: initiator. 
+ * It updates 'Initiator' data with a counter's value of connected 'Content'.
+ */
+define('BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR', 'initiator');
+
+/**
+ * Connections trigger type: content. 
+ * It updates 'Content' data with a counter's value of connected 'Initiators'.
+ */
+define('BX_CONNECTIONS_TRIGGER_TYPE_CONTENT', 'content');
+
 /**
  * Connection is usefull when you need to organize some sorts of connections between different content,
  * for example: friends, contacts, favorites, block lists, subscriptions, etc.
@@ -121,6 +133,7 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
     protected $_aObject;
     protected $_oQuery;
     protected $_sType;
+    protected $_aTriggerTypes;
 
     /**
      * Constructor
@@ -136,6 +149,8 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
 
         $this->_sType = $aObject['type'];
 
+        $this->_aTriggerTypes = [BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR, BX_CONNECTIONS_TRIGGER_TYPE_CONTENT];
+
         $this->_oQuery = new BxDolConnectionQuery($aObject);
     }
 
@@ -371,11 +386,8 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
     {
         $this->checkAllowedConnect($iInitiator, $iContent, true, $iMutual, false);
 
-        $bOneWay = $this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY;
-        $bMutual = $this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $iMutual;
-
-        if($bOneWay || $bMutual)
-            $this->_triggerValue($iInitiator, $iContent, 1);
+        if($this->_isTriggerable($iMutual))
+            $this->_updateTriggerValue($iInitiator, $iContent, 1);
 
         /**
          * Call socket.
@@ -400,7 +412,7 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
             $oSockets->sendEvent('sys_connections', $iContent , 'changed', json_encode($aMessageContent));
         }
 
-        if($bOneWay || $bMutual) {
+        if($this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($bMutual = ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $iMutual))) {
             $oProfileQuery = BxDolProfileQuery::getInstance();
 
             /**
@@ -479,7 +491,7 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
 
     public function onRemoved($iInitiator, $iContent, $iMutual)
     {
-        $this->_triggerValue($iInitiator, $iContent, -1);
+        $this->_updateTriggerValue($iInitiator, $iContent, -1);
 
         /**
          * Call socket.
@@ -558,6 +570,9 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
      */
     public function getConnectedContentCount ($iInitiator, $isMutual = false, $iFromDate = 0)
     {
+        if($this->_isTriggerable($isMutual) && ($iValue = $this->_getTriggerValueByContentType(BX_CONNECTIONS_CONTENT_TYPE_CONTENT, $iInitiator)) !== false)
+            return $iValue;
+
         return $this->_oQuery->getConnectedContentCount($iInitiator, $isMutual, $iFromDate);
     }
 
@@ -581,6 +596,9 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
      */
     public function getConnectedInitiatorsCount ($iContent, $isMutual = false)
     {
+        if($this->_isTriggerable($isMutual) && ($iValue = $this->_getTriggerValueByContentType(BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, $iContent)) !== false)
+            return $iValue;
+
         return $this->_oQuery->getConnectedInitiatorsCount($iContent, $isMutual);
     }
 
@@ -1149,24 +1167,56 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
         return $oContent->checkAllowedProfileView();
     }
 
-    protected function _triggerValue($iInitiator, $iContent, $iValue)
+    protected function _isTriggerable($mixedMutual)
+    {
+        return $this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $mixedMutual);
+    }
+
+    protected function _updateTriggerValue($iInitiator, $iContent, $iValue)
+    {
+        foreach($this->_aTriggerTypes as $sType) {
+            if(empty($this->_aObject['tt_' . $sType]) || empty($this->_aObject['tf_id_' . $sType]) || empty($this->_aObject['tf_count_' . $sType]))
+                continue;
+
+            $iObjectId = $this->_getTriggerObject($sType, $iInitiator, $iContent);
+            if(!$iObjectId)
+                continue;
+
+            $this->_oQuery->updateTriggerValue($sType, $iObjectId, $iValue);
+        }
+    }
+
+    protected function _getTriggerValueByContentType($sContentType, $iParticipantId)
     {
-        if(!$this->_aObject['trigger_table'])
+        $aCt2Tt = [
+            BX_CONNECTIONS_CONTENT_TYPE_CONTENT => BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR,
+            BX_CONNECTIONS_CONTENT_TYPE_INITIATORS => BX_CONNECTIONS_TRIGGER_TYPE_CONTENT
+        ];
+
+        if(!isset($aCt2Tt[$sContentType]))
             return false;
 
-        $iObjectId = $this->_getTriggerObject($iInitiator, $iContent);
-        if(!$iObjectId)
+        $sTriggerType = $aCt2Tt[$sContentType];
+        if(empty($this->_aObject['tt_' . $sTriggerType]) || empty($this->_aObject['tf_id_' . $sTriggerType]) || empty($this->_aObject['tf_count_' . $sTriggerType]))
             return false;
 
-        return $this->_oQuery->updateConnectionTriggerTableValue($iObjectId, $iValue);
+        $iObjectId = 0;
+        if((int)$this->_aObject['profile_' . $sTriggerType]) {
+            if(($oParticipant = BxDolProfile::getInstance($iParticipantId)) !== false)
+                $iObjectId = $oParticipant->getContentId();
+        }
+        else
+            $iObjectId = $iParticipantId;
+
+        return $iObjectId ? $this->_oQuery->getTriggerValue($sTriggerType, $iObjectId) : false;
     }
 
     /**
      * Should be overwritten in Connection class which uses triggerable fields.
      */
-    protected function _getTriggerObject($iInitiator, $iContent)
+    protected function _getTriggerObject($sType, $iInitiator, $iContent)
     {
-        return 0;
+        return false;
     }
 }
 

+ 14 - 16
inc/classes/BxDolConnectionQuery.php

@@ -16,10 +16,6 @@ class BxDolConnectionQuery extends BxDolDb
     protected $_aObject;
     protected $_sTable;
     protected $_sType;
-    
-    protected $_sTriggerTable;
-    protected $_sTriggerFieldId;
-    protected $_sTriggerFieldCount;
 
     public function __construct($aObject)
     {
@@ -27,10 +23,6 @@ class BxDolConnectionQuery extends BxDolDb
         $this->_aObject = $aObject;
         $this->_sTable = $aObject['table'];
         $this->_sType = $aObject['type'];
-
-        $this->_sTriggerTable = $aObject['trigger_table'];
-        $this->_sTriggerFieldId = $aObject['trigger_field_id'];
-        $this->_sTriggerFieldCount = $aObject['trigger_field_count'];
     }
 
     static public function getConnectionObject ($sObject)
@@ -410,14 +402,6 @@ class BxDolConnectionQuery extends BxDolDb
         return $bResult;
     }
 
-    public function updateConnectionTriggerTableValue($iObjectId, $iValue)
-    {
-        return (int)$this->query("UPDATE `{$this->_sTriggerTable}` SET `{$this->_sTriggerFieldCount}` = `{$this->_sTriggerFieldCount}` + :count WHERE `{$this->_sTriggerFieldId}` = :id", [
-            'id' => $iObjectId,
-            'count' => (int)$iValue
-        ]) > 0;
-    }
-
     public function removeConnection ($iInitiator, $iContent)
     {
         if (!($aConnection = $this->getConnection($iInitiator, $iContent))) // connection doesn't exist
@@ -457,6 +441,20 @@ class BxDolConnectionQuery extends BxDolDb
         return $this->query($sQuery);
     }
 
+    public function getTriggerValue($sType, $iObjectId)
+    {
+        return (int)$this->getOne("SELECT `{$this->_aObject['tf_count_' . $sType]}` FROM `{$this->_aObject['tt_' . $sType]}` WHERE `{$this->_aObject['tf_id_' . $sType]}` = :id", [
+            'id' => $iObjectId
+        ]);
+    }
+
+    public function updateTriggerValue($sType, $iObjectId, $iValue)
+    {
+        return (int)$this->query("UPDATE `{$this->_aObject['tt_' . $sType]}` SET `{$this->_aObject['tf_count_' . $sType]}` = `{$this->_aObject['tf_count_' . $sType]}` + :count WHERE `{$this->_aObject['tf_id_' . $sType]}` = :id", [
+            'id' => $iObjectId,
+            'count' => (int)$iValue
+        ]) > 0;
+    }
 }
 
 /** @} */

+ 8 - 6
modules/base/groups/classes/BxBaseModGroupsConnectionFans.php

@@ -222,15 +222,17 @@ class BxBaseModGroupsConnectionFans extends BxTemplConnection
         return parent::_checkAllowedConnectContent($oContent);
     }
 
-    protected function _getTriggerObject($iInitiator, $iContent)
+    protected function _getTriggerObject($sType, $iInitiator, $iContent)
     {
-        if(($oInitiator = BxDolProfile::getInstance($iInitiator)) !== false && $oInitiator->getModule() == $this->_sModule)
-            return $oInitiator->getContentId();
+        if(($oInitiator = BxDolProfile::getInstance($iInitiator)) !== false && ($sModule = $oInitiator->getModule()))
+            if(($sModule == $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_CONTENT) || ($sModule != $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR))
+                return $oInitiator->getContentId();
 
-        if(($oContent = BxDolProfile::getInstance($iContent)) !== false && $oContent->getModule() == $this->_sModule)
-            return $oContent->getContentId();
+        if(($oContent = BxDolProfile::getInstance($iContent)) !== false && ($sModule = $oContent->getModule())  == $this->_sModule)
+            if(($sModule == $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_CONTENT) || ($sModule != $this->_sModule && $sType == BX_CONNECTIONS_TRIGGER_TYPE_INITIATOR))
+                return $oContent->getContentId();
 
-        return parent::_getTriggerObject($iInitiator, $iContent);
+        return false;
     }
 }
 

+ 1 - 1
modules/boonex/courses/install/sql/enable.sql

@@ -547,7 +547,7 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 ('bx_courses', '_bx_courses', @iSearchOrder + 1, 'BxCoursesSearchResult', 'modules/boonex/courses/classes/BxCoursesSearchResult.php');
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `trigger_table`, `trigger_field_id`, `trigger_field_count`, `override_class_name`, `override_class_file`) VALUES
+INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `tt_content`, `tf_id_content`, `tf_count_content`, `override_class_name`, `override_class_file`) VALUES
 ('bx_courses_fans', 'bx_courses_fans', 1, 1, 'mutual', 'bx_courses_data', 'id', 'members', 'BxCoursesConnectionFans', 'modules/boonex/courses/classes/BxCoursesConnectionFans.php');
 
 -- STATS

+ 1 - 1
modules/boonex/events/install/sql/enable.sql

@@ -603,7 +603,7 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 ('bx_events', '_bx_events', @iSearchOrder + 1, 'BxEventsSearchResult', 'modules/boonex/events/classes/BxEventsSearchResult.php');
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `trigger_table`, `trigger_field_id`, `trigger_field_count`, `override_class_name`, `override_class_file`) VALUES
+INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `tt_content`, `tf_id_content`, `tf_count_content`, `override_class_name`, `override_class_file`) VALUES
 ('bx_events_fans', 'bx_events_fans', 1, 1, 'mutual', 'bx_events_data', 'id', 'members', 'BxEventsConnectionFans', 'modules/boonex/events/classes/BxEventsConnectionFans.php');
 
 -- STATS

+ 1 - 1
modules/boonex/groups/install/sql/enable.sql

@@ -540,7 +540,7 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 ('bx_groups', '_bx_groups', @iSearchOrder + 1, 'BxGroupsSearchResult', 'modules/boonex/groups/classes/BxGroupsSearchResult.php');
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `trigger_table`, `trigger_field_id`, `trigger_field_count`, `override_class_name`, `override_class_file`) VALUES
+INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `tt_content`, `tf_id_content`, `tf_count_content`, `override_class_name`, `override_class_file`) VALUES
 ('bx_groups_fans', 'bx_groups_fans', 1, 1, 'mutual', 'bx_groups_data', 'id', 'members', 'BxGroupsConnectionFans', 'modules/boonex/groups/classes/BxGroupsConnectionFans.php');
 
 -- RECOMMENDATIONS

+ 1 - 1
modules/boonex/jobs/install/sql/enable.sql

@@ -552,7 +552,7 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 ('bx_jobs', '_bx_jobs', @iSearchOrder + 1, 'BxJobsSearchResult', 'modules/boonex/jobs/classes/BxJobsSearchResult.php');
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `trigger_table`, `trigger_field_id`, `trigger_field_count`, `override_class_name`, `override_class_file`) VALUES
+INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `tt_content`, `tf_id_content`, `tf_count_content`, `override_class_name`, `override_class_file`) VALUES
 ('bx_jobs_fans', 'bx_jobs_fans', 1, 1, 'mutual', 'bx_jobs_data', 'id', 'members', 'BxJobsConnectionFans', 'modules/boonex/jobs/classes/BxJobsConnectionFans.php');
 
 -- RECOMMENDATIONS

+ 1 - 1
modules/boonex/organizations/install/sql/enable.sql

@@ -599,7 +599,7 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `trigger_table`, `trigger_field_id`, `trigger_field_count`, `override_class_name`, `override_class_file`) VALUES
+INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `tt_content`, `tf_id_content`, `tf_count_content`, `override_class_name`, `override_class_file`) VALUES
 ('bx_organizations_fans', 'bx_organizations_fans', 1, 1, 'mutual', 'bx_organizations_data', 'id', 'members', 'BxOrgsConnectionFans', 'modules/boonex/organizations/classes/BxOrgsConnectionFans.php');
 
 

+ 1 - 1
modules/boonex/spaces/install/sql/enable.sql

@@ -523,7 +523,7 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 ('bx_spaces', '_bx_spaces', @iSearchOrder + 1, 'BxSpacesSearchResult', 'modules/boonex/spaces/classes/BxSpacesSearchResult.php');
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `trigger_table`, `trigger_field_id`, `trigger_field_count`, `override_class_name`, `override_class_file`) VALUES
+INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `tt_content`, `tf_id_content`, `tf_count_content`, `override_class_name`, `override_class_file`) VALUES
 ('bx_spaces_fans', 'bx_spaces_fans', 1, 1, 'mutual', 'bx_spaces_data', 'id', 'members', 'BxSpacesConnectionFans', 'modules/boonex/spaces/classes/BxSpacesConnectionFans.php');
 
 -- STATS