<?php
/**
 * @version		$Id: record.php 122 2009-06-19 08:56:53Z eddieajau $
 * @copyright	Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
 * @license		GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
 * @link		http://www.theartofjoomla.com
 */

// no direct access
defined('_JEXEC') or die;

jimport('joomla.database.table');

/**
 * Record table class
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaTableRecord extends JTable
{
	/**
	 * @var int unsigned
	 */
	var $id = null;
	/**
	 * @var varchar
	 */
	var $title = null;
	/**
	 * @var varchar
	 */
	var $alias = null;
	/**
	 * @var varchar
	 */
	var $label = null;
	/**
	 * @var tinyint
	 */
	var $published = null;
	/**
	 * @var tinyint unsigned
	 */
	var $access = null;
	/**
	 * @var varchar
	 */
	var $description = null;
	/**
	 * @var double
	 */
	var $maximum_value = null;
	/**
	 * @var double
	 */
	var $latest_value = null;
	/**
	 * @var timestamp
	 */
	var $latest_time = null;
	/**
	 * @var int
	 */
	var $created_user_id = null;
	/**
	 * @var timestamp
	 */
	var $created_time = null;
	/**
	 * @var int
	 */
	var $modified_user_id = null;
	/**
	 * @var timestamp
	 */
	var $modified_time = null;
	/**
	 * @var int
	 */
	var $checked_out = null;
	/**
	 * @var timestamp
	 */
	var $checked_out_time = null;
	/**
	 * @var int
	 */
	var $category_id = null;
	/**
	 * @var varchar
	 */
	var $params = null;

	/**
	 * @param database A database connector object
	 */
	function __construct(&$db)
	{
		parent::__construct('#__taoj_quanta_records', 'id', $db);
	}

	/**
	 * Check the internal properties of the table class.
	 *
	 * @return	boolean
	 */
	function check()
	{
		// check for valid name
		if (trim($this->title) == '') {
			$this->setError(JText::_('Record must contain a title'));
			return false;
		}
		if (empty($this->alias)) {
			$this->alias = strtolower($this->title);
		}
		// clean the alias
		$this->alias = str_replace('&', 'and', $this->alias);
		$this->alias = preg_replace('#[\s\-]+#', '-', $this->alias);
		$this->alias = preg_replace('#[^A-Z0-9\~\.\-\_]#i', '', $this->alias);
		return true;
	}

	/**
	 * Override store
	 */
	function store($updateNulls=false)
	{
		$app	= &JFactory::getApplication();
		$user	= &JFactory::getUser();
		$now	= &JFactory::getDate();

		if (is_array($this->params)) {
			$this->params = json_encode($this->params);
		}

		if ($this->id == 0) {
			$this->created_time		= $now->toMySQL();
			$this->created_user_id	= $user->get('id');
		}
		else {
			$this->modified_time	= $now->toMySQL();
			$this->modified_user_id	= $user->get('id');
		}

		return parent::store($updateNulls);
	}
}