HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Properties/Kms/PropertyKeyService.php
<?php
namespace App\Properties\Kms;
use App\Categories\Models\Category;
use App\Properties\Models\PropertizableInterface;
use App\Properties\Models\PropertyKey;
use App\Properties\Models\PropertyKeyTranslation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Models\Traits\HasThumbnailInterface;
use Komma\KMS\Core\ModelService;
use Komma\KMS\Core\Sections\SidebarListItem;
use Komma\KMS\Globalization\Languages\Models\Language;

/**
 * Class PropertyService
 * @package App\Komma\Shop\Properties
 */
class PropertyKeyService extends ModelService
{
    function __construct()
    {
        $this->setModelClassName(PropertyKey::class);
        parent::__construct();
    }

    /**
     * Puts the values of attributes in an Eloquent model. And then saves that model.
     *
     * @param Model|PropertizableInterface $model
     * @param Collection                   $attributes
     *
     * @return Model|PropertizableInterface
     */
    public function save(Model $model, Collection $attributes = null): Model
    {
        if($attributes === null) {
            $this->debug('Skipping saving properties for model "'.get_class($model).'". Because we did not get attributes to save');
            return $model;
        }

        $this->checkContainsAttributes($attributes);

        $model->load('translations');

        $attributes->each(function(Attribute $attribute) use($model) {
            if($attribute->getsValueFromReference() !== 'key' ||
                !$attribute->getAssociatedLanguage()
            ) return;

            $propertyKeyTranslation = $model->translations->where('language_id', $attribute->getAssociatedLanguage()->id)->first();
            if(!$propertyKeyTranslation) {
                if($model) $model->save();
                $propertyKeyTranslation = new PropertyKeyTranslation();
                $propertyKeyTranslation->translatable()->associate($model);
                $propertyKeyTranslation->language()->associate($attribute->getAssociatedLanguage());
            }

//            if($attribute->getAssociatedLanguage()->iso_2 === 'de') dd($attribute->getValue());
            $propertyKeyTranslation->name = $attribute->getValue();
            $propertyKeyTranslation->save();
        });


        return $model;
    }

    /**
     * Delete property keys that do not belong to a property or a given property
     */
    public function deleteOrphanedPropertyKeys() {
        \DB::transaction(function () {
            $orphanedKeysQuery = PropertyKey::whereDoesntHave('properties')->with('translations');

            $orphanedKeysQuery->get()->each(function (PropertyKey $propertyKey) {
                $translationIds = $propertyKey->translations->pluck('id');
                PropertyKeyTranslation::destroy($translationIds); //We can safely delete the translations because they belong only to 1 PropertyKey
            });

            $orphanedKeysQuery->delete();
        });
    }

    /**
     * Gets the values of an Eloquent model and passes them to a collection of attributes
     *
     * @param Model      $model
     * @param Collection $attributes
     *
     * @return mixed
     */
    public function load(Model $model, Collection $attributes = null): Collection
    {
        if($attributes === null) {
            $this->debug('Skipping loading properties for model "'.get_class($model).'". Because we did not get attributes to load');
            return $model;
        }

        $this->checkContainsAttributes($attributes);

        $model->load('translations');
        $attributes->each(function(Attribute $attribute) use($model) {
            if($attribute->getsValueFromReference() !== 'key' ||
                !$attribute->getAssociatedLanguage()
            ) return;

            $propertyKeyTranslation = $model->translations->where('language_id', $attribute->getAssociatedLanguage()->id)->first();
            if(!$propertyKeyTranslation) return;
            $attribute->setValue($propertyKeyTranslation->name);
        });

        return $attributes;
    }

    /**
     * Get or create a PropertyKey with a given translation value for a specific language.
     *
     * @param string   $name
     * @param Language $language
     *
     * @return PropertyKey
     */
    public function getOrCreatePropertyKeyWithTranslationValue(string $name, Language $language):PropertyKey
    {
        /** @var PropertyKeyTranslation $keyTranslation */
        $keyTranslation = PropertyKeyTranslation::firstOrNew([
            'language_id' => $language->id,
            'name' => strtolower($name)
        ]);

        /** @var PropertyKey $propertyKey */
        if($keyTranslation->exists && $keyTranslation->translatable) {
            $propertyKey = $keyTranslation->translatable;
        } else {
            $propertyKey = $keyTranslation->translatable;
            if(!$propertyKey) {
                $propertyKey = new PropertyKey();
                $propertyKey->save();
            }
            $propertyKey->translations()->save($keyTranslation);
            $propertyKey->load('translations');
        }

        return $propertyKey;
    }

    /**
     * Destroys the appropriate related models for a given model.
     * Those related models must be the responsibility of this service
     *
     * @param Model $model
     * @return Model
     */
    public function destroyForModel(Model $model): Model
    {
        return $model;
    }

    public function getModelsForSideBar(): array
    {
        $propertyKeys = PropertyKey::with('translations')->get();

        $sidebarList = [];
        foreach ($propertyKeys as $propertyKey) {
            /** @var PropertyKey $propertyKey */
            //New SidebarListItem
            $sidebarListItem = new SidebarListItem();

            /** @var HasThumbnailInterface $propertyKey */
            $propertyKey->generateThumbnail();

            //Set the values for the sidebar
            $sidebarListItem->setId($propertyKey->id);
            $sidebarListItem->setStatus(!$propertyKey->is_admin);
            $sidebarListItem->setName($propertyKey->getDisplayName());
            $sidebarListItem->setThumbnail($propertyKey->getThumbnail());
            $sidebarListItem->alsoSearchInAttributesOfModel($propertyKey);

            $sidebarList[] = $sidebarListItem;
        }

        return $sidebarList;
    }

    /**
     * @param Language|null $language
     * @param array         $categoryIds
     *
     * @return mixed
     */
    public function requiredPropertyKeysForCategories(array $categoryIds)
    {
        $categories = Category::whereIn('id', $categoryIds)->with('requiredPropertyKeys.translations')->get();

        return $categories->map(function(Category $category) {
           return $category->requiredPropertyKeys;
        })->collapse();
    }
}