Răsfoiți Sursa

#62 - Base module for Notifications like modules.

AntonLV 12 ani în urmă
părinte
comite
4f3e4779f8

+ 181 - 0
modules/base/notifications/classes/BxBaseModNotificationsConfig.php

@@ -0,0 +1,181 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    BaseNotifications Base classes for Notifications like modules
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+bx_import('BxDolModuleConfig');
+
+class BxBaseModNotificationsConfig extends BxDolModuleConfig
+{
+	protected $_oDb;
+
+	protected $_aSystemNames;
+    protected $_aPrefixes;
+    protected $_aObjects;
+
+	protected $_aHandlerDescriptor;
+    protected $_sHandlersMethod;
+    protected $_aHandlers;
+    protected $_aHandlersHidden;
+
+    protected $_aJsClass;
+    protected $_aJsObjects;
+
+    protected $_aPerPage;
+    protected $_aHtmlIds;
+	
+    protected $_sAnimationEffect;
+    protected $_iAnimationSpeed;
+
+    protected $_iPrivacyViewDefault;
+
+    function __construct($aModule)
+    {
+        parent::__construct($aModule);
+
+        $this->_aSystemNames = array(
+			'alert' => $this->_sName
+		);
+		$this->_aPrefixes = array();
+		$this->_aObjects = array(
+			'conn_subscriptions' => 'sys_profiles_subscriptions'
+		);
+
+        $this->_aHandlerDescriptor = array();
+        $this->_sHandlersMethod = '';
+        $this->_aHandlersHidden = array();
+        $this->_aHandlers = array();
+
+		$this->_aJsClass = array();
+		$this->_aJsObjects = array();
+
+		$this->_aPerPage = array();
+		$this->_aHtmlIds = array();
+
+		$this->_sAnimationEffect = 'fade';
+        $this->_iAnimationSpeed = 'slow';
+
+        $this->_iPrivacyViewDefault = BX_DOL_PG_ALL;
+    }
+
+	public function init(&$oDb)
+    {
+        $this->_oDb = &$oDb;
+        $sOptionPrefix = $this->getPrefix('option');
+
+        $aHandlers = $this->_oDb->getHandlers();
+        foreach($aHandlers as $aHandler) {
+            if($aHandler['type'] === BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT && !empty($aHandler['content'])) {
+                $aContent = unserialize($aHandler['content']);
+                if(is_array($aContent) && !empty($aContent))
+                    $aHandler = array_merge($aHandler, $aContent);
+            }
+
+           $this->_aHandlers[$aHandler['alert_unit'] . '_' . $aHandler['alert_action']] = $aHandler;
+        }
+
+        $sHideTimeline = getParam($sOptionPrefix . 'events_hide');
+        if(!empty($sHideTimeline))
+            $this->_aHandlersHidden = explode(',', $sHideTimeline);
+    }
+
+	public function getSystemName($sType = '')
+    {
+    	if(empty($sType))
+            return $this->_aSystemNames;
+
+        return $this->_aSystemNames[$sType];
+    }
+
+    public function getPrefix($sType = '')
+    {
+    	if(empty($sType))
+            return $this->_aPrefixes;
+
+        return $this->_aPrefixes[$sType];
+    }
+
+    public function getObject($sType = '')
+    {
+    	if(empty($sType))
+            return $this->_aObjects;
+
+        return $this->_aObjects[$sType];
+    }
+
+	public function getHandlerDescriptor()
+    {
+    	return $this->_aHandlerDescriptor;
+    }
+    public function getHandlersMethod()
+    {
+    	return $this->_sHandlersMethod;
+    }
+
+    public function isHandler($sKey = '')
+    {
+        return isset($this->_aHandlers[$sKey]);
+    }
+
+    public function getHandlers($sKey = '')
+    {
+        if($sKey == '')
+            return $this->_aHandlers;
+
+        return $this->_aHandlers[$sKey];
+    }
+
+    public function getHandlersHidden()
+    {
+        return $this->_aHandlersHidden;
+    }
+
+	public function getJsClass($sType)
+    {
+        return $this->_aJsClass[$sType];
+    }
+
+    public function getJsObject($sType)
+    {
+        return $this->_aJsObjects[$sType];
+    }
+
+	public function getPerPage($sType = 'default')
+    {
+    	if(empty($sType))
+            return $this->_aPerPage;
+
+        return $this->_aPerPage[$sType];
+    }
+
+    public function getHtmlIds($sType, $sKey = '')
+    {
+        if(empty($sKey))
+            return $this->_aHtmlIds[$sType];
+
+        return $this->_aHtmlIds[$sType][$sKey];
+    }
+
+    public function getAnimationEffect()
+    {
+        return $this->_sAnimationEffect;
+    }
+
+    public function getAnimationSpeed()
+    {
+        return $this->_iAnimationSpeed;
+    }
+
+    public function getPrivacyViewDefault()
+    {
+        return $this->_iPrivacyViewDefault;
+    }
+}
+
+/** @} */

