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/fire-tech/fire-tech.nl/app/KommaApp/Kms/Core/Tree/TreeNode.php
<?php

namespace App\KommaApp\Kms\Core\Tree;

use App\KommaApp\Kms\Core\NestedSets\Nodes\TranslatableEloquentNode;
use App\KommaApp\Languages\Models\Language;
use Illuminate\Database\Eloquent\Model;

class TreeNode
{
    /**
     * @var Model $node
     */
    public $node;

    /**
     * @var array
     */
    protected $children = [];

    /**
     * @var TreeNode or null
     */
    protected $parent;

    /**
     * @param Model $node
     * @param Model|object $parent
     * @internal param object $attributes
     */
    public function __construct(Model $node, &$parent)
    {
        $this->node = $node;
        $this->parent = $parent;
    }

    public function addChildNode(TreeNode &$child)
    {
        return $this->children[] = $child;
    }

    public function getParent()
    {
        return $this->parent;
    }

    public function hasChildren()
    {
        return count($this->children) > 0 ? true : false;
    }

    public function getChildren()
    {
        return $this->children;
    }

    public function getDepth()
    {
        if ($this->parent) {
            return $this->parent->getDepth() + 1;
        }
        return 0;
    }

    public function getPath()
    {
        if ($this->parent != null) {
            $array = $this->parent->getPath();
            return array_merge($array, [$this->node->id]);
        }
        return [];
    }

    public function getSlugPath()
    {
        if ($this->parent != null) {
            $array = $this->parent->getSlugPath();
            $value = isset($this->node->slug) ? $this->node->slug : $this->node->id;
            return array_merge($array, [$value]);
        }
        return [];
    }

    /**
     * TODO JULES VERWIJDEREN?
     *
     * @param $language
     * @return mixed
     */
    public function getPrimaryRouteAlias($language)
    {

        if ($this->node->translations->count() == 0) return $language->iso_2;

        if (!$translation = $this->node->translations->where('language_id', $language->id)->first()) return $language->iso_2;

        if ($translation->routes->count() == 0) return $language->iso_2;

        if (!$route = $translation->routes->where('primary', 1)->first()) return $language->iso_2;

        return $route->alias;
    }

    public function getTranslationField($key, $language)
    {
        if ($this->node->name) return $this->node->name;

        if( ! is_a($this->node, TranslatableEloquentNode::class)) return false;

        if ($this->node->translations->count() == 0) return false;

        if(!is_a($language, Language::class)){
            if (!$translation = $this->node->translations->where('language_id', $language)->first()) return false;
        }
        else{
            if (!$translation = $this->node->translations->where('language_id', $language->id)->first()) return false;
        }


        if (!$translation->$key) return false;

        return $translation->$key;
    }


    /**
     * Get the routes of a given item
     * Used by an api call to for example kms/api/default/pages
     *
     * @param $item
     * @return array
     */
    public function getRoutes($item)
    {
        //Create empty Routes array
        $routes = [];
        //If there are no translations, return empty array
        if (!isset($item->node->translations)) return $routes;

        //Loop trough the translations
        foreach ($item->node->translations as $translation) {

            //Get the primary route, if non existing, skip to the next language
            if (!$route = $translation->route) continue;

            //Set the alias based on the language id
            $routes[$translation->language_id] = $route->alias;
        }
        //Return the array of routes
        return $routes;

    }

}