BxRemindersDb.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Reminders Reminders
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxRemindersDb extends BxBaseModGeneralDb
  15. {
  16. function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function getProfiles($mixedProfileType)
  21. {
  22. if(!is_array($mixedProfileType))
  23. $mixedProfileType = array($mixedProfileType);
  24. return $this->getAll("SELECT `id`, `content_id` FROM `sys_profiles` WHERE `type` IN (" . $this->implode_escape($mixedProfileType) . ")");
  25. }
  26. public function getType($aParams)
  27. {
  28. $CNF = &$this->_oConfig->CNF;
  29. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  30. $sWhereClause = $sLimitClause = '';
  31. switch($aParams['type']) {
  32. case 'id':
  33. $aMethod['name'] = 'getRow';
  34. $aMethod['params'][1] = array(
  35. 'id' => $aParams['id']
  36. );
  37. $sWhereClause = " AND `id`=:id";
  38. $sLimitClause = " LIMIT 1";
  39. break;
  40. case 'all':
  41. $aMethod['params'][1] = array();
  42. if(isset($aParams['personal'])) {
  43. $aMethod['params'][1]['personal'] = $aParams['personal'];
  44. $sWhereClause .= " AND `personal`=:personal";
  45. }
  46. if(isset($aParams['active'])) {
  47. $aMethod['params'][1]['active'] = $aParams['active'];
  48. $sWhereClause .= " AND `active`=:active";
  49. }
  50. break;
  51. }
  52. $aMethod['params'][0] = "SELECT * FROM `" . $CNF['TABLE_TYPES'] . "` WHERE 1 " . $sWhereClause . $sLimitClause;
  53. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  54. }
  55. public function getEntry($aParams)
  56. {
  57. $CNF = &$this->_oConfig->CNF;
  58. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  59. $sSelectClause = "`te`.*";
  60. $sJoinClause = $sWhereClause = $sLimitClause = "";
  61. switch($aParams['type']) {
  62. case 'id':
  63. $aMethod['name'] = 'getRow';
  64. $aMethod['params'][1] = array(
  65. 'id' => $aParams['id']
  66. );
  67. if(isset($aParams['full']) && $aParams['full'] === true) {
  68. $sSelectClause .= ", `tt`.`text` AS `text`, `tt`.`link` AS `link`";
  69. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_TYPES'] . "` AS `tt` ON `te`.`type_id`=`tt`.`id`";
  70. }
  71. $sWhereClause = " AND `te`.`id`=:id";
  72. $sLimitClause = " LIMIT 1";
  73. break;
  74. case 'reminder':
  75. $aMethod['name'] = 'getRow';
  76. $aMethod['params'][1] = array(
  77. 'type_id' => $aParams['type_id'],
  78. 'rmd_pid' => $aParams['rmd_pid']
  79. );
  80. $sWhereClause = " AND `te`.`type_id`=:type_id AND `te`.`rmd_pid`=:rmd_pid";
  81. if(isset($aParams['cnt_pid'])) {
  82. $aMethod['params'][1]['cnt_pid'] = (int)$aParams['cnt_pid'];
  83. $sWhereClause .= " AND `te`.`cnt_pid`=:cnt_pid";
  84. }
  85. if(isset($aParams['expired'])) {
  86. $aMethod['params'][1]['expired'] = (int)$aParams['expired'];
  87. $sWhereClause .= " AND `te`.`expired`=:expired";
  88. }
  89. $sLimitClause = " LIMIT 1";
  90. break;
  91. case 'rmd_pid':
  92. $aMethod['params'][1] = array(
  93. 'rmd_pid' => $aParams['rmd_pid'],
  94. 'active' => isset($aParams['active']) ? (int)$aParams['active'] : 1
  95. );
  96. if(isset($aParams['full']) && $aParams['full'] === true) {
  97. $sSelectClause .= ", `tt`.`text` AS `text`, `tt`.`link` AS `link`";
  98. $sJoinClause = "LEFT JOIN `" . $CNF['TABLE_TYPES'] . "` AS `tt` ON `te`.`type_id`=`tt`.`id`";
  99. }
  100. $sWhereClause .= " AND `te`.`rmd_pid`=:rmd_pid AND `te`.`active`=:active";
  101. if(isset($aParams['visible'])) {
  102. $aMethod['params'][1]['visible'] = (int)$aParams['visible'];
  103. $sWhereClause .= " AND `te`.`visible`=:visible";
  104. }
  105. break;
  106. }
  107. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_ENTRIES'] . "` AS `te` " . $sJoinClause . " WHERE 1 " . $sWhereClause . $sLimitClause;
  108. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  109. }
  110. public function isEntrySystem($iTypeId, $iRmdPid)
  111. {
  112. $CNF = &$this->_oConfig->CNF;
  113. $aEntry = $this->getEntry(array('type' => 'reminder', 'type_id' => $iTypeId, 'rmd_pid' => $iRmdPid));
  114. if(empty($aEntry) || !is_array($aEntry))
  115. return false;
  116. $aEntry['notified'] = !empty($aEntry['notified']) ? unserialize($aEntry['notified']) : array();
  117. return (int)$aEntry['id'];
  118. }
  119. public function isEntryPersonal($iTypeId, $iRmdPid, $iCntPid, $iExpired)
  120. {
  121. $CNF = &$this->_oConfig->CNF;
  122. $aEntry = $this->getEntry(array('type' => 'reminder', 'type_id' => $iTypeId, 'rmd_pid' => $iRmdPid, 'cnt_pid' => $iCntPid, 'expired' => $iExpired));
  123. if(empty($aEntry) || !is_array($aEntry))
  124. return false;
  125. $aEntry['notified'] = !empty($aEntry['notified']) ? unserialize($aEntry['notified']) : array();
  126. return $aEntry;
  127. }
  128. public function insertEntry($aValues)
  129. {
  130. $CNF = &$this->_oConfig->CNF;
  131. if(empty($aValues) || !is_array($aValues))
  132. return false;
  133. return (int)$this->query("INSERT INTO `" . $CNF['TABLE_ENTRIES'] . "` SET " . $this->arrayToSQL($aValues)) > 0 ? $this->lastId() : false;
  134. }
  135. public function updateEntry($aValues, $aWhere)
  136. {
  137. $CNF = &$this->_oConfig->CNF;
  138. if(empty($aValues) || !is_array($aValues) || empty($aWhere) || !is_array($aWhere))
  139. return false;
  140. return (int)$this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET " . $this->arrayToSQL($aValues) . " WHERE " . $this->arrayToSQL($aWhere, ' AND ')) > 0;
  141. }
  142. public function deleteEntry($mixedId)
  143. {
  144. $CNF = &$this->_oConfig->CNF;
  145. if(!is_array($mixedId))
  146. $mixedId = array($mixedId);
  147. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `id` IN (" . $this->implode_escape($mixedId) . ")") > 0;
  148. }
  149. public function deleteEntryExpired($iDays)
  150. {
  151. $CNF = &$this->_oConfig->CNF;
  152. $sWhereClause = "";
  153. if($iDays > 0)
  154. $sWhereClause = $this->prepareAsString("`expired` < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? DAY))", $iDays);
  155. else if($iDays == 0)
  156. $sWhereClause = $this->prepareAsString("`expired` < UNIX_TIMESTAMP()", $iDays);
  157. else
  158. return true;
  159. return (int)$this->query("DELETE FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE " . $sWhereClause) > 0;
  160. }
  161. }
  162. /** @} */