BxRemindersCron.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. bx_import('BxDolPrivacy');
  12. class BxRemindersCron extends BxDolCron
  13. {
  14. protected $_sModule;
  15. protected $_oModule;
  16. public function __construct()
  17. {
  18. $this->_sModule = 'bx_reminders';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. parent::__construct();
  21. }
  22. function processing()
  23. {
  24. $CNF = &$this->_oModule->_oConfig->CNF;
  25. //--- Delete expired reminders.
  26. $iExpireDays = (int)$this->_oModule->_oDb->getParam($CNF['PARAM_DELETE_AFTER']);
  27. if($iExpireDays >= 0)
  28. $this->_oModule->_oDb->deleteEntryExpired($iExpireDays);
  29. //--- Check and Add new reminders.
  30. $iSystemProfileId = $this->_oModule->_oConfig->getSystemProfileId();
  31. $aTypesSystem = $this->_oModule->_oDb->getType(array('type' => 'all', 'personal' => 0, 'active' => 1));
  32. $bTypesSystem = !empty($aTypesSystem) && is_array($aTypesSystem);
  33. $aTypesPersonal = $this->_oModule->_oDb->getType(array('type' => 'all', 'personal' => 1, 'active' => 1));
  34. $bTypesPersonal = !empty($aTypesPersonal) && is_array($aTypesPersonal);
  35. if(!$bTypesSystem && !$bTypesPersonal)
  36. return;
  37. $iNow = time();
  38. list($iNowYear, $iNowMonth, $iNowDay) = explode('-', date('Y-m-j'));
  39. /**
  40. * Checkes whether some of system reminders
  41. * can be shown today or not.
  42. */
  43. if($bTypesSystem) {
  44. foreach($aTypesSystem as $iKey => $aType) {
  45. list($iMonth, $iDay) = explode('-', $aType['when']);
  46. $aTypesSystem[$iKey]['expired'] = mktime(0, 0, 0, $iMonth, $iDay, $iNowYear);
  47. /**
  48. * Check whether system reminder should be shown or not.
  49. */
  50. $iDays = (int)$aType['show'];
  51. $aTypesSystem[$iKey]['show'] = false;
  52. if($iDays != 0 && $this->_checkDateBegin($iNowYear . '-' . $aType['when'], $iDays, $iNowMonth, $iNowDay))
  53. $aTypesSystem[$iKey]['show'] = $iDays;
  54. /**
  55. * Check whether a notification about system reminder
  56. * should be sent or not.
  57. */
  58. $aDays = explode($CNF['PARAM_DAYS_DELIMITER'], $aType['notify']);
  59. $aTypesSystem[$iKey]['notify'] = false;
  60. if(!empty($aDays) && is_array($aDays))
  61. foreach($aDays as $iDays) {
  62. $iDays = (int)$iDays;
  63. if($iDays != 0 && $this->_checkDateBegin($iNowYear . '-' . $aType['when'], $iDays, $iNowMonth, $iNowDay)) {
  64. $aTypesSystem[$iKey]['notify'] = $iDays;
  65. break;
  66. }
  67. }
  68. if($aTypesSystem[$iKey]['show'] === false && $aTypesSystem[$iKey]['notify'] === false)
  69. unset($aTypesSystem[$iKey]);
  70. }
  71. $bTypesSystem = !empty($aTypesSystem) && is_array($aTypesSystem);
  72. }
  73. /**
  74. * Get all profiles from profile based modules
  75. * and start to show reminders and/or notify about them.
  76. */
  77. $aProfileModules = array();
  78. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  79. foreach($aModules as $aModule)
  80. if(BxDolRequest::serviceExists($aModule['name'], 'act_as_profile') && bx_srv($aModule['name'], 'act_as_profile'))
  81. $aProfileModules[] = $aModule['name'];
  82. $aProfiles = $this->_oModule->_oDb->getProfiles($aProfileModules);
  83. foreach($aProfiles as $aProfile) {
  84. $iProfileId = (int)$aProfile['id'];
  85. $oProfile = BxDolProfile::getInstance($iProfileId);
  86. if(!$oProfile)
  87. continue;
  88. //--- Process personal(related to followed friends) reminders.
  89. if($bTypesPersonal) {
  90. $aFriendsIds = $this->_getFriendsIds($iProfileId);
  91. foreach($aFriendsIds as $iFriendId) {
  92. $oFriend = BxDolProfile::getInstance($iFriendId);
  93. if(!$oFriend)
  94. continue;
  95. $aFriend = BxDolService::call($oFriend->getModule(), 'get_content_info_by_id', array($oFriend->getContentId()));
  96. if(empty($aFriend) || !is_array($aFriend))
  97. continue;
  98. foreach($aTypesPersonal as $aType) {
  99. $iTypeId = (int)$aType['id'];
  100. $sTypePostfix = bx_gen_method_name($aType['name']);
  101. $sMethod = '_checkDateBegin' . $sTypePostfix;
  102. if(!method_exists($this, $sMethod))
  103. continue;
  104. $aEntryNew = array(
  105. 'type_id' => $iTypeId,
  106. 'rmd_pid' => $iProfileId,
  107. 'cnt_pid' => $iFriendId,
  108. 'params' => '',
  109. 'active' => 1,
  110. 'visible' => 1,
  111. 'added' => $iNow,
  112. 'expired' => 0
  113. );
  114. //--- Perform Show.
  115. $iDays = (int)$aType['show'];
  116. if($iDays != 0 && ($aBeginDates = $this->$sMethod($oFriend, $aFriend, $iDays, $iNowYear, $iNowMonth, $iNowDay)) !== false)
  117. foreach($aBeginDates as $aBeginDate) {
  118. $aEntry = $this->_oModule->_oDb->isEntryPersonal($iTypeId, $iProfileId, $iFriendId, $aBeginDate['expired']);
  119. if($aEntry === false)
  120. $this->_oModule->_oDb->insertEntry(array_merge($aEntryNew, array(
  121. 'params' => serialize($aBeginDate['params']),
  122. 'visible' => 1,
  123. 'expired' => $aBeginDate['expired']
  124. )));
  125. else if((int)$aEntry['visible'] == 0)
  126. $this->_oModule->_oDb->updateEntry(array('visible' => 1), array('id' => (int)$aEntry['id']));
  127. }
  128. //--- Perform Notify.
  129. $aDays = explode($CNF['PARAM_DAYS_DELIMITER'], $aType['notify']);
  130. if(empty($aDays) || !is_array($aDays))
  131. continue;
  132. foreach($aDays as $iDays) {
  133. $iDays = (int)$iDays;
  134. if(empty($iDays))
  135. continue;
  136. $aBeginDates = $this->$sMethod($oFriend, $aFriend, $iDays, $iNowYear, $iNowMonth, $iNowDay);
  137. if($aBeginDates === false)
  138. continue;
  139. foreach($aBeginDates as $aBeginDate) {
  140. $iExpired = (int)$aBeginDate['expired'];
  141. $iEntry = 0;
  142. $aEntry = $this->_oModule->_oDb->isEntryPersonal($iTypeId, $iProfileId, $iFriendId, $iExpired);
  143. if($aEntry === false)
  144. $iEntry = $this->_oModule->_oDb->insertEntry(array_merge($aEntryNew, array(
  145. 'params' => serialize($aBeginDate['params']),
  146. 'visible' => 0,
  147. 'expired' => $iExpired
  148. )));
  149. else
  150. $iEntry = (int)$aEntry['id'];
  151. /**
  152. * Stop the process if there is no an entry to notify about.
  153. */
  154. if(empty($iEntry))
  155. continue;
  156. /*
  157. * Stop the process if the action was already porformed.
  158. */
  159. $bNotified = !empty($aEntry['notified']) && is_array($aEntry['notified']);
  160. if($bNotified && array_key_exists($iExpired, $aEntry['notified']) && in_array($iDays, $aEntry['notified'][$iExpired]))
  161. continue;
  162. bx_alert($this->_oModule->getName(), 'added', $iEntry, $iFriendId, array(
  163. 'object_author_id' => $iProfileId,
  164. 'privacy_view' => BX_DOL_PG_ALL
  165. ));
  166. $aNotified = $bNotified ? $aEntry['notified'] : array();
  167. if(!isset($aNotified[$iExpired]))
  168. $aNotified[$iExpired] = array();
  169. $aNotified[$iExpired][] = $iDays;
  170. $this->_oModule->_oDb->updateEntry(array('notified' => serialize($aNotified)), array('id' => $iEntry));
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. //--- Process system (related to holidays) reminders.
  178. if($bTypesSystem) {
  179. foreach($aTypesSystem as $aType) {
  180. $iTypeId = (int)$aType['id'];
  181. $aEntryNew = array(
  182. 'type_id' => $iTypeId,
  183. 'rmd_pid' => $iProfileId,
  184. 'cnt_pid' => 0,
  185. 'params' => '',
  186. 'active' => 1,
  187. 'visible' => 1,
  188. 'added' => $iNow,
  189. 'expired' => 0
  190. );
  191. //--- Perform Show.
  192. if($aType['show'] !== false)
  193. if($this->_oModule->_oDb->isEntrySystem($iTypeId, $iProfileId) === false)
  194. $this->_oModule->_oDb->insertEntry(array_merge($aEntryNew, array(
  195. 'visible' => 1,
  196. 'expired' => $aType['expired']
  197. )));
  198. //--- Perform Notify.
  199. if($aType['notify'] !== false) {
  200. $iDays = (int)$aType['notify'];
  201. $iExpired = (int)$aType['expired'];
  202. $iEntry = 0;
  203. $aEntry = $mResultExists = $this->_oModule->_oDb->isEntrySystem($iTypeId, $iProfileId);
  204. if($aEntry === false)
  205. $iEntry = $this->_oModule->_oDb->insertEntry(array_merge($aEntryNew, array(
  206. 'visible' => 0,
  207. 'expired' => $iExpired
  208. )));
  209. else
  210. $iEntry = (int)$aEntry['id'];
  211. /**
  212. * Stop the process if there is no an entry to notify about.
  213. */
  214. if(empty($iEntry))
  215. continue;
  216. /*
  217. * Stop the process if the action was already porformed.
  218. */
  219. $bNotified = !empty($aEntry['notified']) && is_array($aEntry['notified']);
  220. if($bNotified && array_key_exists($iExpired, $aEntry['notified']) && in_array($iDays, $aEntry['notified'][$iExpired]))
  221. continue;
  222. bx_alert($this->_oModule->getName(), 'added', $iEntry, $iSystemProfileId, array(
  223. 'object_author_id' => $iProfileId,
  224. 'privacy_view' => BX_DOL_PG_ALL
  225. ));
  226. $aNotified = $bNotified ? $aEntry['notified'] : array();
  227. if(!isset($aNotified[$iExpired]))
  228. $aNotified[$iExpired] = array();
  229. $aNotified[$iExpired][] = $iDays;
  230. $this->_oModule->_oDb->updateEntry(array('notified' => serialize($aNotified)), array('id' => $iEntry));
  231. }
  232. }
  233. }
  234. }
  235. }
  236. protected function _checkDateBeginBirthday(&$oProfile, &$aProfile, $iDays, $iNowYear, $iNowMonth, $iNowDay)
  237. {
  238. if(!isset($aProfile['birthday']))
  239. return false;
  240. $mResult = $this->_checkDate($oProfile, $aProfile['birthday'], $iDays, $iNowYear, $iNowMonth, $iNowDay);
  241. if(!$mResult)
  242. return $mResult;
  243. return array($mResult);
  244. }
  245. protected function _checkDateBeginAnniversary(&$oProfile, &$aProfile, $iDays, $iNowYear, $iNowMonth, $iNowDay)
  246. {
  247. $CNF = &$this->_oModule->_oConfig->CNF;
  248. $aDates = array();
  249. bx_alert($this->_oModule->getName(), 'get_important_dates', 0, 0, array(
  250. 'profile_id' => $oProfile->id(),
  251. 'profile_content_info' => $aProfile,
  252. 'days' => $iDays,
  253. 'now_year' => $iNowYear,
  254. 'now_month' => $iNowMonth,
  255. 'now_day' => $iNowDay,
  256. 'override_result' => &$aDates
  257. ));
  258. if(empty($aDates) || !is_array($aDates))
  259. return false;
  260. $aResults = array();
  261. foreach($aDates as $aDate)
  262. if(($aResult = $this->_checkDate($oProfile, date('Y-m-d', $aDate['important_dates_date_start']), $iDays, $iNowYear, $iNowMonth, $iNowDay)) !== false) {
  263. $aResult['params']['title'] = $aDate['important_dates_description'];
  264. $aResults[] = $aResult;
  265. }
  266. return $aResults;
  267. }
  268. protected function _checkDate(&$oProfile, $sDate, $iDays, $iNowYear, $iNowMonth, $iNowDay)
  269. {
  270. $bResult = $this->_checkDateBegin($sDate, $iDays, $iNowMonth, $iNowDay);
  271. if(!$bResult)
  272. return $bResult;
  273. list($iYear, $iMonth, $iDay) = explode('-', $sDate);
  274. $iDate = mktime(0, 0, 0, $iMonth, $iDay, $iNowYear);
  275. return array(
  276. 'params' => array(
  277. 'profile_id' => $oProfile->id(),
  278. 'profile_name' => $oProfile->getDisplayName(),
  279. 'profile_link' => $oProfile->getUrl(),
  280. 'date' => bx_time_js($iDate)
  281. ),
  282. 'expired' => $iDate
  283. );
  284. }
  285. protected function _checkDateBegin($sDate, $iDays, $iNowMonth, $iNowDay)
  286. {
  287. list($iMonth, $iDay) = explode('-', date('m-d', strtotime("-" . $iDays . " days", strtotime($sDate))));
  288. return (int)$iMonth == (int)$iNowMonth && (int)$iDay == (int)$iNowDay;
  289. }
  290. /**
  291. * Returns followed friends.
  292. */
  293. protected function _getFriendsIds($iProfileId)
  294. {
  295. $CNF = &$this->_oModule->_oConfig->CNF;
  296. $aFriends = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS_FRD'])->getConnectedContent($iProfileId, true);
  297. $aSubscriptions = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS_SBN'])->getConnectedContent($iProfileId);
  298. return array_intersect($aFriends, $aSubscriptions);
  299. }
  300. }
  301. /** @} */