2022-11-01 04:56:53 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\View;
|
|
|
|
|
2022-11-10 02:54:02 -08:00
|
|
|
use App\Models\Labels\Field;
|
2022-11-01 04:56:53 -07:00
|
|
|
use App\Models\Labels\Label as LabelModel;
|
|
|
|
use App\Models\Labels\Sheet;
|
|
|
|
use Illuminate\Contracts\View\View;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Illuminate\Support\Traits\Macroable;
|
|
|
|
use TCPDF;
|
|
|
|
|
|
|
|
class Label implements View
|
|
|
|
{
|
|
|
|
use Macroable { __call as macroCall; }
|
|
|
|
|
|
|
|
protected const NAME = 'label';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Collection of passed data.
|
|
|
|
*
|
|
|
|
* @var Collection
|
|
|
|
*/
|
|
|
|
protected $data;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->data = new Collection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the PDF label.
|
|
|
|
*
|
|
|
|
* @param callable|null $callback
|
|
|
|
*/
|
|
|
|
public function render(callable $callback = null)
|
|
|
|
{
|
|
|
|
$settings = $this->data->get('settings');
|
|
|
|
$assets = $this->data->get('assets');
|
|
|
|
$offset = $this->data->get('offset');
|
2022-11-02 01:04:40 -07:00
|
|
|
$template = $this->data->get('template');
|
2022-11-01 04:56:53 -07:00
|
|
|
|
|
|
|
// If disabled, pass to legacy view
|
2022-11-02 01:04:40 -07:00
|
|
|
if ((!$settings->label2_enable) && (!$template)) {
|
2022-11-01 04:56:53 -07:00
|
|
|
return view('hardware/labels')
|
|
|
|
->with('assets', $assets)
|
|
|
|
->with('settings', $settings)
|
|
|
|
->with('bulkedit', $this->data->get('bulkedit'))
|
|
|
|
->with('count', $this->data->get('count'));
|
|
|
|
}
|
|
|
|
|
2022-11-02 01:04:40 -07:00
|
|
|
if (empty($template)) $template = LabelModel::find($settings->label2_template);
|
|
|
|
elseif (is_string($template)) $template = LabelModel::find($template);
|
2022-11-01 04:56:53 -07:00
|
|
|
|
|
|
|
$template->validate();
|
|
|
|
|
|
|
|
$pdf = new TCPDF(
|
|
|
|
$template->getOrientation(),
|
|
|
|
$template->getUnit(),
|
|
|
|
[ $template->getWidth(), $template->getHeight() ]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reset parameters
|
|
|
|
$pdf->SetPrintHeader(false);
|
|
|
|
$pdf->SetPrintFooter(false);
|
|
|
|
$pdf->SetAutoPageBreak(false);
|
|
|
|
$pdf->SetMargins(0, 0, null, true);
|
|
|
|
$pdf->SetCellMargins(0, 0, 0, 0);
|
|
|
|
$pdf->SetCellPaddings(0, 0, 0, 0);
|
|
|
|
$pdf->setCreator('Snipe-IT');
|
|
|
|
$pdf->SetSubject('Asset Labels');
|
|
|
|
$template->preparePDF($pdf);
|
|
|
|
|
2022-11-10 02:54:02 -08:00
|
|
|
// Get fields from settings
|
|
|
|
$fieldDefinitions = collect(explode(';', $settings->label2_fields))
|
|
|
|
->filter(fn($fieldString) => !empty($fieldString))
|
|
|
|
->map(fn($fieldString) => Field::fromString($fieldString));
|
2022-11-01 04:56:53 -07:00
|
|
|
|
|
|
|
// Prepare data
|
|
|
|
$data = $assets
|
|
|
|
->map(function ($asset) use ($template, $settings, $fieldDefinitions) {
|
|
|
|
|
|
|
|
$assetData = new Collection();
|
|
|
|
|
2022-11-02 02:20:01 -07:00
|
|
|
$assetData->put('asset', $asset);
|
2022-11-01 04:56:53 -07:00
|
|
|
$assetData->put('id', $asset->id);
|
|
|
|
$assetData->put('tag', $asset->asset_tag);
|
|
|
|
|
|
|
|
if ($template->getSupportTitle()) {
|
2022-11-02 01:05:52 -07:00
|
|
|
$title = !empty($settings->label2_title) ?
|
2023-08-15 12:27:47 -07:00
|
|
|
str_ireplace('{COMPANY}', $asset->company->name, $settings->label2_title) :
|
2022-11-02 01:05:52 -07:00
|
|
|
$settings->qr_text;
|
|
|
|
if (!empty($title)) $assetData->put('title', $title);
|
2022-11-01 04:56:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($template->getSupportLogo()) {
|
2022-11-02 01:05:52 -07:00
|
|
|
$logo = $settings->label2_asset_logo ?
|
2022-11-01 04:56:53 -07:00
|
|
|
(
|
|
|
|
!empty($asset->company->image) ?
|
|
|
|
Storage::disk('public')->path('companies/'.e($asset->company->image)) :
|
|
|
|
null
|
|
|
|
) :
|
|
|
|
(
|
|
|
|
!empty($settings->label_logo) ?
|
|
|
|
Storage::disk('public')->path(''.e($settings->label_logo)) :
|
|
|
|
null
|
2022-11-02 01:05:52 -07:00
|
|
|
);
|
|
|
|
if (!empty($logo)) $assetData->put('logo', $logo);
|
2022-11-01 04:56:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($template->getSupport1DBarcode()) {
|
|
|
|
$barcode1DType = $settings->label2_1d_type;
|
|
|
|
$barcode1DType = ($barcode1DType == 'default') ?
|
|
|
|
(($settings->alt_barcode_enabled) ? $settings->alt_barcode : null) :
|
|
|
|
$barcode1DType;
|
2022-11-02 01:05:52 -07:00
|
|
|
if ($barcode1DType != 'none') {
|
2022-11-01 04:56:53 -07:00
|
|
|
$assetData->put('barcode1d', (object)[
|
|
|
|
'type' => $barcode1DType,
|
|
|
|
'content' => $asset->asset_tag,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($template->getSupport2DBarcode()) {
|
|
|
|
$barcode2DType = $settings->label2_2d_type;
|
|
|
|
$barcode2DType = ($barcode2DType == 'default') ?
|
2023-01-31 22:48:39 -08:00
|
|
|
$settings->barcode_type :
|
2022-11-01 04:56:53 -07:00
|
|
|
$barcode2DType;
|
2023-01-31 22:48:39 -08:00
|
|
|
if (($barcode2DType != 'none') && (!is_null($barcode2DType))) {
|
2022-11-01 04:56:53 -07:00
|
|
|
switch ($settings->label2_2d_target) {
|
|
|
|
case 'ht_tag': $barcode2DTarget = route('ht/assetTag', $asset->asset_tag); break;
|
|
|
|
case 'hardware_id':
|
|
|
|
default: $barcode2DTarget = route('hardware.show', $asset->id); break;
|
|
|
|
}
|
|
|
|
$assetData->put('barcode2d', (object)[
|
|
|
|
'type' => $barcode2DType,
|
|
|
|
'content' => $barcode2DTarget,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = $fieldDefinitions
|
2022-11-10 02:54:02 -08:00
|
|
|
->map(fn($field) => $field->toArray($asset))
|
|
|
|
->filter(fn($field) => $field != null)
|
|
|
|
->reduce(function($myFields, $field) {
|
|
|
|
// Remove Duplicates
|
|
|
|
$toAdd = $field
|
|
|
|
->filter(fn($o) => !$myFields->contains('dataSource', $o['dataSource']))
|
|
|
|
->first();
|
|
|
|
|
|
|
|
return $toAdd ? $myFields->push($toAdd) : $myFields;
|
2022-11-01 04:56:53 -07:00
|
|
|
}, new Collection());
|
2022-11-10 02:54:02 -08:00
|
|
|
|
2022-11-01 04:56:53 -07:00
|
|
|
$assetData->put('fields', $fields->take($template->getSupportFields()));
|
|
|
|
|
|
|
|
return $assetData;
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($template instanceof Sheet) {
|
|
|
|
$template->setLabelIndexOffset($offset ?? 0);
|
|
|
|
}
|
|
|
|
$template->writeAll($pdf, $data);
|
|
|
|
|
|
|
|
$filename = $assets->count() > 1 ? 'assets.pdf' : $assets->first()->asset_tag.'.pdf';
|
|
|
|
$pdf->Output($filename, 'I');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a piece of data.
|
|
|
|
*
|
|
|
|
* @param string|array $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function with($key, $value = null)
|
|
|
|
{
|
|
|
|
$this->data->put($key, $value);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the array of view data.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the view.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function name()
|
|
|
|
{
|
|
|
|
return $this->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the view.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return self::NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|