Kaynağa Gözat

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

AntonLV 1 yıl önce
ebeveyn
işleme
86be2efc5d

+ 71 - 33
inc/classes/BxDolConnection.php

@@ -343,15 +343,15 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
          * - $object_id - not used
          * - $sender_id - logged in profile id
          * - $extra_params - array of additional params with the following array keys:
-         *      - `initiator` - [int] by ref, profile id who created the connection, can be overridden in hook processing
-         *      - `content` - [int] by ref, profile id with whom the connection was created, can be overridden in hook processing
+         *      - `initiator` - [int] by ref, profile id who is creating the connection, can be overridden in hook processing
+         *      - `content` - [int] by ref, profile id with whom the connection is creating, can be overridden in hook processing
          *      - `mutual` - [int] by ref, if the relation is mutual or not, can be overridden in hook processing
          *      - `object` - [object] an instance of relation, @see BxDolConnection
          * @hook @ref hook-bx_dol_connection-connection_before_add
          */
-        bx_alert($this->_sObject, 'connection_before_add', 0, bx_get_logged_profile_id(), $aAlertExtras);
+        bx_alert($this->_sObject, 'connection_before_add', 0, false, $aAlertExtras);
         
-        if (!$this->_oQuery->addConnection((int)$iInitiator, (int)$iContent, $iMutual))
+        if (!$this->_oQuery->addConnection($iInitiator, $iContent, $iMutual))
             return false;
 
         /**
@@ -360,7 +360,7 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
          * It's equivalent to @ref hook-bx_dol_connection-connection_before_add
          * @hook @ref hook-bx_dol_connection-connection_added
          */
-        bx_alert($this->_sObject, 'connection_added', 0, bx_get_logged_profile_id(), $aAlertExtras);
+        bx_alert($this->_sObject, 'connection_added', 0, false, $aAlertExtras);
 
         $this->onAdded($iInitiator, $iContent, $iMutual);
 
@@ -369,14 +369,18 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
 
     public function onAdded($iInitiator, $iContent, $iMutual)
     {
-        $this->checkAllowedConnect ($iInitiator, $iContent, true, $iMutual, false);
+        $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);
 
-        $bMutual = false;
-        
         /**
          * Call socket.
          */
