forked from cfinke/anyInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_class.php
executable file
·221 lines (173 loc) · 7.06 KB
/
alert_class.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
class alert {
var $id;
var $item_ids;
var $category_ids;
var $title;
var $field_id;
var $condition;
var $value;
var $time;
var $unix_time;
var $expire_time;
var $unix_expire_time;
var $timed = false;
var $expires = false;
function alert($alert_id){
global $db;
$this->id = $alert_id;
$query = "SELECT *, UNIX_TIMESTAMP(" . $db->quoteIdentifier('time') . ") AS " . $db->quoteIdentifier('unix_time') . ", UNIX_TIMESTAMP(" . $db->quoteIdentifier('expire_time') . ") AS " . $db->quoteIdentifier('unix_expire_time') . " FROM " . $db->quoteIdentifier('anyInventory_alerts') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
$row = $result->fetchRow();
$this->title = $row["title"];
$this->item_ids = unserialize($row["item_ids"]);
$this->category_ids = unserialize($row["category_ids"]);
$this->field_id = $row["field_id"];
$this->condition = $row["condition"];
$this->value = $row["value"];
$this->time = $row["time"];
$this->unix_time = $row["unix_time"];
$this->expire_time = $row["expire_time"];
$this->unix_expire_time = $row["unix_expire_time"];
if ($this->expire_time != '00000000000000'){
$this->expires = true;
}
$this->timed = $row["timed"];
}
// This function returns a "teaser" or short description for the alert.
function export_teaser(){
return $this->title;
}
// This function removes an item from the alert.
function remove_item($item_id){
global $db;
$this->item_ids = array_unique($this->item_ids);
// Find the key of the category id in the array.
$key = array_search($item_id, $this->item_ids);
// If the category id is in the array, remove it.
if ($key !== false) unset($this->item_ids[$key]);
$query = "UPDATE " . $db->quoteIdentifier('anyInventory_alerts') . " SET " . $db->quoteIdentifier('item_ids') . "='".serialize($this->item_ids)."' WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
}
function remove_category($category_id){
global $db;
$query = "SELECT " . $db->quoteIdentifier("id") . ", " . $db->quoteIdentifier("item_category") . " FROM " . $db->quoteIdentifier("anyInventory_items") . " WHERE " . $db->quoteIdentifier("id") . " IN ('".implode("','", $this->item_ids)."')";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
while ($row = $result->fetchRow()){
if ($row["item_category"] == $category_id){
$this->remove_item($row["id"]);
}
}
// Find the key of the category id in the array.
$key = array_search($category_id, $this->category_ids);
// If the category id is in the array, remove it.
if ($key !== false) unset($this->category_ids[$key]);
// Update the database
if (count($this->category_ids) == 0){
$query = "DELETE FROM " . $db->quoteIdentifier('anyInventory_alerts') . " WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
}
else{
$query = "UPDATE " . $db->quoteIdentifier('anyInventory_alerts') . " SET " . $db->quoteIdentifier('category_ids') . "='".serialize($this->category_ids)."' WHERE " . $db->quoteIdentifier('id') . "='".$this->id."'";
}
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
}
// This function returns a full description of the item.
function export_description(){
global $db;
global $DIR_PREFIX;
// Create the header with the name.
$output .= '
<table class="standardHeader" cellspacing="0" cellpadding="0">
<tr class="tableHeader">
<td>'.$this->title.'</td>
</tr>
<tr>
<td class="tableData">
<table>
<tr>
<td class="form_label">'.APPLIES_TO.':</td>
<td>';
if (is_array($this->item_ids)){
$query = "SELECT " . $db->quoteIdentifier("id") . ", " . $db->quoteIdentifier("item_category") . ", " . $db->quoteIdentifier("name") . " FROM " . $db->quoteIdentifier("anyInventory_items") . " WHERE " . $db->quoteIdentifier("id") . " IN ('".implode("','", $this->item_ids)."')";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
while ($row = $result->fetchRow()){
$output .= '<a href="'.$DIR_PREFIX.'index.php?c='.$row["item_category"].'&id='.$row["id"].'" style="text-decoration: none;"><b>'.$row["name"].'</b></a>';
}
}
$output .= '</td>
</tr>
<tr>
<td class="form_label">'.ACTIVE_WHEN.':</td>
<td>';
$query = "SELECT " . $db->quoteIdentifier("name") . " FROM " . $db->quoteIdentifier("anyInventory_fields") . " WHERE " . $db->quoteIdentifier("id") . " = '".$this->field_id."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
$row = $result->fetchRow();
$output .= $row["name"]." ";
$output .= $this->condition;
$output .= (trim($this->value) == '') ? " ''" : ' '.$this->value;
$output .= '</td>
</tr>
<tr>
<td class="form_label">'.EFFECTIVE_DATE.':</td>
<td>'.date("Y m d",$this->unix_time).'</td>
</tr>';
if ($this->expires){
$output .= '
<tr>
<td class="form_label">'.EXPIRATION_DATE.':</td>
<td>'.date("Y m d",$this->unix_expire_time).'</td>
</tr>';
}
if ($this->email != ''){
$output .= '
<tr>
<td class="form_label">'.EMAIL_ALERT_TO.':</td>
<td>'.$this->email.'</td>
</tr>';
}
$output .= '
</table>
</td>
</tr>
</table>';
return $output;
}
function export_box($item_id = null){
// This function creates an alert box for an activated alert.
global $DIR_PREFIX;
global $db;
if ($item_id != null){
$query = "SELECT " . $db->quoteIdentifier("name") . " FROM " . $db->quoteIdentifier("anyInventory_items") . " WHERE " . $db->quoteIdentifier("id") . " ='".$item_id."'";
$result = $db->query($query);
if (DB::isError($result)) die($result->getMessage().'<br /><br />'.SUBMIT_REPORT . '<br /><br />'. $query);
$item_link = '<br /><a href="'.$DIR_PREFIX.'index.php?id='.$item_id.'">'.$row["name"].'</a>';
}
else{
$item_link = '';
}
$output = '
<table class="alertBox" cellspacing="0" cellpadding="2" border="0">
<tr class="alertTitle">
<td>
'.ALERT.'
</td>
<td style="text-align: right;">
<a href="'.$DIR_PREFIX.'docs/alerts.php">?</a>
</td>
</tr>
<tr class="alertContent">
<td colspan="2">
<b>'.$this->title.'</b>'.$item_link.'
</td>
</tr>
</table>';
return $output;
}
}
?>