public static function getDataTree(
        $rs,
        $title_field_name,
        $id_field_name,
        $level_field_name,
        $tree = null
    ) {
        $stack = array();
        if ($tree !== null && $tree instanceof SwatDataTreeNode) {
            $current_parent = $tree;
        } else {
            $current_parent = new SwatDataTreeNode('', Swat::_('Root'));
        }

        $base_parent = $current_parent;
        array_push($stack, $current_parent);
        $last_node = $current_parent;

        foreach ($rs as $row) {
            $title = $row->$title_field_name;
            $id = $row->$id_field_name;
            $level = $row->$level_field_name;

            if ($level > count($stack)) {
                array_push($stack, $current_parent);
                $current_parent = $last_node;
            } elseif ($level < count($stack)) {
                $current_parent = array_pop($stack);
            }

            $last_node = new SwatDataTreeNode($id, $title);
            $current_parent->addChild($last_node);
        }

        return $base_parent;
    }