-        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()){
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
             $aMessageInitiator = $aMessageContent = [
                 'object' => $this->_sObject, 
                 'action' => 'added',
@@ -395,8 +399,8 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
             $oSockets->sendEvent('sys_connections', $iInitiator , 'changed', json_encode($aMessageInitiator));
             $oSockets->sendEvent('sys_connections', $iContent , 'changed', json_encode($aMessageContent));
         }
-        
-        if($this->_aObject['type'] == BX_CONNECTIONS_TYPE_ONE_WAY || ($bMutual = ($this->_aObject['type'] == BX_CONNECTIONS_TYPE_MUTUAL && $iMutual))) {
+
+        if($bOneWay || $bMutual) {
             $oProfileQuery = BxDolProfileQuery::getInstance();
 
             /**
@@ -413,8 +417,6 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
                 if(bx_srv($aContent['type'], 'act_as_profile'))
                     BxDolRecommendation::updateData($iContent);
             }
-
-            
         }
     }
 
@@ -426,47 +428,63 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
      */
     public function removeConnection ($iInitiator, $iContent)
     {
-        if (!($aConnection = $this->_oQuery->getConnection((int)$iInitiator, (int)$iContent))) // connection doesn't exist
-            return false;
+        $iInitiator = (int)$iInitiator;
+        $iContent = (int)$iContent;
 
-        if (!$this->_oQuery->removeConnection((int)$iInitiator, (int)$iContent))
+        if(!($aConnection = $this->_oQuery->getConnection($iInitiator, $iContent))) // connection doesn't exist
             return false;
 
+        $iMutual = isset($aConnection['mutual']) ? $aConnection['mutual'] : 0;
+
+        $aAlertExtras = [
+            'initiator' => &$iInitiator,
+            'content' => &$iContent,
+            'mutual' => &$iMutual,
+            'object' => $this,
+        ];
+        if(!empty($aParams['alert_extras']) && is_array($aParams['alert_extras']))
+            $aAlertExtras = array_merge($aAlertExtras, $aParams['alert_extras']);
+
         /**
          * @hooks
-         * @hookdef hook-bx_dol_connection-connection_removed '{object_name}', 'connection_removed' - hook after a connection was removed.
+         * @hookdef hook-bx_dol_connection-connection_before_remove '{object_name}', 'connection_before_remove' - hook before connection was removed. Connection params can be overridden
          * - $unit_name - connection object name
-         * - $action - equals `connection_removed`
+         * - $action - equals `connection_before_remove`
          * - $object_id - not used
          * - $sender_id - logged in profile id
          * - $extra_params - array of additional params with the following array keys:
-         *      - `initiator` - [int] profile id who created the connection
-         *      - `content` - [int] profile id with whom the connection was created
-         *      - `mutual` - [int] if the relation is mutual or not
+         *      - `initiator` - [int] by ref, profile id who is removing the connection, can be overridden in hook processing
+         *      - `content` - [int] by ref, profile id with whom the connection is removing, can be overridden in hook processing
+         *      - `mutual` - [int] by ref, if the relation is mutual or not, can be overridden in hook processing
          *      - `object` - [object] an instance of relation, @see BxDolConnection
+         * @hook @ref hook-bx_dol_connection-connection_before_remove
+         */
+        bx_alert($this->_sObject, 'connection_before_remove', 0, false, $aAlertExtras);
+
+        if(!$this->_oQuery->removeConnection($iInitiator, $iContent))
+            return false;
+
+        /**
+         * @hooks
+         * @hookdef hook-bx_dol_connection-connection_removed '{object_name}', 'connection_removed' - hook after a connection was removed.
+         * It's equivalent to @ref hook-bx_dol_connection-connection_before_remove
          * @hook @ref hook-bx_dol_connection-connection_removed
          */
-        bx_alert($this->_sObject, 'connection_removed', 0, bx_get_logged_profile_id(), array(
-            'initiator' => (int)$iInitiator,
-            'content' => (int)$iContent,
-            'mutual' => isset($aConnection['mutual']) ? $aConnection['mutual'] : 0,
-            'object' => $this,
-        ));
+        bx_alert($this->_sObject, 'connection_removed', 0, false, $aAlertExtras);
 
-        $this->onRemoved($iInitiator, $iContent);
+        $this->onRemoved($iInitiator, $iContent, $iMutual);
 
         return true;
     }
 
-    /**
-     * TODO: Improve - add $iMutual like in onAdded
-     */
-    public function onRemoved($iInitiator, $iContent)
+    public function onRemoved($iInitiator, $iContent, $iMutual)
     {
+        $this->_triggerValue($iInitiator, $iContent, -1);
+
         /**
          * Call socket.
          */
-        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()){
+        if(($oSockets = BxDolSockets::getInstance()) && $oSockets->isEnabled()) {
             $aMessageInitiator = $aMessageContent = [
                 'object' => $this->_sObject, 
                 'action' => 'deleted',
@@ -1130,6 +1148,26 @@ class BxDolConnection extends BxDolFactory implements iBxDolFactoryObject
     {
         return $oContent->checkAllowedProfileView();
     }
+
+    protected function _triggerValue($iInitiator, $iContent, $iValue)
+    {
+        if(!$this->_aObject['trigger_table'])
+            return false;
+
+        $iObjectId = $this->_getTriggerObject($iInitiator, $iContent);
+        if(!$iObjectId)
+            return false;
+
+        return $this->_oQuery->updateConnectionTriggerTableValue($iObjectId, $iValue);
+    }
+
+    /**
+     * Should be overwritten in Connection class which uses triggerable fields.
+     */
+    protected function _getTriggerObject($iInitiator, $iContent)
+    {
+        return 0;
+    }
 }
 
 /** @} */

+ 16 - 0
inc/classes/BxDolConnectionQuery.php

@@ -16,6 +16,10 @@ class BxDolConnectionQuery extends BxDolDb
     protected $_aObject;
     protected $_sTable;
     protected $_sType;
+    
+    protected $_sTriggerTable;
+    protected $_sTriggerFieldId;
+    protected $_sTriggerFieldCount;
 
     public function __construct($aObject)
     {
@@ -23,6 +27,10 @@ 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)
@@ -402,6 +410,14 @@ 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

+ 3 - 0
install/sql/system.sql

@@ -5669,6 +5669,9 @@ CREATE TABLE IF NOT EXISTS `sys_objects_connection` (
   `profile_initiator` tinyint(4) NOT NULL DEFAULT '1',
   `profile_content` tinyint(4) NOT NULL DEFAULT '0',
   `type` enum('one-way','mutual') NOT NULL,
+  `trigger_table` varchar(32) NOT NULL DEFAULT '',
+  `trigger_field_id` varchar(32) NOT NULL DEFAULT '',
+  `trigger_field_count` varchar(32) NOT NULL DEFAULT '',
   `override_class_name` varchar(255) NOT NULL,
   `override_class_file` varchar(255) NOT NULL,
   PRIMARY KEY (`id`),

+ 12 - 1
modules/base/groups/classes/BxBaseModGroupsConnectionFans.php

@@ -204,7 +204,7 @@ class BxBaseModGroupsConnectionFans extends BxTemplConnection
         return parent::_checkAllowedConnectInitiator($oInitiator, $isPerformAction);
     }
 
-    public function _checkAllowedConnectContent ($oContent)
+    protected function _checkAllowedConnectContent ($oContent)
     {
         $CNF = &$this->_oModule->_oConfig->CNF;
 
@@ -221,6 +221,17 @@ class BxBaseModGroupsConnectionFans extends BxTemplConnection
 
         return parent::_checkAllowedConnectContent($oContent);
     }
+
+    protected function _getTriggerObject($iInitiator, $iContent)
+    {
+        if(($oInitiator = BxDolProfile::getInstance($iInitiator)) !== false && $oInitiator->getModule() == $this->_sModule)
+            return $oInitiator->getContentId();
+
+        if(($oContent = BxDolProfile::getInstance($iContent)) !== false && $oContent->getModule() == $this->_sModule)
+            return $oContent->getContentId();
+
+        return parent::_getTriggerObject($iInitiator, $iContent);
+    }
 }
 
 /** @} */

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

@@ -547,8 +547,8 @@ 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`, `override_class_name`, `override_class_file`) VALUES
-('bx_courses_fans', 'bx_courses_fans', 1, 1, 'mutual', 'BxCoursesConnectionFans', 'modules/boonex/courses/classes/BxCoursesConnectionFans.php');
+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
+('bx_courses_fans', 'bx_courses_fans', 1, 1, 'mutual', 'bx_courses_data', 'id', 'members', 'BxCoursesConnectionFans', 'modules/boonex/courses/classes/BxCoursesConnectionFans.php');
 
 -- STATS
 SET @iMaxOrderStats = (SELECT IFNULL(MAX(`order`), 0) FROM `sys_statistics`);

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

@@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS `bx_courses_data` (
   `desc` text NOT NULL,
   `labels` text NOT NULL,
   `location` text NOT NULL,
+  `members` int(11) NOT NULL default '0'
   `views` int(11) NOT NULL default '0',
   `rate` float NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',

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

@@ -603,8 +603,8 @@ 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`, `override_class_name`, `override_class_file`) VALUES
-('bx_events_fans', 'bx_events_fans', 1, 1, 'mutual', 'BxEventsConnectionFans', 'modules/boonex/events/classes/BxEventsConnectionFans.php');
+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
+('bx_events_fans', 'bx_events_fans', 1, 1, 'mutual', 'bx_events_data', 'id', 'members', 'BxEventsConnectionFans', 'modules/boonex/events/classes/BxEventsConnectionFans.php');
 
 -- STATS
 SET @iMaxOrderStats = (SELECT IFNULL(MAX(`order`), 0) FROM `sys_statistics`);

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

@@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS `bx_events_data` (
   `labels` text NOT NULL,
   `location` text NOT NULL,
   `threshold` int(11) unsigned NOT NULL default '0',
+  `members` int(11) NOT NULL default '0'
   `views` int(11) NOT NULL default '0',
   `rate` float NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',

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

@@ -540,8 +540,8 @@ 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`, `override_class_name`, `override_class_file`) VALUES
-('bx_groups_fans', 'bx_groups_fans', 1, 1, 'mutual', 'BxGroupsConnectionFans', 'modules/boonex/groups/classes/BxGroupsConnectionFans.php');
+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
+('bx_groups_fans', 'bx_groups_fans', 1, 1, 'mutual', 'bx_groups_data', 'id', 'members', 'BxGroupsConnectionFans', 'modules/boonex/groups/classes/BxGroupsConnectionFans.php');
 
 -- RECOMMENDATIONS
 INSERT INTO `sys_objects_recommendation` (`name`, `module`, `connection`, `content_info`, `countable`, `active`, `class_name`, `class_file`) VALUES

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

@@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS `bx_groups_data` (
   `group_desc` text NOT NULL,
   `labels` text NOT NULL,
   `location` text NOT NULL,
+  `members` int(11) NOT NULL default '0',
   `views` int(11) NOT NULL default '0',
   `rate` float NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',

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

@@ -552,8 +552,8 @@ 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`, `override_class_name`, `override_class_file`) VALUES
-('bx_jobs_fans', 'bx_jobs_fans', 1, 1, 'mutual', 'BxJobsConnectionFans', 'modules/boonex/jobs/classes/BxJobsConnectionFans.php');
+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
+('bx_jobs_fans', 'bx_jobs_fans', 1, 1, 'mutual', 'bx_jobs_data', 'id', 'members', 'BxJobsConnectionFans', 'modules/boonex/jobs/classes/BxJobsConnectionFans.php');
 
 -- RECOMMENDATIONS
 INSERT INTO `sys_objects_recommendation` (`name`, `module`, `connection`, `content_info`, `countable`, `active`, `class_name`, `class_file`) VALUES

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

@@ -19,6 +19,7 @@ CREATE TABLE IF NOT EXISTS `bx_jobs_data` (
   `pay_total` float NOT NULL default '0',
   `labels` text NOT NULL,
   `location` text NOT NULL,
+  `members` int(11) NOT NULL default '0',
   `views` int(11) NOT NULL default '0',
   `rate` float NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',

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

@@ -598,8 +598,8 @@ INSERT INTO `sys_objects_search` (`ObjectName`, `Title`, `Order`, `ClassName`, `
 
 
 -- CONNECTIONS
-INSERT INTO `sys_objects_connection` (`object`, `table`, `profile_initiator`, `profile_content`, `type`, `override_class_name`, `override_class_file`) VALUES
-('bx_organizations_fans', 'bx_organizations_fans', 1, 1, 'mutual', 'BxOrgsConnectionFans', 'modules/boonex/organizations/classes/BxOrgsConnectionFans.php');
+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
+('bx_organizations_fans', 'bx_organizations_fans', 1, 1, 'mutual', 'bx_organizations_data', 'id', 'members', 'BxOrgsConnectionFans', 'modules/boonex/organizations/classes/BxOrgsConnectionFans.php');
 
 
 -- STATS

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

@@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS `bx_organizations_data` (
   `org_desc` text NOT NULL,
   `labels` text NOT NULL,
   `location` text NOT NULL,
+  `members` int(11) NOT NULL default '0',
   `views` int(11) NOT NULL default '0',
   `rate` float NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',

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

@@ -523,8 +523,8 @@ 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`, `override_class_name`, `override_class_file`) VALUES
-('bx_spaces_fans', 'bx_spaces_fans', 1, 1, 'mutual', 'BxSpacesConnectionFans', 'modules/boonex/spaces/classes/BxSpacesConnectionFans.php');
+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
+('bx_spaces_fans', 'bx_spaces_fans', 1, 1, 'mutual', 'bx_spaces_data', 'id', 'members', 'BxSpacesConnectionFans', 'modules/boonex/spaces/classes/BxSpacesConnectionFans.php');
 
 -- STATS
 SET @iMaxOrderStats = (SELECT IFNULL(MAX(`order`), 0) FROM `sys_statistics`);

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

@@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS `bx_spaces_data` (
   `space_desc` text NOT NULL,
   `labels` text NOT NULL,
   `location` text NOT NULL,
+  `members` int(11) NOT NULL default '0',
   `views` int(11) NOT NULL default '0',
   `rate` float NOT NULL default '0',
   `votes` int(11) NOT NULL default '0',