-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
156 lines (122 loc) · 4.04 KB
/
db.php
File metadata and controls
156 lines (122 loc) · 4.04 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
Class DB{
function DB(){
require("config.php");
$this->dateFormat = "Y-m-d H:i:s";
$this->db = new PDO($dsn, $user, $password);
$this->checkStmt = $this->db->prepare("SELECT pages FROM data WHERE jobid=:jobid AND printer=:printer");
$this->insertStmt = $this->db->prepare("INSERT INTO data(jobid, printer, date, user, pages, costcenter) VALUES(:jobid, :printer, :date, :user, :pages, :costcenter)");
$this->getStmt = $this->db->prepare("SELECT * FROM data WHERE date >= :startDate AND date <= :endDate");
$this->user2cc = $this->db->prepare("SELECT * FROM user2cc");
$this->user2ccByUser = $this->db->prepare(" SELECT costcenter FROM user2cc WHERE username = :user ");
$this->costcenter = $this->db->prepare("SELECT * FROM costcenter");
$this->getUserPrintJobsWithoutCCString = $this->db->prepare("SELECT uid FROM data WHERE (costcenter IS NULL OR costcenter = 0 ) AND user = :user");
}
function getUserPrintJobsWithoutCC($user = null){
if(is_null($user)){
return array();
}
$this->getUserPrintJobsWithoutCCString->execute(array(
":user" => $user
));
$uidList = array();
foreach($this->getUserPrintJobsWithoutCCString->fetchAll(PDO::FETCH_ASSOC) as $uid){
$uidList[] = $uid["uid"];
}
return $uidList;
}
function pdo_sql_debug($string,$data) {
$indexed=$data==array_values($data);
foreach($data as $k=>$v) {
if(is_string($v)) $v="'$v'";
if($indexed) $string=preg_replace('/\?/',$v,$string,1);
else $string=str_replace(":$k",$v,$string);
}
return $string;
}
function getAll($timeStart = 1, $timeEnd = null){
if(!$timeEnd)
$timeEnd = time();
$dateStart = new DateTime();
$dateStart->setTimestamp($timeStart);
$dateEnd = new DateTime();
$dateEnd->setTimestamp($timeEnd);
$data = array(
'startDate' => $dateStart->format($this->dateFormat),
'endDate' => $dateEnd->format($this->dateFormat)
);
$this->getStmt->execute($data);
$rows = $this->getStmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
function insert($jobid = null, $printer = null, $date = null, $user = null, $pages = null, $costcenter = null){
if(is_null($jobid) || is_null($printer) || is_null($date) || is_null($user) || is_null($pages))
echo "Wrong Parameters";
$this->checkStmt->execute(array(
':jobid' => $jobid,
':printer' => $printer
));
$rows = $this->checkStmt->fetchAll(PDO::FETCH_ASSOC);
if(count($rows) == 0){
$this->insertStmt->execute(array(
':jobid' => $jobid,
':printer' => $printer,
':date' => $date,
':user' => $user,
':pages' => $pages,
':costcenter' => $costcenter
));
}else{
print_r($rows);
}
}
function getCostcenter(){
$this->costcenter->execute();
$rows = $this->costcenter->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
function getUser2CCbyUser($user = null){
if(is_null($user)){
return null;
}
$this->user2ccByUser->execute(array(
':user' => $user
));
$rows = $this->user2ccByUser->fetchAll(PDO::FETCH_ASSOC);
if(count($rows) > 1){
return null;
}
return $rows[0]["costcenter"];
}
function insertUser2CC($user, $costcenter){
$costcenterDB = $this->getUser2CCbyUser($user);
$data = array(
":user" => $user,
":costcenter" => $costcenter
);
if( is_null($costcenterDB) ){
// neuen Eintrag anlegen
$stmt = $this->db->prepare("INSERT INTO user2cc(username, costcenter) VALUES(:user, :costcenter)");
return $stmt->execute($data);
}else if( $costcenter != $costcenterDB ){
// Eintrag ändern
$stmt = $this->db->prepare("UPDATE user2cc SET costcenter = :costcenter WHERE username = :user;");
return $stmt->execute($data);
}
return false;
}
function updatePrintLog($uidList, $costcenter){
$data = array(
":costcenter" => $costcenter,
// ":uidList" => $uidList
);
// var_dump($data);
$stmt = $this->db->prepare("UPDATE data SET costcenter = :costcenter WHERE uid IN(".$uidList.")");
return $stmt->execute($data);
}
function getUser2CC(){
$this->user2cc->execute();
$rows = $this->user2cc->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
}