File: D:/HostingSpaces/stafa/stafa.nl/app/Komma/Events/Kms/EventService.php
<?php
namespace App\Komma\Events\Kms;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOption;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\Tree\NestedSets\Nodes\TreeModel;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Events\Models\Event;
use App\Komma\Sites\HasSitesInterface;
use App\Komma\Sites\Kms\SiteService;
use App\Komma\Sites\SiteServiceInterface;
use App\Komma\Users\Models\KmsUser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Collection;
final class EventService extends SectionService
{
protected $sortable = false;
protected $orderBy = 'date';
protected $orderReverse = true;
function __construct()
{
$this->forModelName = Event::class;
parent::__construct();
}
/**
* This method will save an model
*
* @param $model Model or null
* @param DatabaseCollection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, DatabaseCollection $sectionTabItems): Model
{
/** @var SiteService $siteService */
$siteService = app(SiteServiceInterface::class);
/** @var TreeModel $model */
//Process Page Specific attributes
// dd($model);
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromModel:
if($reference == 'parent_id') {
// Get parent_id of the input
$parentId = $attribute->getValue();
$parentPage = $model->find($parentId);
// Set default to false
$currentParentId = null;
// Check if model id isset so if it is a new model or not
// if true then get current parent_id
if($model->id) $currentParentId = $model->getParentId();
//Check if parent page is found, model isset and parent page id isn't the current parent id
if($parentPage && $model && $parentPage->id !== $currentParentId)
{
$model->makeLastChildOf($parentPage);
}
$attribute->mapValueFrom(Attribute::ValueFromItself, ''); //unset parent_id it so it won't get saved directly on the model in the parent_id field. The makeLastChildOf call above does save the parent id via lft rgt
} elseif($reference == 'code_name') {
}
break;
case Attribute::ValueFromTranslationModel;
/** @var Language $language */
$this->doAfterSave(function() use($attribute, $model, $reference) {
$language = $attribute->getAssociatedLanguage();
$translation = $this->getTranslationModelForModelByLanguage($model, $language);
if($reference == 'name')
{
$slug = $this->createOrGetUniqueSlug($translation, $attribute->getValue() );
$translation['slug'] = $slug;
}});
break;
}
});
$model->save(); //Save the page
$this->saveModelTranslations($model);
$this->siteService->linkModelToSitesUsingIdCsvString($model, 1);
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
//Return the page
return $model;
}
/**
* @return BaseCollection containing SelectOptionInterface
*/
public function getOptionsForAuthorSelect(): BaseCollection
{
$selectOptions = new Collection();
$models = KmsUser::all();
foreach ($models as $model) {
/** @var Event $model */
/** @var SelectOption $selectOption */
$selectOption = (app(SelectOptionInterface::class))
->setContent($model->getDisplayName())
->setHtmlContent($model->getDisplayName())
->setValue($model->id);
$selectOptions->push($selectOption);
}
return $selectOptions;
}
/**
* Fills non-event specific attributes. Event specific attributes are processed in the parent
*
* @param DatabaseCollection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
* @param Model $model
* @return DatabaseCollection
*/
public function fillAttributesWithData(DatabaseCollection $sectionTabItems, Model $model)
{
$filledAttributesCollection = parent::fillAttributesWithData($sectionTabItems, $model);
$sectionTabItems->each(
function ($sectionTabItem, $key) use ($model, $filledAttributesCollection, &$quantityDiscountAttribute, &$quantityPriceAttribute) {
/** @var $sectionTabItem SectionTabItem */
$attribute = $sectionTabItem->getAttribute();
if (!is_a($attribute, Attribute::class)) throw new \InvalidArgumentException("One of the attributes in a AbstractSectionTabItem instance is not but must be an child instance of Attribute.");
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($valueReference) {
case 'site_id':
/** @var HasSitesInterface $model */
$idString = $this->siteService->getSiteIdsForModel($model);
if($idString) $attribute->setValue($idString);
break;
}
}
);
return $filledAttributesCollection;
}
}