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

Ticket #4008 - Notifications: Add possibility to group notifications.

AntonLV 4 лет назад
Родитель
Сommit
1130cc0cfa

+ 9 - 0
modules/boonex/notifications/classes/BxNtfsConfig.php

@@ -32,6 +32,8 @@ class BxNtfsConfig extends BxBaseModNotificationsConfig
     protected $_iDeliveryTimeout;
     protected $_iProcessedEvent;
 
+    protected $_bEventsGrouped;
+
     /**
      * Constructor
      */
@@ -132,6 +134,8 @@ class BxNtfsConfig extends BxBaseModNotificationsConfig
 
         $this->_iDeliveryTimeout = (int)getParam($sOptionPrefix . 'delivery_timeout');
         $this->_iProcessedEvent = (int)getParam($sOptionPrefix . 'processed_event');
+
+        $this->_bEventsGrouped = getParam($sOptionPrefix . 'enable_group_events') == 'on';
     }
 
     public function getOwnerNameMaxLen()
@@ -172,6 +176,11 @@ class BxNtfsConfig extends BxBaseModNotificationsConfig
         return $this->_iDeliveryTimeout;
     }
 
+    public function isEventsGrouped()
+    {
+        return $this->_bEventsGrouped;
+    }
+
     public function getProcessedEvent()
     {
         return $this->_iProcessedEvent;

+ 6 - 2
modules/boonex/notifications/classes/BxNtfsDb.php

@@ -144,8 +144,12 @@ class BxNtfsDb extends BxBaseModNotificationsDb
                     $sMethod = 'getOne';
                     $sSelectClause = 'COUNT(DISTINCT `' . $this->_sTable . '`.`id`)';
                 }
-                else if(in_array($aParams['type'], array(BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS)))
-                    $sSelectClause = 'DISTINCT ' . $sSelectClause;
+                else {
+                    $sSelectClause .= ", `{$this->_sTableHandlers}`.`priority` AS `priority`";
+
+                    if(in_array($aParams['type'], array(BX_BASE_MOD_NTFS_TYPE_CONNECTIONS, BX_NTFS_TYPE_OBJECT_OWNER_AND_CONNECTIONS)))
+                        $sSelectClause = 'DISTINCT ' . $sSelectClause;
+                }
 
                 break;
         }

+ 53 - 4
modules/boonex/notifications/classes/BxNtfsModule.php

@@ -249,6 +249,9 @@ class BxNtfsModule extends BxBaseModNotificationsModule
 
         $aEvents = $this->_oDb->getEvents($aParams);
 
+        if($this->_oConfig->isEventsGrouped())
+            $this->groupEvents($aEvents);
+
         // returns parsed content for React Jot
         if (!is_array($aBrowseParams) && $aBrowseParams === 'return_parsed_content')
             foreach ($aEvents as &$aContent)
@@ -305,15 +308,32 @@ class BxNtfsModule extends BxBaseModNotificationsModule
             return 0;
 
         $aParams = $this->_prepareParams(BX_NTFS_TYPE_DEFAULT, $iOwnerId, 0, PHP_INT_MAX);
-        $aParams = array_merge($aParams, array(
-            'new' => 1,
-        ));
 
         $sParamCheck = 'perform_privacy_check';
         $sParamCheckFor = 'perform_privacy_check_for';
 
         $iEvents = 0;
-        $aEvents = $this->_oDb->getEvents($aParams);
+        if($this->_oConfig->isEventsGrouped()) {
+            $aEvents = $this->_oDb->getEvents($aParams);
+
+            $this->groupEvents($aEvents);
+
+            $iCount = 0;
+            $iLastRead = $this->_oDb->getLastRead((int)$aParams['owner_id']);
+            foreach($aEvents as $aEvent)
+                if($aEvent['id'] > $iLastRead)
+                    $iCount++;
+
+            $aEvents = array_slice($aEvents, 0, $iCount);
+        }
+        else {
+            $aParams = array_merge($aParams, [
+                'new' => 1,
+            ]);
+
+            $aEvents = $this->_oDb->getEvents($aParams);
+        }
+
         foreach($aEvents as $aEvent) {
             if(isset($aBrowseParams[$sParamCheck]) && $aBrowseParams[$sParamCheck] !== true) 
                 continue;
@@ -519,6 +539,35 @@ class BxNtfsModule extends BxBaseModNotificationsModule
         ), true);
     }
 
