BxBaseModNotificationsDb.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 $_sTableAlias;
  19. protected $_sTableHandlers;
  20. protected $_sTableSettings;
  21. protected $_sTableSettings2Users;
  22. protected $_sHandlerMask;
  23. protected $_aDeliveryTypes;
  24. public function __construct(&$oConfig)
  25. {
  26. parent::__construct($oConfig);
  27. $this->_oConfig = $oConfig;
  28. $this->_sTable = $this->_sPrefix . 'events';
  29. $this->_sTableAlias = '';
  30. $this->_sTableHandlers = $this->_sPrefix . 'handlers';
  31. $this->_sTableSettings = $this->_sPrefix . 'settings';
  32. $this->_sTableSettings2Users = $this->_sPrefix . 'settings2users';
  33. $this->_sHandlerMask = "%s-%s";
  34. $this->_aDeliveryTypes = array(BX_BASE_MOD_NTFS_DTYPE_SITE);
  35. }
  36. public function getTable()
  37. {
  38. return $this->_sTable;
  39. }
  40. public function isTableAlias()
  41. {
  42. return !empty($this->_sTableAlias);
  43. }
  44. public function getTableAlias()
  45. {
  46. return !empty($this->_sTableAlias) ? $this->_sTableAlias : $this->_sTable;
  47. }
  48. public function getAlertHandlerId()
  49. {
  50. $sQuery = $this->prepare("SELECT `id` FROM `sys_alerts_handlers` WHERE `name`=? LIMIT 1", $this->_oConfig->getObject('alert'));
  51. return (int)$this->getOne($sQuery);
  52. }
  53. public function insertData($aData)
  54. {
  55. $aHandlers = array();
  56. $aHandlerDescriptor = $this->_oConfig->getHandlerDescriptor();
  57. //--- Update Timeline Handlers ---//
  58. foreach($aData['handlers'] as $aHandler) {
  59. $sContent = $sPrivacy = '';
  60. $bInsert = $aHandler['type'] == BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT;
  61. if($bInsert) {
  62. if(empty($aHandler['module_class']))
  63. $aHandler['module_class'] = 'Module';
  64. $sContent = serialize(array_intersect_key($aHandler, $aHandlerDescriptor));
  65. $sPrivacy = !empty($aHandler['module_event_privacy']) ? $aHandler['module_event_privacy'] : '';
  66. }
  67. $sQuery = $this->prepare("INSERT INTO
  68. `{$this->_sTableHandlers}`
  69. SET
  70. `group`=?,
  71. `type`=?,
  72. `alert_unit`=?,
  73. `alert_action`=?,
  74. `content`=?,
  75. `privacy`=?", $aHandler['group'], $aHandler['type'], $aHandler['alert_unit'], $aHandler['alert_action'], $sContent, $sPrivacy);
  76. if(!$this->query($sQuery))
  77. continue;
  78. $aHandlers[sprintf($this->_sHandlerMask, $aHandler['alert_unit'], $aHandler['alert_action'])] = (int)$this->lastId();
  79. }
  80. //--- Update Settings ---//
  81. if(!empty($aData['settings']) && is_array($aData['settings']))
  82. foreach($aData['settings'] as $aSetting) {
  83. $sHandler = sprintf($this->_sHandlerMask, $aSetting['unit'], $aSetting['action']);
  84. if(empty($aHandlers[$sHandler]))
  85. continue;
  86. foreach($this->_aDeliveryTypes as $sDeliveryType) {
  87. $iOrder = (int)$this->getSetting(array('by' => 'delivery_max_order', 'delivery' => $sDeliveryType));
  88. foreach($aSetting['types'] as $sType) {
  89. $sTitle = $this->_oConfig->getHandlersActionTitle($aSetting['unit'], $aSetting['action'], $sType);
  90. $this->query("INSERT INTO `{$this->_sTableSettings}` SET `group`=:group, `handler_id`=:handler_id, `type`=:type, `delivery`=:delivery, `title`=:title, `order`=:order", array(
  91. 'group' => $aSetting['group'],
  92. 'handler_id' => (int)$aHandlers[$sHandler],
  93. 'type' => $sType,
  94. 'delivery' => $sDeliveryType,
  95. 'title' => strcmp($sTitle, _t($sTitle)) !== 0 ? $sTitle : '',
  96. 'order' => ++$iOrder
  97. ));
  98. }
  99. }
  100. }
  101. //--- Update System Alerts ---//
  102. $iHandlerId = $this->getAlertHandlerId();
  103. foreach($aData['alerts'] as $aAlert) {
  104. $sQuery = $this->prepare("INSERT INTO
  105. `sys_alerts`
  106. SET
  107. `unit`=?,
  108. `action`=?,
  109. `handler_id`=?", $aAlert['unit'], $aAlert['action'], $iHandlerId);
  110. $this->query($sQuery);
  111. }
  112. }
  113. public function deleteData($aData)
  114. {
  115. $aHandlers = array();
  116. //--- Update Timeline Handlers ---//
  117. foreach($aData['handlers'] as $aHandler) {
  118. $sHandler = sprintf($this->_sHandlerMask, $aHandler['alert_unit'], $aHandler['alert_action']);
  119. $aBindings = array(
  120. 'alert_unit' => $aHandler['alert_unit'],
  121. 'alert_action' => $aHandler['alert_action'],
  122. 'group' => $aHandler['group'],
  123. 'type' => $aHandler['type']
  124. );
  125. $aHandlers[$sHandler] = $this->getOne("SELECT `id` FROM `{$this->_sTableHandlers}` WHERE (`alert_unit`=:alert_unit AND `alert_action`=:alert_action) OR (`group`=:group AND `type`=:type) LIMIT 1", $aBindings);
  126. $this->query("DELETE FROM `{$this->_sTableHandlers}` WHERE (`alert_unit`=:alert_unit AND `alert_action`=:alert_action) OR (`group`=:group AND `type`=:type) LIMIT 1", $aBindings);
  127. }
  128. //--- Update Settings ---//
  129. if(!empty($aData['settings']) && is_array($aData['settings']))
  130. foreach($aData['settings'] as $aSetting) {
  131. $sHandler = sprintf($this->_sHandlerMask, $aSetting['unit'], $aSetting['action']);
  132. if(empty($aHandlers[$sHandler]))
  133. continue;
  134. foreach($aSetting['types'] as $sType)
  135. $this->query("DELETE FROM `ts`, `tsu` USING `{$this->_sTableSettings}` AS `ts` LEFT JOIN `{$this->_sTableSettings2Users}` AS `tsu` ON `ts`.`id`=`tsu`.`setting_id` WHERE `ts`.`handler_id`=:handler_id AND `ts`.`type`=:type", array(
  136. 'handler_id' => (int)$aHandlers[$sHandler],
  137. 'type' => $sType,
  138. ));
  139. }
  140. //--- Update System Alerts ---//
  141. $iHandlerId = $this->getAlertHandlerId();
  142. foreach($aData['alerts'] as $aAlert) {
  143. $sQuery = $this->prepare("DELETE FROM
  144. `sys_alerts`
  145. WHERE
  146. `unit`=? AND
  147. `action`=? AND
  148. `handler_id`=?
  149. LIMIT 1", $aAlert['unit'], $aAlert['action'], $iHandlerId);
  150. $this->query($sQuery);
  151. }
  152. }
  153. public function deleteModuleEvents($aData)
  154. {
  155. //Delete system events.
  156. foreach($aData['handlers'] as $aHandler)
  157. $this->deleteEvent(array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  158. }
  159. public function activateModuleEvents($aData, $bActivate = true)
  160. {
  161. $iActivate = $bActivate ? 1 : 0;
  162. //Activate (deactivate) system events.
  163. foreach($aData['handlers'] as $aHandler)
  164. $this->updateEvent(array('active' => $iActivate), array('type' => $aHandler['alert_unit'], 'action' => $aHandler['alert_action']));
  165. }
  166. public function getHandlers($aParams = array())
  167. {
  168. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  169. $sWhereClause = '';
  170. if(!empty($aParams))
  171. switch($aParams['type']) {
  172. case 'by_type':
  173. $aMethod['params'][1] = array(
  174. 'type' => $aParams['value']
  175. );
  176. $sWhereClause = "AND `type`=:type";
  177. break;
  178. case 'by_group_key_type':
  179. $aMethod['name'] = 'getAllWithKey';
  180. $aMethod['params'][1] = 'type';
  181. $aMethod['params'][2] = array(
  182. 'group' => $aParams['group']
  183. );
  184. $sWhereClause = "AND `group`=:group";
  185. break;
  186. }
  187. $aMethod['params'][0] = "SELECT * FROM `{$this->_sTableHandlers}` WHERE 1 " . $sWhereClause;
  188. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  189. }
  190. public function getSetting($aParams = array())
  191. {
  192. $CNF = &$this->_oConfig->CNF;
  193. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  194. $sSelectClause = "`ts`.*, `th`.`alert_unit` AS `unit`, `th`.`alert_action` AS `action`";
  195. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  196. switch($aParams['by']) {
  197. case 'id':
  198. $aMethod['name'] = 'getRow';
  199. $aMethod['params'][1] = array(
  200. 'id' => $aParams['id']
  201. );
  202. $sWhereClause = " AND `ts`.`id`=:id";
  203. break;
  204. case 'tsu_id':
  205. $aMethod['name'] = 'getRow';
  206. $aMethod['params'][1] = array(
  207. 'id' => $aParams['id']
  208. );
  209. $sJoinClause = "LEFT JOIN `" . $this->_sTableSettings2Users . "` AS `tsu` ON `ts`.`id`=`tsu`.`setting_id`";
  210. $sWhereClause = " AND `tsu`.`id`=:id";
  211. break;
  212. case 'tsu_allowed':
  213. $aMethod['name'] = 'getRow';
  214. $aMethod['params'][1] = array(
  215. 'handler_id' => $aParams['handler_id'],
  216. 'delivery' => $aParams['delivery'],
  217. 'type' => $aParams['type'],
  218. 'user_id' => $aParams['user_id']
  219. );
  220. $sSelectClause = "`ts`.`active` AS `active_adm`, IF(NOT ISNULL(`tsu`.`active`), `tsu`.`active`, `ts`.`value`) AS `active_pnl`";
  221. $sJoinClause = "LEFT JOIN `" . $this->_sTableSettings2Users . "` AS `tsu` ON `ts`.`id`=`tsu`.`setting_id` AND `tsu`.`user_id`=:user_id";
  222. $sWhereClause = " AND `ts`.`handler_id`=:handler_id AND `ts`.`delivery`=:delivery AND `ts`.`type`=:type";
  223. break;
  224. case 'delivery_max_order':
  225. $aMethod['name'] = 'getOne';
  226. $aMethod['params'][1] = array(
  227. 'delivery' => $aParams['delivery'],
  228. );
  229. $sSelectClause = "`ts`.`order`";
  230. $sWhereClause = " AND `ts`.`delivery`=:delivery";
  231. $sOrderClause = "`ts`.`order` DESC";
  232. $sLimitClause = "1";
  233. break;
  234. case 'group_type_delivery':
  235. $aMethod['name'] = 'getColumn';
  236. $aMethod['params'][1] = array(
  237. 'group' => $aParams['group'],
  238. 'delivery' => $aParams['delivery'],
  239. 'type' => $aParams['type']
  240. );
  241. $sSelectClause = "`ts`.`id`";
  242. $sWhereClause = " AND `ts`.`group`=:group AND `ts`.`delivery`=:delivery AND `ts`.`type`=:type";
  243. if($aParams['active'])
  244. $sWhereClause .= " AND `ts`.`active`='1'";
  245. break;
  246. case 'user_id_pairs':
  247. $aMethod['name'] = 'getPairs';
  248. $aMethod['params'][1] = 'id';
  249. $aMethod['params'][2] = 'active';
  250. $aMethod['params'][3] = array(
  251. 'user_id' => $aParams['user_id']
  252. );
  253. $sSelectClause = "`ts`.`id` AS `id`, `tsu`.`active` AS `active`";
  254. $sJoinClause = "LEFT JOIN `" . $this->_sTableSettings2Users . "` AS `tsu` ON `ts`.`id`=`tsu`.`setting_id`";
  255. $sWhereClause = " AND `tsu`.`user_id`=:user_id";
  256. break;
  257. case 'all_active':
  258. $sWhereClause = " AND `ts`.`active`='1'";
  259. break;
  260. case 'all_inactive':
  261. $sWhereClause = " AND `ts`.`active`='0'";
  262. break;
  263. }
  264. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  265. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  266. $aMethod['params'][0] = "SELECT
  267. " . $sSelectClause . "
  268. FROM `" . $this->_sTableSettings . "` AS `ts`
  269. LEFT JOIN `" . $this->_sTableHandlers . "` AS `th` ON `ts`.`handler_id`=`th`.`id` " . $sJoinClause . "
  270. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  271. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  272. }
  273. public function updateSetting($aParamsSet, $aParamsWhere)
  274. {
  275. if(empty($aParamsSet) || empty($aParamsWhere))
  276. return false;
  277. $sSql = "UPDATE `{$this->_sTableSettings}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  278. return $this->query($sSql);
  279. }
  280. public function activateSettingById($bActive, $mixedId)
  281. {
  282. if(!is_array($mixedId))
  283. $mixedId = array($mixedId);
  284. return $this->query("UPDATE `{$this->_sTableSettings}` SET `active`=:active WHERE `id` IN (" . $this->implode_escape($mixedId) . ")", array(
  285. 'active' => (int)$bActive
  286. ));
  287. }
  288. public function activateSettingByIdUser($bActive, $iUserId, $mixedSettingId)
  289. {
  290. if(!is_array($mixedSettingId))
  291. $mixedSettingId = array($mixedSettingId);
  292. return $this->query("UPDATE `{$this->_sTableSettings2Users}` SET `active`=:active WHERE `user_id`=:user_id AND `setting_id` IN (" . $this->implode_escape($mixedSettingId) . ")", array(
  293. 'user_id' => $iUserId,
  294. 'active' => (int)$bActive
  295. ));
  296. }
  297. public function changeSettingById($sField, $mixedValue, $mixedId)
  298. {
  299. if(!is_array($mixedId))
  300. $mixedId = array($mixedId);
  301. return $this->query("UPDATE `{$this->_sTableSettings}` SET `" . $sField . "`=:value WHERE `id` IN (" . $this->implode_escape($mixedId) . ")", array(
  302. 'value' => $mixedValue
  303. ));
  304. }
  305. public function changeSettingByIdUser($sField, $mixedValue, $iUserId, $mixedSettingId)
  306. {
  307. if(!is_array($mixedSettingId))
  308. $mixedSettingId = array($mixedSettingId);
  309. return $this->query("UPDATE `{$this->_sTableSettings2Users}` SET `" . $sField . "`=:value WHERE `user_id`=:user_id AND `setting_id` IN (" . $this->implode_escape($mixedSettingId) . ")", array(
  310. 'user_id' => $iUserId,
  311. 'value' => $mixedValue
  312. ));
  313. }
  314. public function initSettingUser($iUserId)
  315. {
  316. $aSettingsAll = $this->getSetting(array('by' => 'all_active'));
  317. $aSettingsUser = $this->getSetting(array('by' => 'user_id_pairs', 'user_id' => $iUserId));
  318. foreach($aSettingsAll as $aSetting) {
  319. if(isset($aSettingsUser[$aSetting['id']]))
  320. continue;
  321. $this->insertSettingUser(array(
  322. 'user_id' => $iUserId,
  323. 'setting_id' => $aSetting['id'],
  324. 'active' => $aSetting['value']
  325. ));
  326. }
  327. $aSettingsAll = $this->getSetting(array('by' => 'all_inactive'));
  328. foreach($aSettingsAll as $aSetting) {
  329. if(!isset($aSettingsUser[$aSetting['id']]))
  330. continue;
  331. $this->deleteSettingUser(array(
  332. 'user_id' => $iUserId,
  333. 'setting_id' => $aSetting['id']
  334. ));
  335. }
  336. }
  337. public function insertSettingUser($aParamsSet)
  338. {
  339. if(empty($aParamsSet))
  340. return 0;
  341. if((int)$this->query("INSERT INTO `{$this->_sTableSettings2Users}` SET " . $this->arrayToSQL($aParamsSet)) <= 0)
  342. return 0;
  343. return (int)$this->lastId();
  344. }
  345. public function deleteSettingUser($aParamsWhere)
  346. {
  347. if(empty($aParamsWhere))
  348. return false;
  349. return (int)$this->query("DELETE FROM `{$this->_sTableSettings2Users}` WHERE " . $this->arrayToSQL($aParamsWhere, ' AND ')) <= 0;
  350. }
  351. public function insertEvent($aParamsSet)
  352. {
  353. if(empty($aParamsSet))
  354. return 0;
  355. $aSet = array();
  356. foreach($aParamsSet as $sKey => $sValue)
  357. $aSet[] = "`" . $sKey . "`=:" . $sKey;
  358. if(!isset($aParamsSet['date']))
  359. $aSet[] = "`date`=UNIX_TIMESTAMP()";
  360. if((int)$this->query("INSERT INTO `{$this->_sTable}` SET " . implode(", ", $aSet), $aParamsSet) <= 0)
  361. return 0;
  362. return (int)$this->lastId();
  363. }
  364. public function updateEvent($aParamsSet, $aParamsWhere)
  365. {
  366. if(empty($aParamsSet) || empty($aParamsWhere))
  367. return false;
  368. $sSql = "UPDATE `{$this->_sTable}` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND ");
  369. return $this->query($sSql);
  370. }
  371. public function deleteEvent($aParams, $sWhereAddon = "")
  372. {
  373. $sSql = "DELETE FROM `{$this->_sTable}` WHERE " . $this->arrayToSQL($aParams, " AND ") . $sWhereAddon;
  374. return $this->query($sSql);
  375. }
  376. public function getEvents($aParams)
  377. {
  378. $sTableAlias = $this->getTableAlias();
  379. $sFromClause = "`{$this->_sTable}`" . ($this->isTableAlias() ? " AS `" . $sTableAlias . "`" : "");
  380. list($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause) = $this->_getSqlPartsEvents($aParams);
  381. $sSql = "SELECT {select}
  382. FROM {$sFromClause}
  383. LEFT JOIN `{$this->_sTableHandlers}` ON `{$sTableAlias}`.`type`=`{$this->_sTableHandlers}`.`alert_unit` AND `{$sTableAlias}`.`action`=`{$this->_sTableHandlers}`.`alert_action` " . $sJoinClause . "
  384. WHERE 1 " . $sWhereClause . " {order} {limit}";
  385. return $this->$sMethod(str_replace(array('{select}', '{order}', '{limit}'), array($sSelectClause, $sOrderClause, $sLimitClause), $sSql));
  386. }
  387. protected function _getSqlPartsEvents($aParams)
  388. {
  389. $sTableAlias = $this->getTableAlias();
  390. $sMethod = 'getAll';
  391. $sSelectClause = "`{$sTableAlias}`.*";
  392. $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  393. switch($aParams['browse']) {
  394. case 'id':
  395. $sMethod = 'getRow';
  396. $sWhereClause = $this->prepareAsString("AND `{$sTableAlias}`.`id`=? ", $aParams['value']);
  397. $sLimitClause = "LIMIT 1";
  398. break;
  399. case 'first':
  400. $sMethod = 'getRow';
  401. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  402. $sOrderClause = "ORDER BY `{$sTableAlias}`.`date` DESC, `{$sTableAlias}`.`id` DESC";
  403. $sLimitClause = "LIMIT 1";
  404. break;
  405. case 'list':
  406. list($sJoinClause, $sWhereClause) = $this->_getSqlPartsEventsList($aParams);
  407. $sOrderClause = "ORDER BY `{$sTableAlias}`.`date` DESC, `{$sTableAlias}`.`id` DESC";
  408. $sLimitClause = isset($aParams['per_page']) ? "LIMIT " . $aParams['start'] . ", " . $aParams['per_page'] : "";
  409. break;
  410. }
  411. return array($sMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
  412. }
  413. protected function _getSqlPartsEventsList($aParams)
  414. {
  415. $sJoinClause = $sWhereClause = "";
  416. return array($sJoinClause, $sWhereClause);
  417. }
  418. }
  419. /** @} */