<?php 
class Application_Model_State extends MyWeb_Model_Database
{

    protected $_storageName = 'State';

    protected $_formClasses = array(
        'insert' => 'stateNew',
        'update' => 'stateEdit',
        'search' => 'stateSearch'
        );

        
        
        
        
	public function fetchEntries($dataInput)
    {
    	// databázový adaptér
        $db = $this->getStorage()->getAdapter();
        $select = $this->createFetchEntrySelect($dataInput);
        // načítá údaje
        //echo "<p>".$select."</p>";
        $data = $db->fetchAll($select);
        return $data;
    }
    
    
    
    public function fetchEntry($id)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db->select();
        if (is_null($id)){
        	$select	->	from(array('S' => 'state'),
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        
        					->	join(array('N' => 'NameState'),
        						'S.NameState_id_NameState=N.id_NameState',
        						array('id_NameState','nameState_name'))
        					->	join(array('T' => 'TableDatabase'),
        						'S.TableDatabase_id_tableDatabase=T.id_tableDatabase',
        						array('id_tableDatabase','tableDatabase_table'));
        }else {
        	$select	->	from(array('S' => 'state'),
        				array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        
        					->	join(array('N' => 'NameState'),
        						'S.NameState_id_NameState=N.id_NameState',
        						array('id_NameState','nameState_name'))
        					->	join(array('T' => 'TableDatabase'),
        						'S.TableDatabase_id_tableDatabase=T.id_tableDatabase',
        						array('id_tableDatabase','tableDatabase_table'))
        			->	where('S.id_state = ?',$id);
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        $data['data'] = $db->fetchRow($select);
        return $data;
    }

    
    
    
    
	/**
     * vrati select pro fetchEntry
     * @see MyWeb_Model_Database::getFetchEntrySelect()
     */
    public function getFetchEntrySelect($data){
    	$select = $this->createFetchEntrySelect($data);
    	return $select;
    }
    
    
    
    public function createFetchEntrySelect($data)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db	->	select()
        				->	from(array('S' => 'state'),
        					array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        
        					->	join(array('N' => 'NameState'),
        						'S.NameState_id_NameState=N.id_NameState',
        						array('id_NameState','nameState_name'))
        					->	join(array('T' => 'TableDatabase'),
        						'S.TableDatabase_id_tableDatabase=T.id_tableDatabase',
        						array('id_tableDatabase','tableDatabase_table'));
        // načítá údaje
         if (is_null($data)){
        	//echo "<p>kompletni vypis</p>";
        }else {
        	if (array_key_exists('id_state', $data)){
        		if (!is_null($data['id_state']) && (strcmp($data['id_state'],"none") !== 0) && (strcmp($data['id_state'],"") !== 0)){
        			$select	->	where('S.id_state = ?',$data['id_state']); 
        		}
        	}
        
        	if (array_key_exists('state_state', $data)){
        		if (!is_null($data['state_state']) && (strcmp($data['state_state'],"none") !== 0) && (strcmp($data['state_state'],"") !== 0)){
        			$select	->	where('S.state_state = ?',$data['state_state']); 
        		}
        	}
        
        	if (array_key_exists('NameState_id_NameState', $data)){
        		if (!is_null($data['NameState_id_NameState']) && (strcmp($data['NameState_id_NameState'],"none") !== 0) && (strcmp($data['NameState_id_NameState'],"") !== 0)){
        			$select	->	where('S.NameState_id_NameState = ?',$data['NameState_id_NameState']); 
        		}
        	}
        
        	if (array_key_exists('TableDatabase_id_tableDatabase', $data)){
        		if (!is_null($data['TableDatabase_id_tableDatabase']) && (strcmp($data['TableDatabase_id_tableDatabase'],"none") !== 0) && (strcmp($data['TableDatabase_id_tableDatabase'],"") !== 0)){
        			$select	->	where('S.TableDatabase_id_tableDatabase = ?',$data['TableDatabase_id_tableDatabase']); 
        		}
        	}
        	
        	// slouzi k predani jmena tabulky do popup window
        	// vstupem jmeno tabulky user 
        	if (array_key_exists('tableDatabase_table', $data)){
        		if (!is_null($data['tableDatabase_table']) && (strcmp($data['tableDatabase_table'],"none") !== 0) && (strcmp($data['tableDatabase_table'],"") !== 0)){
        			$select	->	where('T.tableDatabase_table = ?',$data['tableDatabase_table']); 
        		}
        	}
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        	return $select;
    }


}