+    public function groupEvents(&$aEvents)
+    {
+        //--- Check for Visual Grouping
+        $aGroups = [];
+        foreach($aEvents as $iIndex => $aEvent) {
+            if(empty($aEvent['source']))
+                continue;
+
+            $sSource = $aEvent['source'];
+            if(!isset($aGroups[$sSource]))
+               $aGroups[$sSource] = [];
+
+            $aGroups[$sSource][$iIndex] = $aEvent['priority'];
+        }
+
+        //--- Perform Visual Grouping
+        foreach($aGroups as $sSource => $aGroup) {
+            if(!is_array($aGroup) || count($aGroup) < 2)
+                continue;
+
+            arsort($aGroup);
+            $iPriorityMax = reset($aGroup);
+
+            foreach($aGroup as $iIndex => $iPriority)
+                if($iPriority != $iPriorityMax)
+                    unset($aEvents[$iIndex]);
+        }
+    }
+
     /*
      * INTERNAL METHODS
      */

+ 10 - 0
modules/boonex/notifications/classes/BxNtfsResponse.php

@@ -111,6 +111,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 'object_privacy_view' => $iObjectPrivacyView,
                 'subobject_id' => $iSubobjectId,
                 'content' => $this->_getContent($oAlert->aExtras),
+                'source' => $this->_getSource($oAlert->aExtras),
                 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
                 'processed' => 0
     	    );
@@ -186,6 +187,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 'object_privacy_view' => $iObjectPrivacyView,
                 'subobject_id' => $oAlert->aExtras['comment_uniq_id'],
                 'content' => $this->_getContent($oAlert->aExtras),
+                'source' => $this->_getSource($oAlert->aExtras),
                 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
                 'processed' => 0
             ];
@@ -216,6 +218,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 'content' => $this->_getContent($oAlert->aExtras, array(
                     'request' => empty($oAlert->aExtras['mutual']) ? 1 : 0
                 )),
+                'source' => $this->_getSource($oAlert->aExtras),
                 'allow_view_event_to' => $iPrivacyView,
                 'processed' => 0
     	    )
@@ -242,6 +245,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 'content_id' => $oAlert->aExtras['content_id'],
                 'module' => $oAlert->aExtras['object']
             )),
+            'source' => $this->_getSource($oAlert->aExtras),
             'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
             'processed' => 0
         ]];
@@ -285,6 +289,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
                 'object_privacy_view' => $this->_getObjectPrivacyView($oAlert->aExtras),
                 'subobject_id' => 0,
                 'content' => $this->_getContent($oAlert->aExtras),
+                'source' => $this->_getSource($oAlert->aExtras),
                 'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
                 'processed' => 0
     	    )
@@ -330,6 +335,11 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
 
         return !empty($aResult) && is_array($aResult) ? serialize($aResult) : '';
     }
+
+    protected function _getSource($aExtras)
+    {
+        return isset($aExtras['source']) ? $aExtras['source'] : '';
+    }
 }
 
 /** @} */

+ 4 - 1
modules/boonex/notifications/classes/BxNtfsTemplate.php

@@ -80,7 +80,10 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
 
         $aEvents = $this->_oDb->getEvents($aParamsDb);
         if(empty($aEvents))
