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

Ticket #62 - Event privacy in Base Notifications and Notifications modules.

AntonLV 10 лет назад
Родитель
Сommit
fc7128f2af

+ 19 - 4
modules/base/notifications/classes/BxBaseModNotificationsConfig.php

@@ -26,7 +26,7 @@ class BxBaseModNotificationsConfig extends BxBaseModGeneralConfig
     protected $_sAnimationEffect;
     protected $_iAnimationSpeed;
 
-    protected $_iPrivacyViewDefault;
+    protected $_aPrivacyViewDefault;
 
     function __construct($aModule)
     {
@@ -55,7 +55,10 @@ class BxBaseModNotificationsConfig extends BxBaseModGeneralConfig
 		$this->_sAnimationEffect = 'fade';
         $this->_iAnimationSpeed = 'slow';
 
-        $this->_iPrivacyViewDefault = BX_DOL_PG_ALL;
+        $this->_aPrivacyViewDefault = array(
+        	'object' => BX_DOL_PG_ALL,
+        	'event' => BX_DOL_PG_ALL
+        );
     }
 
 	public function init(&$oDb)
@@ -106,6 +109,18 @@ class BxBaseModNotificationsConfig extends BxBaseModGeneralConfig
         return $this->_aHandlersHidden;
     }
 
+    public function getPrivacyObject($sKey = '')
+    {
+    	if(!$this->isHandler($sKey))
+    		return false;
+
+    	$aHandler = $this->getHandlers($sKey);
+    	if(empty($aHandler) || !is_array($aHandler) || empty($aHandler['privacy']))
+    		return false;
+
+		return BxDolPrivacy::getObjectInstance($aHandler['privacy']);
+    }
+
 	public function getPerPage($sType = 'default')
     {
     	if(empty($sType))
@@ -132,9 +147,9 @@ class BxBaseModNotificationsConfig extends BxBaseModGeneralConfig
         return $this->_iAnimationSpeed;
     }
 
-    public function getPrivacyViewDefault()
+    public function getPrivacyViewDefault($sType)
     {
-        return $this->_iPrivacyViewDefault;
+        return isset($this->_aPrivacyViewDefault[$sType]) ? $this->_aPrivacyViewDefault[$sType] : BX_DOL_PG_ALL;
     }
 }
 

+ 4 - 2
modules/base/notifications/classes/BxBaseModNotificationsDb.php

@@ -41,12 +41,13 @@ class BxBaseModNotificationsDb extends BxDolModuleDb
 
     	//--- Update Timeline Handlers ---//
         foreach($aData['handlers'] as $aHandler) {
-            $sContent = '';
+            $sContent = $sPrivacy = '';
             if($aHandler['type'] == BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT) {
             	if(empty($aHandler['module_class']))
             		$aHandler['module_class'] = 'Module';
 
             	$sContent = serialize(array_intersect_key($aHandler, $aHandlerDescriptor));
+            	$sPrivacy = !empty($aHandler['module_event_privacy']) ? $aHandler['module_event_privacy'] : '';
             }
 
             $sQuery = $this->prepare("INSERT INTO
@@ -56,7 +57,8 @@ class BxBaseModNotificationsDb extends BxDolModuleDb
                     `type`=?,
                     `alert_unit`=?,
                     `alert_action`=?,
-                    `content`=?", $aHandler['group'], $aHandler['type'], $aHandler['alert_unit'], $aHandler['alert_action'], $sContent);
+                    `content`=?,
+                    `privacy`=?", $aHandler['group'], $aHandler['type'], $aHandler['alert_unit'], $aHandler['alert_action'], $sContent, $sPrivacy);
 
             $this->query($sQuery);
         }

+ 1 - 1
modules/base/notifications/classes/BxBaseModNotificationsResponse.php

@@ -25,7 +25,7 @@ class BxBaseModNotificationsResponse extends BxDolAlertsResponse
 
     protected function _getObjectPrivacyView($aExtras)
     {
-        return is_array($aExtras) && isset($aExtras['privacy_view']) ? (int)$aExtras['privacy_view'] : $this->_oModule->_oConfig->getPrivacyViewDefault();
+        return is_array($aExtras) && isset($aExtras['privacy_view']) ? (int)$aExtras['privacy_view'] : $this->_oModule->_oConfig->getPrivacyViewDefault('object');
     }
 
 	protected function _getSubObjectId($aExtras)

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

@@ -75,6 +75,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
 			'object_privacy_view' => $this->_getObjectPrivacyView($oAlert->aExtras),
 			'subobject_id' => $this->_getSubObjectId($oAlert->aExtras),
 			'content' => '',
+    		'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
 			'processed' => 0
 		);
     }
