<?php 
class Application_Model_ArticleHasSubject extends MyWeb_Model_Database
{

    protected $_storageName = 'ArticleHasSubject';

    protected $_formClasses = array(
        'insert' => 'ArticleHasSubjectNew',
        'update' => 'ArticleHasSubjectEdit',
        'search' => 'ArticleHasSubjectSearch'
        );

    public function fetchEntry($id)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db->select();
        if (is_null($id)){
        	$select	->	from(array('A' => 'ArticleHasSubject'),
        						array('Article_id_article','SubjectLevel_id_subjectLevel'))
        
        					->	join(array('S' => 'SubjectLevel'),
        						'A.SubjectLevel_id_subjectLevel=S.id_subjectLevel',
        						array('id_subjectLevel','subjectLevel_name','State_id_state'));
        }else {
        	$select	->	from(array('A' => 'ArticleHasSubject'),
        				array('Article_id_article','SubjectLevel_id_subjectLevel'))
        
        					->	join(array('S' => 'SubjectLevel'),
        						'A.SubjectLevel_id_subjectLevel=S.id_subjectLevel',
        						array('id_subjectLevel','subjectLevel_name','State_id_state'))
        			->	where('A.Article_id_article = ?',$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('A' => 'ArticleHasSubject'),
        					array('Article_id_article','SubjectLevel_id_subjectLevel'))
        
        					->	join(array('S' => 'SubjectLevel'),
        						'A.SubjectLevel_id_subjectLevel=S.id_subjectLevel',
        						array('id_subjectLevel','subjectLevel_name','State_id_state'));
        // načítá údaje
         if (is_null($data)){
        	//echo "<p>kompletni vypis</p>";
        }else {
        	if (array_key_exists('Article_id_article', $data)){
        		if (!is_null($data['Article_id_article']) && (strcmp($data['Article_id_article'],"none") !== 0) && (strcmp($data['Article_id_article'],"") !== 0)){
        			$select	->	where('A.Article_id_article = ?',$data['Article_id_article']); 
        		}
        	}
        
        	if (array_key_exists('SubjectLevel_id_subjectLevel', $data)){
        		if (!is_null($data['SubjectLevel_id_subjectLevel']) && (strcmp($data['SubjectLevel_id_subjectLevel'],"none") !== 0) && (strcmp($data['SubjectLevel_id_subjectLevel'],"") !== 0)){
        			$select	->	where('A.SubjectLevel_id_subjectLevel = ?',$data['SubjectLevel_id_subjectLevel']); 
        		}
        	}
        
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        $data = $db->fetchAll($select);
        	return $data;
    }


}
