Skip to content

Commit

Permalink
Update blade php to enhance support of QR code
Browse files Browse the repository at this point in the history
  • Loading branch information
xWTF committed Jan 12, 2023
1 parent f91d331 commit 0070d9a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 71 deletions.
79 changes: 8 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Create a shortcut in `shell:startup` folder.

Just pass `-m` to the argument list.

# The Snipe-IT part
## The Snipe-IT part

## Keys Available
### Keys Available

All keys are prepended with a prefix (e.g. `S: ` for serial) except for `asset_tag` and `id`.
All keys are prepended with a prefix (e.g. `S:` for serial) except for `asset_tag` and `asset_url`.

| Key | Description |
| --------- | -------------------------- |
Expand All @@ -37,75 +37,12 @@ All keys are prepended with a prefix (e.g. `S: ` for serial) except for `asset_t
| model | Model name |
| company | Company name |
| asset_tag | The asset tag, for 1D-code |
| asset_url | The asset URL, for 2D-code |

## labels.blade.php
### Install labels.blade.php

Replace `resources/views/hardware/labels.blade.php` with content below.
Take a look at [labels.blade.php](./labels.blade.php), modify the top `define` lines according to your configuration. The first PHP codeblock is dead simple, you may modify it according to your needs too.

Specifically, if you're using docker, mount it to `/var/www/html/resources/views/hardware/labels.blade.php`.

```php
<?php
define('ACCESS_KEY', 'YOUR-ACCESS-KEY-HERE');
define('SERVER_ENDPOINT', 'http://127.0.0.1:8000/print');

$labels = [];
foreach ($assets as $asset) {
$labels[] = [
'id' => 'ID: ' . $asset->id,
'name' => empty($asset->name) ? '' : 'N: ' . $asset->name,
'serial' => empty($asset->serial) ? '' : 'S: ' . $asset->serial,
'model' => empty($asset->model->name) ? '' : 'M: ' . $asset->model->name,
'company' => $asset->company === null ? null : 'C: ' . $asset->company->name,
'asset_tag' => $asset->asset_tag,
];
}
?>

<!doctype html>
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Labels Print</title>
</head>
Then, replace `resources/views/hardware/labels.blade.php` with it, and you're good to go.

<body>
<div style="display: flex;flex-flow: column;gap: 12px">
<span>Labels Data: <?php echo count($labels); ?></span>
<div>
<button type="button" id="print" onclick="doPrint()">Click here to print</button>
</div>
<ul style="margin: 0;">
<?php
foreach ($labels as $l) {
echo ('<li>' . $l['id'] . ', ' . $l['asset_tag'] . ', ' . $l['model'] . '</li>');
}
?>
</ul>
</div>
<script>
function doPrint() {
var el = document.getElementById('print');
el.setAttribute('disabled', true);
el.innerText = 'Requesting...';
el = el.parentElement;
fetch(<?php echo json_encode(SERVER_ENDPOINT); ?>, {
credentials: 'omit',
method: 'POST',
headers: {
'Authorization': 'Bearer <?php echo ACCESS_KEY; ?>',
},
// For readability
body: JSON.stringify(<?php echo json_encode($labels); ?>),
}).then(r => r.text())
.then(r => el.innerText = r)
.catch(e => el.innerText = 'Error: ' + e);
}
</script>
</body>
</html>
```
Specifically, if you're using docker, mount it to `/var/www/html/resources/views/hardware/labels.blade.php`.
64 changes: 64 additions & 0 deletions labels.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
define('ACCESS_KEY', 'YOUR-ACCESS-KEY-HERE');
define('SERVER_ENDPOINT', 'http://127.0.0.1:8000/print');
$labels = [];
foreach ($assets as $asset) {
$labels[] = [
'id' => 'ID: ' . $asset->id,
'name' => empty($asset->name) ? '' : 'N: ' . $asset->name,
'serial' => empty($asset->serial) ? '' : 'S: ' . $asset->serial,
'model' => empty($asset->model->name) ? '' : 'M: ' . $asset->model->name,
'company' => $asset->company === null ? null : 'C: ' . $asset->company->name,
'asset_tag' => $asset->asset_tag,
'asset_url' => $_ENV['APP_URL'] . '/hardware/' . $asset->id,
];
}
?>

<!doctype html>
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Labels Print</title>
</head>

<body>
<div style="display: flex;flex-flow: column;gap: 12px">
<span>Labels Data: <?php echo count($labels); ?></span>
<div>
<button type="button" id="print" onclick="doPrint()">Click here to print</button>
</div>
<ul style="margin: 0;">
<?php
foreach ($labels as $l) {
echo ('<li>' . $l['asset_tag'] . ', ' . $l['model'] . ', ' . $l['serial'] . '</li>');
}
?>
</ul>
</div>
<script>
function doPrint() {
var el = document.getElementById('print');
el.setAttribute('disabled', true);
el.innerText = 'Requesting...';
el = el.parentElement;
fetch(<?php echo json_encode(SERVER_ENDPOINT); ?>, {
credentials: 'omit',
method: 'POST',
headers: {
'Authorization': 'Bearer <?php echo ACCESS_KEY; ?>',
},
// For readability
body: JSON.stringify(<?php echo json_encode($labels); ?>),
}).then(r => r.text())
.then(r => el.innerText = r)
.catch(e => el.innerText = 'Error: ' + e);
}
</script>
</body>

</html>

0 comments on commit 0070d9a

Please sign in to comment.