BxBaseModNotificationsDb.php 19 KB

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