| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?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
- *
- * @{
- */
- /*
- * 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
- `group`=?,
- `type`=?,
- `alert_unit`=?,
- `alert_action`=?,
- `content`=?", $aHandler['group'], $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)
- {
- //Delete system events.
- foreach($aData['handlers'] as $aHandler)
- $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
- }
- public function activateModuleEvents($aData, $bActivate = true)
- {
- $iActivate = $bActivate ? 1 : 0;
- //Activate (deactivate) system events.
- foreach($aData['handlers'] as $aHandler)
- $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
- }
- public function getHandlers($aParams = array())
- {
- $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
- $sWhereClause = '';
- if(!empty($aParams))
- switch($aParams['type']) {
- case 'by_group_key_type':
- $aMethod['name'] = 'getAllWithKey';
- $aMethod['params'][1] = 'type';
- $aMethod['params'][2] = array(
- 'group' => $aParams['group']
- );
- $sWhereClause = "AND `group`=:group";
- break;
- }
- $aMethod['params'][0] = "SELECT * FROM `{$this->_sTableHandlers}` WHERE 1 " . $sWhereClause;
- return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
- }
- public function insertEvent($aParamsSet)
- {
- if(empty($aParamsSet))
- return 0;
- $aSet = array();
- foreach($aParamsSet as $sKey => $sValue)
- $aSet[] = "`" . $sKey . "`=:" . $sKey;
- if(!isset($aParamsSet['date']))
- $aSet[] = "`date`=UNIX_TIMESTAMP()";
- if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet), $aParamsSet) <= 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 {select}
- 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 . " {order} {limit}";
- $aEntries = $this->$sMethod(str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause . "`{$this->_sTable}`.*", $sOrderClause, $sLimitClause), $sSql));
- if(!$bReturnCount)
- return $aEntries;
- return array($aEntries, (int)$this->getOne(str_replace(array('{select}', '{order}', '{limit}'), array("COUNT(*)", "", ""), $sSql)));
- }
- protected function _getSqlPartsEvents($aParams)
- {
- $sMethod = 'getAll';
- $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
- switch($aParams['browse']) {
- case 'id':
- $sMethod = 'getRow';
- $sWhereClause = $this->prepareAsString("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);
- }
- }
- /** @} */
|