Question

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

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?

Was it helpful?

Solution

Looks like no one can help me (apart from pointing out typo errors) so I figured out it by myself. It's little bit ugly solution but it works like I want:

private function validateTree($tree)
    {
        foreach($tree as $node)
        {
            $parentName = $node['title'];
            if(isset($node['children']) and is_array($node['children']))
            {
                if((bool)$node = $this->validateLevel($node['children']))
                {
                    $this->errors[] = $node;
                }
            }
            else
            {
                $this->errors[] = $node;
            }
        }
    }

    private function validateLevel($nodes)
    {
        foreach($nodes as $node)
        {
            if(isset($node['children']) and (bool)$node['children'] and is_array($node['children']))
            {
                foreach($node['children'] as $nodeLvl3)
                {
                    if(isset($nodeLvl3['children']) and (bool)$nodeLvl3['children'] and is_array($nodeLvl3['children']))
                    {
                        foreach($nodeLvl3['children'] as $nodeLbl)
                        {
                            //everything is OK
                        }
                    }
                    else
                    {
                        $this->errors[] = 'Category'.$nodeLvl3['title'].' does not have lvl 4';
                    }
                }
            }
            else
            {
                $this->errors[] = 'Category'.$node['title'].' does not have lvl 3';
            }
        }
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top