-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevent_controller.php
97 lines (87 loc) · 2.44 KB
/
event_controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
use Symfony\Component\Yaml\Yaml;
/**
* Event module class
*
* @package munkireport
* @author AvB
**/
class Event_controller extends Module_controller
{
private $conf;
public function __construct()
{
if (! $this->authorized()) {
$out['items'] = array();
$out['reload'] = true;
$out['error'] = 'Session expired: please login';
$obj = new View();
$obj->view('json', array('msg' => $out));
die();
}
// Add local config
configAppendFile(__DIR__ . '/config.php', 'event');
$this->module_path = dirname(__FILE__) .'/';
try {
$this->conf = Yaml::parseFile(conf('event')['config_path']);
} catch (\Exception $e) {
$this->conf = [];
}
}
public function index()
{
echo "You've loaded the Event module!";
}
private function hasFilter()
{
return array_key_exists('filter', $this->conf) &&
is_array($this->conf['filter']);
}
private function createFilter($query, $filter)
{
foreach ($filter as $module => $types) {
if($types){
$where[] = ['module', $module];
foreach ($types as $type) {
$where[] = ['type', '<>', $type];
}
$query->where(function($query) use ($where){
$query->where($where);
}, NULL, NULL, 'AND NOT'); // <- little hack to get NOT (Subquery)
}
else{
$query->where('module', '<>', $module);
}
}
return $query;
}
/**
* Get Event
*
* @author AvB
**/
public function get($limit = 0)
{
$queryobj = Event_model::select(
'event.serial_number', 'module', 'type', 'msg', 'data',
'event.timestamp', 'machine.computer_name'
)
->join('machine', 'machine.serial_number', '=', 'event.serial_number')
->filter()
->orderBy('event.timestamp', 'desc');
if($limit){
$queryobj->limit($limit);
}
if($this->hasFilter()){
$this->createFilter($queryobj, $this->conf['filter']);
}
// dumpQuery($queryobj);
$obj = new View();
$obj->view('json', [
'msg' => [
'error' => '',
'items' => $queryobj->get()->toArray(),
]
]);
}
} // END class Event_controller