<?php 
class Application_Model_Subject extends MyWeb_Model_Database
{

    protected $_storageName = 'Subject';

    protected $_formClasses = array(
        'insert' => 'subjectNew',
        'update' => 'subjectEdit',
        'search' => 'subjectSearch'
        );

    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' => 'subject'),
        						array('id_subject','subject_name','State_id_state'))
        
        					->	join(array('S' => 'State'),
        						'S.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'));
        }else {
        	$select	->	from(array('S' => 'subject'),
        				array('id_subject','subject_name','State_id_state'))
        
        					->	join(array('S' => 'State'),
        						'S.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        			->	where('S.id_subject = ?',$id);
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        $data['data'] = $db->fetchRow($select);
        return $data;
    }

    public function fetchEntries($data)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db	->	select()
        				->	from(array('S' => 'subject'),
        					array('id_subject','subject_name','State_id_state'))
        
        					->	join(array('S' => 'State'),
        						'S.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'));
        // načítá údaje
         if (is_null($data)){
        	//echo "<p>kompletni vypis</p>";
        }else {
        	if (array_key_exists('id_subject', $data)){
        		if (!is_null($data['id_subject']) && (strcmp($data['id_subject'],"none") !== 0) && (strcmp($data['id_subject'],"") !== 0)){
        			$select	->	where('S.id_subject = ?',$data['id_subject']); 
        		}
        	}
        
        	if (array_key_exists('subject_name', $data)){
        		if (!is_null($data['subject_name']) && (strcmp($data['subject_name'],"none") !== 0) && (strcmp($data['subject_name'],"") !== 0)){
        			$select	->	where('S.subject_name = ?',$data['subject_name']); 
        		}
        	}
        
        	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('S.State_id_state = ?',$data['State_id_state']); 
        		}
        	}
        
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        $data = $db->fetchAll($select);
        	return $data;
    }


}