@@ -93,6 +94,7 @@ class BxNtfsResponse extends BxBaseModNotificationsResponse
 			'object_privacy_view' => $this->_getObjectPrivacyView($oAlert->aExtras),
 			'subobject_id' => 0,
 			'content' => '',
+    		'allow_view_event_to' => $this->_oModule->_oConfig->getPrivacyViewDefault('event'),
 			'processed' => 0
 		);
     }

+ 47 - 31
modules/boonex/notifications/classes/BxNtfsTemplate.php

@@ -40,32 +40,34 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
     	$sJsObject = $this->_oConfig->getJsObject('view');
 
     	$aParamsDb = $aParams;
-    	$aParamsDb['per_page'] = $aParamsDb['per_page'] + 1;
+    	$aParamsDb['per_page'] = 3 * $aParamsDb['per_page'];
 
         $aEvents = $this->_oDb->getEvents($aParamsDb);
         if(empty($aEvents))
         	return $this->getEmpty();
 
-        $oPaginate = new BxTemplPaginate(array(
-        	'start' => $aParams['start'],
-            'per_page' => $aParams['per_page'],
-        	'page_url' => $this->_oConfig->getViewUrl(),
-        	'on_change_page' => $sJsObject . ".changePage(this, {start}, {per_page})"
-        ));
-        $oPaginate->setNumFromDataArray($aEvents);
-
-        $sEvents = '';
+        $aTmplVarsEvents = array();
         foreach($aEvents as $aEvent) {
             $sEvent = $this->getPost($aEvent, $aParams);
             if(empty($sEvent))
                 continue;
 
-            $sEvents .= $sEvent;
+            $aTmplVarsEvents[] = array('event' => $sEvent);
+            if(count($aTmplVarsEvents) >= ($aParams['per_page'] + 1))
+            	break;
         }
 
+        $oPaginate = new BxTemplPaginate(array(
+        	'start' => $aParams['start'],
+            'per_page' => $aParams['per_page'],
+        	'page_url' => $this->_oConfig->getViewUrl(),
+        	'on_change_page' => $sJsObject . ".changePage(this, {start}, {per_page})"
+        ));
+        $oPaginate->setNumFromDataArray($aTmplVarsEvents);
+
         return $this->parseHtmlByName('events.html', array(
         	'style_prefix' => $this->_oConfig->getPrefix('style'),
-        	'events' => $sEvents,
+        	'bx_repeat:events' => $aTmplVarsEvents,
         	'paginate' => $oPaginate->getSimplePaginate()
         ));
     }
@@ -74,25 +76,12 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
     {
     	$oModule = $this->getModule();
 
-    	if((int)$aEvent['processed'] == 0) {
-    		$aContent = $this->_getContent($aEvent);
-    		if(!empty($aContent) && is_array($aContent)) {
-    			$aSet = array();
-
-    			if(!empty($aContent['entry_author'])) {
-    				$aSet['object_owner_id'] = (int)$aContent['entry_author'];
-    				unset($aContent['entry_author']);
-    			}
+    	if((int)$aEvent['processed'] == 0)
+    		$this->_processContent($aEvent);
 
-    			$aEvent['content'] = serialize($aContent);
-    			$aSet = array_merge($aSet, array(
-    				'content' => $aEvent['content'], 
-    				'processed' => 1
-    			));
-
-    			$this->_oDb->updateEvent($aSet, array('id' => $aEvent['id']));
-    		}
-    	}
+    	$oPrivacy = $oModule->_oConfig->getPrivacyObject($aEvent['type'] . '_' . $aEvent['action']);
+    	if($oPrivacy !== false && !$oPrivacy->check($aEvent['id'])) 
+    		return '';
 
         list($sOwnerName, $sOwnerUrl, $sOwnerIcon) = $oModule->getUserInfo($aEvent['owner_id']);
         $bAuthorIcon = !empty($sOwnerIcon);
@@ -136,6 +125,34 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
         ));
     }
 
