Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
37 changes: 21 additions & 16 deletions resources/views/category/label.blade.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
<span
v-for="label in item.mirasvit_label"
v-if="label.cat_title"
v-bind:style="label.cat_style"
class="absolute transform"
:class="{
'top-0': label.cat_position.includes('top'),
'bottom-0': label.cat_position.includes('bottom'),
'right-0': label.cat_position.includes('right'),
'left-0': label.cat_position.includes('left'),
'top-1/2 -translate-y-1/2': label.cat_position.includes('middle'),
'right-1/2 -translate-x-1/2': label.cat_position.includes('center')
}"
>
@{{ label.cat_title }}
</span>
<template v-for="(labels, position) in Object.groupBy(Object.values(item.mirasvit_labels ? item.mirasvit_labels : {}), (element) => element.position)">
<div class="absolute z-10 flex flex-col gap-1" :class="{
'left-1': position[1] === 'L', // Left
'right-1 items-end': position[1] === 'R', // Right
'left-1/2 -translate-x-1/2': position[1] === 'C', // Center (horizontally)

'top-1': position[0] === 'T', // Top
'bottom-1': position[0] === 'B', // Bottom
'top-1/2 -translate-y-1/2': position[0] === 'M', // Middle (vertically)
}">
<div
v-for="label in labels.filter(label => label.title)"
class="relative"
v-bind:style="label.style"
>
<span>
@{{ label.title }}
</span>
</div>
</div>
</template>
42 changes: 24 additions & 18 deletions resources/views/product/label.blade.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
@foreach($product->mirasvit_label as $label)
@if($label->prod_title)
<span
style="{{ $label->prod_style }}"
@class([
'absolute transform',
'top-0' => 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 }}
</span>
@endif
@endforeach
@foreach(collect($product->mirasvit_labels)->sortBy('sort_order')->groupBy('viewLabel.position') as $position => $labels)
@php([$y, $x] = str_split($position))
<div @class([
'absolute z-10 flex flex-col gap-1',

'left-2.5' => $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)
<span
style="{{ $label->viewLabel->style }}"
>
{{ $label->viewLabel->title }}
</span>
@endforeach
</div>
@endforeach
26 changes: 20 additions & 6 deletions src/MirasvitLabelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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',
],
],
Expand Down
28 changes: 0 additions & 28 deletions src/Models/Casts/CastMirasvitLabelVariables.php

This file was deleted.

56 changes: 56 additions & 0 deletions src/Models/MirasvitProductLabelIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Rapidez\MirasvitLabel\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Rapidez\Core\Models\Model;

class MirasvitProductLabelIndex extends Model
{
protected $primaryKey = 'id';

protected $table = 'mst_productlabel_index';

protected static function booted(): void
{
static::addGlobalScope('default', function (Builder $query) {
$query->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']);
}
}
63 changes: 63 additions & 0 deletions src/Models/MirasvitProductLabelLabelDisplay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Rapidez\MirasvitLabel\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Rapidez\Core\Models\Model;
use Rapidez\MirasvitLabel\Models\MirasvitProductLabelPlaceholder;

class MirasvitProductLabelLabelDisplay extends Model
{
protected $primaryKey = 'display_id';

protected $table = 'mst_productlabel_label_display';

protected $appends = [
'position',
'labels_direction',
'labels_limit',
];

protected $hidden = [
'placeholder',
'display_id',
'type',
'label_id',
'placeholder_id',
'template_id',
'attribute_option_id',
];

public function placeholder(): HasOne
{
return $this->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;
}
);
}
}
12 changes: 12 additions & 0 deletions src/Models/MirasvitProductLabelPlaceholder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rapidez\MirasvitLabel\Models;

use Rapidez\Core\Models\Model;

class MirasvitProductLabelPlaceholder extends Model
{
protected $primaryKey = 'placeholder_id';

protected $table = 'mst_productlabel_placeholder';
}
37 changes: 0 additions & 37 deletions src/Models/Scopes/WithMirasvitLabelsScope.php

This file was deleted.

16 changes: 16 additions & 0 deletions src/Models/Scopes/WithMirasvitListLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rapidez\MirasvitLabel\Models\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class WithMirasvitListLabels implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->with('mirasvit_labels.listLabel.placeholder');
}
}
16 changes: 16 additions & 0 deletions src/Models/Scopes/WithMirasvitViewLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rapidez\MirasvitLabel\Models\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class WithMirasvitViewLabels implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->with('mirasvit_labels.viewLabel.placeholder');
}
}