123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * Taskman daily tasks notification
- */
- class TaskmanNotify {
- /**
- * Contains all existing tasks data as id=>taskData
- *
- * @var array
- */
- protected $allTasksData = array();
- /**
- * Taskman database abstraction layer placeholder
- *
- * @var object
- */
- protected $tasksDb = '';
- /**
- * Telegram abstraction layer placeholder
- *
- * @var object
- */
- protected $telegram = '';
- /**
- * Contains all employee administator logins as login=>employeeId
- *
- * @var array
- */
- protected $allEmployeeLogins = array();
- /**
- * Contains all employee data as id=>name
- *
- * @var array
- */
- protected $allEmployee = array();
- /**
- * Contains all employee Telegram chatId as id=>chatid
- *
- * @var array
- */
- protected $allEmployeeChatIds = array();
- /**
- * Contains all active employee data as id=>name
- *
- * @var array
- */
- protected $allActiveEmployee = array();
- public function __construct() {
- $this->initTasksDb();
- $this->initTelegram();
- $this->loadEmployeeData();
- $this->loadTasksData();
- }
- /**
- * Inits taskman database abstraction layer
- *
- * @return void
- */
- protected function initTasksDb() {
- $this->tasksDb = new NyanORM('taskman');
- }
- /**
- * Inits telegram abstraction instance
- *
- * @return void
- */
- protected function initTelegram() {
- $this->telegram = new UbillingTelegram();
- }
- /**
- * Preloads all existing employee data
- *
- * @return void
- */
- protected function loadEmployeeData() {
- $allEmployeeTmp = ts_GetAllEmployeeData();
- if (!empty($allEmployeeTmp)) {
- foreach ($allEmployeeTmp as $io => $each) {
- $this->allEmployee[$each['id']] = $each['name'];
- if (!empty($each['admlogin'])) {
- $this->allEmployeeLogins[$each['admlogin']] = $each['id'];
- }
- if ($each['active']) {
- $this->allActiveEmployee[$each['id']] = $each['name'];
- }
- if ($each['telegram']) {
- $this->allEmployeeChatIds[$each['id']] = $each['telegram'];
- }
- }
- }
- }
- /**
- * Preloads all existing tasks data
- *
- * @return void
- */
- protected function loadTasksData() {
- $curDate = curdate();
- $this->tasksDb->where('status', '=', '0'); //only open tasks
- $this->tasksDb->where('startdate', '=', $curDate); //saving few resources
- $this->allTasksData = $this->tasksDb->getAll();
- }
- /**
- * Returns chatId if he associated with active employee
- *
- * @param string $employeeId
- *
- * @return int
- */
- protected function getEmployeeChatId($employeeId) {
- $result = 0;
- //is it active?
- if (isset($this->allActiveEmployee[$employeeId])) {
- //have chat id?
- if (isset($this->allEmployeeChatIds[$employeeId])) {
- $result = $this->allEmployeeChatIds[$employeeId];
- }
- }
- return($result);
- }
- /**
- * Performs telegram sending of some messages queue as chatId=>message
- *
- * @param array $sendingQueue
- *
- * @return void
- */
- protected function sendNotifications($sendingQueue) {
- if (!empty($sendingQueue)) {
- foreach ($sendingQueue as $eachChatId => $eachMessage) {
- $this->telegram->sendMessage($eachChatId, $eachMessage, false, 'TASKMANNOTIFY');
- }
- }
- }
- /**
- * Returns message queue for active tasks planned today for each active employee
- *
- * @return array
- */
- protected function getTasksTodayCount() {
- $result = array();
- $curDate = curdate();
- if (!empty($this->allTasksData)) {
- $sendingQueue = array(); //contains sending queue as chatId=>tasksCount
- foreach ($this->allTasksData as $io => $each) {
- if ($each['startdate'] == $curDate) {
- $adminChatId = $this->getEmployeeChatId($each['employee']);
- if ($adminChatId) {
- if (isset($sendingQueue[$adminChatId])) {
- $sendingQueue[$adminChatId]++;
- } else {
- $sendingQueue[$adminChatId] = 1;
- }
- }
- }
- }
- if (!empty($sendingQueue)) {
- foreach ($sendingQueue as $eachChatId => $tasksCount) {
- $result[$eachChatId] = '😾 ' . __('You have') . ' ' . $tasksCount . ' ' . __('undone tasks for today');
- }
- }
- }
- return($result);
- }
- /**
- * Performs notification for tasks planned today
- *
- * @return void
- */
- public function run() {
- $todayPlannedTasks = $this->getTasksTodayCount();
- $this->sendNotifications($todayPlannedTasks);
- }
- }
|