<?php
/**
* @version		3.0.0
* @package		JomLand TicketCenter
* @copyright	Copyright (C) 2010 JomLand.com. All rights reserved.
* @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


jimport('joomla.application.component.model');
jimport('joomla.application.categories');

/**
 * This models supports retrieving lists of contact categories.
 *
 * @package		Joomla.Site
 * @subpackage	com_contact
 * @since		1.6
 */


class JLTicketCenterModelCategory extends JModelLegacy
{
	/**
	 * Model context string.
	 *
	 * @var		string
	 */
	public $_context = 'com_contact.categories';

	/**
	 * The category context (allows other extensions to derived from this model).
	 *
	 * @var		string
	 */
	protected $_extension = 'com_contact';

	private $_parent = null;

	private $_items = null;

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState()
	{
		$app = JFactory::getApplication();
		$this->setState('filter.extension', $this->_extension);

		// Get the parent id if defined.
		$parentId = JRequest::getInt('id');
		$this->setState('filter.parentId', $parentId);

		$params = $app->getParams();
		$this->setState('params', $params);

		$this->setState('filter.published',	1);
		$this->setState('filter.access',	true);
	}

	/**
	 * Method to get a store id based on model configuration state.
	 *
	 * This is necessary because the model is used by the component and
	 * different modules that might need different sets of data or different
	 * ordering requirements.
	 *
	 * @param	string		$id	A prefix for the store id.
	 *
	 * @return	string		A store id.
	 */
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.extension');
		$id	.= ':'.$this->getState('filter.published');
		$id	.= ':'.$this->getState('filter.access');
		$id	.= ':'.$this->getState('filter.parentId');

		return parent::getStoreId($id);
	}

	/**
	 * redefine the function an add some properties to make the styling more easy
	 *
	 * @return mixed An array of data items on success, false on failure.
	 */
	public function getItems()
	{
		
		if(!count($this->_items))
		{
			$app = JFactory::getApplication();
			$menu = $app->getMenu();
			$active = $menu->getActive();
			$params = new JRegistry();
			if($active)
			{
				$params->loadString($active->params);
			}
			$options = array();
			$options['countItems'] = $params->get('show_cat_items_cat', 1) || !$params->get('show_empty_categories_cat', 0);
			$categories = JCategories::getInstance('JLTicketCenter', $options);
			
			$this->_parent = $categories->get($this->getState('filter.parentId', 'root'));
			
			if(is_object($this->_parent))
			{
				$this->_items = $this->_parent->getChildren();
			} else {
				$this->_items = false;
			}
		}
		
		return $this->_items;
	}

	public function getParent()
	{
		if(!is_object($this->_parent))
		{
			$this->getItems();
		}
		return $this->_parent;
	}
	
	function getCategory(){
		$db = JFactory::getDBO();
		
		$cat_id = JRequest::getInt('id',0);

		$query = "SELECT * FROM #__categories 
				WHERE `extension`='com_jlticketcenter'
				AND `id`=".$cat_id." 
				AND `published`=1";
		
  		$db->setQuery($query);
		$item  =$db->loadObject();
		
		return $item  ;
	}//EOF getCategory
	
	function getCategoryPerformers(){
		$db = JFactory::getDBO();
		
		$cat_id = JRequest::getInt('id',0);
		
		$query = "SELECT * FROM #__jltcenter_performer 
				WHERE `cat_id`=".$cat_id." 
				AND `published`=1
				ORDER BY `performer_name`";
		
  		$db->setQuery($query);
		$items  =$db->loadObjectList();
		
		return $items;
	}//EOF getCategoryPerformers
}



/*

class JLTicketCenterModelCategory extends JModelLegacy
{
	function getCategories(){
		
		if (!JLoader::import('components.com_content.models.category',JPATH_BASE)){
			return false; 
		}
		
		$model = JModelLegacy::getInstance('Category', 'ContentModel');

		var_dump($model );
		//$items = $model->getCategories();
		$items = $model->getCategory();
		
		
		echo "getCategories";
		die;
		$db =& JFactory::getDBO();
		
		$query = "SELECT * FROM #__categories 
				WHERE `extension`='com_jlticketcenter' 
				AND `published`=1 
				ORDER BY `title`";
		
  		$db->setQuery($query);
		$items  =$db->loadObjectList();
		
		return $items;
	}//EOF getCategories
	
	function getCategory(){
		$db =& JFactory::getDBO();
		
		$cat_id = JRequest::getInt('id',0);
		
		$query = "SELECT * FROM #__categories 
				WHERE `extension`='com_jlticketcenter'
				AND `id`=".$cat_id." 
				AND `published`=1";
		
  		$db->setQuery($query);
		$item  =$db->loadObject();
		
		return $item  ;
	}//EOF getCategory
	
	function getCategoryPerformers(){
		$db =& JFactory::getDBO();
		
		$cat_id = JRequest::getInt('id',0);
		
		$query = "SELECT * FROM #__jltcenter_performer 
				WHERE `cat_id`=".$cat_id." 
				AND `published`=1
				ORDER BY `performer_name`";
		
  		$db->setQuery($query);
		$items  =$db->loadObjectList();
		
		return $items;
	}//EOF getCategoryPerformers
}//EOC
*/