1
0

BxBaseModNotificationsDb.php 20 KB

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