BxBaseModNotificationsDb.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. `type`=?,
  46. `alert_unit`=?,
  47. `alert_action`=?,
  48. `content`=?", $aHandler['type'], $aHandler['alert_unit'], $aHandler['alert_action'], $sContent);
  49. $this->query($sQuery);
  50. }
  51. //--- Update System Alerts ---//
  52. $iHandlerId = $this->getAlertHandlerId();
  53. foreach($aData['alerts'] as $aAlert) {
  54. $sQuery = $this->prepare("INSERT INTO
  55. `sys_alerts`
  56. SET
  57. `unit`=?,
  58. `action`=?,
  59. `handler_id`=?", $aAlert['unit'], $aAlert['action'], $iHandlerId);
  60. $this->query($sQuery);
  61. }
  62. }
  63. public function deleteData($aData)
  64. {
  65. //--- Update Timeline Handlers ---//
  66. foreach($aData['handlers'] as $aHandler) {
  67. $sQuery = $this->prepare("DELETE FROM
  68. `{$this->_sTableHandlers}`
  69. WHERE
  70. `alert_unit`=? AND
  71. `alert_action`=?
  72. LIMIT 1", $aHandler['alert_unit'], $aHandler['alert_action']);
  73. $this->query($sQuery);
  74. }
  75. //--- Update System Alerts ---//
  76. $iHandlerId = $this->getAlertHandlerId();
  77. foreach($aData['alerts'] as $aAlert) {
  78. $sQuery = $this->prepare("DELETE FROM
  79. `sys_alerts`
  80. WHERE
  81. `unit`=? AND
  82. `action`=? AND
  83. `handler_id`=?
  84. LIMIT 1", $aAlert['unit'], $aAlert['action'], $iHandlerId);
  85. $this->query($sQuery);
  86. }
  87. }
  88. public function deleteModuleEvents($aData)
  89. {
  90. //Delete system events.
  91. foreach($aData['handlers'] as $aHandler)
  92. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  93. }
  94. public function activateModuleEvents($aData, $bActivate = true)
  95. {
  96. $iActivate = $bActivate ? 1 : 0;
  97. //Activate (deactivate) system events.
  98. foreach($aData['handlers'] as $aHandler)
  99. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  100. }
  101. public function getHandlers($aParams = array())
  102. {
  103. $sMethod = 'getAll';
  104. $sWhereClause = '';
  105. if(!empty($aParams))
  106. switch($aParams['type']) {}
  107. $sSql = "SELECT
  108. `id` AS `id`,
  109. `type` AS `type`,
  110. `alert_unit` AS `alert_unit`,
  111. `alert_action` AS `alert_action`,
  112. `content` AS `content`
  113. FROM `{$this->_sTableHandlers}`
  114. WHERE 1 " . $sWhereClause;
  115. return $this->$sMethod($sSql);
  116. }
  117. public function insertEvent($aParamsSet)
  118. {
  119. if(empty($aParamsSet))
  120. return 0;
  121. $aSet = array();
  122. foreach($aParamsSet as $sKey => $sValue)
  123. $aSet[] = $this->prepare("`" . $sKey . "`=?", $sValue);
  124. if(!isset($aParamsSet['date']))
  125. $aSet[] = "`date`=UNIX_TIMESTAMP()";
  126. if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet)) <= 0)
  127. return 0;
  128. return (int)$this->lastId();
  129. }
  130. public function updateEvent($aParamsSet, $aParamsWhere)
  131. {
  132. if(empty($aParamsSet) || empty($aParamsWhere))
  133. return false;
  134. $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  135. return $this->query($sSql);
  136. }
  137. public function deleteEvent($aParams, $sWhereAddon = "")
  138. {
  139. $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
  140. return $this->query($sSql);
  141. }
  142. public function getEvents($aParams, $bReturnCount = false)
  143. {
  144. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  145. $sSql = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . $sSelectClause . "
  146. `{$this->_sTable}`.*
  147. FROM `{$this->_sTable}`
  148. LEFT JOIN `{$this->_sTableHandlers}` ON `{$this->_sTable}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$this->_sTable}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
  149. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  150. $aEntries = $this->$sMethod($sSql);
  151. if(!$bReturnCount)
  152. return $aEntries;
  153. return array($aEntries, (int)$this->getOne("SELECT FOUND_ROWS()"));
  154. }
  155. protected function _getSqlPartsEvents($aParams)
  156. {
  157. $sMethod = 'getAll';
  158. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  159. switch($aParams['browse']) {
  160. case 'id':
  161. $sMethod = 'getRow';
  162. $sWhereClause = $this->prepare("AND `{$this->_sTable}`.`id`=? ", $aParams['value']);
  163. $sLimitClause = "LIMIT 1";
  164. break;
  165. case 'first':
  166. $sMethod = 'getRow';
  167. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  168. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  169. $sLimitClause = "LIMIT 1";
  170. break;
  171. case 'last':
  172. $sMethod = 'getRow';
  173. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  174. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` ASC";
  175. $sLimitClause = "LIMIT 1";
  176. break;
  177. case 'list':
  178. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  179. $sOrderClause = "ORDER BY `{$this->_sTable}`.`date` DESC";
  180. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  181. break;
  182. }
  183. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  184. }
  185. protected function _getSqlPartsEventsList($aParams)
  186. {
  187. $sJoinClause = $sWhereClause = "";
  188. return array($sJoinClause, $sWhereClause);
  189. }
  190. }
  191. /** @} */