Ignore:
Timestamp:
Mar 9, 2016, 9:16:11 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Network interface state notification made registrated as callback instead of fixed method in Notify class.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Notify/Notify.php

    r772 r813  
    11<?php
     2
     3define('CONTACT_CATEGORY_EMAIL', 4);
    24
    35class ModuleNotify extends AppModule
    46{
     7  var $Checks;
     8 
    59  function __construct($System)
    610  {
     
    1115    $this->License = 'GNU/GPL';
    1216    $this->Description = 'Send notification messages to selected users';
    13     $this->Dependencies = array('User', 'Subject');
     17    $this->Dependencies = array('User');
     18    $this->Checks = array();
    1419  }
    1520
     
    3439    ));
    3540  }
    36 
    37   function OnlineList($Title, $OnlineNow, $OnlinePrevious, $MinDuration)
     41 
     42  function RegisterCheck($Name, $Callback)
    3843  {
    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]);
    7154  }
    7255
     
    7457  {
    7558    $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
    7677    $DbResult = $this->Database->query('SELECT `NotifyUser`.`Id`, `NotifyUser`.`LastTime`, `User`.`Name`, `Contact`.`Value`, `Contact`.`Category` FROM `NotifyUser` '.
    7778      'LEFT JOIN `User` ON `User`.`Id` = `NotifyUser`.`User` '.
     
    8081    {
    8182      $Time = time();
    82       $Content = '';
    8383      $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     
    9885      $this->Database->update('NotifyUser', '`Id`='.$User['Id'], array('LastTime' => TimeToMysqlDateTime($Time)));
    9986
    100       if(($User['Category'] == 4) and ($Content !='')) {
     87      if(($User['Category'] == CONTACT_CATEGORY_EMAIL) and ($Content !='')) {
    10188        $Mail = new Mail();
    10289        $Mail->Subject = 'Notification';
    10390        $Mail->AddTo($User['Value'], $User['Name']);
    104         $Mail->AddBody(strip_tags($Content), 'text/plain');
     91        //$Mail->AddBody(strip_tags($Content), 'text/plain');
    10592        $Mail->AddBody('<style>
    10693table { border-collapse: collapse; }
     
    11097        $Mail->Send();
    11198        $Output .= 'Sent email to '.$User['Value'].' ('.$User['Name'].')<br/>';
    112         $Output .= $Content;
     99        $Output .= strip_tags(str_replace("</", " </", str_replace('<br/>', "\n", $Content)));
    113100      }
    114101
     
    147134ADD CONSTRAINT `NotifyUser_ibfk_1` FOREIGN KEY (`User`) REFERENCES `User` (`Id`),
    148135ADD CONSTRAINT `NotifyUser_ibfk_2` FOREIGN KEY (`Contact`) REFERENCES `Contact` (`Id`);');
    149 
    150136  }
    151137
Note: See TracChangeset for help on using the changeset viewer.