File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/CustomerStories/Kms/CustomerStoryService.php
<?php
namespace App\Komma\CustomerStories\Kms;
use App\Komma\CustomerStories\Models\CustomerStory;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Models\SelectOptionInterface;
use App\Komma\Kms\Core\NestedSets\Nodes\TreeModel;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Routes\Models\Route;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;
class CustomerStoryService extends SectionService
{
protected $sortable = true;
public function __construct()
{
$this->forModelName = CustomerStory::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 CustomerStory Specific attributes
$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();
$parentCustomerStory = $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 customerStory is found, model isset and parent customerStory id isn't the current parent id
if ($parentCustomerStory && $model && $parentCustomerStory->id !== $currentParentId) {
$model->makeLastChildOf($parentCustomerStory);
}
$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
}
if ($reference == 'name') {
$model->slug = str_slug($attribute->getValue());
}
break;
}
});
$model->save(); //Save the customerStory
$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);
//Return the customerStory
return $model;
}
/**
* 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();
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($valueReference) {
case 'link':
// if(isset($model) && $model->lft == 1) break;
$link = '';
$language = $attribute->getAssociatedLanguage();
$translation = $this->getTranslationModelForModelByLanguage($model, $language);
$route = Route::where('route', 'customerStories')
->where('language_id', $language->id)
->first();
if (! empty($model->slug) && isset($route)) {
$link = '/'.$route->alias.'/'.$model->slug;
}
$attribute->setLink($link)->setLinkText($link);
break;
}
}
);
return $filledAttributesCollection;
}
public function getOptionsForSelect($withRoot = false, $withNone = false): BaseCollection
{
$selectOptions = collect();
$products = CustomerStory::with('translation');
if (! $withRoot) {
$products = $products->where('lft', '!=', 1);
}
$products = $products->get();
if ($withNone) {
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent(__('kms/global.none'))
->setHtmlContent(__('kms/global.none'))
->setValue(null);
$selectOptions->push($selectOption);
}
foreach ($products as $product) {
$name = $product->getDisplayName();
if (empty($name)) {
if ($product->lft == 1) {
$name = 'Root';
} else {
$name = __('kms/products.entity').' '.$product->id;
}
}
/** @var SelectOptionInterface $selectOption */
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent($name)
->setHtmlContent($name)
->setValue($product->id);
$selectOptions->push($selectOption);
}
return $selectOptions;
}
}