<?php 
class Application_Model_Comment extends MyWeb_Model_Database
{

    protected $_storageName = 'Comment';

    protected $_formClasses = array(
        'insert' => 'commentNew',
        'update' => 'commentEdit',
        'search' => 'commentSearch'
        );

        
        
        
        
        
        
        
	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->fetchRow($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('C' => 'comment'),
        						array('id_comment','comment_text','comment_dateTime','Article_id_article','State_id_state'))
        
        					->	join(array('A' => 'Article'),
        						'C.Article_id_article=A.id_article',
        						array('id_article','article_text','article_dateTime','article_content','User_id_user','State_id_state'))
        					->	join(array('S' => 'State'),
        						'C.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'));
        }else {
        	$select	->	from(array('C' => 'comment'),
        				array('id_comment','comment_text','comment_dateTime','Article_id_article','State_id_state'))
        
        					->	join(array('A' => 'Article'),
        						'C.Article_id_article=A.id_article',
        						array('id_article','article_text','article_dateTime','article_content','User_id_user','State_id_state'))
        					->	join(array('S' => 'State'),
        						'A.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'))
        				->	join(array('T' => 'TableDatabase'),
        						'S.TableDatabase_id_tableDatabase=T.id_tableDatabase',
        						array('id_tableDatabase','tableDatabase_table'))
        			->	where('C.id_comment = ?',$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');
        }else {
        	$acl = MyWeb_Acl::getInstance();
        	$request = Zend_Controller_Front::getInstance()->getRequest();
        	if (!$acl->isAllowed(Zend_Auth::getInstance()->getIdentity()->role_role,strtolower($request->getControllerName()),'disabledData')){
        		$select->where('N.nameState_name <> ?','disabled');
        	}
        }
    	return $select;
    }
    
    
    
    /**
     * Vytvori select pro fetch entries
     * 
     * @param unknown_type $data
     */
    public function createFetchEntrySelect($data)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db	->	select()
        				->	from(array('C' => 'comment'),
        					array('id_comment','comment_text','comment_dateTime','Article_id_article','State_id_state'))
        
        					->	join(array('A' => 'Article'),
        						'C.Article_id_article=A.id_article',
        						array('id_article','article_text','article_dateTime','article_content','User_id_user','State_id_state'))
        					->	join(array('S' => 'State'),
        						'C.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'))
        					->	join(array('T' => 'TableDatabase'),
        						'S.TableDatabase_id_tableDatabase=T.id_tableDatabase',
        						array('id_tableDatabase','tableDatabase_table'));
        						
        $select	->	where('T.tableDatabase_table = ?',$this->_storageName);
        // načítá údaje
         if (is_null($data)){
        	//echo "<p>kompletni vypis</p>";
        }else {
        	if (array_key_exists('id_comment', $data)){
        		if (!is_null($data['id_comment']) && (strcmp($data['id_comment'],"none") !== 0) && (strcmp($data['id_comment'],"") !== 0)){
        			$select	->	where('C.id_comment = ?',$data['id_comment']); 
        		}
        	}
        
        	if (array_key_exists('comment_text', $data)){
        		if (!is_null($data['comment_text']) && (strcmp($data['comment_text'],"none") !== 0) && (strcmp($data['comment_text'],"") !== 0)){
        			$select	->	where('C.comment_text = ?',$data['comment_text']); 
        		}
        	}
        
        	if (array_key_exists('comment_dateTime', $data)){
        		if (!is_null($data['comment_dateTime']) && (strcmp($data['comment_dateTime'],"none") !== 0) && (strcmp($data['comment_dateTime'],"") !== 0)){
        			$select	->	where('C.comment_dateTime = ?',$data['comment_dateTime']); 
        		}
        	}
        
        	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('C.Article_id_article = ?',$data['Article_id_article']); 
        		}
        	}
        
        	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('C.State_id_state = ?',$data['State_id_state']); 
        		}
        	}
        
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        return $select;
    }


}
