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/kooken.komma.pro/vendor/komma/kms/src/Core/Sections/Section.php
<?php

namespace Komma\KMS\Core\Sections;

use Carbon\Carbon;
use http\Exception\RuntimeException;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Sections\Tabs\Collections\AbstractSectionTabs;
use Komma\KMS\Core\Sections\Tabs\SectionTab;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Lang;
use Komma\KMS\Providers\SetupServiceProvider;

/**
 * Class Section. Will replace KmsSection
 * @package App\Kms\Core\Sections
 */
abstract class Section
{
    /**
     * When you set this variable to for example "mypackage::",
     * The sections translations will be loaded from the composer package called "mypackage" if it is registered
     * to the laravel application.
     *
     * @var string
     */
    protected $packagePrefix = '';

    /** @var AbstractSectionTabs */
    protected $tabs;

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

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

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


    function __construct(AbstractSectionTabs $tabs, string $slug) {
        $this->slug = $slug;
        $this->tabs = $tabs;
        $this->setSectionText();
    }

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

    /**
     * Generates the attributes for this section. They all must extend the App\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
     */
    protected 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);
    }

    /**
     * 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(__(SetupServiceProvider::PACKAGE_NAMESPACE.'::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';

//        dd($this->packagePrefix.'kms/'.$sectionTitleTranslationKey);
        $this->setSectionNewModelTranslation(__($this->packagePrefix.'kms/'.$newModelTranslationKey));
        $this->setSectionTitle(__($this->packagePrefix.'kms/'.$sectionTitleTranslationKey));

        if( Lang::has('kms/'.$sectionTranslationFile.'.section.submitButton')){
            $this->setSubmitButtonLabel(__($this->packagePrefix.'kms/'.$sectionTranslationFile.'.section.submitButton'));
        }
        else {
            $this->setSubmitButtonLabel(__('KMS::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 $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 getSubmitButtonLabel()
    {
        return $this->submitButtonLabel;
    }
}