BxBaseModNotificationsDb.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. bx_import('BxDolModuleDb');
  12. /*
  13. * Module database queries
  14. */
  15. class BxBaseModNotificationsDb extends BxDolModuleDb
  16. {
  17. protected $_oConfig;
  18. protected $_sTable;
  19. protected $_sTableHandlers;
  20. public function __construct(&$oConfig)
  21. {
  22. parent::__construct($oConfig);
  23. $this->_oConfig = $oConfig;
  24. $this->_sTable = $this->_sPrefix . 'events';
  25. $this->_sTableHandlers = $this->_sPrefix . 'handlers';
  26. }
  27. public function getAlertHandlerId()
  28. {
  29. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name`=? LIMIT 1", $this->_oConfig->getObject('alert'));
  30. return (int)$this->getOne($sQuery);
  31. }
  32. public function insertData($aData)
  33. {
  34. $aHandlerDescriptor = $this->_oConfig->getHandlerDescriptor();
  35. //--- Update Timeline Handlers ---//
  36. foreach($aData['handlers'] as $aHandler) {
  37. $sContent = '';
  38. if($aHandler['type'] == BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT) {
  39. if(empty($aHandler['module_class']))
  40. $aHandler['module_class'] = 'Module';
  41. $sContent = serialize(array_intersect_key($aHandler, $aHandlerDescriptor));
  42. }
  43. $sQuery = $this->prepare("INSERT INTO
  44. `{$this->_sTableHandlers}`
  45. SET
  46. `type`=?,
  47. `alert_unit`=?,
  48. `alert_action`=?,
  49. `content`=?", $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. $sMethod = 'getAll';
  105. $sWhereClause = '';
  106. if(!empty($aParams))
  107. switch($aParams['type']) {}
  108. $sSql = "SELECT
  109. `id` AS `id`,
  110. `type` AS `type`,
  111. `alert_unit` AS `alert_unit`,
  112. `alert_action` AS `alert_action`,
  113. `content` AS `content`
  114. FROM `{$this->_sTableHandlers}`
  115. WHERE 1 " . $sWhereClause;
  116. return $this->$sMethod($sSql);
  117. }
  118. public function insertEvent($aParamsSet)
  119. {
  120. if(empty($aParamsSet))
  121. return 0;
  122. $aSet = array();
  123. foreach($aParamsSet as $sKey => $sValue)
  124. $aSet[] = $this->prepare("`" . $sKey . "`=?", $sValue);
  125. if(empty($aParamsSet['date']))
  126. $aSet[] = "`date`=UNIX_TIMESTAMP()";
  127. if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet)) <= 0)
  128. return 0;
  129. return (int)$this->lastId();
  130. }
  131. public function updateEvent($aParamsSet, $aParamsWhere)
  132. {
  133. if(empty($aParamsSet) || empty($aParamsWhere))
  134. return false;
  135. $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  136. return $this->query($sSql);
  137. }
  138. public function deleteEvent($aParams, $sWhereAddon = "")
  139. {
  140. $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
  141. return $this->query($sSql);
  142. }
  143. public function getEvents($aParams, $bReturnCount = false)
  144. {
  145. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  146. $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . $sSelectClause . "
  147. `{$this->_sTable}`.*
  148. FROM `{$this->_sTable}`
  149. LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
  150. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  151. $aEntries = $this->$sMethod($sSql);
  152. if(!$bReturnCount)
  153. return $aEntries;
  154. return array($aEntries, (int)$this->getOne("SELECT FOUND_ROWS()"));
  155. }
  156. protected function _getSqlPartsEvents($aParams)
  157. {
  158. $sMethod = 'getAll';
  159. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  160. switch($aParams['browse']) {
  161. case 'id':
  162. $sMethod = 'getRow';
  163. $sWhereClause = $this->prepare("AND `{$this->_sTable}`.`id`=? ", $aParams['value']);
  164. $sLimitClause = "LIMIT 1";
  165. break;
  166. case 'first':
  167. $sMethod = 'getRow';
  168. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  169. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  170. $sLimitClause = "LIMIT 1";
  171. break;
  172. case 'last':
  173. $sMethod = 'getRow';
  174. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  175. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` ASC";
  176. $sLimitClause = "LIMIT 1";
  177. break;
  178. case 'list':
  179. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  180. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  181. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  182. break;
  183. }
  184. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  185. }
  186. protected function _getSqlPartsEventsList($aParams)
  187. {
  188. $sJoinClause = $sWhereClause = "";
  189. return array($sJoinClause, $sWhereClause);
  190. }
  191. }
  192. /** @} */