BxBaseModNotificationsDb.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet) . ", `date`=UNIX_TIMESTAMP()") <= 0)
  126. return 0;
  127. return (int)$this->lastId();
  128. }
  129. public function updateEvent($aParamsSet, $aParamsWhere)
  130. {
  131. if(empty($aParamsSet) || empty($aParamsWhere))
  132. return false;
  133. $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  134. return $this->query($sSql);
  135. }
  136. public function deleteEvent($aParams, $sWhereAddon = "")
  137. {
  138. $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
  139. return $this->query($sSql);
  140. }
  141. public function getEvents($aParams, $bReturnCount = false)
  142. {
  143. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  144. $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . $sSelectClause . "
  145. `{$this->_sTable}`.*
  146. FROM `{$this->_sTable}`
  147. LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
  148. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  149. $aEntries = $this->$sMethod($sSql);
  150. if(!$bReturnCount)
  151. return $aEntries;
  152. return array($aEntries, (int)$this->getOne("SELECT FOUND_ROWS()"));
  153. }
  154. protected function _getSqlPartsEvents($aParams)
  155. {
  156. $sMethod = 'getAll';
  157. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  158. switch($aParams['browse']) {
  159. case 'id':
  160. $sMethod = 'getRow';
  161. $sWhereClause = $this->prepare("AND `{$this->_sTable}`.`id`=? ", $aParams['value']);
  162. $sLimitClause = "LIMIT 1";
  163. break;
  164. case 'first':
  165. $sMethod = 'getRow';
  166. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  167. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  168. $sLimitClause = "LIMIT 1";
  169. break;
  170. case 'last':
  171. $sMethod = 'getRow';
  172. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  173. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` ASC";
  174. $sLimitClause = "LIMIT 1";
  175. break;
  176. case 'list':
  177. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  178. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  179. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  180. break;
  181. }
  182. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  183. }
  184. protected function _getSqlPartsEventsList($aParams)
  185. {
  186. $sJoinClause = $sWhereClause = "";
  187. return array($sJoinClause, $sWhereClause);
  188. }
  189. }
  190. /** @} */