File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/App/Categories/CategoryTree.php
<?php
namespace App\Categories;
include_once($_SERVER['DOCUMENT_ROOT'] . '/App/Tree/Tree.php');
use App\Tree\Tree;
class CategoryTree extends Tree
{
private $categoryRepo;
private $activeId;
public function __construct()
{
$this->categoryRepo = new CategoryRepository();
}
/**
* @param $category
* @return bool
*/
public function isActive($category)
{
// If no active id is set, return false
if( ! $this->activeId) return false;
// See if the ids match
$isEqual = $category->id == $this->activeId;
// See if any of the children ids match
$isChild = $category->childNodeById($this->activeId);
// See if any of the grandchildren ids match
$isGrandchild = false;
foreach($category->children() as $child)
{
if($child->childNodeById($this->activeId))
{
$isGrandchild = true;
break;
}
}
// See what to active
switch($this->levelIndex[$this->activeId])
{
case 1:
return $isEqual;
break;
case 2:
return $isEqual || $isChild;
break;
case 3:
return $isEqual || $isChild || $isGrandchild;
}
return false;
}
/**
* @param $route
* @return mixed
*/
public function setActiveIdByRoute($route)
{
if( ! $route) return false;
// If the active route is a category, simply return the id
if($route->routeable_type == 'category')
$this->activeId = $route->routeable_id;
// If the active route is a product, get its category id
if($route->routeable_type == 'product')
$this->activeId = $this->categoryRepo->categoryIdByProductId($route->routeable_id);
}
}