-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudits.php
executable file
·79 lines (67 loc) · 1.69 KB
/
Audits.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
<?php
namespace Adnduweb\Ci4Logs;
use CodeIgniter\Config\BaseConfig;
use Adnduweb\Ci4Logs\Models\AuditModel;
//use Adnduweb\Ci4Logs\Exceptions\AuditsException;
/*** CLASS ***/
class Audits
{
/**
* Our configuration instance.
*
* @var \Tatter\Audits\Config\Audits
*/
protected $config;
/**
* Audit rows waiting to add to the database.
*
* @var array
*/
protected $queue = [];
/**
* Store the configuration
*
* @param BaseConfig $config The Audits configuration to use
*/
public function __construct(BaseConfig $config)
{
$this->config = $config;
}
// checks for a logged in user based on config
// returns user ID, 0 for "not logged in", -1 for CLI
public function sessionUserId(): int
{
if (is_cli()) {
return -1;
}
return session($this->config->sessionUserId) ?? 0;
}
// add an audit row to the queue
public function add($audit)
{
if (empty($audit)) {
return false;
}
// add common data
$audit['user_id'] = $this->sessionUserId();
$audit['created_at'] = date('Y-m-d H:i:s');
// @TODO 2019-11-22 18:57:57
//print_r($audit);
$this->queue[] = $audit;
if ($audit['event'] == "delete") {
$audits = new AuditModel();
$audits->insertBatch($this->queue);
}
}
// batch insert all audits from the queue
public function save(): self
{
if (! empty($this->queue))
{
//print_r($this->queue); exit;
$audits = new AuditModel();
$audits->insertBatch($this->queue);
}
return $this;
}
}