<?php
/**
 * Virtuemart Product files table
 *
 * @package		CSVIVirtueMart
 * @author 		Roland Dalmulder
 * @link 		http://www.csvimproved.com
 * @copyright 	Copyright (C) 2006 - 2012 RolandD Cyber Produksi
 * @license 	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 * @version 	$Id: vm_product_files.php 1892 2012-02-11 11:01:09Z RolandD $
 */

/* No direct access */
defined('_JEXEC') or die('Restricted access');

/**
 * @package CSVIVirtueMart
 */
class TableVm_product_files extends JTable {
	
	// Sets whether the database columns have been loaded
	private $_loaded = false;
	
	/**
	* @param database A database connector object
	 */
	function __construct($db) {
		$this->reset();
		parent::__construct('#__vm_product_files', 'file_id', $db );
	}
	
	/**
	 * Set a value for the class
	 * 
	 * @copyright 
	 * @author		RolandD 
	 * @todo 
	 * @see 
	 * @access 		public
	 * @param 		string $field	the name of the field to update
	 * @param 		mixed $value	the value to assign to the field
	 * @return 		void
	 * @since 
	 */
	public function setValue($field, $value) {
		$this->$field = $value;
	}
	
	/**
	 * Get a value from the class 
	 * 
	 * @copyright 
	 * @author 		RolandD
	 * @todo 
	 * @see 
	 * @access 		public
	 * @param 		string	$field	the name of the field the value to return
	 * @return 		mixed	the chosen field value
	 * @since 		3.0
	 */
	public function getValue($field) {
		return $this->$field;
	}
	
	/**
	 * Resets the default properties
	 * 
	 * @copyright 
	 * @author		RolandD 
	 * @todo 
	 * @see 
	 * @access 		public
	 * @param 
	 * @return 
	 * @since 		3.0
	 */
	public function reset() {
		if (!$this->_loaded) {
			$this->setProperties(CsvivirtuemartModelAvailablefields::DbFields('vm_product_files'));
			$this->_loaded = true;
		}
		else {
			$class_vars = get_class_vars(get_class($this));
			foreach ($this as $name => $value) {
				if (substr($name, 0, 1) != '_') {
					$this->$name = null;
				}
			}
		}
	}
	
	/**
	 * Check if the file already exists 
	 * 
	 * @copyright 
	 * @author		RolandD	 
	 * @todo 
	 * @see 
	 * @access 		public
	 * @param 
	 * @return 
	 * @since 		3.0
	 */
	public function check() {
		$db = JFactory::getDBO();
		$csvilog = JRequest::getVar('csvilog');
		
		$q = "SELECT ".$this->_tbl_key."
			FROM ".$this->_tbl."
			WHERE file_product_id = ".$this->file_product_id."
			AND file_name = '".$this->file_name."'";
		$db->setQuery($q);
		$csvilog->addDebug(JText::_('COM_CSVIVIRTUEMART_CHECK_PRODUCT_FILE_EXISTS'), true);
		$db->query();
		
		// There should only be 1 file that is the same
		if ($db->getAffectedRows() == 1) $this->file_id = $db->loadResult();
	}
}
?>
