<?php 
class Application_Model_File extends MyWeb_Model_Database
{

    protected $_storageName = 'File';

    protected $_formClasses = array(
        'insert' => 'fileNew',
        'update' => 'fileEdit',
        'search' => 'fileSearch'
        );

    public function fetchEntry($id)
    {
        // databázový adaptér
        $db = $this->getStorage()->getAdapter();
        
        // vytvoří SELECT
        $select = $db->select();
        if (is_null($id)){
        	$select	->	from(array('F' => 'file'),
        						array('id_file','file_path','file_name','file_dateTime','TypeFile_id_typeFile','State_id_state','User_id_user'))
        
        					->	join(array('T' => 'TypeFile'),
        						'F.TypeFile_id_typeFile=T.id_typeFile',
        						array('id_typeFile','typeFile_type'))
        					->	join(array('S' => 'State'),
        						'F.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        					->	join(array('U' => 'User'),
        						'F.User_id_user=U.id_user',
        						array('id_user','user_name','user_userName','user_registrationDateTime','user_passwd','State_id_state'));
        }else {
        	$select	->	from(array('F' => 'file'),
        				array('id_file','file_path','file_name','file_dateTime','TypeFile_id_typeFile','State_id_state','User_id_user'))
        
        					->	join(array('T' => 'TypeFile'),
        						'F.TypeFile_id_typeFile=T.id_typeFile',
        						array('id_typeFile','typeFile_type'))
        					->	join(array('S' => 'State'),
        						'F.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        					->	join(array('U' => 'User'),
        						'F.User_id_user=U.id_user',
        						array('id_user','user_name','user_userName','user_registrationDateTime','user_passwd','State_id_state'))
        			->	where('F.id_file = ?',$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('F' => 'file'),
        					array('id_file','file_path','file_name','file_dateTime','TypeFile_id_typeFile','State_id_state','User_id_user'))
        
        					->	join(array('T' => 'TypeFile'),
        						'F.TypeFile_id_typeFile=T.id_typeFile',
        						array('id_typeFile','typeFile_type'))
        					->	join(array('S' => 'State'),
        						'F.State_id_state=S.id_state',
        						array('id_state','state_state','NameState_id_NameState','TableDatabase_id_tableDatabase'))
        					->	join(array('U' => 'User'),
        						'F.User_id_user=U.id_user',
        						array('id_user','user_name','user_userName','user_registrationDateTime','user_passwd','State_id_state'));
        // načítá údaje
         if (is_null($data)){
        	//echo "<p>kompletni vypis</p>";
        }else {
        	if (array_key_exists('id_file', $data)){
        		if (!is_null($data['id_file']) && (strcmp($data['id_file'],"none") !== 0) && (strcmp($data['id_file'],"") !== 0)){
        			$select	->	where('F.id_file = ?',$data['id_file']); 
        		}
        	}
        
        	if (array_key_exists('file_path', $data)){
        		if (!is_null($data['file_path']) && (strcmp($data['file_path'],"none") !== 0) && (strcmp($data['file_path'],"") !== 0)){
        			$select	->	where('F.file_path = ?',$data['file_path']); 
        		}
        	}
        
        	if (array_key_exists('file_name', $data)){
        		if (!is_null($data['file_name']) && (strcmp($data['file_name'],"none") !== 0) && (strcmp($data['file_name'],"") !== 0)){
        			$select	->	where('F.file_name = ?',$data['file_name']); 
        		}
        	}
        
        	if (array_key_exists('file_dateTime', $data)){
        		if (!is_null($data['file_dateTime']) && (strcmp($data['file_dateTime'],"none") !== 0) && (strcmp($data['file_dateTime'],"") !== 0)){
        			$select	->	where('F.file_dateTime = ?',$data['file_dateTime']); 
        		}
        	}
        
        	if (array_key_exists('TypeFile_id_typeFile', $data)){
        		if (!is_null($data['TypeFile_id_typeFile']) && (strcmp($data['TypeFile_id_typeFile'],"none") !== 0) && (strcmp($data['TypeFile_id_typeFile'],"") !== 0)){
        			$select	->	where('F.TypeFile_id_typeFile = ?',$data['TypeFile_id_typeFile']); 
        		}
        	}
        
        	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('F.State_id_state = ?',$data['State_id_state']); 
        		}
        	}
        
        	if (array_key_exists('User_id_user', $data)){
        		if (!is_null($data['User_id_user']) && (strcmp($data['User_id_user'],"none") !== 0) && (strcmp($data['User_id_user'],"") !== 0)){
        			$select	->	where('F.User_id_user = ?',$data['User_id_user']); 
        		}
        	}
        
        }
        // načítá údaje
        //echo "<p>".$select."</p>";
        $data = $db->fetchAll($select);
        	return $data;
    }


}
