File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Magazines/Kms/MagazineService.php
<?php
namespace App\KommaApp\Magazines\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\Attributes\Models\SelectOptionInterface;
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\MagazineArticles\Kms\MagazineArticleService;
use App\KommaApp\Magazines\Models\Magazine;
use App\KommaApp\Magazines\Models\MagazineTranslation;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;
class MagazineService extends SectionService
{
protected $sortable = false;
protected $orderBy = 'date';
protected $orderReverse = true;
function __construct()
{
$this->forModelName = Magazine::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
{
$magazineArticleService = \App::make(MagazineArticleService::class);
/** @var TreeModel $model */
//Process Magazine Specific attributes
$sectionTabItems->each(function ($sectionTabItem, $key) use ($model, $magazineArticleService) {
/** @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 magazine
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
// Determine the edition_numbers
$this->determineEditionNumberOfYear($model->date->year, $model->site_id);
$this->updateSearchDatabases($model);
//Return the magazine
return $model;
}
/**
* Update the searchable databases
*
* @param $model
*/
private function updateSearchDatabases($model)
{
// Remove from search if inactive
if(! $model->active) $model->translations()->unsearchable();
else{
//Loop through the bind articles
foreach ($model->magazineArticles as $magazineArticle) {
//When active append them to the search
if($magazineArticle->active) $magazineArticle->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;
case 'magazineArticles':
if (isset($model->id)) {
$magazineArticles = implode(",", array_flatten($model->magazineArticles->keyBy('id')->keys()->toArray()));
} else $magazineArticles = '';
$attribute->setValue($magazineArticles);
}
}
);
return $filledAttributesCollection;
}
/**
* This method will remove an TranslatableModelInterface instance
*
* @param $model
* @throws \Exception
*/
public function destroyModel(Model $model)
{
/** @var Model|TreeModelInterface $model */
//delete the images
$this->imageService->deleteModelImages($model);
if (is_a($model, DocumentableInterface::class)) {
/** @var DocumentableInterface $model */
$this->documentService->deleteDocumentsForModel($model);
}
if (is_a($model, AbstractTranslatableModel::class)) {
/** @var AbstractTranslatableModel $model */
foreach ($model->translations()->get() as $translation) {
//Delete the translation
$translation->delete();
}
}
$model->delete();
}
/**
* @return BaseCollection containing SelectOptionInterface
*/
public function getOptionsForSelect($allowNullableSelectOption = false): BaseCollection
{
$selectOptions = collect();
if ($allowNullableSelectOption) {
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent(__('kms/global.none'))
->setHtmlContent(__('kms/global.none'))
->setValue(null);
$selectOptions->push($selectOption);
}
$magazines = Magazine::with('translation')
->orderBy('date', 'desc')
->get();
foreach ($magazines as $magazine) {
/** @var SelectOptionInterface $selectOption */
$selectOption = (\App::make(SelectOptionInterface::class))
->setContent($magazine->edition_number . '. ' . $magazine->translation->name)
->setHtmlContent($magazine->edition_number . '. ' . $magazine->translation->name)
->setValue($magazine->id);
$selectOptions->push($selectOption);
}
return $selectOptions;
}
public function determineEditionNumberOfYear(int $year, int $siteId = 1)
{
$carbonYearDate = Carbon::createMidnightDate($year, 1, 1);
$magazines = Magazine::where('site_id', $siteId)
->where('active', 1)
->where('date', '>=', $carbonYearDate->format('Y-m-d H:i:s'))
->where('date', '<=', $carbonYearDate->addYear()->format('Y-m-d H:i:s'))
->orderBy('date', 'asc')
->orderBy('created_at', 'asc')
->get();
foreach ($magazines as $key => $magazine) {
$magazine->edition_number = $key + 1;
$magazine->save();
}
}
}