+ 194 - 0
modules/base/notifications/classes/BxBaseModNotificationsDb.php

@@ -0,0 +1,194 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    BaseNotifications Base classes for Notifications like modules
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+bx_import('BxDolModuleDb');
+
+/*
+ * Module database queries
+ */
+class BxBaseModNotificationsDb extends BxDolModuleDb
+{
+    protected $_oConfig;
+
+    protected $_sTable;
+    protected $_sTableHandlers;
+
+    public function __construct(&$oConfig)
+    {
+        parent::__construct($oConfig);
+
+        $this->_oConfig = $oConfig;
+
+		$this->_sTable = $this->_sPrefix . 'events';
+		$this->_sTableHandlers = $this->_sPrefix . 'handlers';
+    }
+
+    public function getAlertHandlerId()
+    {
+        $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name`=? LIMIT 1", $this->_oConfig->getSystemName('alert'));
+        return (int)$this->getOne($sQuery);
+    }
+
+    public function insertData($aData)
+    {
+    	$aHandlerDescriptor = $this->_oConfig->getHandlerDescriptor();
+
+    	//--- Update Timeline Handlers ---//
+        foreach($aData['handlers'] as $aHandler) {
+            $sContent = '';
+            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));
+            }
+
+            $sQuery = $this->prepare("INSERT INTO
+                    `{$this->_sTableHandlers}`
+                SET
+                    `type`=?,
+                    `alert_unit`=?,
+                    `alert_action`=?,
+                    `content`=?", $aHandler['type'], $aHandler['alert_unit'], $aHandler['alert_action'], $sContent);
+
+            $this->query($sQuery);
+        }
+
+        //--- Update System Alerts ---//
+        $iHandlerId = $this->getAlertHandlerId();
+        foreach($aData['alerts'] as $aAlert) {
+            $sQuery = $this->prepare("INSERT INTO
+                    `sys_alerts`
+                SET
+                    `unit`=?,
+                    `action`=?,
+                    `handler_id`=?", $aAlert['unit'], $aAlert['action'], $iHandlerId);
+
+            $this->query($sQuery);
+        }
+    }
+
+    public function deleteData($aData)
+    {
+    	//--- Update Timeline Handlers ---//
+        foreach($aData['handlers'] as $aHandler) {
+            $sQuery = $this->prepare("DELETE FROM
+                    `{$this->_sTableHandlers}`
+                WHERE
+                    `alert_unit`=? AND
+                    `alert_action`=?
+                LIMIT 1", $aHandler['alert_unit'], $aHandler['alert_action']);
+
+            $this->query($sQuery);
+        }
+
+        //--- Update System Alerts ---//
+        $iHandlerId = $this->getAlertHandlerId();
+        foreach($aData['alerts'] as $aAlert) {
+            $sQuery = $this->prepare("DELETE FROM
+                    `sys_alerts`
+                WHERE
+                    `unit`=? AND
+                    `action`=? AND
+                    `handler_id`=?
+                LIMIT 1", $aAlert['unit'], $aAlert['action'], $iHandlerId);
+
+            $this->query($sQuery);
+        }
+    }
+
+    public function deleteModuleEvents($aData)
+    {
+    	foreach($aData['handlers'] as $aHandler) {
+    		//Delete system events.
+            $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+
+            //Delete shared events.
+    		$aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+			foreach($aEvents as $aEvent) {
+				$aContent = unserialize($aEvent['content']);
+				if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
+					$this->deleteEvent(array('id' => (int)$aEvent['id']));
+			}
+    	}
+    }
+
+	public function activateModuleEvents($aData, $bActivate = true)
+    {
+    	$iActivate = $bActivate ? 1 : 0;
+
+    	foreach($aData['handlers'] as $aHandler) {
+    		//Activate (deactivate) system events.
+            $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+
+			//Activate (deactivate) shared events.
+			$aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
+			foreach($aEvents as $aEvent) {
+				$aContent = unserialize($aEvent['content']);
+				if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
+					$this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
+			}
+    	}
+    }
+
+    public function getHandlers($aParams = array())
+    {
+        $sMethod = 'getAll';
+        $sWhereClause = '';
+
+        if(!empty($aParams))
+            switch($aParams['type']) {}
+
+        $sSql = "SELECT
+                `id` AS `id`,
+                `type` AS `type`,
+                `alert_unit` AS `alert_unit`,
+                `alert_action` AS `alert_action`,
+                `content` AS `content`
+            FROM `{$this->_sTableHandlers}`
+            WHERE 1 " . $sWhereClause;
+
+        return $this->$sMethod($sSql);
+    }
+
+    public function insertEvent($aParamsSet)
+    {
+        if(empty($aParamsSet))
+            return 0;
+
+        $aSet = array();
+        foreach($aParamsSet as $sKey => $sValue)
+           $aSet[] = $this->prepare("`" . $sKey . "`=?", $sValue);
+
+        if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet) . ", `date`=UNIX_TIMESTAMP()") <= 0)
+            return 0;
+
+        return (int)$this->lastId();
+    }
+
+    public function updateEvent($aParamsSet, $aParamsWhere)
+    {
+        if(empty($aParamsSet) || empty($aParamsWhere))
+            return false;
+
+        $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
+        return $this->query($sSql);
+    }
+
+    public function deleteEvent($aParams, $sWhereAddon = "")
+    {
+        $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
+        return $this->query($sSql);
+    }
+    
+}
+
+/** @} */

+ 48 - 0
modules/base/notifications/classes/BxBaseModNotificationsInstaller.php

@@ -0,0 +1,48 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    BaseNotifications Base classes for Notifications like modules
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+bx_import('BxBaseModGeneralInstaller');
+
+class BxBaseModNotificationsInstaller extends BxBaseModGeneralInstaller
+{
+    function __construct($aConfig)
+    {
+        parent::__construct($aConfig);
+    }
+
+	public function enable($aParams)
+    {
+    	$aResult = parent::enable($aParams);
+        if($aResult['result'])
+        	$this->_processHandlers('add_handlers');
+
+        return $aResult;
+    }
+
+    public function disable($aParams)
+    {
+    	$this->_processHandlers('delete_handlers');
+
+        return parent::disable($aParams);
+    }
+
+    protected function _processHandlers($sAction)
+    {
+    	$aModules = $this->oDb->getModules();
+	    foreach($aModules as $aModule) {
+	    	$aConfig = self::getModuleConfig($aModule);
+			if(!empty($aConfig['connections']) && is_array($aConfig['connections']) && in_array($this->_aConfig['name'], $aConfig['connections']))
+				BxDolService::call($this->_aConfig['name'], $sAction, array($aModule['uri']));
+		}
+    }
+}
+
+/** @} */

+ 144 - 0
modules/base/notifications/classes/BxBaseModNotificationsModule.php

@@ -0,0 +1,144 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    BaseNotifications Base classes for Notifications like modules
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+bx_import ('BxDolModule');
+
+define('BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT', 'insert');
+define('BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE', 'update');
+define('BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE', 'delete');
+
+define('BX_BASE_MOD_NTFS_TYPE_OWNER', 'owner');
+define('BX_BASE_MOD_NTFS_TYPE_CONNECTIONS', 'connections');
+
+/**
+ * Base module class.
+ */
+class BxBaseModNotificationsModule extends BxDolModule
+{
+	public $_iOwnerId;
+
+    function __construct(&$aModule)
+    {
+        parent::__construct($aModule);
+
+        $this->_oConfig->init($this->_oDb);
+
+        $this->_iOwnerId = 0;
+    }
+
+	public function serviceAddHandlers($sModuleUri)
+    {
+        $this->_updateModuleData('add_handlers', $sModuleUri);
+    }
+
+    public function serviceDeleteHandlers($sModuleUri)
+    {
+        $this->_updateModuleData('delete_handlers', $sModuleUri);
+    }
+
+	public function serviceDeleteModuleEvents($sModuleUri)
+    {
+        $this->_updateModuleData('delete_module_events', $sModuleUri);
+    }
+
+    function serviceGetActionsChecklist()
+    {
+    	$sLangPrefix = $this->_oConfig->getPrefix('language');
+        $aHandlers = $this->_oConfig->getHandlers();
+
+        $aResults = array();
+        foreach($aHandlers as $aHandler) {
+            if($aHandler['type'] != BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT)
+                continue;
+
+            $aModule = $this->_oDb->getModuleByName($aHandler['module_name']);
+            if(empty($aModule))
+                $aModule['title'] = _t($sLangPrefix . '_alert_module_' . $aHandler['alert_unit']);
+
+            $aResults[$aHandler['id']] = $aModule['title'] . ' (' . _t($sLangPrefix . '_alert_action_' . $aHandler['alert_action']) . ')';
+        }
+
+        asort($aResults);
+        return $aResults;
+    }
+
+	public function getUserId()
+    {
+        return isLogged() ? bx_get_logged_profile_id() : 0;
+    }
+
+    public function getUserIp()
+    {
+        return getVisitorIP();
+    }
+
+    public function getUserInfo($iUserId = 0)
+    {
+        bx_import('BxDolProfile');
+        $oProfile = BxDolProfile::getInstance($iUserId);
+        if (!$oProfile) {
+            bx_import('BxDolProfileUndefined');
+            $oProfile = BxDolProfileUndefined::getInstance();
+        }
+
+        return array(
+            $oProfile->getDisplayName(),
+            $oProfile->getUrl(),
+            $oProfile->getThumb(),
+            $oProfile->getUnit()
+        );
+    }
+
+	protected function _updateModuleData($sAction, $sModuleUri)
+    {
+    	$sMethod = $this->_oConfig->getHandlersMethod();
+
+        $aModule = $this->_oDb->getModuleByUri($sModuleUri);
+		if(!BxDolRequest::serviceExists($aModule, $sMethod))
+        	return;
+
+		$aData = BxDolService::call($aModule['name'], $sMethod);
+		if(empty($aData) || !is_array($aData))
+        	return;
+
+		switch($sAction) {
+			case 'add_handlers':
+				$this->_oDb->insertData($aData);
+				BxDolAlerts::cacheInvalidate();
+
+				$this->_oDb->activateModuleEvents($aData, true);
+				break;
+
+			case 'delete_handlers':
+				$this->_oDb->deleteData($aData);
+				BxDolAlerts::cacheInvalidate();
+
+				$this->_oDb->activateModuleEvents($aData, false);
+				break;
+
+			case 'delete_module_events':
+				$this->_oDb->deleteModuleEvents($aData);
+				break;
+		}
+    }
+
+    protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
+    {
+        header('Content-type: text/html; charset=utf-8');
+
+        $s = json_encode($a);
+        if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
+            $s = '<textarea>' . $s . '</textarea>';
+        echo $s;
+    }
+}
+
+/** @} */

+ 29 - 0
modules/base/notifications/classes/BxBaseModNotificationsResponse.php

@@ -0,0 +1,29 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    BaseNotifications Base classes for Notifications like modules
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+bx_import('BxDolAlertsResponse');
+
+class BxBaseModNotificationsResponse extends BxDolAlertsResponse
+{
+    protected $_oModule;
+
+    function __construct()
+    {
+        parent::__construct();
+    }
+
+    protected function _getPrivacyView($aExtras)
+    {
+        return is_array($aExtras) && isset($aExtras['privacy_view']) ? (int)$aExtras['privacy_view'] : $this->_oModule->_oConfig->getPrivacyViewDefault();
+    }
+}
+
+/** @} */

+ 68 - 0
modules/base/notifications/classes/BxBaseModNotificationsTemplate.php

@@ -0,0 +1,68 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    BaseNotifications Base classes for Notifications like modules
+ * @ingroup     DolphinModules
+ *
+ * @{
+ */
+
+bx_import('BxDolModuleTemplate');
+
+class BxBaseModNotificationsTemplate extends BxDolModuleTemplate
+{
+	function __construct(&$oConfig, &$oDb)
+    {
+        parent::__construct($oConfig, $oDb);
+    }
+
+	public function getCssJs()
+    {
+        $this->addCss(array(
+            'view.css',
+            'view-media-tablet.css',
+            'view-media-desktop.css',
+        ));
+        $this->addJs(array(
+        	'jquery.anim.js',
+            'main.js',
+            'view.js',
+        ));
+    }
+
+	public function getJsCode($sType, $aRequestParams = array(), $bWrap = true)
+    {
+        $oModule = $this->getModule();
+
+        $sBaseUri = $this->_oConfig->getBaseUri();
+        $sJsClass = $this->_oConfig->getJsClass($sType);
+        $sJsObject = $this->_oConfig->getJsObject($sType);
+
+        $aParams = array(
+            'sActionUri' => $sBaseUri,
+            'sActionUrl' => BX_DOL_URL_ROOT . $sBaseUri,
+            'sObjName' => $sJsObject,
+            'iOwnerId' => $oModule->_iOwnerId,
+            'sAnimationEffect' => $this->_oConfig->getAnimationEffect(),
+            'iAnimationSpeed' => $this->_oConfig->getAnimationSpeed(),
+            'aHtmlIds' => $this->_oConfig->getHtmlIds($sType),
+            'oRequestParams' => !empty($aRequestParams) ? $aRequestParams : array()
+        );
+
+        $sContent = "var " . $sJsObject . " = new " . $sJsClass . "(" . json_encode($aParams) . ");";
+
+        $this->getCssJs();
+        return !$bWrap ? $sContent : $this->_wrapInTagJsCode($sContent);
+    }
+
+	protected function getModule()
+    {
+        $sName = $this->_oConfig->getName();
+        return BxDolModule::getInstance($sName);
+    }
+
+}
+
+/** @} */