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/SBogers60/agrimac.nl/workbench/komma/kms/src/Komma/Kms/Faq/FaqRepository.php
<?php
/**
 * Short description for the file.
 *
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2015, Komma Mediadesign
 */

namespace Komma\Kms\Faq;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Komma\Kms\Core\Entities\KmsHierarchicalListItemEntity;
use Komma\Kms\Core\Kms;
use Komma\Kms\Core\KmsRepository;

// Change these into new entity-type
use Komma\Kms\Core\Tree\Tree;
use Komma\Kms\Languages\Language;
use Komma\Kms\Faq\Models\Faq;
use Komma\Kms\Faq\Models\FaqTranslation;

class FaqRepository extends KmsRepository
{

    // Here are some variables repeated in this repository
    // These change for every type of entity

    // Namespace
    protected $namespace = 'Faq';
    // Tables
    protected $table = 'faq';
    protected $tableTranslation = 'faq_translations';
    protected $translationId = 'faq_translation_id';
    protected $foreignKey = 'faq_id';
    // Entities
    protected $entity = 'FaqEntity';
    protected $entityTranslation = 'FaqTranslationEntity';
    // Model-string
    protected $modelString = 'Faq';
    protected $modelTranslationString = 'FaqTranslation';

    /**
     * @var Tree
     */
    protected $tree;

    /**
     * @var
     */
    private $model;

    /**
     * @var
     */
    private $modelTranslation;

    private $languages;

    /**
     * ProjectRepository constructor.
     * @param Kms $kms
     */
    function __construct(
        Kms $kms,
        Tree $tree,
        Language $language
    )
    {
        parent::__construct($kms);
        $this->tree = $tree;
        $this->languages = $language;
    }

    public function newEntity()
    {
        $entity = new FaqEntity();

        foreach ($this->kms->getCurrentLanguages() as $language) {
            $entity->addTranslationEntity(new FaqTranslationEntity($language->id));
        }

        return $entity;
    }

    public function getEntity($id)
    {
        if ($id == null) return $this->newEntity();

        $model = Faq::find($id);


        if (!$model) return false;

        $entity = new FaqEntity($model->attributesToArray());

        foreach ($this->kms->getCurrentLanguages() as $language){

            $languageTrans = FaqTranslation::where('language_id', '=', $language->id)
                ->where('faq_id', '=', $id)
                ->first();

            if(!$languageTrans){
                $entity->addTranslationEntity(
                    new FaqTranslationEntity($language->id)
                );
                continue;
            };

            $entityTranslationString = 'Komma\Kms\\' . $this->namespace . '\\' . $this->entityTranslation;


            $entity->addTranslationEntity(
                new $entityTranslationString(
                    $languageTrans->language_id,
                    $languageTrans->attributesToArray()
                )
            );

        }

        return $entity;
    }

    public function getEntities()
    {
        $tree = $this->getEntitiesAsTree();
        $this->makeRootIfNotExists();

        return $tree;
    }

    public function getEntitiesAsTree($langId = null)
    {

        if($langId == null) $langId = $this->kms->getDefaultLanguageId();

        $records = \DB::table($this->table)
            ->select(
                $this->table . '.lft',
                $this->table . '.rgt',
                $this->table . '.id as id',

                $this->tableTranslation . '.id as ' . $this->translationId,
                $this->tableTranslation . '.name'
            )
            ->leftJoin(
                $this->tableTranslation,
                $this->table . '.id', '=', $this->tableTranslation . '.' . $this->foreignKey
            )
            ->where(function ($query) use ($langId)
            {
                $query->whereNull($this->tableTranslation . '.language_id')
                    ->orWhere($this->tableTranslation . '.language_id', '=', $langId);
            })
            ->orderBy($this->table . '.lft', 'asc')
            ->get();
        $categories = [];

        foreach ($records as $record)
        {
            $categories[] = new KmsHierarchicalListItemEntity((array)$record);
        }

        $tree = new Tree();
        $tree->make($categories);

        return $tree;
    }


    /**
     * @param $entity
     * @return mixed
     */
    public function saveEntity($entity)
    {
        $faq = Faq::firstOrNew(['id' => $entity->id]);

        if($faq->id == null)
        {
            $faq->makeFirstChildOf(Faq::where('lft', '=', 1)->first());
        }

        //Set the fields
        $faq->active = $entity->active;

        $faq->save();

        // Append id to entity for restfull routing
        $entity->id = $faq->id;

        foreach ($entity->getTranslations() as $translation) {
            if(!isset($translation->name) || $translation->name == ''){
                continue;
            }

            $projectTranslation = FaqTranslation::firstOrNew(['faq_id' => $faq->id, 'language_id' => $translation->getLanguageId()]);
            $projectTranslation->name = $translation->name;
            $projectTranslation->description = $translation->description;

            $projectTranslation->save();

        }


        return $faq;
    }


    public function destroyEntity($id)
    {
        $entity = Faq::find($id);
        $entity->delete();
    }

    protected function makeRootIfNotExists()
    {
        $model = \DB::table($this->table)
            ->where('lft', 1)
            ->first();
        if( ! $model)
        {
            $modelString = 'Komma\Kms\\' . $this->namespace . '\Models\\' . $this->modelString;
            $model = new $modelString;
            $model->makeRoot();
        }
    }

}