forked from mikaarvola/trail-listing-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trail_equipment_listing2.php
executable file
·208 lines (178 loc) · 7.34 KB
/
trail_equipment_listing2.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Equipment listing from Trail</title>
<style>
body {
font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;
font-size: 14px;
}
table {
table-layout: auto;
width: 1200px; /* NOTE: Change to relative values! */
border: none;
border-collapse: collapse;
}
td {
border: none;
width: auto;
white-space: nowrap;
padding: 2px 0px;
}
table td:nth-child(2),
table td:nth-child(3) {
white-space: nowrap;
width: 1px;
padding-right: 30px;
margin: 10px;
}
tr {
border: none;
}
.left-align {
text-align: left;
}
</style>
</head>
<body>
<?php
require_once('./config.php');
// Set SEARCH variables
$freematch = ''; // set manually here if needed
if (isset($_GET['free'])) {
$freematch = $_GET['free']; // get from url parameter, ?free=xxx
// Replace Finnish alphabets
$freematch = str_replace('å', '%C3%A5', $freematch);
$freematch = str_replace('Å', '%C3%85', $freematch);
$freematch = str_replace('ä', '%C3%A4', $freematch);
$freematch = str_replace('Ä', '%C3%84', $freematch);
$freematch = str_replace('ö', '%C3%B6', $freematch);
$freematch = str_replace('Ö', '%C3%96', $freematch);
}
$model1 = ''; // set manually here if needed, for example 303979439
if (isset($_GET['model1'])) {
$model1 = $_GET['model1']; // get from url parameter, ?model1=xxx
}
$model2 = ''; // set manually here if needed
if (isset($_GET['model2'])) {
$model2 = $_GET['model2']; // get from url parameter, ?model2=xxx
}
$location1 = ''; // set manually here if needed
if (isset($_GET['location1'])) {
$location1 = $_GET['location1']; // get from url parameter, ?location1=xxx
}
$deleted = ''; // set manually here if needed
if (isset($_GET['deleted'])) {
$deleted = $_GET['deleted']; // get from url parameter, ?deleted=0 or deleted=1
}
// Minimal error check
if ($code == '') {
echo '<p>API key not set.</p>';
die;
}
// Check if models are defined
$model_category_id1 = '';
$model_category_id2 = '';
if ($model1 != '') {
$model_category_id1 = '&search%5Bmodel_category_ids%5D%5B%5D=' . $model1;
}
if ($model2 != '') {
$model_category_id2 = '&search%5Bmodel_category_ids%5D%5B%5D=' . $model2;
}
// Set POST variables
$url = 'https://api.trail.fi/api/v1/items?&search%5Bfree%5D='.$freematch.'&search%5Blocations%5D%5B%5D='.$location1.''.$model_category_id1.''.$model_category_id2.'&search%5Bitem_type_id%5D=&search%5Bafter%5D=&search%5Bbefore%5D=&search%5Baudited_after%5D=&search%5Baudited_before%5D=&search%5Bexpires_after%5D=&search%5Bexpires_before%5D=&search%5Bprice_above%5D=&search%5Bprice_below%5D=&search%5Bcreated_after%5D=&search%5Bmarked%5D=&search%5Bdeleted%5D='.$deleted.'&search%5Bdeleted_after%5D=&search%5Bdeleted_before%5D=&search%5Bdelete_reason%5D=&search%5Breservable%5D=&page=1&per_page=50000';
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . $code));
// Save response to variable $result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute post
$json = curl_exec($ch);
// Close connection
curl_close($ch);
// Create PHP array from Trail JSON export
$array = json_decode($json, true);
// Display root location
echo "<h1>Room " . htmlspecialchars($array['data'][0]['root_location']) . "</h1>";
echo "<h3>This room has the following equipment</h3>";
$results = [];
// Grouping devices by model
foreach ($array['data'] as $item) {
$name = $item['model']['name'] ?? 'Unknown Model';
$manufacturer = $item['manufacturer'] ?? 'Unknown Manufacturer';
$description = $item['model_description'] ?? 'No description available';
$model_id = $item['model']['id'] ?? 0;
$item_id = $item['id']; // Unique item ID
$identity = $item['identity'] ?? 'No identity available'; // Get identity
// Adding device to the group
if (!isset($results[$name])) {
$results[$name] = [
'count' => 0,
'manufacturer' => $manufacturer,
'model_description' => $description, // Store model description
'model_id' => $model_id,
'items' => [] // List of individual devices
];
}
$results[$name]['count']++;
$results[$name]['items'][] = [
'manufacturer' => $manufacturer,
'model_id' => $model_id,
'description' => $description,
'item_id' => $item_id, // Store the unique ID for each item
'identity' => $identity, // Store the identity for each item
'item' => $item // You can add more details here
];
}
echo "<table>";
echo "<tr><th></th><th class='left-align'>Amount</th><th class='left-align'>Manufacturer</th><th class='left-align'>Model</th><th class='left-align'>Description</th></tr>"; // Table headers
foreach ($results as $name => $information) {
// Output model information with toggle button
echo "<tr>";
echo "<td><button class='toggle' onclick='toggleVisibility(\"{$name}\", this)'>+</button></td>"; // Toggle button
echo "<td>{$information['count']} pcs</td>";
echo "<td>{$information['manufacturer']}</td>";
echo "<td><a href='$trail_models_baseurl{$information['model_id']}' target='_blank'>{$name}</a></td>"; // Model name and link
echo "<td>{$information['model_description']}</td>"; // Model description
echo "<td></td>"; // Placeholder for identity
echo "</tr>";
// Output detailed information for each device
echo "<tr id='{$name}' class='item-list' style='display:none;'>"; // Initially hidden
echo "<td colspan='6'>"; // Adjust colspan to include identity column
echo "<ul style='list-style-type:none; padding-left: 50px;'>"; // No bullets and indent
foreach ($information['items'] as $item) {
// Display manufacturer, model, and identity for each item
echo "<li><a href='$trail_items_baseurl{$item['item_id']}' target='_blank'>{$item['manufacturer']} - {$name} ({$item['identity']})</a></li>";
}
echo "</ul></td></tr>";
}
echo "</table>";
?>
<script>
function toggleVisibility(name, button) {
var row = document.getElementById(name);
if (row.style.display === "none") {
row.style.display = "table-row"; // Show the row
button.textContent = '-'; // Change button text to minus
} else {
row.style.display = "none"; // Hide the row
button.textContent = '+'; // Change button text back to plus
}
}
</script>
<p>
Please do <b>NOT</b> move equipment out of this room.<br>
If something is missing or broken, please contact <i>siba.avhelp@uniarts.fi</i>
</p>
<!-- Print API URL and PHP array for debugging purposes -->
<?php if (isset($_GET['debug'])): ?>
<h3>Query URL</h3>
<?php echo htmlspecialchars($url); ?>
<h3>PHP array</h3>
<pre><?php print_r($array); ?></pre>
<p>end of report</p>
<?php endif; ?>
</body>
</html>