File: D:/HostingSpaces/fire-tech/fire-tech.nl/app/KommaApp/Course/Kms/CourseService.php
<?php
namespace App\KommaApp\Courses\Kms;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNode;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Courses\Models\Course;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class CourseService extends SectionService
{
protected $sortable = false;
function __construct()
{
$this->forModelName = Course::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 EloquentNode $model */
//Process Course 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();
$parentCourse = $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 course is found, model isset and parent course id isn't the current parent id
if($parentCourse && $model && $parentCourse->id !== $currentParentId)
{
$model->makeLastChildOf($parentCourse);
}
$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;
if($reference == 'name')
{
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
if(!$model->exists) $model->save();
$translation = $this->getOrCreateTranslationModelForModel($model, $language);
$translation['slug'] = (str_slug($attribute->getValue()));
$translation->save();
}
break;
}
});
$model->save(); //Save the course
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
//Return the course
return $model;
}
/**
* @param int|null $siteId
* @param int|null $languageId TODO: Jules. Check if we still need this
* @param int|null $excludeId TODO: Jules. Check if we still need this
* @return SelectOptionInterface[]
*/
public function getOptionsForSelect($languageId = null, int $excludeId = null): array
{
$entities = [];
// Check if the getModelsAsTree method exist
// if ( ! method_exists($this, 'getModelsAsTree')) {
if ( $this->sortable == false) {
/** @var $sidebarListItems SidebarListItem[] */
$sidebarListItems = $this->getModelsForSideBar();
if ($excludeId == -1) $entities = [];
foreach ($sidebarListItems as $sidebarListItem) {
$model = $this->forModelName::find($sidebarListItem->getId());
if(!$model) continue;
$entities[] = (new SelectOption())
->setValue($model->id)
->setContent($sidebarListItem->getName())
->setHtmlContent($sidebarListItem->getName());
}
return $entities;
}
// Get all models as tree
/** @var Tree $tree */
$tree = $this->getModelsAsTree($languageId);
// Exclude the current active item)
if ($excludeId) $tree->removeFromIndex((int)$excludeId);
// QnD !!! Indexing trees per language (multiple queries)
$treePerLanguage = [];
$siteLanguages = $this->kms->getSiteLanguages();
foreach ($siteLanguages as $language) {
$treePerLanguage[$language->id] = $this->getModelsAsTree($language->id);
}
// End QnD !!!
// The tree index contains all nodes keyed by id
/** @var TreeNode $record */
foreach ($tree->getIndex() as $record) {
$nodeName = null;
$sidebarListItem = $record->node;
$entity = (new SelectOption())->setValue($sidebarListItem->id);
// For each site language
foreach ($siteLanguages as $language) {
// Route with lanuage: Get the primary route for the current language
/** @var Tree $currentTree */
$currentTree = $treePerLanguage[$language->id];
//If $nodeName exist, continue
if ($nodeName) continue;
//No nodeName, get one
/** @var Tree[] $treePerLanguage*/
// $nodeName = $treePerLanguage[$language->id]->getEntityById($entity['value'])->getTranslationField('name', $language);
$nodeName = $treePerLanguage[$language->id]->getEntityById($entity->getValue())->getTranslationField('name', $language);
}
$entity->setContent((empty($nodeName) ? '/' : $nodeName));
//Set the spaces for an indent (three)
$indent = str_repeat(' ', $record->getDepth());
//Set the content for the dropdown list
$entity->setHtmlContent($indent . (empty($nodeName) ? '/' : ucfirst($nodeName) ));
$entities[] = $entity;
}
return $entities;
}
}