BxBaseModNotificationsDb.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. foreach($aData['handlers'] as $aHandler) {
  92. //Delete system events.
  93. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  94. //Delete shared events.
  95. $aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  96. foreach($aEvents as $aEvent) {
  97. $aContent = unserialize($aEvent['content']);
  98. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  99. $this->deleteEvent(array('id' => (int)$aEvent['id']));
  100. }
  101. }
  102. }
  103. public function activateModuleEvents($aData, $bActivate = true)
  104. {
  105. $iActivate = $bActivate ? 1 : 0;
  106. foreach($aData['handlers'] as $aHandler) {
  107. //Activate (deactivate) system events.
  108. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  109. //Activate (deactivate) shared events.
  110. $aEvents = $this->getEvents(array('browse' => 'shared_by_descriptor', 'type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  111. foreach($aEvents as $aEvent) {
  112. $aContent = unserialize($aEvent['content']);
  113. if(isset($aContent['type']) && $aContent['type'] == $aHandler['alert_unit'] && isset($aContent['action']) && $aContent['action'] == $aHandler['alert_action'])
  114. $this->updateEvent(array('active' => $iActivate), array('id' => (int)$aEvent['id']));
  115. }
  116. }
  117. }
  118. public function getHandlers($aParams = array())
  119. {
  120. $sMethod = 'getAll';
  121. $sWhereClause = '';
  122. if(!empty($aParams))
  123. switch($aParams['type']) {}
  124. $sSql = "SELECT
  125. `id` AS `id`,
  126. `type` AS `type`,
  127. `alert_unit` AS `alert_unit`,
  128. `alert_action` AS `alert_action`,
  129. `content` AS `content`
  130. FROM `{$this->_sTableHandlers}`
  131. WHERE 1 " . $sWhereClause;
  132. return $this->$sMethod($sSql);
  133. }
  134. public function insertEvent($aParamsSet)
  135. {
  136. if(empty($aParamsSet))
  137. return 0;
  138. $aSet = array();
  139. foreach($aParamsSet as $sKey => $sValue)
  140. $aSet[] = $this->prepare("`" . $sKey . "`=?", $sValue);
  141. if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet) . ", `date`=UNIX_TIMESTAMP()") <= 0)
  142. return 0;
  143. return (int)$this->lastId();
  144. }
  145. public function updateEvent($aParamsSet, $aParamsWhere)
  146. {
  147. if(empty($aParamsSet) || empty($aParamsWhere))
  148. return false;
  149. $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  150. return $this->query($sSql);
  151. }
  152. public function deleteEvent($aParams, $sWhereAddon = "")
  153. {
  154. $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
  155. return $this->query($sSql);
  156. }
  157. public function getEvents($aParams, $bReturnCount = false)
  158. {
  159. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  160. $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . $sSelectClause . "
  161. `{$this->_sTable}`.*
  162. FROM `{$this->_sTable}`
  163. LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
  164. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  165. $aEntries = $this->$sMethod($sSql);
  166. if(!$bReturnCount)
  167. return $aEntries;
  168. return array($aEntries, (int)$this->getOne("SELECT FOUND_ROWS()"));
  169. }
  170. protected function _getSqlPartsEvents($aParams)
  171. {
  172. $sMethod = 'getAll';
  173. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  174. switch($aParams['browse']) {
  175. case 'id':
  176. $sMethod = 'getRow';
  177. $sWhereClause = $this->prepare("AND `{$this->_sTable}`.`id`=? ", $aParams['value']);
  178. $sLimitClause = "LIMIT 1";
  179. break;
  180. case 'first':
  181. $sMethod = 'getRow';
  182. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  183. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  184. $sLimitClause = "LIMIT 1";
  185. break;
  186. case 'last':
  187. $sMethod = 'getRow';
  188. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  189. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` ASC";
  190. $sLimitClause = "LIMIT 1";
  191. break;
  192. case 'list':
  193. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  194. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  195. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  196. break;
  197. }
  198. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  199. }
  200. protected function _getSqlPartsEventsList($aParams)
  201. {
  202. $sJoinClause = $sWhereClause = "";
  203. return array($sJoinClause, $sWhereClause);
  204. }
  205. }
  206. /** @} */