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/brameda/brameda.nl/app/Komma/Pages/Kms/PageSection.php
<?php
/**
 *
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

namespace App\Komma\Pages\Kms;

//The new object oriented attributes
use App\Komma\Buttons\Kms\ButtonService;
use App\Komma\Components\ComponentType\ComponentTypes;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\ComponentArea;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\MultiSelect;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Routes\RouteService;
use App\Komma\Servicepoints\Kms\ServicepointService;
use App\Komma\Servicepoints\Models\Servicepoint;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Models\KmsUserRole;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\ImageProperty;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\Seperator;
use App\Komma\Kms\Core\Attributes\TextArea;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Sections\CurrentSiteLanguagesTabsDirector;
use App\Komma\Kms\Core\Sections\SectionTabGroups;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Sections\SectionTabsBuilder;
use App\Komma\Kms\Core\ValidationSet;
use Illuminate\Database\Eloquent\Collection;

final class PageSection extends Section
{
    protected $slug = "pages";
    protected $hasRoutes = true;

    public $showSave = 'all';
    public $showDelete = [1];
    public $showCreate = [1];

    /**
     * PageSection constructor.
     * @param PageService $sectionService
     * @param RouteService $routeService
     * @param SiteServiceInterface $siteService
     */
    function __construct(PageService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
    {
        $sectionTabDirector = new CurrentSiteLanguagesTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
        parent::__construct($sectionService, $routeService, $siteService, $sectionTabDirector);
    }

    /**
     * Generates the attributes for this section. They all must extend the App\Komma\Kms\Core\Attributes\Attribute class
     * This is the place where you need to setup your sections appearance. Just make sure you build an array of attributes
     * and put each attribute in a AbstractSectionTabItem with a SectionTabGroups constant to link them to a tab.
     *
     * @see PageRepository::saveModel()
     * @return Collection A collection of SectionTabItems
     * @throws \Exception
     */
    protected function generateAttributes(): Collection
    {
        $fixedAttributeTabItems = $this->generateFixedAttributes();
        $tabItems = $fixedAttributeTabItems;

        $languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages([
            (new ComponentArea())
                ->setComponentTypes([
                    ComponentTypes::TEXT_IMAGE,
                    ComponentTypes::DOUBLE_TEXT,
                    ComponentTypes::VIDEO,
                    ComponentTypes::TEXT,
                    ComponentTypes::IMAGE,
                    ComponentTypes::USP,
                    ComponentTypes::QUOTE,
                    ComponentTypes::CONTENT_PERSONAL,
                    ComponentTypes::CONTENT_SLIDER,
                    ComponentTypes::LOGOS,
                ])
                ->setSubFolder('dynamic_group_sections')
                ->mapValueFrom(Attribute::ValueFromComponentArea, 'dynamic_group_sections')
        ]);

        foreach ($languageIndexedAttributes as $attribute) {
            $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::Languages));
        }

        return $tabItems;
    }

    /**
     * Generate fixed attributes
     *
     * @return Collection
     */
    private function generateFixedAttributes(): Collection
    {
        /** @var Page $model */
        $model = $this->getModel();

        //*****************************************************************************************\\
        //*** Define attribute validation sets                                                  ***\\
        //*****************************************************************************************\\
        $codeNameValidationSet = (new ValidationSet())
            ->setRules('required')
            ->setMessages(['required' => __('validation.required')]);

        //*****************************************************************************************\\
        //*** Generate the attributes                                                           ***\\
        //*****************************************************************************************\\
        $attributes = [];

        $attributes[] = (new Title(__('kms/pages.page')));

        $attributes[] = (new TextField(__('kms/global.codeName')))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->setReadOnly(false)
            ->mapValueFrom(Attribute::ValueFromModel, 'code_name')
            ->setValidationSet($codeNameValidationSet);

        $attributes[] = (new OnOff())
            ->setLabelText(__('kms/global.active'))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->switchOn()
            ->mapValueFrom(Attribute::ValueFromModel, 'active');

        $attributes[] = (new OnOff())
            ->setLabelText(__('kms/global.inNav'))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromModel, 'inNav');

        $attributes[] = (new Documents())
            ->setLabelText(__('kms/global.discover_thumbnail'))
            ->onlyAllowImages()
            ->setSmallDragAndDropArea()
            ->setMaxDocuments(1)
            ->setSubFolder('pages')
            ->setImageProperties([
                (new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(444)->setHeight(296),
            ])
            ->mapValueFrom(Attribute::ValueFromDocuments, 'pages');

        $attributes[] = (new Seperator())->setMinimumUserRole(KmsUserRole::SuperAdmin);

        $attributes[] = (new Title('CTA uit config overschrijven'))->setMinimumUserRole(KmsUserRole::SuperAdmin);

//        $attributes[] = (new TextField('Titel'))
//            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
//            ->mapValueFrom(Attribute::ValueFromModel, 'cta_heading');

        $servicepointService = \App::make(ServicepointService::class);
        $servicepointOptions = $servicepointService->getOptionsForSelect(false, true);

        $attributes[] = (new MultiSelect())
            ->setItems($servicepointOptions->toArray())
            ->setLabelText(__('Contactpersoon'))
            ->setMaxItemsToSelect(1)
            ->canBeLinkedWith(Servicepoint::class)
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromModel, 'servicepoint_id');

        $buttonsService = \App::make(ButtonService::class);
        $buttonModels = $buttonsService->getOptionsForSelect();

        $attributes[] = (new MultiSelect())
            ->setItems($buttonModels->toArray())
            ->setLabelText('Knop')
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->setMaxItemsToSelect(1)
            ->mapValueFrom(Attribute::ValueFromModel, 'cta_button_id');



        //Get the discover more pages. Except the root page and the current one.
        $discoverMorePages = $this->getSectionService()->getOptionsForSelect()->filter(function(SelectOption $pageSelectOption) use($model) {
//            return $pageSelectOption->getHtmlContent() !== '/' && $pageSelectOption->getHtmlContent() !== $model->getSidebarName(); //Will not because the getSideBarName will not always return the correct name. Needs to be redone properly.
            return $pageSelectOption->getHtmlContent() !== '/';
        });

        $discoverMoreSeparator = (new Seperator());

        $discoverMoreTitle = (new Title(__('kms/pages.discover_more')));
        $discoverMorePageSelector = (new MultiSelect())
            ->setItems($discoverMorePages->toArray())
            ->setLabelText(__('Pagina\'s'))
            ->mapValueFrom(Attribute::ValueFromModelHasManyRelation, 'discoverPages|id');

        $attributes[] = $discoverMoreSeparator;
        $attributes[] = $discoverMoreTitle;
        $attributes[] = $discoverMorePageSelector;

        if($model->code_name == 'contact') {
            $discoverMoreSeparator->setStyleClass('hidden');
            $discoverMoreTitle->setStyleClass('hidden');
            $discoverMorePageSelector->setStyleClass('hidden');
        }

        //Define language attributes
        $languageAttributes = [];
        $languageAttributes[] = (new Title(__('kms/pages.page')))->setMinimumUserRole(KmsUserRole::SuperAdmin);

        $languageAttributes[] = (new TextField(__('kms/global.title')))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromTranslationModel, 'name');

        $languageAttributes[] = (new TextField(__('kms/global.metaTitle')))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_title');

        $languageAttributes[] = (new TextArea())
             ->setLabelText(__('kms/global.metaDescription'))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
             ->mapValueFrom(Attribute::ValueFromTranslationModel, 'meta_description');

        $heroSeperator = (new Seperator())->setMinimumUserRole(KmsUserRole::SuperAdmin);
        $languageAttributes[] = $heroSeperator;

        $heroKmsTitle = (new Title(__('kms/global.hero')))->setMinimumUserRole(KmsUserRole::SuperAdmin);
        $languageAttributes[] = $heroKmsTitle;

        $heroToggle = (new OnOff())
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->setLabelText(__('kms/global.active'))
            ->mapValueFrom(Attribute::ValueFromTranslationModel, 'hero_active');
        $languageAttributes[] = $heroToggle;

        $heroTitle = (new TextField(__('kms/global.heroTitle')))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromTranslationModel, 'hero_title');
        $languageAttributes[] = $heroTitle;

        $heroDescription = (new TextArea())
            ->enableTinymceEditor()
            ->setLabelText(__('kms/global.heroDescription'))
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->mapValueFrom(Attribute::ValueFromTranslationModel, 'hero_description');
        $languageAttributes[] = $heroDescription;

        $heroButtons = (new MultiSelect())
             ->setItems($buttonModels->toArray())
             ->setLabelText('Button')
             ->setMinimumUserRole(KmsUserRole::SuperAdmin)
             ->mapValueFrom(Attribute::ValueFromTranslationModel, 'hero_button_id');
        $languageIndexedAttributes[] = $heroButtons;

        $heroImages = (new Documents())
            ->setLabelText(__('kms/global.images'))
            ->onlyAllowImages()
            ->setSmallDragAndDropArea()
            ->setMaxDocuments(5)
            ->setMinimumUserRole(KmsUserRole::SuperAdmin)
            ->setSubFolder('page_translations')
            ->setImageProperties([
                (new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Resize)->setWidth(724),
                (new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setWidth(359),
            ])
            ->mapValueFrom(Attribute::ValueFromTranslationDocuments, 'page_translation_heroes');
        $languageAttributes[] = $heroImages;

        if($model->code_name == 'home') {
            $heroSeperator->setStyleClass('hidden');
            $heroKmsTitle->setStyleClass('hidden');
            $heroToggle->setStyleClass('hidden');
            $heroTitle->setStyleClass('hidden');
            $heroDescription->setStyleClass('hidden');
            $heroImages->setStyleClass('hidden');
            $heroButtons->setStyleClass('hidden');
        }

        //Build an array with attributes for each current site language
        $languageIndexedAttributes = $this->createAttributesFromExistingAttributeForCurrentSiteLanguages($languageAttributes);

        //****************************************************************************************************************************************\\
        //*** Put the all attributes in a SectionTabItem so we can track for which tab they are. And then put SectionTabItems in a Collection  ***\\
        //****************************************************************************************************************************************\\
        $tabItems = new Collection();
        foreach($attributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::General));
        foreach($languageIndexedAttributes as $attribute) $tabItems->push(new SectionTabItem($attribute, SectionTabGroups::Languages));

        return $tabItems;
    }


    /**
     * This method will stop the load entities of the kmsSiteSection
     *
     * @return array
     *
     */
    public function loadEntities(){
        return [];
    }
}