BxBaseModNotificationsDb.php 8.0 KB

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