HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/inzigd.komma.pro/app/Komma/Courses/Kms/CourseService.php
<?php
namespace App\Komma\Courses\Kms;


use App\Komma\Categories\Kms\CategoryService;
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\Courses\Models\Course;
use App\Komma\Routes\Models\Route;
use App\Komma\Sites\HasSitesInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;


final class CourseService extends SectionService
{
    protected $sortable = true;

    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 TreeModel $model */
        //Process Page 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();
                        $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);

        $sectionTabItems->each(function($sectionTabItem, $key) use($model) {
            /** @var SectionTabItem $sectionTabItem */

            $attribute = $sectionTabItem->getAttribute();

            $reference = $attribute->getsValueFromReference();
            switch ($attribute->getsValueFrom())
            {
                case Attribute::ValueFromItself:
                    if($reference =='category') {
                        $value = $attribute->getValue();
                        if(empty($value)) $model->category()->detach();
                        else $model->category()->sync([$value]);
                    }
                    break;
            }
        });

        $model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes

        foreach ($model->translations as $translation) {

            // If there isn't a category we remove the slug
            if($model->category->isEmpty()) {
                $translation->slug = '';
                $translation->save();
            }
            else{
                $courseModels = CategoryService::getModelsByCategoryThroughRelation($model->category->first()->id, 'courses');
                $translation->slug = $this->createOrGetUniqueSlugFromSet($translation, $translation->name, 0, 'course_id', $courseModels->pluck('id')->toArray());
                $translation->save();
            }

        }

        //Return the page
        return $model;
    }


    /**
     * Fills non-course specific attributes. Course 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;

                    case 'category':
                        if($model->category->isEmpty()) $attribute->setValue('');
                        else $attribute->setValue($model->category->pluck('id')->toArray()[0]);
                        break;

                    case 'link':

                        $link = '';

                        $language = $attribute->getAssociatedLanguage();
                        $translation = $this->getTranslationModelForModelByLanguage($model, $language);

                        $route = Route::where('route', 'courses')
                            ->where('language_id', $language->id)
                            ->first();

                        if(!empty($translation->slug) && isset($route))
                        {

                            $link = '/' . $route->alias . '/' . $translation->slug;
                        }

                        $attribute->setLink($link)->setLinkText($link);
                        break;
                }
            }
        );

        return $filledAttributesCollection;
    }

    public function getLevels()
    {

        $options = [];

        foreach (__('site/courses.levels') as $key => $level)
        {
            $selectOption = app(SelectOptionInterface::class);
            $selectOption->setValue($key)
                ->setContent($level)
                ->setHtmlContent($level);

            $options[] = $selectOption;

        }

        return $options;
    }

    /**
     * This method will remove an TranslatableModelInterface instance
     *
     * @param $model
     * @throws \Exception
     */
    public function destroyModel(Model $model)
    {
        $model->category()->detach();
        return parent::destroyModel($model);
    }
}