mirror of
https://github.com/snipe/snipe-it.git
synced 2025-02-02 08:21:09 -08:00
Change the way fields are passed
This commit is contained in:
parent
fd9616683c
commit
f849fcca89
39
app/Models/Labels/Field.php
Normal file
39
app/Models/Labels/Field.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\Labels;
|
||||
|
||||
use App\Models\Asset;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class Field {
|
||||
protected Collection $options;
|
||||
public function getOptions() { return $this->options; }
|
||||
public function setOptions($options) {
|
||||
$tempCollect = collect($options);
|
||||
if (!$tempCollect->contains(fn($o) => !is_subclass_of($o, FieldOption::class))) {
|
||||
$this->options = $options;
|
||||
}
|
||||
}
|
||||
|
||||
public function toArray(Asset $asset) { return Field::makeArray($this, $asset); }
|
||||
|
||||
/* Statics */
|
||||
|
||||
public static function makeArray(Field $field, Asset $asset) {
|
||||
return $field->getOptions()
|
||||
->map(fn($option) => $option->toArray($asset))
|
||||
->filter(fn($result) => $result['value'] != null);
|
||||
}
|
||||
|
||||
public static function makeString(Field $option) {
|
||||
return implode('|', $option->getOptions());
|
||||
}
|
||||
|
||||
public static function fromString(string $theString) {
|
||||
$field = new Field();
|
||||
$field->options = collect(explode('|', $theString))
|
||||
->filter(fn($optionString) => !empty($optionString))
|
||||
->map(fn($optionString) => FieldOption::fromString($optionString));
|
||||
return $field;
|
||||
}
|
||||
}
|
49
app/Models/Labels/FieldOption.php
Normal file
49
app/Models/Labels/FieldOption.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models\Labels;
|
||||
|
||||
use App\Models\Asset;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class FieldOption {
|
||||
protected string $label;
|
||||
public function getLabel() { return $this->label; }
|
||||
|
||||
protected string $dataSource;
|
||||
public function getDataSource() { return $this->dataSource; }
|
||||
|
||||
public function getValue(Asset $asset) {
|
||||
$dataPath = collect(explode('.', $this->dataSource));
|
||||
return $dataPath->reduce(function ($myValue, $path) {
|
||||
try { return $myValue ? $myValue->{$path} : ${$myValue}; }
|
||||
catch (\Exception $e) { return $myValue; }
|
||||
}, $asset);
|
||||
}
|
||||
|
||||
public function toArray(Asset $asset=null) { return FieldOption::makeArray($this, $asset); }
|
||||
public function toString() { return FieldOption::makeString($this); }
|
||||
|
||||
/* Statics */
|
||||
|
||||
public static function makeArray(FieldOption $option, Asset $asset=null) {
|
||||
return [
|
||||
'label' => $option->getLabel(),
|
||||
'dataSource' => $option->getDataSource(),
|
||||
'value' => $asset ? $option->getValue($asset) : null
|
||||
];
|
||||
}
|
||||
|
||||
public static function makeString(FieldOption $option) {
|
||||
return $option->getLabel() . '=' . $option->getDataSource();
|
||||
}
|
||||
|
||||
public static function fromString(string $theString) {
|
||||
$parts = explode('=', $theString);
|
||||
if (count($parts) == 2) {
|
||||
$option = new FieldOption();
|
||||
$option->label = $parts[0];
|
||||
$option->dataSource = $parts[1];
|
||||
return $option;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -75,9 +75,9 @@ class L7162_A extends L7162
|
|||
$currentY += self::TITLE_SIZE + self::TITLE_MARGIN;
|
||||
}
|
||||
|
||||
foreach ($record->get('fields') as $label => $value) {
|
||||
foreach ($record->get('fields') as $field) {
|
||||
static::writeText(
|
||||
$pdf, $label,
|
||||
$pdf, $field['label'],
|
||||
$currentX, $currentY,
|
||||
'freesans', '', self::LABEL_SIZE, 'L',
|
||||
$usableWidth, self::LABEL_SIZE, true, 0
|
||||
|
@ -85,7 +85,7 @@ class L7162_A extends L7162
|
|||
$currentY += self::LABEL_SIZE + self::LABEL_MARGIN;
|
||||
|
||||
static::writeText(
|
||||
$pdf, $value,
|
||||
$pdf, $field['value'],
|
||||
$currentX, $currentY,
|
||||
'freemono', 'B', self::FIELD_SIZE, 'L',
|
||||
$usableWidth, self::FIELD_SIZE, true, 0, 0.3
|
||||
|
|
|
@ -71,9 +71,9 @@ class L7162_B extends L7162
|
|||
$currentY += self::TITLE_SIZE + self::TITLE_MARGIN;
|
||||
}
|
||||
|
||||
foreach ($record->get('fields') as $label => $value) {
|
||||
foreach ($record->get('fields') as $field) {
|
||||
static::writeText(
|
||||
$pdf, $label,
|
||||
$pdf, $field['label'],
|
||||
$currentX, $currentY,
|
||||
'freesans', '', self::LABEL_SIZE, 'L',
|
||||
$usableWidth, self::LABEL_SIZE, true, 0
|
||||
|
@ -81,7 +81,7 @@ class L7162_B extends L7162
|
|||
$currentY += self::LABEL_SIZE + self::LABEL_MARGIN;
|
||||
|
||||
static::writeText(
|
||||
$pdf, $value,
|
||||
$pdf, $field['value'],
|
||||
$currentX, $currentY,
|
||||
'freemono', 'B', self::FIELD_SIZE, 'L',
|
||||
$usableWidth, self::FIELD_SIZE, true, 0, 0.3
|
||||
|
|
|
@ -73,9 +73,9 @@ class L7163_A extends L7163
|
|||
);
|
||||
}
|
||||
|
||||
foreach ($record->get('fields') as $label => $value) {
|
||||
foreach ($record->get('fields') as $field) {
|
||||
static::writeText(
|
||||
$pdf, $label,
|
||||
$pdf, $field['label'],
|
||||
$currentX, $currentY,
|
||||
'freesans', '', self::LABEL_SIZE, 'L',
|
||||
$usableWidth, self::LABEL_SIZE, true, 0
|
||||
|
@ -83,7 +83,7 @@ class L7163_A extends L7163
|
|||
$currentY += self::LABEL_SIZE + self::LABEL_MARGIN;
|
||||
|
||||
static::writeText(
|
||||
$pdf, $value,
|
||||
$pdf, $field['value'],
|
||||
$currentX, $currentY,
|
||||
'freemono', 'B', self::FIELD_SIZE, 'L',
|
||||
$usableWidth, self::FIELD_SIZE, true, 0, 0.5
|
||||
|
|
|
@ -45,7 +45,7 @@ class TZe_12mm_A extends TZe_12mm
|
|||
|
||||
if ($record->get('fields')->count() >= 1) {
|
||||
static::writeText(
|
||||
$pdf, $record->get('fields')->values()->get(0),
|
||||
$pdf, $record->get('fields')->values()->get(0)['value'],
|
||||
$pa->x1 + ($tagWidth), $currentY,
|
||||
'freemono', 'b', $fontSize, 'R',
|
||||
$fieldWidth, $usableHeight, true, 0, 0
|
||||
|
|
|
@ -66,9 +66,9 @@ class TZe_24mm_A extends TZe_24mm
|
|||
$currentY += self::TITLE_SIZE + self::TITLE_MARGIN;
|
||||
}
|
||||
|
||||
foreach ($record->get('fields') as $label => $value) {
|
||||
foreach ($record->get('fields') as $field) {
|
||||
static::writeText(
|
||||
$pdf, $label,
|
||||
$pdf, $field['label'],
|
||||
$currentX, $currentY,
|
||||
'freesans', '', self::LABEL_SIZE, 'L',
|
||||
$usableWidth, self::LABEL_SIZE, true, 0, 0
|
||||
|
@ -76,7 +76,7 @@ class TZe_24mm_A extends TZe_24mm
|
|||
$currentY += self::LABEL_SIZE + self::LABEL_MARGIN;
|
||||
|
||||
static::writeText(
|
||||
$pdf, $value,
|
||||
$pdf, $field['value'],
|
||||
$currentX, $currentY,
|
||||
'freemono', 'B', self::FIELD_SIZE, 'L',
|
||||
$usableWidth, self::FIELD_SIZE, true, 0, 0.3
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\View;
|
||||
|
||||
use App\Models\Labels\Field;
|
||||
use App\Models\Labels\Label as LabelModel;
|
||||
use App\Models\Labels\Sheet;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
@ -70,37 +71,10 @@ class Label implements View
|
|||
$pdf->SetSubject('Asset Labels');
|
||||
$template->preparePDF($pdf);
|
||||
|
||||
// 'Label1=field1|Alt1=altfield1;Label2=field2;Alt1=altfield1|Label3=field3'
|
||||
$fieldDefinitions = (new Collection())
|
||||
->merge(explode(';', $settings->label2_fields))
|
||||
->filter(function ($defString) {
|
||||
return strpos($defString, '=') !== false;
|
||||
})
|
||||
->map(function ($defString) {
|
||||
return (new Collection())
|
||||
->merge(explode('|', $defString)) // ['Label1=field1', 'Alt1=altfield1']
|
||||
->mapWithKeys(function ($altString) {
|
||||
$parts = explode('=', $altString);
|
||||
if (count($parts) != 2) throw new \Exception(var_export($parts, true));
|
||||
return [ $parts[0] => $parts[1] ];
|
||||
});
|
||||
});
|
||||
/*
|
||||
$fieldDefinitions should now look like:
|
||||
[
|
||||
[
|
||||
'Label1'=>'field1',
|
||||
'Alt1'=>'altfield1'
|
||||
],
|
||||
[
|
||||
'Label2'=>'field2'
|
||||
],
|
||||
[
|
||||
'Alt1'=>'altfield1',
|
||||
'Label3'=>'field3'
|
||||
]
|
||||
]
|
||||
*/
|
||||
// Get fields from settings
|
||||
$fieldDefinitions = collect(explode(';', $settings->label2_fields))
|
||||
->filter(fn($fieldString) => !empty($fieldString))
|
||||
->map(fn($fieldString) => Field::fromString($fieldString));
|
||||
|
||||
// Prepare data
|
||||
$data = $assets
|
||||
|
@ -166,25 +140,17 @@ class Label implements View
|
|||
}
|
||||
|
||||
$fields = $fieldDefinitions
|
||||
->map(function ($group, $index) use ($asset) {
|
||||
return $group->mapWithKeys(function ($definition, $label) use ($asset) {
|
||||
$value = collect(explode('.', $definition))
|
||||
->reduce(function ($carry, $chunk) {
|
||||
return $carry ? $carry->{$chunk} : ${$carry};
|
||||
}, $asset);
|
||||
return [ $label => $value ];
|
||||
});
|
||||
})
|
||||
->reduce(function ($carry, $group, $index) {
|
||||
$values = $group
|
||||
->filter(function ($value, $label) use ($carry) {
|
||||
if (empty($value)) return false;
|
||||
if ($carry->has($label)) return false;
|
||||
return true;
|
||||
})
|
||||
->take(1);
|
||||
return $carry->merge($values);
|
||||
->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;
|
||||
}, new Collection());
|
||||
|
||||
$assetData->put('fields', $fields->take($template->getSupportFields()));
|
||||
|
||||
return $assetData;
|
||||
|
|
Loading…
Reference in a new issue