1
0

BxBaseModNotificationsDb.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseNotifications Base classes for Notifications like modules
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxBaseModNotificationsDb extends BxDolModuleDb
  15. {
  16. protected $_oConfig;
  17. protected $_sTable;
  18. protected $_sTableHandlers;
  19. public function __construct(&$oConfig)
  20. {
  21. parent::__construct($oConfig);
  22. $this->_oConfig = $oConfig;
  23. $this->_sTable = $this->_sPrefix . 'events';
  24. $this->_sTableHandlers = $this->_sPrefix . 'handlers';
  25. }
  26. public function getAlertHandlerId()
  27. {
  28. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name`=? LIMIT 1", $this->_oConfig->getObject('alert'));
  29. return (int)$this->getOne($sQuery);
  30. }
  31. public function insertData($aData)
  32. {
  33. $aHandlerDescriptor = $this->_oConfig->getHandlerDescriptor();
  34. //--- Update Timeline Handlers ---//
  35. foreach($aData['handlers'] as $aHandler) {
  36. $sContent = '';
  37. if($aHandler['type'] == BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT) {
  38. if(empty($aHandler['module_class']))
  39. $aHandler['module_class'] = 'Module';
  40. $sContent = serialize(array_intersect_key($aHandler, $aHandlerDescriptor));
  41. }
  42. $sQuery = $this->prepare("INSERT INTO
  43. `{$this->_sTableHandlers}`
  44. SET
  45. `group`=?,
  46. `type`=?,
  47. `alert_unit`=?,
  48. `alert_action`=?,
  49. `content`=?", $aHandler['group'], $aHandler['type'], $aHandler['alert_unit'], $aHandler['alert_action'], $sContent);
  50. $this->query($sQuery);
  51. }
  52. //--- Update System Alerts ---//
  53. $iHandlerId = $this->getAlertHandlerId();
  54. foreach($aData['alerts'] as $aAlert) {
  55. $sQuery = $this->prepare("INSERT INTO
  56. `sys_alerts`
  57. SET
  58. `unit`=?,
  59. `action`=?,
  60. `handler_id`=?", $aAlert['unit'], $aAlert['action'], $iHandlerId);
  61. $this->query($sQuery);
  62. }
  63. }
  64. public function deleteData($aData)
  65. {
  66. //--- Update Timeline Handlers ---//
  67. foreach($aData['handlers'] as $aHandler) {
  68. $sQuery = $this->prepare("DELETE FROM
  69. `{$this->_sTableHandlers}`
  70. WHERE
  71. `alert_unit`=? AND
  72. `alert_action`=?
  73. LIMIT 1", $aHandler['alert_unit'], $aHandler['alert_action']);
  74. $this->query($sQuery);
  75. }
  76. //--- Update System Alerts ---//
  77. $iHandlerId = $this->getAlertHandlerId();
  78. foreach($aData['alerts'] as $aAlert) {
  79. $sQuery = $this->prepare("DELETE FROM
  80. `sys_alerts`
  81. WHERE
  82. `unit`=? AND
  83. `action`=? AND
  84. `handler_id`=?
  85. LIMIT 1", $aAlert['unit'], $aAlert['action'], $iHandlerId);
  86. $this->query($sQuery);
  87. }
  88. }
  89. public function deleteModuleEvents($aData)
  90. {
  91. //Delete system events.
  92. foreach($aData['handlers'] as $aHandler)
  93. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  94. }
  95. public function activateModuleEvents($aData, $bActivate = true)
  96. {
  97. $iActivate = $bActivate ? 1 : 0;
  98. //Activate (deactivate) system events.
  99. foreach($aData['handlers'] as $aHandler)
  100. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  101. }
  102. public function getHandlers($aParams = array())
  103. {
  104. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  105. $sWhereClause = '';
  106. if(!empty($aParams))
  107. switch($aParams['type']) {
  108. case 'by_group_key_type':
  109. $aMethod['name'] = 'getAllWithKey';
  110. $aMethod['params'][1] = 'type';
  111. $aMethod['params'][2] = array(
  112. 'group' => $aParams['group']
  113. );
  114. $sWhereClause = "AND `group`=:group";
  115. break;
  116. }
  117. $aMethod['params'][0] = "SELECT * FROM `{$this->_sTableHandlers}` WHERE 1 " . $sWhereClause;
  118. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  119. }
  120. public function insertEvent($aParamsSet)
  121. {
  122. if(empty($aParamsSet))
  123. return 0;
  124. $aSet = array();
  125. foreach($aParamsSet as $sKey => $sValue)
  126. $aSet[] = "`" . $sKey . "`=:" . $sKey;
  127. if(!isset($aParamsSet['date']))
  128. $aSet[] = "`date`=UNIX_TIMESTAMP()";
  129. if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet), $aParamsSet) <= 0)
  130. return 0;
  131. return (int)$this->lastId();
  132. }
  133. public function updateEvent($aParamsSet, $aParamsWhere)
  134. {
  135. if(empty($aParamsSet) || empty($aParamsWhere))
  136. return false;
  137. $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  138. return $this->query($sSql);
  139. }
  140. public function deleteEvent($aParams, $sWhereAddon = "")
  141. {
  142. $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
  143. return $this->query($sSql);
  144. }
  145. public function getEvents($aParams, $bReturnCount = false)
  146. {
  147. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  148. $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . $sSelectClause . "
  149. `{$this->_sTable}`.*
  150. FROM `{$this->_sTable}`
  151. LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
  152. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  153. $aEntries = $this->$sMethod($sSql);
  154. if(!$bReturnCount)
  155. return $aEntries;
  156. return array($aEntries, (int)$this->getOne("SELECT FOUND_ROWS()"));
  157. }
  158. protected function _getSqlPartsEvents($aParams)
  159. {
  160. $sMethod = 'getAll';
  161. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  162. switch($aParams['browse']) {
  163. case 'id':
  164. $sMethod = 'getRow';
  165. $sWhereClause = $this->prepareAsString("AND `{$this->_sTable}`.`id`=? ", $aParams['value']);
  166. $sLimitClause = "LIMIT 1";
  167. break;
  168. case 'first':
  169. $sMethod = 'getRow';
  170. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  171. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  172. $sLimitClause = "LIMIT 1";
  173. break;
  174. case 'last':
  175. $sMethod = 'getRow';
  176. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  177. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` ASC";
  178. $sLimitClause = "LIMIT 1";
  179. break;
  180. case 'list':
  181. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  182. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  183. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  184. break;
  185. }
  186. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  187. }
  188. protected function _getSqlPartsEventsList($aParams)
  189. {
  190. $sJoinClause = $sWhereClause = "";
  191. return array($sJoinClause, $sWhereClause);
  192. }
  193. }
  194. /** @} */