File: D:/HostingSpaces/stafa/stafa.nl/app/Komma/References/Kms/ReferenceService.php
<?php
namespace App\Komma\References\Kms;
use App\Komma\Kms\Core\Attributes\Attribute;
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\References\Models\Reference;
use App\Komma\Sites\HasSitesInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
final class ReferenceService extends SectionService
{
protected $sortable = false;
protected $orderReverse = true;
function __construct()
{
$this->forModelName = Reference::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
{
$model->save(); //Save the page
$this->siteService->linkModelToSitesUsingIdCsvString($model, 1);
$this->saveModelTranslations($model);
$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;
}
/**
* Fills non-reference specific attributes. Reference 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;
}
/**
* @param bool $allowNullableSelectOption
* @return BaseCollection
*/
public function getOptionsForSelect(bool $allowNullableSelectOption = false): BaseCollection
{
$references = Reference::all();
$selectOptions = collect();
foreach ($references as $sidebarListItem) {
/** @var SelectOptionInterface $selectOption */
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent($sidebarListItem->name)
->setHtmlContent($sidebarListItem->name)
->setValue($sidebarListItem->id);
$selectOptions->push($selectOption);
}
return $selectOptions;
}
}