-        	return $this->getEmpty();
+            return $this->getEmpty();
+
+        if($this->_oConfig->isEventsGrouped())
+            $this->_oModule->groupEvents($aEvents);
 
         $aTmplVarsEvents = array();
         foreach($aEvents as $aEvent) {

+ 2 - 1
modules/boonex/notifications/install/langs/en.xml

@@ -42,7 +42,8 @@
     <string name="_bx_ntfs_option_events_hide_site"><![CDATA[Onsite notifications]]></string>
     <string name="_bx_ntfs_option_events_hide_email"><![CDATA[Email notifications]]></string>
     <string name="_bx_ntfs_option_events_hide_push"><![CDATA[Push notifications]]></string>
-    <string name="_bx_ntfs_option_enable_group_settings"><![CDATA[Group Settings]]></string>
+    <string name="_bx_ntfs_option_enable_group_settings"><![CDATA[Group settings by category]]></string>
+    <string name="_bx_ntfs_option_enable_group_events"><![CDATA[Group notifications by event]]></string>
     <string name="_bx_ntfs_option_delivery_timeout"><![CDATA[Delay before sending Email and/or Push notifications (in seconds)]]></string>
     <string name="_bx_ntfs_option_processed_event"><![CDATA[The last processed event upon notifications sending]]></string>
     <string name="_bx_ntfs_option_clear_interval"><![CDATA[Notification lifetime, in days (0 - no limit)]]></string>

+ 1 - 0
modules/boonex/notifications/install/sql/enable.sql

@@ -92,6 +92,7 @@ INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `c
 ('bx_notifications_events_per_page', '12', @iCategId, '_bx_ntfs_option_events_per_page', 'digit', '', '', '', '', 1),
 ('bx_notifications_events_per_preview', '5', @iCategId, '_bx_ntfs_option_events_per_preview', 'digit', '', '', '', '', 5),
 ('bx_notifications_enable_group_settings', 'on', @iCategId, '_bx_ntfs_option_enable_group_settings', 'checkbox', '', '', '', '', 10), 
+('bx_notifications_enable_group_events', 'on', @iCategId, '_bx_ntfs_option_enable_group_events', 'checkbox', '', '', '', '', 11), 
 ('bx_notifications_delivery_timeout', '120', @iCategId, '_bx_ntfs_option_delivery_timeout', 'digit', '', '', '', '', 20),
 ('bx_notifications_clear_interval', '0', @iCategId, '_bx_ntfs_option_clear_interval', 'digit', '', '', '', '', 30),
 ('bx_notifications_enable_comment_post_ext', '', @iCategId, '_bx_ntfs_option_enable_comment_post_ext', 'checkbox', '', '', '', '', 40);

+ 4 - 2
modules/boonex/notifications/install/sql/install.sql

@@ -12,6 +12,7 @@ CREATE TABLE IF NOT EXISTS `bx_notifications_events` (
   `object_privacy_view` varchar(32) NOT NULL default '3',
   `subobject_id` int(11) NOT NULL default '0',
   `content` text NOT NULL,
+  `source` varchar(32) NOT NULL default '',
   `allow_view_event_to` varchar(32) NOT NULL default '3',
   `date` int(11) NOT NULL default '0',
   `processed` tinyint(4) NOT NULL default '0',
@@ -35,6 +36,7 @@ CREATE TABLE IF NOT EXISTS `bx_notifications_handlers` (
   `alert_action` varchar(64) NOT NULL default '',
   `content` text NOT NULL,
   `privacy` varchar(64) NOT NULL default '',
+  `priority` int(11) NOT NULL default '0',
   PRIMARY KEY  (`id`),
   UNIQUE `handler` (`group`, `type`),
   UNIQUE `alert` (`alert_unit`, `alert_action`)
@@ -67,8 +69,8 @@ INSERT INTO `bx_notifications_handlers`(`group`, `type`, `alert_unit`, `alert_ac
 ('profile', 'delete', 'profile', 'delete', '');
 
 -- Mentions
-INSERT INTO `bx_notifications_handlers`(`group`, `type`, `alert_unit`, `alert_action`, `content`) VALUES 
-('mention', 'insert', 'meta_mention', 'added', 'a:3:{s:11:"module_name";s:6:"system";s:13:"module_method";s:30:"get_notifications_post_mention";s:12:"module_class";s:20:"TemplServiceMetatags";}');
+INSERT INTO `bx_notifications_handlers`(`group`, `type`, `alert_unit`, `alert_action`, `content`, `privacy`, `priority`) VALUES 
+('mention', 'insert', 'meta_mention', 'added', 'a:3:{s:11:"module_name";s:6:"system";s:13:"module_method";s:30:"get_notifications_post_mention";s:12:"module_class";s:20:"TemplServiceMetatags";}', '', 1);
 SET @iHandlerId = LAST_INSERT_ID();
 
 INSERT INTO `bx_notifications_settings`(`group`, `handler_id`, `delivery`, `type`, `title`, `order`) VALUES

+ 2 - 1
modules/boonex/russian/data/langs/bx_notifications/ru.xml

@@ -42,7 +42,8 @@
     <string name="_bx_ntfs_option_events_hide_site"><![CDATA[Уведомления на сайте]]></string>
     <string name="_bx_ntfs_option_events_hide_email"><![CDATA[Уведомления через email]]></string>
     <string name="_bx_ntfs_option_events_hide_push"><![CDATA[Push-уведомления]]></string>
-    <string name="_bx_ntfs_option_enable_group_settings"><![CDATA[Сгруппировать настройки]]></string>
+    <string name="_bx_ntfs_option_enable_group_settings"><![CDATA[Сгруппировать настройки по категориям]]></string>
+    <string name="_bx_ntfs_option_enable_group_events"><![CDATA[Сгруппировать оповещения по принадлежности к событию]]></string>
     <string name="_bx_ntfs_option_delivery_timeout"><![CDATA[Задержка перед отправкой Email и/или Push уведомлений (в секундах)]]></string>
     <string name="_bx_ntfs_option_clear_interval"><![CDATA[Срок жизни уведомления, в днях (0 - без ограничений)]]></string>
     <string name="_bx_ntfs_option_processed_event"><![CDATA[Последнее событие, обработанное в процессе отправки оповещений]]></string>