<?php
/**
 * @version		$Id: chart.php 109 2009-06-16 10:36:52Z 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');

/**
 * Chart table class
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaTableChart extends JTable
{
	/**
	 * @public int unsigned
	 */
	public $id = null;
	/**
	 * @public varchar
	 */
	public $title = null;
	/**
	 * @public varchar
	 */
	public $alias = null;
	/**
	 * @public tinyint
	 */
	public $published = null;
	/**
	 * @public tinyint unsigned
	 */
	public $access = null;
	/**
	 * @public varchar
	 */
	public $description = null;
	/**
	 * @public varchar
	 */
	public $code = null;
	/**
	 * @public varchar
	 */
	public $chart_title = null;
	/**
	 * @public varchar
	 */
	public $chart_version = null;
	/**
	 * @public int
	 */
	public $chart_width = null;
	/**
	 * @public int
	 */
	public $chart_height = null;
	/**
	 * @public int
	 */
	public $created_user_id = null;
	/**
	 * @public timestamp
	 */
	public $created_time = null;
	/**
	 * @public int
	 */
	public $modified_user_id = null;
	/**
	 * @public timestamp
	 */
	public $modified_time = null;
	/**
	 * @public int
	 */
	public $checked_out = null;
	/**
	 * @public timestamp
	 */
	public $checked_out_time = null;
	/**
	 * @public int unsigned
	 */
	public $category_id = null;
	/**
	 * @public varchar
	 */
	public $metadata = null;
	/**
	 * @public varchar
	 */
	public $metakey = null;

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

	/**
	 * Check the internal properties of the table class.
	 *
	 * @return	boolean
	 */
	public 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
	 */
	public function store($updateNulls=false)
	{
		$app	= &JFactory::getApplication();
		$user	= &JFactory::getUser();
		$now	= &JFactory::getDate();

		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);
	}
}