api.stickynotify.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Sticky Notes daily notification implementation
  4. */
  5. class StickyNotify {
  6. /**
  7. * Contains all existing notes data as id=>noteData
  8. *
  9. * @var array
  10. */
  11. protected $allNotesData = array();
  12. /**
  13. * SitckyNotes database abstraction layer placeholder
  14. *
  15. * @var object
  16. */
  17. protected $notesDb = '';
  18. /**
  19. * Telegram abstraction layer placeholder
  20. *
  21. * @var object
  22. */
  23. protected $telegram = '';
  24. /**
  25. * Contains all employee administator logins as login=>employeeId
  26. *
  27. * @var array
  28. */
  29. protected $allEmployeeLogins = array();
  30. /**
  31. * Contains all employee data as id=>name
  32. *
  33. * @var array
  34. */
  35. protected $allEmployee = array();
  36. /**
  37. * Contains all employee Telegram chatId as id=>chatid
  38. *
  39. * @var array
  40. */
  41. protected $allEmployeeChatIds = array();
  42. /**
  43. * Contains all active employee data as id=>name
  44. *
  45. * @var array
  46. */
  47. protected $allActiveEmployee = array();
  48. public function __construct() {
  49. $this->initNotesDb();
  50. $this->initTelegram();
  51. $this->loadEmployeeData();
  52. $this->loadNotesData();
  53. }
  54. /**
  55. * Inits notes database abstraction layer
  56. *
  57. * @return void
  58. */
  59. protected function initNotesDb() {
  60. $this->notesDb = new NyanORM('stickynotes');
  61. }
  62. /**
  63. * Inits telegram abstraction instance
  64. *
  65. * @return void
  66. */
  67. protected function initTelegram() {
  68. $this->telegram = new UbillingTelegram();
  69. }
  70. /**
  71. * Preloads all existing employee data
  72. *
  73. * @return void
  74. */
  75. protected function loadEmployeeData() {
  76. $allEmployeeTmp = ts_GetAllEmployeeData();
  77. if (!empty($allEmployeeTmp)) {
  78. foreach ($allEmployeeTmp as $io => $each) {
  79. $this->allEmployee[$each['id']] = $each['name'];
  80. if (!empty($each['admlogin'])) {
  81. $this->allEmployeeLogins[$each['admlogin']] = $each['id'];
  82. }
  83. if ($each['active']) {
  84. $this->allActiveEmployee[$each['id']] = $each['name'];
  85. }
  86. if ($each['telegram']) {
  87. $this->allEmployeeChatIds[$each['id']] = $each['telegram'];
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * Preloads all existing notes data
  94. *
  95. * @return void
  96. */
  97. protected function loadNotesData() {
  98. $this->notesDb->where('active', '=', '1');
  99. $this->allNotesData = $this->notesDb->getAll();
  100. }
  101. /**
  102. * Returns administrator chatId if he associated with active employee
  103. *
  104. * @param string $adminLogin
  105. *
  106. * @return int
  107. */
  108. protected function getAdminChatId($adminLogin) {
  109. $result = 0;
  110. if (!empty($adminLogin)) {
  111. //associated user?
  112. $adminEmployeeId = (isset($this->allEmployeeLogins[$adminLogin])) ? $this->allEmployeeLogins[$adminLogin] : 0;
  113. if ($adminEmployeeId) {
  114. //is it active?
  115. if (isset($this->allActiveEmployee[$adminEmployeeId])) {
  116. //have chat id?
  117. if (isset($this->allEmployeeChatIds[$adminEmployeeId])) {
  118. $result = $this->allEmployeeChatIds[$adminEmployeeId];
  119. }
  120. }
  121. }
  122. }
  123. return($result);
  124. }
  125. /**
  126. * Performs telegram sending of some messages queue as chatId=>message
  127. *
  128. * @param array $sendingQueue
  129. *
  130. * @return void
  131. */
  132. protected function sendNotifications($sendingQueue) {
  133. if (!empty($sendingQueue)) {
  134. foreach ($sendingQueue as $eachChatId => $eachMessage) {
  135. $this->telegram->sendMessage($eachChatId, $eachMessage, false, 'STICKYNOTIFY');
  136. }
  137. }
  138. }
  139. /**
  140. * Returns message queue for active notes planned today for each active employee
  141. *
  142. * @return array
  143. */
  144. protected function getNotesTodayCount() {
  145. $result = array();
  146. $curDate = curdate();
  147. if (!empty($this->allNotesData)) {
  148. $sendingQueue = array(); //contains sending queue as chatId=>notesCount
  149. foreach ($this->allNotesData as $io => $each) {
  150. if ($each['reminddate'] == $curDate) {
  151. $adminChatId = $this->getAdminChatId($each['owner']);
  152. if ($adminChatId) {
  153. if (isset($sendingQueue[$adminChatId])) {
  154. $sendingQueue[$adminChatId]++;
  155. } else {
  156. $sendingQueue[$adminChatId] = 1;
  157. }
  158. }
  159. }
  160. }
  161. if (!empty($sendingQueue)) {
  162. foreach ($sendingQueue as $eachChatId => $notesCount) {
  163. $result[$eachChatId] = '🙀 ' . __('You have') . ' ' . $notesCount . ' ' . __('notes or reminders for today');
  164. }
  165. }
  166. }
  167. return($result);
  168. }
  169. /**
  170. * Performs notification for notes planned today
  171. *
  172. * @return void
  173. */
  174. public function run() {
  175. $todayPlannedNotes = $this->getNotesTodayCount();
  176. $this->sendNotifications($todayPlannedNotes);
  177. }
  178. }