File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Reservations/Kms/ReservationSection.php
<?php
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Reservations\Kms;
//The new object oriented attributes
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\DatePicker;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Select;
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\Attributes\View;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Sections\Tabs\Collections\SectionTabs;
use App\Komma\Kms\Core\ValidationSet;
use App\Komma\Locations\Kms\LocationService;
use App\Komma\Reservations\Models\Reservation;
use App\Komma\Users\Kms\KmsUserService;
use App\Komma\Users\Models\KmsUserRole;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
final class ReservationSection extends Section
{
/** @var KmsUserService */
protected $kmsUserService;
/**
* PageSection constructor.
* @param $slug
*/
// function __construct(Kms $kms, PageRepository $repository)
public function __construct($slug)
{
$this->kmsUserService = app(KmsUserService::class);
$tabs = new SectionTabs();
parent::__construct($tabs, $slug);
}
/**
* 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.
*
* @param Model $currentModel
* @return Collection A collection of SectionTabItems
*/
protected function generateAttributes(Model $currentModel = null): Collection
{
// \Log::info("PageSection::Generating attributes");
//*****************************************************************************************\\
//*** Define attribute validation sets ***\\
//*****************************************************************************************\\
$requiredValidator = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
$numberRequiredValidator = (new ValidationSet())
->setRules('required')
->setMessages(['required' => __('validation.required')]);
$dateValidator = (new ValidationSet())
->setRules('required|date')
->setMessages(['required' => __('validation.required')]);
//*****************************************************************************************\\
//*** Generate the attributes ***\\
//*****************************************************************************************\\
$attributes = [];
//Build the general attributes and put them in the attributes array
//Site selection
// $attributes[] = (new Title(__('kms/sites.type')));
$attributes[] = (new TextField(__('kms/reservations.reservation_number')))
->setPlaceholderText(__('kms/reservations.enter_reservation_number'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'reservation_number');
$reservationTypeOptions = [];
foreach (Reservation::getPossibleStatuses() as $reservationStatus) {
$reservationTypeOptions[] = (new SelectOption())
->setValue($reservationStatus->id)
->setHtmlContent(__('kms/reservations.statuses.'.$reservationStatus->short_name));
}
$attributes[] = (new Select())
->setItems($reservationTypeOptions)
->setLabelText(__('kms/reservations.status'))
->setStyleClass('hidden')
->mapValueFrom(Attribute::ValueFromModel, 'status');
/** @var LocationService $locationService */
$locationService = app(LocationService::class);
$locationSelect = (new Select())
->setLabelText(__('kms/locationproducts.location'))
->setItems($locationService->getOptionsForSelect()->toArray())
->mapValueFrom(Attribute::ValueFromModel, 'location_id');
$datePicker = (new DatePicker(__('kms/reservations.date')))
->setTimeEnabled(false)
// ->disableDateBeforeToday()
->mapValueFrom(Attribute::ValueFromItself, 'date');
if (auth()->user()->isAtLeast(KmsUserRole::Admin)) {
$locationSelect->setExplanation('Let op: Wanneer je deze wijzigt vervallen de geselecteerde activiteiten.<br/>Sla op na wijzigen!');
$datePicker->setExplanation('Let op: Er wordt geen rekening gehouden met de beschikbaarheid van het systeem bij wijzigen.<br/>Sla op na wijzigen!');
} else {
$locationSelect->setReadOnly(true);
$datePicker->setReadOnly(true);
}
$attributes[] = $locationSelect;
$attributes[] = $datePicker;
if (isset($currentModel) && $currentModel->exists && auth()->user()->can('update', $currentModel)) {
$attributes[] = (new Seperator());
// These tabs we only show if the model exists
$attributes[] = (new View('kms.attributes.sendReservationButtons'))
->setViewData([
'reservation' => $currentModel,
]);
}
$attributes[] = (new Seperator());
$attributes[] = (new Title(__('kms/reservations.contact_person')));
$attributes[] = (new TextField('Algemene voorwaarden en privacyverklaring'))
->mapValueFrom(Attribute::ValueFromItself, 'legal')
->setReadOnly(true)
->setValue('De klant heeft bevestigd dat deze de algemene voorwaarden en de privacyverklaring van FarmFun heeft gelezen en ermee akkoord is gegaan.');
$attributes[] = (new TextField(__('kms/reservations.company_name')))
->setPlaceholderText(__('kms/reservations.enter_company_name'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'company_name');
$attributes[] = (new TextField(__('kms/reservations.first_name')))
->setPlaceholderText(__('kms/reservations.enter_first_name'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'first_name');
//
// $attributes[] = (new TextField(__('kms/reservations.name_preposition')))
// ->setPlaceholderText(__('kms/reservations.enter_name_preposition'))
// ->setReadOnly(!auth()->user()->isAtLeast(KmsUserRole::Admin))
// ->mapValueFrom(Attribute::ValueFromModel, 'name_preposition');
$attributes[] = (new TextField(__('kms/reservations.last_name')))
->setPlaceholderText(__('kms/reservations.enter_last_name'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'last_name');
$attributes[] = (new TextField(__('kms/reservations.email')))
->setPlaceholderText(__('kms/reservations.enter_email'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'email');
$attributes[] = (new TextField(__('kms/reservations.phone')))
->setPlaceholderText(__('kms/reservations.enter_phone'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'phone');
$attributes[] = (new TextArea())
->setLabelText(__('kms/reservations.remarks'))
->setPlaceholderText(__('kms/reservations.enter_remarks'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'remarks');
$attributes[] = (new TextArea())
->setLabelText('Reden komst')
->setPlaceholderText(__('kms/reservations.enter_remarks'))
->setReadOnly(! auth()->user()->isAtLeast(KmsUserRole::Admin))
->mapValueFrom(Attribute::ValueFromModel, 'reason');
if (isset($currentModel) && $currentModel->exists) {
/** @var ReservationService $reservationService */
$reservationService = app()->make(ReservationService::class);
$reservedItems = $reservationService->getReservationItemsForGivenDayAndLocation($currentModel->date, $currentModel->location);
$currentModel->loadMissing('order');
if (isset($currentModel->order)) {
// These tabs we only show if the model exists
$attributes[] = (new View('kms.attributes.reservationOrder'))
->setViewData([
'order' => $currentModel->order,
])
->setTab('order');
}
// These tabs we only show if the model exists
$attributes[] = (new View('kms.attributes.reservationItems'))
->setViewData([
'currentModel' => $currentModel,
'reservationLocation' => isset($currentModel->location_id) ? $currentModel->location : null,
'reservationDate' => isset($currentModel->date) ? $currentModel->date : null,
'reservationItems' => isset($currentModel->items) ? $currentModel->items : collect(),
'reservedItems' => $reservedItems,
])
->setTab('activities');
}
$attributes[] = (new Seperator());
$attributes[] = (new Title(__('kms/reservations.extra_data')));
$attributes[] = (new TextField(__('kms/reservations.info_found')))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromModel, 'info_found');
$attributes[] = (new TextField(__('kms/reservations.company_type')))
->setReadOnly(true)
->mapValueFrom(Attribute::ValueFromModel, 'company_type');
//Return all attributes as a collection
return collect($attributes);
}
/**
* Adds attributes to their appropriate tabs.
*
* @param Collection $attributeCollection
* @return void
*/
protected function addAttributesToTabs(Collection $attributeCollection)
{
$sectionTranslationTabKey = 'kms/'.$this->slug.'.tabs';
if (__($sectionTranslationTabKey) == $sectionTranslationTabKey) {
throw new \UnexpectedValueException(static::class.': Translations tabs not found for section: "'.$this->slug.'"');
}
$tabOrder = array_keys(__($sectionTranslationTabKey));
$attributeCollection->each(function (Attribute $attribute) use ($tabOrder) {
// Get the tab key or set to default
$tabKey = $attribute->getTab();
if (! isset($tabKey)) {
$tabKey = 'default';
}
// Get the tab name and make sure that the translation isset
$tabTranslationKey = 'kms/'.$this->slug.'.tabs.'.$tabKey;
$tabTranslation = __($tabTranslationKey);
if ($tabTranslation == $tabTranslationKey) {
throw new \UnexpectedValueException('ReservationSection: Translation for tab not found: "'.$tabTranslationKey.'"');
}
// Append the tab
$tab = $this->tabs->getTab($tabTranslation);
$tab->tab_code_name = $tabKey;
$tab->tab_order = array_search($tabKey, $tabOrder);
$tab->addItem($attribute);
});
// Order the tab by the given tab_order
$this->tabs = $this->tabs->sortBy('tab_order');
}
}