api.dreamkasnotifications.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * DreamKas notification area
  4. */
  5. class DreamKasNotifications {
  6. /**
  7. * UbillingConfig object placeholder
  8. *
  9. * @var null
  10. */
  11. protected $ubConfig = null;
  12. /**
  13. * UbillingCache instance placeholder
  14. *
  15. * @var null
  16. */
  17. protected $ubCache = null;
  18. /**
  19. * Placeholder for DREAMKAS_NOTIFICATIONS_ENABLED alter.ini option
  20. *
  21. * @var bool
  22. */
  23. protected $notysEnabled = false;
  24. /**
  25. * Placeholder for DREAMKAS_CACHE_CHECK_INTERVAL alter.ini option
  26. *
  27. * @var int
  28. */
  29. protected $notysPollingInterval = 8000;
  30. /**
  31. * Placeholder for DREAMKAS_POPUP_TIMEOUT alter.ini option
  32. *
  33. * @var int
  34. */
  35. protected $notysPopupTimeout = 10000;
  36. /**
  37. * Placeholder for DREAMKAS_NOTIFY_ANYWHERE alter.ini option
  38. *
  39. * @var bool
  40. */
  41. protected $notysEverywhere = true;
  42. /**
  43. * Placeholder for DREAMKAS_DESKTOP_NOTIFICATIONS alter.ini option
  44. *
  45. * @var bool
  46. */
  47. protected $notysOnDesktop = false;
  48. /**
  49. * Placeholder for DREAMKAS_ADMINS_ALLOWED alter.ini option
  50. *
  51. * @var array
  52. */
  53. protected $notysAdminsAllowed = array();
  54. /**
  55. * Caching timeout based on polling interval in seconds.
  56. *
  57. * @var int
  58. */
  59. protected $cachingTimeout = 8;
  60. /**
  61. * Contains current instance admin user login
  62. *
  63. * @var string
  64. */
  65. protected $curAdminLogin = '';
  66. const URL_NOTIFICATIONS = '?module=dreamkas&getnotys=true';
  67. const DREAMKAS_NOTYS_CAHCE_KEY = 'DREAMKAS_NOTIFICATIONS';
  68. public function __construct() {
  69. global $ubillingConfig;
  70. $this->ubConfig = $ubillingConfig;
  71. $this->ubCache = new UbillingCache();
  72. $this->loadOptions();
  73. }
  74. /**
  75. * Getting an alter.ini options
  76. *
  77. * @return void
  78. */
  79. protected function loadOptions() {
  80. $this->notysEnabled = wf_getBoolFromVar($this->ubConfig->getAlterParam('DREAMKAS_NOTIFICATIONS_ENABLED'));
  81. $this->notysPollingInterval = ($this->ubConfig->getAlterParam('DREAMKAS_CACHE_CHECK_INTERVAL')) ? $this->ubConfig->getAlterParam('DREAMKAS_CACHE_CHECK_INTERVAL') * 1000 : 8000;
  82. $this->cachingTimeout = ($this->ubConfig->getAlterParam('DREAMKAS_CACHE_CHECK_INTERVAL')) ? $this->ubConfig->getAlterParam('DREAMKAS_CACHE_CHECK_INTERVAL') : 8;
  83. $this->notysPopupTimeout = ($this->ubConfig->getAlterParam('DREAMKAS_POPUP_TIMEOUT')) ? $this->ubConfig->getAlterParam('DREAMKAS_POPUP_TIMEOUT') * 1000 : 10000;
  84. $this->notysEverywhere = wf_getBoolFromVar($this->ubConfig->getAlterParam('DREAMKAS_NOTIFY_ANYWHERE'));
  85. $this->notysOnDesktop = wf_getBoolFromVar($this->ubConfig->getAlterParam('DREAMKAS_DESKTOP_NOTIFICATIONS'));
  86. $this->notysAdminsAllowed = explode(',', str_replace(' ', '', $this->ubConfig->getAlterParam('DREAMKAS_ADMINS_ALLOWED')));
  87. $this->notysAdminsAllowed = array_flip($this->notysAdminsAllowed);
  88. }
  89. public function getDreamkasNotifications() {
  90. $noty = array();
  91. $count = 0;
  92. $notyCached = $this->ubCache->get(self::DREAMKAS_NOTYS_CAHCE_KEY, $this->cachingTimeout);
  93. if (!empty($notyCached)) {
  94. foreach ($notyCached as $eachNoty) {
  95. $notificationText = wf_tag('div', false, 'dreamkastext');
  96. $notificationText .= wf_tag('span', false, 'dreamkastitle') . $eachNoty['title'] . wf_tag('span', true);
  97. $notificationText .= wf_delimiter() . $eachNoty['text'];
  98. $notificationText .= wf_tag('div', true);
  99. $noty[$count]['text'] = $notificationText;
  100. $noty[$count]['type'] = $eachNoty['type'];
  101. $noty[$count]['index'] = $count;
  102. $count++;
  103. }
  104. $this->ubCache->delete(self::DREAMKAS_NOTYS_CAHCE_KEY);
  105. }
  106. die(json_encode($noty));
  107. }
  108. /**
  109. * Returns notification frontend with some background polling
  110. *
  111. * @return string
  112. */
  113. protected function getDreamkasNotificationsJS() {
  114. $result = '';
  115. //some custom style
  116. $result .= wf_tag('style');
  117. //this style is inline for preventing of css caching
  118. $result .= '
  119. #noty_layout__bottomRight {
  120. width: 480px !important;
  121. }
  122. .dreamkastext {
  123. float: left;
  124. display: block;
  125. font-size: 11pt;
  126. margin: 10px 1px;
  127. }
  128. .dreamkastitle {
  129. font-weight: 700;
  130. }
  131. ';
  132. if ($this->notysOnDesktop) {
  133. $result .= '
  134. #noty_layout__bottomRight {
  135. margin-bottom: 120px !important;
  136. }
  137. ';
  138. }
  139. $result .= wf_tag('style', true);
  140. //basic notification frontend
  141. $result .= wf_tag('script');
  142. $result .= '
  143. $(document).ready(function() {
  144. Notification.requestPermission().then(function(result) {
  145. console.log(result);
  146. });
  147. $(".dismiss").click(function(){$("#notification").fadeOut("slow");});
  148. setInterval(
  149. function() {
  150. $.get("' . self::URL_NOTIFICATIONS . '&reqadm=' . $this->curAdminLogin . '", function(message) {
  151. if (message) {
  152. var data= JSON.parse(message);
  153. data.forEach(function(key) {
  154. new Noty({
  155. theme: \'bootstrap-v4\',
  156. timeout: \'' . $this->notysPopupTimeout . '\',
  157. progressBar: true,
  158. type: key.type,
  159. layout: \'bottomRight\',
  160. killer: key.index,
  161. queue: key.index,
  162. text: key.text
  163. }).show();
  164. if (typeof (sendNotificationDesktop) === "function") {
  165. var title = "' . __('Dreamkas notification') . '";
  166. var options = {
  167. body: key.text,
  168. tag: key.index,
  169. dir: "auto"
  170. };
  171. sendDSNotificationDesktop(title, options, key.link);
  172. }
  173. });
  174. }
  175. }
  176. )
  177. },
  178. ' . $this->notysPollingInterval . ');
  179. })
  180. ';
  181. $result .= wf_tag('script', true);
  182. if ($this->notysOnDesktop) {
  183. $result .= wf_tag('script');
  184. $result .= '
  185. function sendDSNotificationDesktop(title, options, link) {
  186. if (Notification.permission === "granted") {
  187. var notification = new Notification(title, options);
  188. if(link) {
  189. notification.onclick = function() {
  190. window.open(link,"_self");
  191. }
  192. }
  193. } else if (Notification.permission !== "denied") {
  194. Notification.requestPermission(function (permission) {
  195. if (permission === "granted") {
  196. var notification = new Notification(title, options);
  197. if(link) {
  198. notification.onclick = function() {
  199. window.open(link,"_self");
  200. }
  201. }
  202. }
  203. });
  204. }
  205. };
  206. ';
  207. $result .= wf_tag('script', true);
  208. }
  209. return ($result);
  210. }
  211. /**
  212. * Renders widget code if it required for current situation
  213. *
  214. * @return string/void
  215. */
  216. public function renderWidget() {
  217. $result = '';
  218. if (cfr('DREAMKAS')) {
  219. if ($this->ubConfig->getAlterParam('DREAMKAS_ENABLED')) {
  220. $widget = $this->getDreamkasNotificationsJS();
  221. if ($this->notysEverywhere) {
  222. $result .= $widget;
  223. } else {
  224. if ((@$_GET['module'] == 'taskbar') OR ( !isset($_GET['module']))) {
  225. $result .= $widget;
  226. }
  227. }
  228. //per-admin controls
  229. if ((!empty($this->notysAdminsAllowed) AND ( !isset($this->notysAdminsAllowed[$this->curAdminLogin])))) {
  230. $result = '';
  231. }
  232. return ($result);
  233. }
  234. }
  235. }
  236. }