<?php 
class Application_Model_Article extends MyWeb_Model_Database
{

    protected $_storageName = 'Article';

    protected $_formClasses = array(
        'insert' => 'articleNew',
        'update' => 'articleEdit',
        'search' => 'articleSearch'
        );

    public function fetchEntries($dataInput)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        $select = $this->createFetchEntrySelect($dataInput);
        
        
        
        // načítá údaje
        $this->showMe($select, 'Q');
        $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('A' => 'article'),
        						array('id_article','article_text','article_dateTime','article_content','User_id_user','State_id_state'))
        
        					->	join(array('U' => 'User'),
        							'A.User_id_user=U.id_user',
        							array('id_user','user_name','user_userName','user_registrationDateTime','user_passwd'))
        					->	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'));
        }else {
        	$select	->	from(array('A' => 'article'),
        				array('id_article','article_text','article_dateTime','article_content','User_id_user','State_id_state'))
        
        					->	join(array('U' => 'User'),
        							'A.User_id_user=U.id_user',
        							array('id_user','user_name','user_userName','user_registrationDateTime','user_passwd'))
        					->	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('A.id_article = ?',$id);
        }
    	if (Zend_Auth::getInstance()->hasIdentity()){
    		
        	$acl = MyWeb_Acl::getInstance();
        	$url = new Zend_View_Helper_Url();
        	$request = Zend_Controller_Front::getInstance()->getRequest();
        	if (!$acl->isAllowed(Zend_Auth::getInstance()->getIdentity()->role_role,strtolower($request->getControllerName()),'disabled')){
        		$select->where('N.nameState_name = ?','enabled');
        	}
        }else {
        	$select->where('N.nameState_name = ?','enabled');
        }
        
        // načítá údaje
        $this->showMe($select, 'Q');
        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        $data['data'] = $db->fetchRow($select);
        return $data;
    }

    public function getFetchEntrySelect($data)
    {
        $select = $this->createFetchEntrySelect($data);
        $select = $this->createWhere($select, $data, " ");
        return $select;
    }

    public function createFetchEntrySelect($data)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db	->	select()
        				->	from(array('A' => 'article'),
        					array('id_article','article_text','article_dateTime','article_content','User_id_user','State_id_state'))
        
        					->	join(array('U' => 'User'),
        							'A.User_id_user=U.id_user',
        							array('id_user','user_name','user_userName','user_registrationDateTime','user_passwd'))
        					->	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'));
        
        $select	->	where('T.tableDatabase_table = ?',$this->_storageName);
        
    	
    	if (Zend_Auth::getInstance()->hasIdentity()){
        	$acl = MyWeb_Acl::getInstance();
        	$url = new Zend_View_Helper_Url();
        	$request = Zend_Controller_Front::getInstance()->getRequest();
        	if (!$acl->isAllowed(Zend_Auth::getInstance()->getIdentity()->role_role,strtolower($request->getControllerName()),'disabled')){
        		$select->where('N.nameState_name = ?','enabled');
        	}
        }else {
        	$select->where('N.nameState_name = ?','enabled');
        }
        
        
        // načítá údaje
         if (is_null($data)){
        	$this->showMe('kompletni vypis', 'info');
        }else {
        
        	if (array_key_exists('id_article',$data)){
        		if (!empty($data['id_article']) && (strcmp($data['id_article'],"none") !== 0) && (strcmp($data['id_article'],"") !== 0)){
        			$select	->	where('A.id_article = ?',$data['id_article']); 
        		}
        	}
        
        	if (array_key_exists('article_text',$data)){
        		if (!empty($data['article_text']) && (strcmp($data['article_text'],"none") !== 0) && (strcmp($data['article_text'],"") !== 0)){
        			$select	->	where('A.article_text = ?',$data['article_text']); 
        		}
        	}
        
        	if (array_key_exists('article_dateTime',$data)){
        		if (!empty($data['article_dateTime']) && (strcmp($data['article_dateTime'],"none") !== 0) && (strcmp($data['article_dateTime'],"") !== 0)){
        			$select	->	where('A.article_dateTime = ?',$data['article_dateTime']); 
        		}
        	}
        
        	if (array_key_exists('article_content',$data)){
        		if (!empty($data['article_content']) && (strcmp($data['article_content'],"none") !== 0) && (strcmp($data['article_content'],"") !== 0)){
        			$select	->	where('A.article_content = ?',$data['article_content']); 
        		}
        	}
        
        	if (array_key_exists('User_id_user',$data)){
        		if (!empty($data['User_id_user']) && (strcmp($data['User_id_user'],"none") !== 0) && (strcmp($data['User_id_user'],"") !== 0)){
        			$select	->	where('A.User_id_user = ?',$data['User_id_user']); 
        		}
        	}
        
        	if (array_key_exists('State_id_stateValue', $data) && !empty($data['State_id_stateValue'])){
        		if ((array_key_exists('State_id_stateInputName', $data)) && (array_key_exists('State_id_stateInputValue', $data))){
        			if (!empty($data['State_id_stateInputName']) && (strcmp($data['State_id_stateInputName'],"none") !== 0) && (strcmp($data['State_id_stateInputName'],"") !== 0)){
        				if (!empty($data['State_id_stateInputValue']) && (strcmp($data['State_id_stateInputValue'],"none") !== 0) || (strcmp($data['State_id_stateInputValue'],"0") !== 0)){
        					$select	->	where('A.State_id_state = ?',$data['State_id_stateInputValue']);
        				}
        			}
        		}
        		$select	->	where('N.nameState_name = ?',$data['State_id_stateValue']);
        	}
        
        }
        $this->showMe($select, 'Q');
        return $select;
    }


}
