File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/PastEvents/Kms/PastEventService.php
<?php
namespace App\KommaApp\PastEvents\Kms;
use App\Helpers\KommaHelpers;
use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\Attributes\Attribute;
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\PastEvents\Models\PastEvent;
use App\KommaApp\Sites\HasSitesInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class PastEventService extends SectionService
{
protected $sortable = false;
protected $orderBy = 'date';
protected $orderReverse = true;
function __construct()
{
$this->forModelName = PastEvent::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 PastEvent Specific attributes
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromTranslationModel:
if($reference == 'name')
{
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
$translation = $this->getTranslationModelForModelByLanguage($model, $language);
$translation['slug'] = $this->createOrGetUniqueSlug($translation, $attribute->getValue());
//Saving is done by the parent
}
break;
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);
$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->translations()->unsearchable();
}
else{
// If active add the translations to the search database
$model->translations()->searchable();
}
}
/**
* 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;
}
}
);
return $filledAttributesCollection;
}
/**
* Returns all models for the sidebar menu in the backend
*
* @return array
*/
// public function getModelsForSideBar():array
// {
// if(isset($this->orderBy)){
// if($this->orderReverse) $models = $this->forModelName::orderBy($this->orderBy, 'desc');
// else $models = $this->forModelName::orderBy($this->orderBy);
// }
// else $models = $this->forModelName::orderBy('id');
//
// //Only load the models from the current site. Or load all when we currently have the default site
// $site = $this->siteService->getCurrentSite();
// if(new $this->forModelName instanceof HasSitesInterface && $site->exists) {
// $models->whereHas('Sites', function ($query) use ($site) {
// $query->where('site_id', '=', $site->id);
// });
// }
//
// // Get the models
// $models = $models->get();
//
// // Preload translation to prevent individual queries
// if(is_a(new $this->forModelName, AbstractTranslatableModel::class)){
// $models = $models->load('translation');
// }
//
// $sidebarList = [];
// foreach ($models as $model) {
//
// $sidebarListItem = new SidebarListItem();
// $this->setThumbnail($model);
// $this->generateThumbnail($model);
//
// //Set the values for the sidebar
// $sidebarListItem->setId($model->id);
//
// $sidebarListItem->setStatus($model->active);
// $title = KommaHelpers::str_limit_full_word($model->getDisplayName(), 75);
// $sidebarListItem->setName($title . '<br/><sub>'.$model->date->format('d-m-Y').'</sub>');
// $sidebarListItem->setThumbnail($model->thumbnail);
//
// // Make sure there is always a unique key
// $key = $title;
// if(isset($sidebarList[$key])) $key = $title.$model->id;
//
// $sidebarList[$key] = $sidebarListItem;
// }
//
// return $sidebarList;
// }
}