This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
halmon/application/classes/events.php
2013-10-16 09:55:29 +11:00

75 lines
2.0 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class is used to handle alarm events just received
*
* @package HAM
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Events {
private $acct;
private $events = array();
public function __construct(array $events) {
foreach ($events as $event) {
// Set our accounts
if (is_null($this->acct))
$this->acct = $event->acct;
elseif ($this->acct != $event->acct)
throw new Kohana_Exception('Events are for multiple accounts, unable to handle this.');
}
// Save our events
$this->events = $events;
}
public function trigger() {
$ao = ORM::factory('account',array('siteid'=>$this->acct));
if (! $ao->loaded())
throw new Kohana_Exception('No site configuration for :siteid?',array(':siteid'=>$this->acct));
if (! $ao->model->model)
throw new Kohana_Exception('No configured Alarm Model for Alarm ID :id',array(':id'=>$ao->model->id));
$ac = sprintf('Panel_%s',$ao->model->model);
if (! class_exists($ac))
throw new Kohana_Exception('Unable to handle events for Alarm :model',array(':model'=>$ao->model->model));
$panel = new $ac($this->events);
foreach ($panel->trigger() as $event) {
$to = $ao->trigger->trigger($event['alarm']);
if (! $to->loaded())
continue;
// We need to email this event
if ($to->email) {
$e = Email::connect();
$sm = Swift_Message::newInstance();
$sm->setSubject('Notice from your alarm');
$sm->setBody(sprintf('Alarm: %s. Received: %s',$event['desc'],date('d-m-Y h:i:s',$event['date'])),'text/plain');
$sm->setTo($to->contact->email);
$sm->setFrom('noreply@leenooks.net');
$e->send($sm);
}
// We need to sms this event
if ($to->sms) {
printf('Alarm: %s, SMS: %s, Email: %s, To: %s (%s)',
$alo->description,
$to->sms,
$to->email,
$to->contact->email,$to->contact->phone);
echo "\n";
}
}
}
}
?>