<?php 
class Application_Model_Role extends MyWeb_Model_Database
{

    protected $_storageName = 'Role';

    protected $_formClasses = array(
        'insert' => 'roleNew',
        'update' => 'roleEdit',
        'search' => 'roleSearch'
        );

        
        
        
        
        
	public function fetchEntries($dataInput)
    {
    	// databázový adaptér
        $db = $this->getStorage()->getAdapter();
        $select = $this->getFetchEntrySelect($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('R' => 'role'),
        						array('id_role','role_role','State_id_state'))
        
        					->	join(array('S' => 'State'),
        						'R.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'));
        }else {
        	$select	->	from(array('R' => 'role'),
        				array('id_role','role_role','State_id_state'))
        
        					->	join(array('S' => 'State'),
        						'R.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        			->	where('R.id_role = ?',$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);
    	
    	// pouze admin muze zobrazit stav komentare
        // naregistrovani nemohou
    	if (!Zend_Auth::getInstance()->hasIdentity()){
    		$select->where('N.nameState_name <> ?','disabled');
        }
        
    	return $select;
    }
    
    
    public function createFetchEntrySelect($data)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db	->	select()
        				->	from(array('R' => 'role'),
        					array('id_role','role_role','State_id_state'))
        
        					->	join(array('S' => 'State'),
        						'R.State_id_state=S.id_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'));
        // načítá údaje
         if (is_null($data)){
        	//echo "<p>kompletni vypis</p>";
        }else {
        	if (array_key_exists('id_role', $data)){
        		if (!is_null($data['id_role']) && (strcmp($data['id_role'],"none") !== 0) && (strcmp($data['id_role'],"") !== 0)){
        			$select	->	where('R.id_role = ?',$data['id_role']); 
        		}
        	}
        
        	if (array_key_exists('role_role', $data)){
        		if (!is_null($data['role_role']) && (strcmp($data['role_role'],"none") !== 0) && (strcmp($data['role_role'],"") !== 0)){
        			$select	->	where('R.role_role = ?',$data['role_role']); 
        		}
        	}
        
        	if (array_key_exists('State_id_state', $data)){
        		if (!is_null($data['State_id_state']) && (strcmp($data['State_id_state'],"none") !== 0) && (strcmp($data['State_id_state'],"") !== 0)){
        			$select	->	where('R.State_id_state = ?',$data['State_id_state']); 
        		}
        	}
        
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        	return $select;
    }


}
