File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Events/Kms/EventService.php
<?php
namespace App\KommaApp\Events\Kms;
use App\Helpers\KommaHelpers;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Events\Models\EventTranslation;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\KommaApp\Kms\Core\Entities\DisplayNameTrait;
use App\KommaApp\Kms\Core\HasRoutesInterface;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TreeModel;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TreeModelInterface;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Kms\SidebarListItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\Sites\HasSitesInterface;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;
class EventService extends SectionService
{
protected $sortable = false;
protected $orderBy = 'date';
protected $orderReverse = false;
function __construct()
{
$this->forModelName = Event::class;
parent::__construct();
}
/**
* This method will save an model
*
* @param $model Model or null
* @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, Collection $sectionTabItems): Model
{
/** @var TreeModel $model */
//Process Event Specific attributes
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromItself:
if($reference == 'site_name'){
$model->site_id = $this->siteService->getCurrentSite()->id;
}
break;
}
});
$model->save(); //Save the event
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
$this->siteService->linkModelToCurrentSite($model);
$model->price = '€ ' . number_format(($model->price_amount / 100), 2, ',', '.');
$model->save();
// Create the slug for each translation
foreach($model->translations as $translation){
if(!$translation->exists) continue;
$translation->slug = $this->createOrGetUniqueSlug($translation, $translation->name . '-'.$model->date->format('d-m-Y'));
$translation->save();
}
$this->updateSearchDatabases($model);
//Return the event
return $model;
}
/**
* Update the searchable databases
*
* @param $model
*/
private function updateSearchDatabases($model)
{
// Remove from search if inactive
if(! $model->active){
$model->unsearchable();
$model->translations()->unsearchable();
}
else{
// If active add the translations to the search database
$model->translations()->searchable();
}
}
public function getModelsForSideBar():array
{
if(!method_exists($this->forModelName, 'getSideBarName')) throw new \RuntimeException('Could not get the name of an instance using a call to a getSideBarName method. Please let that class use the '.DisplayNameTrait::class.' or implement the method yourself.');
$models = Event::where('date', '>=', Carbon::today()->startOfDay())->orderBy('date', 'asc')->get();
// Preload translation to prevent individual queries
if(is_a(new $this->forModelName, AbstractTranslatableModel::class)){
$models = $models->load('translation');
}
if(is_a(new $this->forModelName, DocumentableInterface::class)){
$models = $models->load('images');
}
$sidebarList = [];
$sidebarList = $this->prepareSidebarModels($models, $sidebarList);
$models = Event::where('date', '<', Carbon::today()->startOfDay())->orderBy('date', 'desc')->get();
// Preload translation to prevent individual queries
if(is_a(new $this->forModelName, AbstractTranslatableModel::class)){
$models = $models->load('translation');
}
if(is_a(new $this->forModelName, DocumentableInterface::class)){
$models = $models->load('images');
}
$sidebarList = $this->prepareSidebarModels($models, $sidebarList, 'Geweest: ');
return $sidebarList;
}
private function prepareSidebarModels($models, $list, $prefix = '')
{
foreach ($models as $model) {
if($model->getAttribute('lft') == 1) continue; //Skip the root model if it is one
$sidebarListItem = new SidebarListItem();
$this->setThumbnail($model);
$this->generateThumbnail($model);
//Set the values for the sidebar
$sidebarListItem->setId($model->id);
$sidebarListItem->setStatus($model->active);
$title = $model->getSideBarName();
$sidebarListItem->setName($prefix . $title);
$sidebarListItem->setThumbnail($model->thumbnail);
// Make sure there is always a unique key
$key = $title;
if(isset($list[$key])) $key = $title.$model->id;
$list[$key] = $sidebarListItem;
}
return $list;
}
/**
* Fills non-product specific attributes. Product specific attributes are processed in the parent
*
*
* @param Collection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
* @param Model $model
* @return Collection
*/
public function fillAttributesWithData(Collection $sectionTabItems, Model $model)
{
$filledAttributesCollection = parent::fillAttributesWithData($sectionTabItems, $model);
$sectionTabItems->each(
function ($sectionTabItem, $key) use ($model, $filledAttributesCollection, &$quantityDiscountAttribute, &$quantityPriceAttribute) {
/** @var $sectionTabItem SectionTabItem */
if (!is_a($sectionTabItem->getAttribute(), Attribute::class)) throw new \InvalidArgumentException("One of the attributes in a AbstractSectionTabItem instance is not but must be an child instance of Attribute.");
$attribute = $sectionTabItem->getAttribute();
$value = $attribute->getValue();
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($valueReference) {
case 'site_name':
$site = $this->siteService->getCurrentSite();
$attribute->setValue($site->name);
break;
case 'wefact_code':
$attribute->setValue($model->wefact_code);
break;
}
}
);
return $filledAttributesCollection;
}
/**
* @return BaseCollection containing SelectOptionInterface
*/
public function getOptionsForSelectForSignUps($isCreating = false, $allowNullableSelectOption = false): BaseCollection
{
$selectOptions = collect();
if($allowNullableSelectOption){
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent(__('kms/global.none'))
->setHtmlContent(__('kms/global.none'))
->setValue(null);
$selectOptions->push($selectOption);
}
$sidebarListItems = Event::with('translation');
if($isCreating) $sidebarListItems = $sidebarListItems->where('date', '>=', Carbon::today()->startOfDay());
$sidebarListItems = $sidebarListItems->get();
foreach ($sidebarListItems as $sidebarListItem) {
// $model = $this->forModelName::find($sidebarListItem->getId());
// if ( ! $model) {
// continue;
// }
/** @var SelectOptionInterface $selectOption */
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent($sidebarListItem->translation->name)
->setHtmlContent($sidebarListItem->translation->name)
->setValue($sidebarListItem->id);
$selectOptions->push($selectOption);
}
return $selectOptions;
}
}