Changeset 813 for trunk/Modules/Notify/Notify.php
- Timestamp:
- Mar 9, 2016, 9:16:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/Notify/Notify.php
r772 r813 1 1 <?php 2 3 define('CONTACT_CATEGORY_EMAIL', 4); 2 4 3 5 class ModuleNotify extends AppModule 4 6 { 7 var $Checks; 8 5 9 function __construct($System) 6 10 { … … 11 15 $this->License = 'GNU/GPL'; 12 16 $this->Description = 'Send notification messages to selected users'; 13 $this->Dependencies = array('User', 'Subject'); 17 $this->Dependencies = array('User'); 18 $this->Checks = array(); 14 19 } 15 20 … … 34 39 )); 35 40 } 36 37 function OnlineList($Title, $OnlineNow, $OnlinePrevious, $MinDuration)41 42 function RegisterCheck($Name, $Callback) 38 43 { 39 $Time = time(); 40 $OnlineText = array('<span style="color:red;">Nedostupný</span>', '<span style="color:green;">Dostupný</span>'); 41 $Output = ''; 42 $Condition = 'WHERE (`NetworkInterface`.`Online` = '.$OnlineNow.') AND (`NetworkInterface`.`OnlineNotify` = '.$OnlinePrevious.') '. 43 'AND (`NetworkInterface`.`LastOnline` <= "'.TimeToMysqlDateTime($Time - $MinDuration).'")'; 44 $DbResult3 = $this->Database->query('SELECT CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")) AS `Name`, '. 45 '`NetworkInterface`.`Online`, `NetworkInterface`.`LastOnline` FROM `NetworkInterface` '. 46 'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '. 47 $Condition.' AND (`NetworkDevice`.`PermanentOnline`=1) AND (`NetworkDevice`.`Used`=1) AND '. 48 '(`NetworkInterface`.`LocalIP` != "") '. 49 'ORDER BY `Name` ASC'); 50 if($DbResult3->num_rows > 0) { 51 $Output .= $Title.'<br/>'; 52 $Output .= '<table>'. 53 '<tr><th align="center">Jméno</th><th align="center">Stav</th><th align="center">Čas</th><th align="center">Trvání</th></tr>'."\n"; 54 while($Item = $DbResult3->fetch_assoc()) 55 { 56 $Duration = $Time - MysqlDateTimeToTime($Item['LastOnline']); 57 $DurationText = sprintf('%02d', floor($Duration / 3600 % 24)).':'. 58 sprintf('%02d', floor($Duration / 60 % 60)).':'. 59 sprintf('%02d', floor($Duration % 60)); 60 $Days = floor($Duration / (60 * 60 * 24)); 61 if($Days > 0) $DurationText = $Days.' dnů '.$DurationText; 62 63 $Output .= '<tr><td>'.$Item['Name'].'</td><td>'.$OnlineText[$Item['Online']]. 64 '</td><td>'.$Item['LastOnline'].'</td><td>'.$DurationText.'</td></tr>'."\n"; 65 } 66 $Output .= '</table><br/>'."\n"; 67 } 68 $this->Database->query('UPDATE `NetworkInterface` SET `OnlineNotify` = `Online` '. 69 $Condition); 70 return $Output; 44 if(array_key_exists($Name, $this->Checks)) 45 throw new Exception('Check function "'.$Name.'" already registered.'); 46 $this->Checks[$Name] = array('Callback' => $Callback); 47 } 48 49 function UnregisterCheck($Name) 50 { 51 if(!array_key_exists($Name, $this->Checks)) 52 throw new Exception('Check function "'.$Name.'" not registered.'); 53 unset($this->Checks[$Name]); 71 54 } 72 55 … … 74 57 { 75 58 $Output = ''; 59 $Content = ''; 60 61 // Get output from checks 62 $DbResult2 = $this->Database->query('SELECT `Id`, `Name`, `SysName` FROM `NotifyCategory`'); 63 while($Category = $DbResult2->fetch_assoc()) 64 { 65 if(array_key_exists($Category['SysName'], $this->Checks)) 66 { 67 $List = call_user_func($this->Checks[$Category['SysName']]['Callback']); 68 if($List != '') { 69 $Output .= 'Kategorie: '.$Category['Name'].":\n"; 70 $Content .= 'Kategorie: '.$Category['Name']."<br>\n"; 71 $Content .= $List; 72 } 73 } 74 } 75 76 // Send content to users 76 77 $DbResult = $this->Database->query('SELECT `NotifyUser`.`Id`, `NotifyUser`.`LastTime`, `User`.`Name`, `Contact`.`Value`, `Contact`.`Category` FROM `NotifyUser` '. 77 78 'LEFT JOIN `User` ON `User`.`Id` = `NotifyUser`.`User` '. … … 80 81 { 81 82 $Time = time(); 82 $Content = '';83 83 $Output .= 'User '.$User['Name'].'<br/>'; 84 $DbResult2 = $this->Database->query('SELECT `Id`, `Name`, `SysName` FROM `NotifyCategory`'); 85 while($Category = $DbResult2->fetch_assoc()) 86 { 87 if($Category['SysName'] == 'NetworkReachability') { 88 $List = $this->OnlineList('Nově online', 1, 0, 0); 89 $List .= $this->OnlineList('Nově offline', 0, 1, 5 * 60); 90 if($List != '') $List .= $this->OnlineList('Stále offline', 0, 0, 0); 91 if($List != '') { 92 $Output .= 'Kategorie: '.$Category['Name'].":\n"; 93 $Content .= 'Kategorie: '.$Category['Name']."<br>\n"; 94 $Content .= $List; 95 } 96 } 97 } 84 98 85 $this->Database->update('NotifyUser', '`Id`='.$User['Id'], array('LastTime' => TimeToMysqlDateTime($Time))); 99 86 100 if(($User['Category'] == 4) and ($Content !='')) {87 if(($User['Category'] == CONTACT_CATEGORY_EMAIL) and ($Content !='')) { 101 88 $Mail = new Mail(); 102 89 $Mail->Subject = 'Notification'; 103 90 $Mail->AddTo($User['Value'], $User['Name']); 104 $Mail->AddBody(strip_tags($Content), 'text/plain');91 //$Mail->AddBody(strip_tags($Content), 'text/plain'); 105 92 $Mail->AddBody('<style> 106 93 table { border-collapse: collapse; } … … 110 97 $Mail->Send(); 111 98 $Output .= 'Sent email to '.$User['Value'].' ('.$User['Name'].')<br/>'; 112 $Output .= $Content;99 $Output .= strip_tags(str_replace("</", " </", str_replace('<br/>', "\n", $Content))); 113 100 } 114 101 … … 147 134 ADD CONSTRAINT `NotifyUser_ibfk_1` FOREIGN KEY (`User`) REFERENCES `User` (`Id`), 148 135 ADD CONSTRAINT `NotifyUser_ibfk_2` FOREIGN KEY (`Contact`) REFERENCES `Contact` (`Id`);'); 149 150 136 } 151 137
Note:
See TracChangeset
for help on using the changeset viewer.