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/farmfun.komma.pro/app/Komma/Kms/Core/Sections/Section.php
<?php

namespace App\Komma\Kms\Core\Sections;

use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Sections\Tabs\Collections\AbstractSectionTabs;
use App\Komma\Kms\Core\Sections\Tabs\SectionTab;
use App\Komma\Sites\Models\Site;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Lang;

/**
 * Class Section. Will replace KmsSection
 */
abstract class Section
{
    /** @var AbstractSectionTabs */
    protected $tabs;

    /** @var string */
    protected $submitButtonLabel;

    /** @var string */
    private $sectionNewModel;

    /** @var string */
    private $sectionTitle;

    /** @var string */
    private $sectionSubtitle;

    /** @var SiteServiceInterface */
    protected $siteService;

    public function __construct(AbstractSectionTabs $tabs, string $slug)
    {
        $this->slug = $slug;
        $this->tabs = $tabs;
        $this->siteService = app(SiteServiceInterface::class);
        $this->setSectionText();
    }

    /**
     * @return AbstractSectionTabs
     */
    public function getTabs(): AbstractSectionTabs
    {
        return $this->tabs;
    }

    /**
     * Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
     *
     * @param Model $currentModel
     * @return Collection A collection of SectionTabItems
     */
    abstract protected function generateAttributes(Model $currentModel = null): Collection;

    /**
     * Generates the attributes for the section using the sections generatesAttributes method
     * and adds them to the appropriate tabs
     *
     * @return void
     */
    public function generateAttributesAndAddThemToTabs($currentModel)
    {
        $attributeCollection = $this->generateAttributes($currentModel);
        $this->addAttributesToTabs($attributeCollection);
    }

    /**
     * Creates copies of the $baseAttribute for each language given.
     * Also retrieves the translation that belongs to the model based on each language that is being processed.
     * The translation is stored inside if it exists the attribute to later on check at which tab in a section it belongs.
     *
     * @param Attribute $baseAttribute
     * @param $languages
     * @return array with the created attributes
     */
    private function createAttributesFromExistingAttributeForTheGivenLanguages(Attribute $baseAttribute, $languages)
    {
        $createdAttributes = [];

        //Loop trough each language of the current site
        foreach ($languages as $language) {
            //Get the translation of the model

            $clone = clone $baseAttribute;

            if ($language) {
                $clone->setAssociatedLanguage($language);
            }

            $createdAttributes[] = $clone;
        }

        return $createdAttributes;
    }

    public function getAttributes()
    {
        $attributes = [];
        foreach ($this->tabs as $tab) {
            /** @var SectionTab $tab */
            $attributes = array_merge($attributes, $tab->getItems()->toArray());
        }

        return collect($attributes);
    }

    /**
     * This method create copies of the attributes you pass it for all current site languages and
     * injects translations via another method
     *
     * @param $baseAttributes
     * @return array
     */
    protected function createAttributesFromExistingAttributeForCurrentSiteLanguages(array $baseAttributes)
    {
        //Get the languages
        /** @var SiteServiceInterface $siteService */
        $siteService = App::make(SiteServiceInterface::class); //TODO Refactor to controller level and pass languages down to here.
        $languages = $siteService->getSiteLanguages();

        $attributes = [];
        foreach ($baseAttributes as $baseAttribute) {
            $attributesCreatedFromBaseAttribute = $this->createAttributesFromExistingAttributeForTheGivenLanguages($baseAttribute, $languages);
            $attributes = array_merge($attributes, $attributesCreatedFromBaseAttribute);
        }

        return $attributes;
    }

    /**
     * This method create copies of the attributes you pass it for all used site languages and
     * injects translations via another method.
     *
     * @param $baseAttributes
     * @return array
     */
    protected function createAttributesFromExistingAttributeForAllUsedLanguagesBySites(array $baseAttributes)
    {
        $languages = Site::has('languages')->with(['languages'])->get()->map(function (Site $site) {
            return $site->languages;
        })->collapse();

        $currentSiteDefaultLanguage = $this->siteService->getCurrentSite()->defaultLanguage;
        $languages = $languages->push($currentSiteDefaultLanguage)->unique('id');

        $attributes = [];
        foreach ($baseAttributes as $baseAttribute) {
            /** @var Attribute $baseAttribute */
            $attributesCreatedFromBaseAttribute = $this->createAttributesFromExistingAttributeForTheGivenLanguages($baseAttribute,
                $languages);
//            if($baseAttribute->getsValueFrom() == Attribute::ValueFromItself && $baseAttribute->getsValueFromReference() == 'value') dd($attributesCreatedFromBaseAttribute); //Debugging helper
            $attributes = array_merge($attributes, $attributesCreatedFromBaseAttribute);
        }

        return $attributes;
    }

