| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?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 TridentModules
- *
- * @{
- */
- 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->getObject('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);
- }
- public function getEvents($aParams, $bReturnCount = false)
- {
- list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
- $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . $sSelectClause . "
- `{$this->_sTable}`.*
- FROM `{$this->_sTable}`
- LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
- WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
- $aEntries = $this->$sMethod($sSql);
- if(!$bReturnCount)
- return $aEntries;
- return array($aEntries, (int)$this->getOne("SELECT FOUND_ROWS()"));
- }
- protected function _getSqlPartsEvents($aParams)
- {
- $sMethod = 'getAll';
- $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
- switch($aParams['browse']) {
- case 'id':
- $sMethod = 'getRow';
- $sWhereClause = $this->prepare("AND `{$this->_sTable}`.`id`=? ", $aParams['value']);
- $sLimitClause = "LIMIT 1";
- break;
- case 'first':
- $sMethod = 'getRow';
- list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
- $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
- $sLimitClause = "LIMIT 1";
- break;
- case 'last':
- $sMethod = 'getRow';
- list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
- $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` ASC";
- $sLimitClause = "LIMIT 1";
- break;
- case 'list':
- list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
- $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
- $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
- break;
- }
- return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
- }
- protected function _getSqlPartsEventsList($aParams)
- {
- $sJoinClause = $sWhereClause = "";
- return array($sJoinClause, $sWhereClause);
- }
- }
- /** @} */
|