Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 1a362cc
Author: Ivanka Todorova <1038697+fakeheal@users.noreply.github.com>
Date:   Tue Feb 14 13:25:25 2023 +0200

    Fix validation of attributes of type TranslatedText (#839)

    * fix: validation rules to be applied on elements in `TranslatedText`

    * fix: "reset" the validation rules applied to each sub-field in `TranslatedText`

    * chore: add tests for validating attribute fields (required/optional) and their additional rules

    ---------

    Co-authored-by: Alec Ritson <hello@itsalec.co.uk>

commit 6c1ab08
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Tue Feb 14 10:58:15 2023 +0000

    [0.2] Hotfix - Fix collection name on creation (#852)

    * Squashed commit of the following:

    commit 02c2671
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Fri Feb 10 11:13:49 2023 +0000

        Update lines.blade.php (#849)

    commit 0780a1c
    Author: wychoong <67364036+wychoong@users.noreply.github.com>
    Date:   Fri Feb 10 18:41:36 2023 +0800

        Update CollectionTree.php (#835)

        Co-authored-by: Alec Ritson <hello@itsalec.co.uk>

    commit e558799
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Fri Feb 10 09:47:16 2023 +0000

        :beer:

    commit 8fd4842
    Merge: df6f7f7 51af420
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Fri Feb 10 09:47:03 2023 +0000

        Merge branch '0.2' of github.com:lunarphp/lunar into 0.2

    commit df6f7f7
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Fri Feb 10 09:47:00 2023 +0000

        :beer:

    commit 51af420
    Author: wychoong <67364036+wychoong@users.noreply.github.com>
    Date:   Fri Feb 10 17:43:10 2023 +0800

        [0.2] Hotfix - fix pluck on null (#834)

        * fix pluck on null

        * Update AbstractDiscount.php

        ---------

        Co-authored-by: Alec Ritson <hello@itsalec.co.uk>

    commit 7b5ce85
    Author: wychoong <67364036+wychoong@users.noreply.github.com>
    Date:   Fri Feb 10 17:16:50 2023 +0800

        [0.2] Fix/discount by product (#841)

        * fix

        * add test

        * fix discount minSpend precision

        * fix discount condition logic

        * fix

        * fix coupon check

    commit 72f8454
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Fri Feb 10 09:16:16 2023 +0000

        Hotfix [0.2] - Wrap customer group in array (#846)

        * Wrap customer group in array

        * Update ProductVariantTest.php

    commit b8773d3
    Author: Ryan Mitchell <ryan@thoughtcollective.com>
    Date:   Wed Feb 8 20:43:09 2023 +0000

        When DB::table() is used make sure connection is passed (#826)

    commit 650d9db
    Merge: dcd39cc de20aff
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Wed Feb 1 09:57:04 2023 +0000

        Merge branch 'main' into 0.2

    commit dcd39cc
    Author: Alec Ritson <hello@itsalec.co.uk>
    Date:   Tue Jan 31 07:29:15 2023 +0000

        :beer:

    * Check for attribute type when creating

commit 0942397
Author: Alec Ritson <hello@itsalec.co.uk>
Date:   Tue Feb 14 10:57:37 2023 +0000

    Update HasImages.php (#853)
  • Loading branch information
alecritson committed Feb 14, 2023
1 parent 492234b commit 06eb5f7
Show file tree
Hide file tree
Showing 4 changed files with 289 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Lunar\FieldTypes\TranslatedText;
use Lunar\Hub\Http\Livewire\Traits\MapsCollectionTree;
use Lunar\Hub\Http\Livewire\Traits\Notifies;
use Lunar\Models\Attribute;
use Lunar\Models\Collection;
use Lunar\Models\CollectionGroup;
use Lunar\Models\Language;
Expand Down Expand Up @@ -407,12 +408,22 @@ public function createCollection()
'collection.name.required' => __('adminhub::validation.generic_required'),
]);

$attribute = Attribute::whereHandle('name')->whereAttributeType(Collection::class)->first();

$attributeType = $attribute?->type ?: TranslatedText::class;

$name = $this->collection['name'];

if ($attributeType == TranslatedText::class) {
$name = [
$this->defaultLanguage => $this->collection['name'],
];
}

$collection = Collection::create([
'collection_group_id' => $this->group->id,
'attribute_data' => collect([
'name' => new TranslatedText([
$this->defaultLanguage => $this->collection['name'],
]),
'name' => new $attributeType($name),
]),
], $this->collectionParent);

Expand Down
11 changes: 4 additions & 7 deletions packages/admin/src/Http/Livewire/Traits/HasImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Livewire\TemporaryUploadedFile;
use Spatie\Activitylog\Facades\LogBatch;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Livewire\FileUploadConfiguration;

trait HasImages
{
Expand Down Expand Up @@ -253,16 +254,12 @@ public function updateImages()
->substr(0, 128)
->append('.', $file->getClientOriginalExtension());

$mediaLibaryDisk = config('media-library.disk_name');
$mediaLibaryDriverConfig = Storage::disk($mediaLibaryDisk)->getConfig();
$mediaLibaryDriver = $mediaLibaryDriverConfig['driver'];

if ($mediaLibaryDriver == 'local') {
$media = $owner->addMedia($file->getRealPath())
if (FileUploadConfiguration::isUsingS3()) {
$media = $owner->addMediaFromDisk($file->getRealPath())
->usingFileName($filename)
->toMediaCollection('images');
} else {
$media = $owner->addMediaFromDisk($file->getRealPath())
$media = $owner->addMedia($file->getRealPath())
->usingFileName($filename)
->toMediaCollection('images');
}
Expand Down
76 changes: 44 additions & 32 deletions packages/admin/src/Http/Livewire/Traits/WithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function mapAttributes()
/**
* Parse the attributes into the correct collection format.
*
* @param \Illuminate\Support\Collection $attributes
* @param \Illuminate\Support\Collection $attributes
* @return \Illuminate\Support\Collection
*/
protected function parseAttributes(Collection $attributes, $existingData, $key = 'attributeMapping')
Expand All @@ -64,7 +64,7 @@ protected function parseAttributes(Collection $attributes, $existingData, $key =
return ! class_exists($attribute->type);
})->mapWithKeys(function ($attribute) use ($key, $existingData) {
$data = $existingData ?
$existingData->first(fn ($value, $handle) => $handle == $attribute->handle)
$existingData->first(fn($value, $handle) => $handle == $attribute->handle)
: null;

$value = $data ? $data->getValue() : null;
Expand All @@ -73,24 +73,26 @@ protected function parseAttributes(Collection $attributes, $existingData, $key =
$value = $this->prepareTranslatedText($value);
}

$reference = 'a_'.$attribute->id;

return [$reference => [
'name' => $attribute->translate('name'),
'group' => $attribute->attributeGroup->translate('name'),
'group_id' => $attribute->attributeGroup->id,
'group_handle' => $attribute->attributeGroup->handle,
'group_position' => $attribute->attributeGroup->position,
'id' => $attribute->handle,
'signature' => "{$key}.{$reference}.data",
'type' => $attribute->type,
'handle' => $attribute->handle,
'configuration' => $attribute->configuration,
'required' => $attribute->required,
'view' => app()->make($attribute->type)->getView(),
'validation' => $attribute->validation_rules,
'data' => $value,
]];
$reference = 'a_' . $attribute->id;

return [
$reference => [
'name' => $attribute->translate('name'),
'group' => $attribute->attributeGroup->translate('name'),
'group_id' => $attribute->attributeGroup->id,
'group_handle' => $attribute->attributeGroup->handle,
'group_position' => $attribute->attributeGroup->position,
'id' => $attribute->handle,
'signature' => "{$key}.{$reference}.data",
'type' => $attribute->type,
'handle' => $attribute->handle,
'configuration' => $attribute->configuration,
'required' => $attribute->required,
'view' => app()->make($attribute->type)->getView(),
'validation' => $attribute->validation_rules,
'data' => $value,
]
];
});
}

Expand All @@ -103,7 +105,7 @@ public function getAttributeGroupsProperty()
->get()->map(function ($group) {
return [
'model' => $group,
'fields' => $this->attributeMapping->filter(fn ($att) => $att['group_id'] == $group->id),
'fields' => $this->attributeMapping->filter(fn($att) => $att['group_id'] == $group->id),
];
});
}
Expand Down Expand Up @@ -136,7 +138,7 @@ public function prepareAttributeData($attributes = null)
/**
* Map translated values into field types.
*
* @param array $data
* @param array $data
* @return \Lunar\FieldTypes\TranslatedText
*/
protected function mapTranslatedText($data)
Expand All @@ -152,7 +154,7 @@ protected function mapTranslatedText($data)
/**
* Prepare translated text field for Livewire modeling.
*
* @param string|array $value
* @param string|array $value
* @return array
*/
protected function prepareTranslatedText($value)
Expand Down Expand Up @@ -187,28 +189,38 @@ public function withAttributesValidationRules()
}

$validation = $attribute['validation'] ? explode(',', $attribute['validation']) : [];

$field = $attribute['signature'];

if (($attribute['required'] ?? false) || ($attribute['system'] ?? false)) {
if ($attribute['type'] == TranslatedText::class) {
// Get the default language and make that the only one required.
$field = "{$attribute['signature']}.{$this->defaultLanguage->code}";
$isRequired = ($attribute['required'] ?? false) || ($attribute['system'] ?? false);

// TranslatedText values are in an array, apply rules to each item of the array
if ($attribute['type'] == TranslatedText::class) {
foreach ($this->languages as $language) {
// all rules set when attribute was created (resets on each iteration)
$validationRules = $validation;
if ($language->default && $isRequired) {
// append required for the default language
$validationRules = array_merge($validationRules, ['required']);
}
$rules["{$attribute['signature']}.{$language->code}"] = $validationRules;
}
continue;
}


if ($isRequired) {
$validation = array_merge($validation, ['required']);
}

if ($attribute['type'] == Number::class) {
$validation = array_merge($validation, [
'numeric'.($attribute['configuration']['min'] ? '|min:'.$attribute['configuration']['min'] : ''),
'numeric'.($attribute['configuration']['max'] ? '|max:'.$attribute['configuration']['max'] : ''),
'numeric' . ($attribute['configuration']['min'] ? '|min:' . $attribute['configuration']['min'] : ''),
'numeric' . ($attribute['configuration']['max'] ? '|max:' . $attribute['configuration']['max'] : ''),
]);
}

$rules[$field] = implode('|', $validation);
}

return $rules;
}

Expand Down Expand Up @@ -238,7 +250,7 @@ protected function withAttributesValidationMessages()
/**
* Handle attributes updated event.
*
* @param array $event
* @param array $event
* @return void
*/
public function updatedAttributes($event)
Expand Down
Loading

0 comments on commit 06eb5f7

Please sign in to comment.