File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Announcements/Models/AnnouncementSection.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Announcements\Models;
//The new object-oriented attributes
use App\Komma\Announcements\AnnouncementCountrySetting;
use App\Komma\Announcements\Kms\AnnouncementService;
use App\Komma\Countries\CountryService;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\AutocompleteInput;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\Select;
use App\Komma\Kms\Core\Attributes\Seperator;
use App\Komma\Kms\Core\Attributes\TextField;
use App\Komma\Kms\Core\Sections\NoLanguageTabsDirector;
use App\Komma\Kms\Core\Sections\Section;
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 App\Komma\Languages\Kms\LanguageService;
use App\Komma\Routes\RouteService;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Database\Eloquent\Collection;
class AnnouncementSection extends Section
{
protected $slug = 'announcements';
private $countrySelectOptions;
private $countrySettingOptions;
/**
* PageSection constructor.
* @param AnnouncementService $sectionService
* @param RouteService $routeService
* @param SiteServiceInterface $siteService
*/
public function __construct(AnnouncementService $sectionService, RouteService $routeService, SiteServiceInterface $siteService)
{
$sectionTabDirector = new NoLanguageTabsDirector(new SectionTabsBuilder()); //Can make tabs for us. also see KmsSection::__construct
parent::__construct($sectionService, $routeService, $siteService, $sectionTabDirector);
$countryService = app(CountryService::class);
$this->countrySelectOptions = $countryService->getCountryForSelect();
$this->countrySettingOptions = [
(new SelectOption())
->setContent('All countries')
->setHtmlContent('All countries')
->setValue(AnnouncementCountrySetting::ALL),
(new SelectOption())
->setContent('All countries excluding the selected countries')
->setHtmlContent('All countries excluding the selected countries')
->setValue(AnnouncementCountrySetting::EXCEPT),
(new SelectOption())
->setContent('Only the selected countries')
->setHtmlContent('Only the selected countries')
->setValue(AnnouncementCountrySetting::SPECIFIC),
];
}
/**
* 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
*/
protected function generateAttributes(): Collection
{
// \Log::info("PageSection::Generating attributes");
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$requiredValidationSet = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
$attributes[] = (new OnOff())
->setLabelText(__('kms/global.active'))
->switchOn()
->mapValueFrom(Attribute::ValueFromModel, 'active');
/** @var LanguageService $languageService */
$languageService = app()->make(LanguageService::class);
$attributes[] = (new Select())
->setItems($languageService->getLanguagesForSelect())
->setLabelText(__('kms/global.language'))
->mapValueFrom(Attribute::ValueFromModel, 'language_id');
/** @var AnnouncementService $announcementService */
$announcementService = app()->make(AnnouncementService::class);
$attributes[] = (new Select())
->setItems($announcementService->getTypeOptions())
->setLabelText(__('kms/announcements.type'))
->mapValueFrom(Attribute::ValueFromModel, 'type');
$attributes[] = (new Seperator());
$attributes[] = (new Select())
->setLabelText('Show in')
->setItems($this->countrySettingOptions)
->mapValueFrom(Attribute::ValueFromModel, 'country_setting');
$attributes[] = (new AutocompleteInput())
->setLabelText('Countries')
->setExplanation('Select countries in which to show or exclude the announcement (based on the IP address of the visitor).<br> This setting will be ignored if the "All countries" setting is active.')
->setItems($this->countrySelectOptions->toArray())
->mapValueFrom(Attribute::ValueFromModel, 'country_ids');
$attributes[] = (new Seperator());
$attributes[] = (new TextField(__('kms/announcements.message')))
->setPlaceholderText(__('kms/announcements.enter_message'))
->mapValueFrom(Attribute::ValueFromModel, 'message')
->setValidationSet($requiredValidationSet);
$attributes[] = (new TextField(__('kms/announcements.url')))
->setPlaceholderText(__('kms/announcements.enter_url'))
->mapValueFrom(Attribute::ValueFromModel, 'url');
$attributes[] = (new OnOff())
->setLabelText('Open in new tab')
->mapValueFrom(Attribute::ValueFromModel, 'target_blank')
->setExplanation('This setting will be ignored if the "URL" setting is not set.');
//****************************************************************************************************************************************\\
//*** 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));
}
return $tabItems;
}
/**
* This method will stop the load entities of the kmsSiteSection
*
* @return array
*/
public function loadEntities()
{
return [];
}
}