I've build category tree using fancytree. Deepness of each node starting from root have to equal 4 so I may have:
- category
- subcategory 1
- subcategory 2
- subcategory 4
- subcategory 5
- subcategory 6
- subcategory 2
- subcategory 1
Script should allow to save to database only that kind of structure. I've tried to find some methods related to depth in Fancytree API but I couldn't find proper one.
I decided to write backend validation and I have something like that:
private function validateTree($tree)
{
foreach($tree as $node)
{
$parentName = $node['title'];
if(isset($node['children']) and is_array($node['children']))
{
if($this->validateLevel($node['children']) < 4)
{
$this->errors[] = $parentName;
}
}
else
{
$this->errors[] = $parentName;
}
}
}
private function validateLevel($nodes, &$depth = 2)
{
foreach($nodes as $node)
{
if(isset($node['children']) and !empty($node['children']) and is_array($node['children']))
{
$this->validateLevel($node['children'], ++$depth);
}
else
{
return $depth;
}
}
}
This don't work as expected and probably not validating every childs of each root node. Any ideas?