<?php
/**
 * db.treeitem.class
 *
 * DB Tree Item
 *
 * @version:    0.1a
 * @package:    flame.core
 * @author:    alex <qfox@ya.ru> yaroshevich
 * @created:    28 nov 2007
 * @link:    http://src.qfox.ru/
 * 
 * @copyright:    (c) 2007 flamebox. All rights reserved.
 */

/** standart verifying */
if( !defined'FLAMECMS' ) ) return;

if( 
defined'__DB_TREEITEM_CLASS_PHP' ) ) return;
define'__DB_TREEITEM_CLASS_PHP''0.1' );

require_once( 
DIR_CLS.'db.plainitem.class.php' );

class 
fbDBTreeItem extends fbDBPlainItem
{
    var 
$_table '';
    var 
$_fields null;
    var 
$_keys null;
    var 
$_id 0;
    var 
$_data null;
    
    
    var 
$_parents = array( );
    var 
$_childs = array( );
    
    var 
$_breadcrumb = array( );
    
    
    function 
fbDBTreeItem$_table$_id )
    {
        
parent::fbDBPlainItem$_table$_id );
    }
    
    function 
save( )
    {
        
parent::save( );
    }
    
    function 
load( )
    {
        
parent::load( );
        
//init
        
    
}
    
    function 
getChilds( )
    {
        
$item_id __$item_id$this ->getId( ) );
        
        
$__table $this ->_table;
        
$__st sqlTNName$__table );
        
$q = <<<sql
select r0.*, count( r1.id_{$__st} ) as 'childs'
from 
{$__table} r0
left join 
{$__table} r1 on r1.{$__st}_id = r0.id_{$__st}
where r0.
{$__st}_id in ( {$item_id} )
group by 1
order by r0.
{$__st}_sort asc;
sql;
        
//echo "<pre>$q</pre>";
        
$items sql$q );
        
        
$_r = array( );
        foreach( 
$items as $itemKey => $item )
            
$_r$item'id_'.$__st ] ] = $item;
        
        
$this ->_childs $_r;
        
        return 
$this ->_childs;
    }
    
    function 
getParent( )
    {
        
$class get_class$this );
        return new 
$class$this ->getParentId( ) );
    }
    
    function 
getParentId( )
    {
        return 
__$this ->_datasqlTNName$this ->_table ).'_id' ], $this ->_data'parent_id' ] );
    }
    
    function 
getRoot( )
    {
        
//todo: write code here
        //return __( $this ->_data[ sqlTNName( $this ->_table ).'_id' ], $this ->_data[ 'parent_id' ] );
    
}
    
    function 
getBreadcrumb( ) //chain to root
    
{
        
$parent_id $this ->getId( );
        do {
            list( 
$item ) = array_valuessqlget$this ->_table$parent_id ) );
            
array_unshift$this ->_breadcrumb$item );
            
$parent_id __$itemsqlTNName$this ->_table ).'_id' ], $item'parent_id' ] );
        } while( !
is_null$parent_id ) );
        
        return 
$this ->_breadcrumb;
    }
    
    function 
getTree$item_id null$recursiveLevel 0$__sub false )
    {
        
$item_id __$item_id$this ->getId( ) );
        
        
$__table $this ->_table;
        
$__st sqlTNName$__table );
        
$q = <<<sql
select r0.*, count( r1.id_{$__st} ) as 'childs'
from 
{$__table} r0
left join 
{$__table} r1 on r1.{$__st}_id = r0.id_{$__st}
where r0.
{$__st}_id in ( {$item_id} )
group by 1
order by r0.
{$__st}_sort asc;
sql;
        
//echo "<pre>$q</pre>";
        
$items sql$q );
        
        
$_r = array( );
        foreach( 
$items as $itemKey => $item )
        {
            if( 
$recursiveLevel )
            if( 
$item'childs' ] > )
                
$item'childs' ] = $this ->getTree$item'id_'.$__st ], $recursiveLevel 1true );
            else
                unset( 
$item'childs' ] );
            
            
$_r$item'id_'.$__st ] ] = $item;
        }
    
        return 
$_r;
    
    }
    
    function 
getFullTree( )
    {
        
$_r = array( );
        
$_range range1maxTREE_LEVEL_MAX) );
        
        
// yo query
        
$_sql "select *\n";
        
$_sql .= "from fbc_rubric_tree_tb r0\n";
        foreach( 
$_range as $plvl => $lvl )
            
$_sql .= "left join fbc_rubric_tree_tb r{$lvl} on r{$plvl}.id_rubric = r{$lvl}.rubric_id\n";
        
$_sql .= "where r0.rubrictype_id = 1;";
        
        
asd$_sql );
    }
};




?>