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/anvil.komma.pro/app/KommaApp/Employees/Kms/EmployeeService.php
<?php
namespace App\KommaApp\Employees\Kms;


use App\KommaApp\Kms\Core\Attributes\Attribute;
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\Employees\Models\Employee;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class EmployeeService extends SectionService
{
    protected $sortable = true;

    function __construct()
    {
        $this->forModelName = Employee::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 Employee 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();
                        $parentEmployee = $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 employee is found, model isset and parent employee id isn't the current parent id
                        if($parentEmployee && $model && $parentEmployee->id !== $currentParentId)
                        {
                            $model->makeLastChildOf($parentEmployee);
                        }

                        $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;
                case Attribute::ValueFromTranslationModel;
                    if($reference == 'name')
                    {
                        /** @var Language $language */
                        $language = $attribute->getAssociatedLanguage();

                        if(!$model->exists) $model->save();
                    }
                    break;
            }
        });


        $model->save(); //Save the employee

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

        //Return the employee
        return $model;
    }
}