+    protected function _processContent(&$aEvent)
+    {
+    	$aContent = $this->_getContent($aEvent);
+		if(empty($aContent) || !is_array($aContent)) 
+			return;
+
+		$aSet = array();
+		if(!empty($aContent['entry_author'])) {
+			$aSet['object_owner_id'] = (int)$aContent['entry_author'];
+			unset($aContent['entry_author']);
+		}
+
+		if(!empty($aContent['entry_privacy'])) {
+			$aSet['allow_view_event_to'] = $aContent['entry_privacy'];
+			$aEvent['allow_view_event_to'] = $aContent['entry_privacy'];
+			unset($aContent['entry_privacy']);
+		}
+
+		$aEvent['content'] = serialize($aContent);
+		$aSet = array_merge($aSet, array(
+			'content' => $aEvent['content'], 
+			'processed' => 1
+		));
+
+		$this->_oDb->updateEvent($aSet, array('id' => $aEvent['id']));
+		return;
+    }
+
     protected function _getContent(&$aEvent)
     {
         $sHandler = $aEvent['type'] . '_' . $aEvent['action'];
@@ -145,7 +162,6 @@ class BxNtfsTemplate extends BxBaseModNotificationsTemplate
         $aHandler = $this->_oConfig->getHandlers($sHandler);
         if(!empty($aHandler['module_name']) && !empty($aHandler['module_class']) && !empty($aHandler['module_method']))
         	return BxDolService::call($aHandler['module_name'], $aHandler['module_method'], array($aEvent), $aHandler['module_class']);
-        
 
         $sMethod = 'display' . bx_gen_method_name($aHandler['alert_unit'] . '_' . $aHandler['alert_action']);
 		if(!method_exists($this, $sMethod))

+ 3 - 1
modules/boonex/notifications/install/sql/install.sql

@@ -9,9 +9,10 @@ CREATE TABLE IF NOT EXISTS `bx_notifications_events` (
   `action` varchar(255) collate utf8_unicode_ci NOT NULL,
   `object_id` text collate utf8_unicode_ci NOT NULL,
   `object_owner_id` int(11) unsigned NOT NULL default '0',
-  `object_privacy_view` int(11) NOT NULL default '3',
+  `object_privacy_view` varchar(32) NOT NULL default '3',
   `subobject_id` int(11) NOT NULL default '0',
   `content` text collate utf8_unicode_ci NOT NULL,
+  `allow_view_event_to` varchar(32) NOT NULL default '3',
   `date` int(11) NOT NULL default '0',
   `processed` tinyint(4) NOT NULL default '0',
   `active` tinyint(4) NOT NULL default '1',
@@ -32,6 +33,7 @@ CREATE TABLE IF NOT EXISTS `bx_notifications_handlers` (
   `alert_unit` varchar(64) NOT NULL default '',
   `alert_action` varchar(64) NOT NULL default '',
   `content` text collate utf8_unicode_ci NOT NULL,
+  `privacy` varchar(64) NOT NULL default '',
   PRIMARY KEY  (`id`),
   UNIQUE `handler` (`group`, `type`),
   UNIQUE `alert` (`alert_unit`, `alert_action`)

+ 1 - 1
modules/boonex/notifications/template/events.html

@@ -1,2 +1,2 @@
-__events__
+<bx_repeat:events>__event__</bx_repeat:events>
 <div class="__style_prefix__-paginate bx-def-margin-sec-top">__paginate__</div>

+ 1 - 1
modules/boonex/timeline/classes/BxTimelineDb.php

@@ -130,7 +130,7 @@ class BxTimelineDb extends BxBaseModNotificationsDb
 				'type' => $sType,
 				'action' => $sAction,
 				'object_id' => $iObjectId,
-				'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
+				'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
 				'content' => '',
 				'title' => '',
 				'description' => '',

+ 2 - 2
modules/boonex/timeline/classes/BxTimelineModule.php

@@ -124,7 +124,7 @@ class BxTimelineModule extends BxBaseModNotificationsModule
             'type' => $this->_oConfig->getPrefix('common_post') . 'share',
             'action' => '',
             'object_id' => $iAuthorId,
-            'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
+            'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
             'content' => serialize($aContent),
             'title' => '',
             'description' => ''
@@ -664,7 +664,7 @@ class BxTimelineModule extends BxBaseModNotificationsModule
 
             $iId = $oForm->insert(array(
                 'object_id' => $iUserId,
-                'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault(),
+                'object_privacy_view' => $this->_oConfig->getPrivacyViewDefault('object'),
                 'content' => serialize($aContent),
                 'title' => $sTitle,
                 'description' => $sDescription,

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

@@ -30,6 +30,7 @@ CREATE TABLE IF NOT EXISTS `bx_timeline_handlers` (
   `alert_unit` varchar(64) NOT NULL default '',
   `alert_action` varchar(64) NOT NULL default '',
   `content` text collate utf8_unicode_ci NOT NULL,
+  `privacy` varchar(64) NOT NULL default '',
   PRIMARY KEY  (`id`),
   UNIQUE `handler` (`group`, `type`),
   UNIQUE `alert` (`alert_unit`, `alert_action`)