<?php
/*********************************************
Desciption: Database Lists
File: /boffice/includes/classe.bd.listas.php
Project: Saborearte
Developers: João Vieira (Brevespot)
Start Date: 2007/04/20
*********************************************/

class DB_List
{
	var $connectDb;
	
	var $sqlStmt;
	
	var $lngDefault = "pt";
	
	var $recordSelected;
	
	var $showNotBefore = 0;
	var $showLikeTree = False;
	
	var $treeGetree = array();
	var $treeRetree = array();
	
	var $fieldShowMain = true;
	
        var $designList = array (
	       'name'  => '', 
	       'css'   => '',
	       'style' => ''
	       );
	
	function DB_List ($connectDb) {
		$this->connectDb = $connectDb;
	}
	
	function sqlStmt ($sqlStmt) {
		$this->sqlStmt = $sqlStmt;
	}
	
	function recordSelected ($recordSelected) {
		$this->recordSelected = $recordSelected;
	}	
	
        function showNotBefore ($showNotBefore) {
		$this->showNotBefore = $showNotBefore-1;
	}	
		
        function showLikeTree ($showLikeTree) {
		$this->showLikeTree = $showLikeTree;
	}	

	function processTree($id_parent = 0, $indent = 0) {
	    $tt = array();
	    $x = 0;
	    $loop = 0;
	    	
	    foreach($this->treeGetree as $n) {         
		if( $n['id_parent'] == $id_parent)
		{
		    $tt[$x++] = $loop;  
		}  
		$loop++;
	    }
	    if( $x != 0) {             
		foreach($tt as $d) {
		    $tmp = array();		    
		    foreach($this->treeGetree[$d] as $key => $value) {                
			$tmp[$key] = $value; 		
		    }
		    $tmp['indent'] = $indent;
		    $this->treeRetree[] = $tmp;		    
		    $this->processTree($tmp['id'], $indent+1);
		}
	    }
	    else {
		return;
	    }
	}
		
		
		
		
	function MakeTree() {
	    $resultDb = $this->connectDb->pedido($this->sqlStmt);
	    $this->recordsTotal = $this->connectDb->registosNumero();
	    
	    while ($row = $resultDb->fetch(PDO::FETCH_NUM)) {
		$this->treeGetree[]  = array(
		'id'        => $row[0],
		'id_parent' => $row[1],
		'title'     => $row[2]
		);
	    }
	    $this->processTree(0, 0);
	    
	    $resultString = "<select name=\"" . $this->designList['name'] . "\" style=\"" . $this->designList['style'] . "\" class=\"" . $this->designList['css'] . "\">\n";
	    if ($this->fieldShowMain) $resultString .= "<option value=\"\"></option>\n";
	    
            if ($this->connectDb->registosNumero()) {	    
		foreach($this->treeRetree as $cat) {
		    $selectedString = "";
		    $fieldReturn = "";
		    $indent = "";
		    for($i=0;$i<$cat['indent'];$i++) {                        
			$indent .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
		    }		   
		     
		    if ( stripos($cat['title'], "[pt]") !== false ) $cat['title'] = trd($cat['title'], $this->lngDefault);
		     
		    if ($cat['id_parent'] > 0 ) {
		          $fieldReturn .= $indent."&nbsp;|-".$cat['title'];
		    }else $fieldReturn = $cat['title'];
		
		    if ((int) $this->recordSelected == (int) $cat['id']) $selectedString = "selected ";
		    else $selectedString = "";
                    
                    $resultString .= "<option " . $selectedString . "value=\"" . $cat['id'] . "\">";
		    $resultString .= $fieldReturn;
		    $resultString .= "</option>\n";
		}
            }	 
               
	    
	    print $resultString . "</select>\n";
	    

	}
		
		
		
		
		
		
	function Make()	{
	    if ($this->showLikeTree) {
		$this->MakeTree();
		return;
	    }
	
	    $resultDb = $this->connectDb->pedido($this->sqlStmt);
	    $resultString = "<select name=\"" . $this->designList['name'] . "\" style=\"" . $this->designList['style'] . "\" class=\"" . $this->designList['css'] . "\">\n";
	    if ($this->fieldShowMain) $resultString .= "<option value=\"\"></option>\n";
	    $selectedString = "";
	    if ($this->connectDb->registosNumero()) {
		while ($row = $resultDb->fetch(PDO::FETCH_NUM)) {
			$fez = false;
		    for($i=0; $i<count($row); $i++) {					
		          
		        if ( stripos($row[$i], "[pt]") !== false ) $row[$i] = trd($row[$i], $this->lngDefault);
                        
                        if ($i == 0) {					
                                if ($this->recordSelected == $row[0]) $selectedString = "selected ";
                                else $selectedString = "";
                                $resultString .= "<option " . $selectedString . "value=\"" . $row[0] . "\">";
                        }
                        elseif ($i >= $this->showNotBefore) {											    
                                if ($fez) $resultString .= "&nbsp;>&nbsp;";
                                $resultString .= $row[$i];
                                $fez = true;
                        }
		    }
		    $resultString .= "</option>\n";
		}
	    }
	    print $resultString . "</select>\n";
	}
}
?>