¨4.0.1¨
This commit is contained in:
174
Modules/Variation/Entities/Variation.php
Normal file
174
Modules/Variation/Entities/Variation.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Variation\Entities;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
use Modules\Variation\Admin\VariationTable;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Variation extends Model
|
||||
{
|
||||
use Translatable, SoftDeletes;
|
||||
|
||||
/**
|
||||
* Available variation types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
const TYPES = ['text', 'color', 'image'];
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations', 'values'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['uid', 'type', 'is_global', 'position'];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'is_global' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = ['deleted_at'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected array $translatedAttributes = ['name'];
|
||||
|
||||
|
||||
/**
|
||||
* Perform any actions required after the model boots.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saved(function ($variation) {
|
||||
if (request()->routeIs('admin.variations.*')) {
|
||||
$variation->saveValuesForGlobal();
|
||||
}
|
||||
|
||||
if (request()->routeIs('admin.products.*')) {
|
||||
$variation->saveValuesForLocal();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save values for the variation.
|
||||
*
|
||||
* @param array $values
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function saveValues(array $values = []): void
|
||||
{
|
||||
$ids = $this->getDeleteCandidates($values);
|
||||
|
||||
if ($ids->isNotEmpty()) {
|
||||
$this->values()
|
||||
->whereIn('id', $ids)
|
||||
->delete();
|
||||
}
|
||||
|
||||
$counter = 0;
|
||||
|
||||
foreach (array_reset_index($values) as $attributes) {
|
||||
$attributes += ['position' => ++$counter];
|
||||
$attributes += ['value' => $attributes['color'] ?? ''];
|
||||
|
||||
$this->values()->updateOrCreate(
|
||||
[
|
||||
'id' => array_get($attributes, 'id'),
|
||||
],
|
||||
$attributes,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the values for the variation.
|
||||
*
|
||||
* @return HasMany
|
||||
*/
|
||||
public function values(): HasMany
|
||||
{
|
||||
return $this->hasMany(VariationValue::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scope a query to only include global variations.
|
||||
*
|
||||
* @param Builder $query
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function scopeGlobals(Builder $query): Builder
|
||||
{
|
||||
return $query->where('is_global', true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get table data for the resource
|
||||
*
|
||||
* @return VariationTable
|
||||
*/
|
||||
public function table(): VariationTable
|
||||
{
|
||||
return new VariationTable($this->newQuery()->globals());
|
||||
}
|
||||
|
||||
|
||||
protected function saveValuesForGlobal()
|
||||
{
|
||||
$this->saveValues(request('values', []));
|
||||
}
|
||||
|
||||
|
||||
protected function saveValuesForLocal()
|
||||
{
|
||||
$this->saveValues(
|
||||
request('variations.' . $this->uid . '.values', [])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $values
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
private function getDeleteCandidates($values): Collection
|
||||
{
|
||||
return $this->values()
|
||||
->pluck('id')
|
||||
->diff(array_pluck($values, 'id'));
|
||||
}
|
||||
}
|
||||
15
Modules/Variation/Entities/VariationTranslation.php
Normal file
15
Modules/Variation/Entities/VariationTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Variation\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class VariationTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['name'];
|
||||
}
|
||||
99
Modules/Variation/Entities/VariationValue.php
Normal file
99
Modules/Variation/Entities/VariationValue.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Variation\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\Model;
|
||||
use Modules\Media\Eloquent\HasMedia;
|
||||
use Modules\Support\Eloquent\Translatable;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
|
||||
class VariationValue extends Model
|
||||
{
|
||||
use Translatable, HasMedia;
|
||||
|
||||
/**
|
||||
* The relations to eager load on every query.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $with = ['translations'];
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['uid', 'value', 'position'];
|
||||
|
||||
/**
|
||||
* The attributes that are translatable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected array $translatedAttributes = ['label'];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected $appends = ['color', 'image'];
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getColorAttribute(): mixed
|
||||
{
|
||||
return $this->value ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getImageAttribute(): mixed
|
||||
{
|
||||
return $this->files()->first() ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return BelongsTo
|
||||
*/
|
||||
public function variation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Variation::class);
|
||||
}
|
||||
|
||||
|
||||
protected function extractMediaFromRequest()
|
||||
{
|
||||
if (request()->routeIs('admin.variations.*')) {
|
||||
return $this->extractMediaForGlobal();
|
||||
}
|
||||
|
||||
if (request()->routeIs('admin.products.*')) {
|
||||
return $this->extractMediaForLocal();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function extractMediaForGlobal()
|
||||
{
|
||||
if (request('type') === 'image') {
|
||||
return [
|
||||
'media' => [request('values.' . $this->uid . '.image')],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function extractMediaForLocal()
|
||||
{
|
||||
if ($this->variation->type === 'image') {
|
||||
return [
|
||||
'media' => [request('variations.' . $this->variation->uid . '.values.' . $this->uid . '.image')],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
15
Modules/Variation/Entities/VariationValueTranslation.php
Normal file
15
Modules/Variation/Entities/VariationValueTranslation.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Variation\Entities;
|
||||
|
||||
use Modules\Support\Eloquent\TranslationModel;
|
||||
|
||||
class VariationValueTranslation extends TranslationModel
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['label'];
|
||||
}
|
||||
Reference in New Issue
Block a user