api.taskmannotify.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Taskman daily tasks notification
  4. */
  5. class TaskmanNotify {
  6. /**
  7. * Contains all existing tasks data as id=>taskData
  8. *
  9. * @var array
  10. */
  11. protected $allTasksData = array();
  12. /**
  13. * Taskman database abstraction layer placeholder
  14. *
  15. * @var object
  16. */
  17. protected $tasksDb = '';
  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->initTasksDb();
  50. $this->initTelegram();
  51. $this->loadEmployeeData();
  52. $this->loadTasksData();
  53. }
  54. /**
  55. * Inits taskman database abstraction layer
  56. *
  57. * @return void
  58. */
  59. protected function initTasksDb() {
  60. $this->tasksDb = new NyanORM('taskman');
  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 tasks data
  94. *
  95. * @return void
  96. */
  97. protected function loadTasksData() {
  98. $curDate = curdate();
  99. $this->tasksDb->where('status', '=', '0'); //only open tasks
  100. $this->tasksDb->where('startdate', '=', $curDate); //saving few resources
  101. $this->allTasksData = $this->tasksDb->getAll();
  102. }
  103. /**
  104. * Returns chatId if he associated with active employee
  105. *
  106. * @param string $employeeId
  107. *
  108. * @return int
  109. */
  110. protected function getEmployeeChatId($employeeId) {
  111. $result = 0;
  112. //is it active?
  113. if (isset($this->allActiveEmployee[$employeeId])) {
  114. //have chat id?
  115. if (isset($this->allEmployeeChatIds[$employeeId])) {
  116. $result = $this->allEmployeeChatIds[$employeeId];
  117. }
  118. }
  119. return($result);
  120. }
  121. /**
  122. * Performs telegram sending of some messages queue as chatId=>message
  123. *
  124. * @param array $sendingQueue
  125. *
  126. * @return void
  127. */
  128. protected function sendNotifications($sendingQueue) {
  129. if (!empty($sendingQueue)) {
  130. foreach ($sendingQueue as $eachChatId => $eachMessage) {
  131. $this->telegram->sendMessage($eachChatId, $eachMessage, false, 'TASKMANNOTIFY');
  132. }
  133. }
  134. }
  135. /**
  136. * Returns message queue for active tasks planned today for each active employee
  137. *
  138. * @return array
  139. */
  140. protected function getTasksTodayCount() {
  141. $result = array();
  142. $curDate = curdate();
  143. if (!empty($this->allTasksData)) {
  144. $sendingQueue = array(); //contains sending queue as chatId=>tasksCount
  145. foreach ($this->allTasksData as $io => $each) {
  146. if ($each['startdate'] == $curDate) {
  147. $adminChatId = $this->getEmployeeChatId($each['employee']);
  148. if ($adminChatId) {
  149. if (isset($sendingQueue[$adminChatId])) {
  150. $sendingQueue[$adminChatId]++;
  151. } else {
  152. $sendingQueue[$adminChatId] = 1;
  153. }
  154. }
  155. }
  156. }
  157. if (!empty($sendingQueue)) {
  158. foreach ($sendingQueue as $eachChatId => $tasksCount) {
  159. $result[$eachChatId] = '😾 ' . __('You have') . ' ' . $tasksCount . ' ' . __('undone tasks for today');
  160. }
  161. }
  162. }
  163. return($result);
  164. }
  165. /**
  166. * Performs notification for tasks planned today
  167. *
  168. * @return void
  169. */
  170. public function run() {
  171. $todayPlannedTasks = $this->getTasksTodayCount();
  172. $this->sendNotifications($todayPlannedTasks);
  173. }
  174. }