From f849fcca894f1ee37e51243a45ce65797bbd0ae4 Mon Sep 17 00:00:00 2001 From: Cram42 Date: Thu, 10 Nov 2022 18:54:02 +0800 Subject: [PATCH] Change the way fields are passed --- app/Models/Labels/Field.php | 39 +++++++++++ app/Models/Labels/FieldOption.php | 49 ++++++++++++++ app/Models/Labels/Sheets/Avery/L7162_A.php | 6 +- app/Models/Labels/Sheets/Avery/L7162_B.php | 6 +- app/Models/Labels/Sheets/Avery/L7163_A.php | 6 +- .../Labels/Tapes/Brother/TZe_12mm_A.php | 2 +- .../Labels/Tapes/Brother/TZe_24mm_A.php | 6 +- app/View/Label.php | 64 +++++-------------- 8 files changed, 116 insertions(+), 62 deletions(-) create mode 100644 app/Models/Labels/Field.php create mode 100644 app/Models/Labels/FieldOption.php diff --git a/app/Models/Labels/Field.php b/app/Models/Labels/Field.php new file mode 100644 index 0000000000..b264c7ac29 --- /dev/null +++ b/app/Models/Labels/Field.php @@ -0,0 +1,39 @@ +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; + } +} \ No newline at end of file diff --git a/app/Models/Labels/FieldOption.php b/app/Models/Labels/FieldOption.php new file mode 100644 index 0000000000..76427accaf --- /dev/null +++ b/app/Models/Labels/FieldOption.php @@ -0,0 +1,49 @@ +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; + } + } +} \ No newline at end of file diff --git a/app/Models/Labels/Sheets/Avery/L7162_A.php b/app/Models/Labels/Sheets/Avery/L7162_A.php index c6d10cc5f7..0b3312ba7c 100644 --- a/app/Models/Labels/Sheets/Avery/L7162_A.php +++ b/app/Models/Labels/Sheets/Avery/L7162_A.php @@ -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 diff --git a/app/Models/Labels/Sheets/Avery/L7162_B.php b/app/Models/Labels/Sheets/Avery/L7162_B.php index 8aea715e00..268754e04f 100644 --- a/app/Models/Labels/Sheets/Avery/L7162_B.php +++ b/app/Models/Labels/Sheets/Avery/L7162_B.php @@ -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 diff --git a/app/Models/Labels/Sheets/Avery/L7163_A.php b/app/Models/Labels/Sheets/Avery/L7163_A.php index 5fc3d03ee3..6dc33f64dd 100644 --- a/app/Models/Labels/Sheets/Avery/L7163_A.php +++ b/app/Models/Labels/Sheets/Avery/L7163_A.php @@ -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 diff --git a/app/Models/Labels/Tapes/Brother/TZe_12mm_A.php b/app/Models/Labels/Tapes/Brother/TZe_12mm_A.php index 786f6eca45..f89cfc5d47 100644 --- a/app/Models/Labels/Tapes/Brother/TZe_12mm_A.php +++ b/app/Models/Labels/Tapes/Brother/TZe_12mm_A.php @@ -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 diff --git a/app/Models/Labels/Tapes/Brother/TZe_24mm_A.php b/app/Models/Labels/Tapes/Brother/TZe_24mm_A.php index 36d185d64c..ea4c6c9dfb 100644 --- a/app/Models/Labels/Tapes/Brother/TZe_24mm_A.php +++ b/app/Models/Labels/Tapes/Brother/TZe_24mm_A.php @@ -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 diff --git a/app/View/Label.php b/app/View/Label.php index eef17785f3..67742d88e6 100644 --- a/app/View/Label.php +++ b/app/View/Label.php @@ -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;