diff --git a/README.md b/README.md
index fe6b299..562c84e 100644
--- a/README.md
+++ b/README.md
@@ -24,6 +24,8 @@ Add `@include('mirasvitlabel::product.label')` where you'd like to display the l
### Category page
Add `@include('mirasvitlabel::category.label')` in: `resources/views/vendor/rapidez/category/partials/listing/item.blade.php`.
+Don't forget to add `mirasvit_labels` to `config/rapidez/searchkit.php` `result_attributes`!
+
## Notes
Not all features of Mirasvit product labels are integrated yet. Missing features are:
* Label directions
diff --git a/composer.json b/composer.json
index 30b899f..2a671dc 100644
--- a/composer.json
+++ b/composer.json
@@ -18,7 +18,7 @@
"prefer-stable": true,
"require": {
"php": "^8.1",
- "rapidez/core": "^2.0|^3.0|^4.0"
+ "rapidez/core": "^5.0"
},
"autoload": {
"psr-4": {
diff --git a/resources/views/category/label.blade.php b/resources/views/category/label.blade.php
index 25e26dc..8cb6829 100644
--- a/resources/views/category/label.blade.php
+++ b/resources/views/category/label.blade.php
@@ -1,16 +1,21 @@
-
- @{{ label.cat_title }}
-
+
+
+
+
+ @{{ label.title }}
+
+
+
+
diff --git a/resources/views/product/label.blade.php b/resources/views/product/label.blade.php
index c6a5cff..d29eaba 100644
--- a/resources/views/product/label.blade.php
+++ b/resources/views/product/label.blade.php
@@ -1,18 +1,24 @@
-@foreach($product->mirasvit_label as $label)
- @if($label->prod_title)
- str($label->prod_position)->contains('top'),
- 'bottom-0' => str($label->prod_position)->contains('bottom'),
- 'right-0' => str($label->prod_position)->contains('right'),
- 'left-0' => str($label->prod_position)->contains('left'),
- 'top-1/2 -translate-y-1/2' => str($label->prod_position)->contains('middle'),
- 'right-1/2 -translate-x-1/2' => str($label->prod_position)->contains('center'),
- ])
- >
- {{ $label->prod_title }}
-
- @endif
-@endforeach
\ No newline at end of file
+@foreach(collect($product->mirasvit_labels)->sortBy('sort_order')->groupBy('viewLabel.position') as $position => $labels)
+ @php([$y, $x] = str_split($position))
+
$x === 'L', // Left
+ 'right-2.5 items-end' => $x === 'R', // Right
+ 'left-1/2 -translate-x-1/2' => $x === 'C', // Center (horizontally)
+
+ 'top-2.5' => $y === 'T', // Top
+ 'bottom-2.5' => $y === 'B', // Bottom
+ 'top-1/2 -translate-y-1/2' => $y === 'M', // Middle (vertically)
+ ])
+ >
+ @foreach($labels as $label)
+ @continue(!$label->viewLabel)
+
+ {{ $label->viewLabel->title }}
+
+ @endforeach
+
+@endforeach
diff --git a/src/MirasvitLabelServiceProvider.php b/src/MirasvitLabelServiceProvider.php
index 8cdac8a..cec59c4 100644
--- a/src/MirasvitLabelServiceProvider.php
+++ b/src/MirasvitLabelServiceProvider.php
@@ -3,9 +3,11 @@
namespace Rapidez\MirasvitLabel;
use Illuminate\Support\ServiceProvider;
-use Rapidez\MirasvitLabel\Models\Casts\CastMirasvitLabelVariables;
-use Rapidez\MirasvitLabel\Models\Scopes\WithMirasvitLabelsScope;
-use TorMorten\Eventy\Facades\Eventy;
+use Rapidez\Core\Models\Product;
+use Rapidez\MirasvitLabel\Models\MirasvitProductLabelIndex;
+use Rapidez\MirasvitLabel\Models\Scopes\WithMirasvitListLabels;
+use Rapidez\MirasvitLabel\Models\Scopes\WithMirasvitViewLabels;
+ use TorMorten\Eventy\Facades\Eventy;
class MirasvitLabelServiceProvider extends ServiceProvider
{
@@ -17,11 +19,23 @@ public function boot()
protected function bootEventyFilters(): static
{
- Eventy::addFilter('product.scopes', fn ($scopes) => array_merge($scopes ?: [], [WithMirasvitLabelsScope::class]));
- Eventy::addFilter('product.casts', fn ($casts) => array_merge($casts ?: [], ['mirasvit_label' => CastMirasvitLabelVariables::class]));
+ Eventy::addFilter('productpage.scopes', fn ($scopes) => array_merge($scopes ?: [], [WithMirasvitViewLabels::class]));
+ Eventy::addFilter('index.product.scopes', fn ($scopes) => array_merge($scopes ?: [], [WithMirasvitListLabels::class]));
+ Eventy::addFilter('index.product.data', function ($data, $product) {
+ /** @var Product $product */
+ $data['mirasvit_labels'] = $product->mirasvit_labels?->map?->listLabel;
+
+ return $data;
+ }, arguments: 2);
+
+ Product::resolveRelationUsing('mirasvit_labels', function ($product) {
+ /** @var Product $product */
+ return $product->hasMany(MirasvitProductLabelIndex::class, 'product_id', 'entity_id');
+ });
+
Eventy::addFilter('index.product.mapping', fn ($mapping) => array_merge_recursive($mapping ?: [], [
'properties' => [
- 'mirasvit_label' => [
+ 'mirasvit_labels' => [
'type' => 'flattened',
],
],
diff --git a/src/Models/Casts/CastMirasvitLabelVariables.php b/src/Models/Casts/CastMirasvitLabelVariables.php
deleted file mode 100644
index ffdd85e..0000000
--- a/src/Models/Casts/CastMirasvitLabelVariables.php
+++ /dev/null
@@ -1,28 +0,0 @@
-where('store_id', config('rapidez.store'));
+ });
+ }
+
+ public function displayIds(): Attribute
+ {
+ return Attribute::make(
+ get: function ($value) {
+ return explode(',', $value);
+ }
+ );
+ }
+
+ public function customerGroups(): Attribute
+ {
+ return Attribute::make(
+ get: function ($value) {
+ return explode(',', $value);
+ }
+ );
+ }
+
+ public function product(): BelongsTo
+ {
+ return $this->belongsTo(config('rapidez.models.product'), 'entity_id', 'product_id');
+ }
+
+ public function viewLabel(): HasOne
+ {
+ return $this->hasOne(MirasvitProductLabelLabelDisplay::class, 'label_id', 'label_id')->whereIn('type', ['view', 'both']);
+ }
+
+ public function listLabel(): HasOne
+ {
+ return $this->hasOne(MirasvitProductLabelLabelDisplay::class, 'label_id', 'label_id')->whereIn('type', ['list', 'both']);
+ }
+}
diff --git a/src/Models/MirasvitProductLabelLabelDisplay.php b/src/Models/MirasvitProductLabelLabelDisplay.php
new file mode 100644
index 0000000..67adc15
--- /dev/null
+++ b/src/Models/MirasvitProductLabelLabelDisplay.php
@@ -0,0 +1,63 @@
+hasOne(MirasvitProductLabelPlaceholder::class, 'placeholder_id', 'placeholder_id');
+ }
+
+ protected function position(): Attribute
+ {
+ return Attribute::make(
+ get: function () {
+ return $this->placeholder->position;
+ }
+ );
+ }
+
+ protected function labelsDirection(): Attribute
+ {
+ return Attribute::make(
+ get: function () {
+ return $this->placeholder->labels_direction;
+ }
+ );
+ }
+
+ protected function labelsLimit(): Attribute
+ {
+ return Attribute::make(
+ get: function () {
+ return $this->placeholder->labels_limit;
+ }
+ );
+ }
+}
diff --git a/src/Models/MirasvitProductLabelPlaceholder.php b/src/Models/MirasvitProductLabelPlaceholder.php
new file mode 100644
index 0000000..0a277f0
--- /dev/null
+++ b/src/Models/MirasvitProductLabelPlaceholder.php
@@ -0,0 +1,12 @@
+selectRaw('JSON_REMOVE(JSON_OBJECTAGG(IFNULL(label_index.label_id, "null_"), JSON_OBJECT(
- "prod_title", prod.title,
- "prod_position", prod_position.code,
- "prod_style", prod.style,
- "cat_title", cat.title,
- "cat_position", cat_position.code,
- "cat_style", cat.style
- )), "$.null__") as mirasvit_label')
- ->leftJoin('mst_productlabel_index as label_index', function($join) use ($model) {
- $join->on('label_index.product_id', '=', $model->getTable() .'.entity_id')
- ->where('label_index.store_id', config('rapidez.store'));
- })
- ->leftJoin('mst_productlabel_label_display as prod', function($join) {
- $join->on('prod.label_id', '=', 'label_index.label_id')
- ->whereIn('prod.type', ['view', 'both']);
- })
- ->leftJoin('mst_productlabel_label_display as cat', function($join) {
- $join->on('cat.label_id', '=', 'label_index.label_id')
- ->whereIn('cat.type', ['list', 'both']);
- })
- ->leftJoin('mst_productlabel_placeholder as cat_position', 'cat_position.placeholder_id', '=', 'cat.placeholder_id')
- ->leftJoin('mst_productlabel_placeholder as prod_position', 'prod_position.placeholder_id', '=', 'prod.placeholder_id');
- }
-}
diff --git a/src/Models/Scopes/WithMirasvitListLabels.php b/src/Models/Scopes/WithMirasvitListLabels.php
new file mode 100644
index 0000000..fae7511
--- /dev/null
+++ b/src/Models/Scopes/WithMirasvitListLabels.php
@@ -0,0 +1,16 @@
+with('mirasvit_labels.listLabel.placeholder');
+ }
+}
diff --git a/src/Models/Scopes/WithMirasvitViewLabels.php b/src/Models/Scopes/WithMirasvitViewLabels.php
new file mode 100644
index 0000000..be5c15e
--- /dev/null
+++ b/src/Models/Scopes/WithMirasvitViewLabels.php
@@ -0,0 +1,16 @@
+with('mirasvit_labels.viewLabel.placeholder');
+ }
+}