File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/PricingProducts/Kms/PricingProductService.php
<?php
namespace App\Komma\PricingProducts\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\NestedSets\Nodes\TreeModel;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\PricingProducts\Models\PricingProduct;
use App\Komma\Sites\HasSitesInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Database\Eloquent\Model;
class PricingProductService extends SectionService
{
protected $sortable = true;
public function __construct()
{
$this->forModelName = PricingProduct::class;
parent::__construct();
}
public function getIconOptions() : array
{
$entities[] = (new SelectOption())
->setHtmlContent('Circle')
->setContent('Circle')
->setValue('circle');
$entities[] = (new SelectOption())
->setHtmlContent('Box')
->setContent('Box')
->setValue('box');
$entities[] = (new SelectOption())
->setHtmlContent('Balloon')
->setContent('balloon')
->setValue('balloon');
$entities[] = (new SelectOption())
->setHtmlContent('Layers')
->setContent('Layers')
->setValue('layers');
$entities[] = (new SelectOption())
->setHtmlContent('Stacks')
->setContent('Stacks')
->setValue('stacks');
$entities[] = (new SelectOption())
->setHtmlContent('Backup')
->setContent('Backup')
->setValue('backup');
$entities[] = (new SelectOption())
->setHtmlContent('House')
->setContent('House')
->setValue('house');
$entities[] = (new SelectOption())
->setHtmlContent('Location')
->setContent('Location')
->setValue('location');
$entities[] = (new SelectOption())
->setHtmlContent('Support')
->setContent('Support')
->setValue('support');
return $entities;
}
public function getTypeOptions() : array
{
$entities[] = (new SelectOption())
->setHtmlContent('Normal')
->setContent('Normal')
->setValue('normal');
$entities[] = (new SelectOption())
->setHtmlContent('Add on')
->setContent('Add on')
->setValue('addon');
$entities[] = (new SelectOption())
->setHtmlContent('Levels')
->setContent('Levels')
->setValue('levels');
return $entities;
}
public function getProductOptionsForSelect(): \Illuminate\Support\Collection
{
$selectOptions = collect();
$products = PricingProduct::with('translation')->where('lft', '!=', 1);
$products = $products->get();
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;
}
/**
* 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 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
}
break;
}
});
$model->save(); //Save the page
$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
$this->siteService->linkModelToCurrentSite($model);
//Return the page
return $model;
}
/**
* Fills non-job specific attributes. Job 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;
}
}