-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.php
More file actions
145 lines (122 loc) · 3.67 KB
/
Functions.php
File metadata and controls
145 lines (122 loc) · 3.67 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
<?php
include_once 'includes.php';
class Functions {
public static function get_all_events($user) {
$conn = new Connection();
$db = $conn->get_db();
$req = "select * from trip.user_event
where user_login like '" . $user . "';";
$result = pg_query($db, $req);
$array = array();
while ($row = pg_fetch_assoc($result)) {
$req = "select * from trip.event
where id = '" . $row["event_id"] . "';";
$result2 = pg_query($db, $req);
$entry = pg_fetch_assoc($result2);
array_push($array, $entry);
}
return $array;
}
public static function get_event_by_id($id) {
$conn = new Connection();
$db = $conn->get_db();
$req = "select * from trip.event
where id = " . $id . ";";
$result = pg_query($db, $req);
return pg_fetch_assoc($result);
}
public static function get_users_by_event($event_id) {
$conn = new Connection();
$db = $conn->get_db();
$req = "select * from trip.user_event
where event_id = " . $event_id . ";";
$result = pg_query($db, $req);
$array = array();
while ($row = pg_fetch_assoc($result)) {
array_push($array, $row);
}
return $array;
}
public static function get_all_expenses($event_id) {
$conn = new Connection();
$db = $conn->get_db();
$req = "select * from trip.expense
where event_id = " . $event_id . ";";
$result = pg_query($db, $req);
$array = array();
while ($row = pg_fetch_assoc($result)) {
array_push($array, $row);
}
return $array;
}
public static function get_all_expense_concerned($expense_id) {
$conn = new Connection();
$db = $conn->get_db();
$req = "select * from trip.expense_concerned
where expense_id = " . $expense_id . ";";
$result = pg_query($db, $req);
$array = array();
while ($row = pg_fetch_assoc($result)) {
array_push($array, $row);
}
return $array;
}
public static function get_users_by_login_pattern($pattern)
{
$conn = new Connection();
$db = $conn->get_db();
$req = "select * from trip.user where login ilike '" .$pattern . "%';";
$result = pg_query($db, $req);
$array = array();
while ($row = pg_fetch_assoc($result)) {
array_push($array, $row);
}
return $array;
}
public static function insert_event($event) {
$conn = new Connection();
$db = $conn->get_db();
$req = "INSERT INTO trip.event (name, date, creator)
VALUES ('".$event["name"]."', '".$event["date"]."', '".$event["creator"]."')
RETURNING trip.event.id;";
if ($result = pg_query($db, $req)) {
$row = pg_fetch_assoc($result);
return $row["id"];
}
return -1;
}
public static function insert_user_event($user_event) {
$conn = new Connection();
$db = $conn->get_db();
if (pg_insert($db, 'trip.user_event', $user_event))
return true;
return false;
}
public static function insert_expense($expense) {
$conn = new Connection();
$db = $conn->get_db();
$req = "INSERT INTO trip.expense (nature, cost, event_id, user_login)
VALUES ('".$expense["nature"]."', '".$expense["cost"]."', '".$expense["event_id"]."', '".$expense["user_login"]."')
RETURNING trip.expense.id;";
if ($result = pg_query($db, $req)) {
$row = pg_fetch_assoc($result);
return $row["id"];
}
return -1;
}
public static function insert_expense_concerned($expense_concerned) {
$conn = new Connection();
$db = $conn->get_db();
if (pg_insert($db, 'trip.expense_concerned', $expense_concerned))
return true;
return false;
}
public static function get_user_situation($event_id, $user, $other_user) {
$conn = new Connection();
$db = $conn->get_db();
$req = "select trip.get_user_situation(".$event_id.",'".$user."','".$other_user."');";
$result = pg_query($db, $req);
$row = pg_fetch_assoc($result);
return $row["get_user_situation"];
}
}