    /**
     * Adds attributes to their appropriate tabs.
     *
     * @param Collection $attributeCollection
     * @return void
     */
    protected function addAttributesToTabs(Collection $attributeCollection)
    {
        $attributeCollection->each(function (Attribute $attribute) {
            if ($attribute->hasAssociatedLanguage()) {
                $this->tabs->getTab($attribute->getAssociatedLanguage()->iso_2)->addItem($attribute);
            } else {
                $this->tabs->getTab(__('kms/global.general'))->addItem($attribute);
            }
        });
    }

    /**
     * Set the section text
     */
    public function setSectionText()
    {
        $sectionTranslationFile = 'global';
        if ($this->slug != '') {
            $sectionTranslationFile = $this->slug;
        }

        $newModelTranslationKey = ''.$sectionTranslationFile.'.section.new';
        $sectionTitleTranslationKey = $sectionTranslationFile.'.section.title';
        $sectionSubtitleTranslationKey = $sectionTranslationFile.'.section.subtitle';

        $newModelTranslationShop = __('shop/'.$newModelTranslationKey);
        if ($newModelTranslationShop !== 'shop/'.$newModelTranslationKey) {
            $this->setSectionNewModelTranslation($newModelTranslationShop);
        } else {
            $this->setSectionNewModelTranslation(__('kms/'.$newModelTranslationKey));
        }

        $sectionTitleShop = __('shop/'.$sectionTitleTranslationKey);
        if ($sectionTitleShop !== 'shop/'.$sectionTitleTranslationKey) {
            $this->setSectionTitle($sectionTitleShop);
        } else {
            $this->setSectionTitle(__('kms/'.$sectionTitleTranslationKey));
        }

        $sectionSubtitleShop = __('shop/'.$sectionSubtitleTranslationKey);
        if ($sectionSubtitleShop !== 'shop/'.$sectionSubtitleTranslationKey) {
            $this->setSectionSubtitle($sectionSubtitleShop);
        } else {
            $this->setSectionSubtitle(__('kms/'.$sectionSubtitleTranslationKey));
        }

        if (Lang::has('kms/'.$sectionTranslationFile.'.section.submitButton')) {
            $this->setSubmitButtonLabel(__('kms/'.$sectionTranslationFile.'.section.submitButton'));
        } else {
            $this->setSubmitButtonLabel(__('kms/global.save'));
        }
    }

    /**
     * @param string $sectionNewModel
     */
    public function setSectionNewModelTranslation(string $sectionNewModel)
    {
        $this->sectionNewModel = $sectionNewModel;
    }

    /**
     * @param string $sectionTitle
     */
    public function setSectionTitle(string $sectionTitle)
    {
        $this->sectionTitle = ucfirst($sectionTitle);
    }

    /**
     * @param string $sectionSubtitle
     */
    public function setSectionSubtitle(string $sectionSubtitle)
    {
        $this->sectionSubtitle = ucfirst($sectionSubtitle);
    }

    /**
     * @param string $submitButtonLabel
     */
    public function setSubmitButtonLabel(string $submitButtonLabel)
    {
        $this->submitButtonLabel = $submitButtonLabel;
    }

    /**
     * @return string
     */
    public function getSectionNewModel()
    {
        return $this->sectionNewModel;
    }

    /**
     * @return string
     */
    public function getSectionTitle()
    {
        return $this->sectionTitle;
    }

    /**
     * @return string
     */
    public function getSectionSubtitle()
    {
        return $this->sectionSubtitle;
    }

    /**
     * @return string
     */
    public function getSubmitButtonLabel()
    {
        return $this->submitButtonLabel;
    }
}