<?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;

jximport('jxtended.application.component.modelitem');

/**
 * About Page Model
 *
 * @package		TAOJ.Quanta
 * @subpackage	com_quanta
 */
class QuantaModelChart extends JxModelItem
{
	/**
	 * Returns a reference to the a Table object, always creating it
	 *
	 * @param	type 	$type 	 The table type to instantiate
	 * @param	string 	$prefix	 A prefix for the table class name. Optional.
	 * @param	array	$options Configuration array for model. Optional.
	 * @return	JTable	A database object
	*/
	public function &getTable($type = 'Chart', $prefix = 'QuantaTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Auto-populate the model state.
	 *
	 * @return	void
	 */
	public function _populateState()
	{
		$app = &JFactory::getApplication('administrator');

		// Load the User state.
		if (!($pk = (int) $app->getUserState('com_quanta.edit.chart.id'))) {
			$pk = (int) JRequest::getInt('chart_id');
		}
		$this->setState('chart.id', $pk);

		// Load the parameters.
		$params = JComponentHelper::getParams('com_quanta');
		$this->setState('params', $params);
	}

	/**
	 * Method to get a row.
	 *
	 * @param	integer	An optional id of the object to get, otherwise the id from the model state is used.
	 *
	 * @return	mixed	Object on success, false on failure.
	 */
	public function &getItem($pk = null)
	{
		// Initialize variables.
		$pk = (!empty($pk)) ? $pk : (int)$this->getState('chart.id');

		// Get a level row instance.
		$table = &$this->getTable();

		// Attempt to load the row.
		$table->load($pk);

		// Check for a table object error.
		if ($error = $table->getError())
		{
			$this->setError($error);
			$false = false;
			return $false;
		}

		// Convert the params field to an array.
		//$registry = new JRegistry();
		//$registry->loadJSON($table->params);
		//$table->params = $registry->toArray();

		// Convert the JTable to a clean JObject.
		$result = JArrayHelper::toObject($table->getProperties(1), 'JObject');

		return $result;
	}

	/**
	 * Method to get the form object.
	 *
	 * @param	string	$type	The type of form to load (view, model);
	 * @return	mixed	JXForm object on success, false on failure.
	 */
	public function &getForm($type = 'view')
	{
		jximport('jxtended.form.helper');
		JXFormHelper::addIncludePath(JPATH_COMPONENT.DS.'models');

		if ($type == 'model') {
			$result = &JXFormHelper::getModel($this->getName());
		}
		else {
			$result = &JXFormHelper::getView($this->getName());
		}

		if (JError::isError($result)) {
			$this->setError($result->message);
			return false;
		}

		return $result;
	}

	/**
	 * Method to checkin a row.
	 *
	 * @param	integer	$pk		The numeric id of a row
	 * @return	boolean	True on success/false on failure
	 */
	public function checkin($pk = null)
	{
		// Initialize variables.
		$pk	= (!empty($pk)) ? $pk : (int) $this->getState('chart.id');

		if ($pk)
		{
			$user	= &JFactory::getUser();

			// Get an instance of the row to checkin.
			$table = &$this->getTable();
			if (!$table->load($pk)) {
				$this->setError($table->getError());
				return false;
			}

			// Check if this is the user having previously checked out the row.
			if ($table->checked_out > 0 && $table->checked_out != $user->get('id')) {
				$this->setError(JText::_('JError_Checkin_user_mismatch'));
				return false;
			}

			// Attempt to check the row in.
			if (!$table->checkin($pk)) {
				$this->setError($table->getError());
				return false;
			}
		}

		return true;
	}

	/**
	 * Method to check-out a row for editing.
	 *
	 * @param	int		$pk	The numeric id of the object to check-out.
	 *
	 * @return	bool	False on failure or error, success otherwise.
	 */
	public function checkout($pk)
	{
		// Initialize variables.
		$pk = (!empty($pk)) ? $pk : (int) $this->getState('chart.id');

		// Only attempt to check the row in if it exists.
		if ($pk)
		{
			// Get a row instance.
			$table = &$this->getTable();

			// Get the current user object.
			$user = &JFactory::getUser();

			// Attempt to check the row out.
			if (!$table->checkout($user->get('id'), $pk)) {
				$this->setError($table->getError());
				return false;
			}
		}

		return true;
	}

	/**
	 * Method to save the form data.
	 *
	 * @access	public
	 * @param	array	The form data.
	 * @return	boolean	True on success.
	 */
	public function save($data)
	{
		$pk		= (!empty($data['id'])) ? $data['id'] : (int) $this->getState('chart.id');
		$isNew	= true;

		// Get a group row instance.
		$table = &$this->getTable();

		// Load the row if saving an existing item.
		if ($pk > 0) {
			$table->load($pk);
			$isNew = false;
		}

		// Bind the data.
		if (!$table->bind($data)) {
			$this->setError(JText::sprintf('JTable_Error_Bind_failed', $table->getError()));
			return false;
		}

		// Check the data.
		if (!$table->check()) {
			$this->setError($table->getError());
			return false;
		}

		// Store the data.
		if (!$table->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		$this->setState('chart.id', $table->id);

		return true;
	}

	/**
	 * Method to delete groups.
	 *
	 * @access	public
	 * @param	array	An array of group ids.
	 * @return	boolean	Returns true on success, false on failure.
	 */
	public function delete($pks)
	{
		$pks = (array) $pks;

		// Get a row instance.
		$table = &$this->getTable();

		// Iterate the items to delete each one.
		foreach ($pks as $pk)
		{
			if (!$table->delete((int) $pk))
			{
				$this->setError($table->getError());
				return false;
			}
		}

		return true;
	}

	/**
	 * Method to change the published state of an item
	 *
	 * @param	array	$pks	The IDs of the taxonomy rows to publish.
	 * @param	int		$value	The value to set
	 * @return	mixed	True on success or JExeception object on failure
	 */
	public function publish($pks, $value = 1)
	{
		$pks = (array) $pks;

		// Get the current user object.
		$user = &JFactory::getUser();

		// Get an instance of the table row.
		$table = &$this->getTable();

		// Attempt to publish the items.
		if (!$table->publish($pks, $value, $user->get('id')))
		{
			$this->setError($table->getError());
			return false;
		}

		return true;
	}
}