<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
	defined('_JEXEC') or die('Restricted access');

// Import Joomla predefined functions
	jimport( 'joomla.application.component.view' );

class jegroupbuyViewCategory  extends JView
{
	protected $state;
	protected $items;
	protected $total;

	function display( $tpl = null )
	{
		$db = JFactory::getDBO();
		$query		= $db->getQuery(true);
		$app		= JFactory::getApplication();
		$params 	= $app->getParams();
		$user		= JFactory::getUser();
		$Itemid 	= JRequest::getInt("Itemid","1");
	 	$cid 		= JRequest::getInt("catid","");

		// Get some data from the models
		$state		= $this->get('State');
		$items		= $this->get('Items');
			$pagination	= $this->get('Pagination');
		$total		= $this->get('total');

		$query->select('layout');
		$query->from('#__jegroupbuy_settings');
		$query->where('id = 1');
		$db->setQuery($query);
		$layout			= $db->loadResult();

		if($layout == "")
		{
			$layout = "default";
		}
		$this->_layout = $layout;


		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		$this->assignRef('items', 		$items);
		$this->assignRef('total',		$total);
		$this->assignRef('pagination',	$pagination);
		$this->assignRef('params',		$params);
		$this->assignRef('Itemid',		$Itemid);
		$this->assignRef('cid',			 $cid);

		$this->_prepareDocument();

		parent::display( $tpl );
	}

	/**
	 * Prepares the document
	 */
	protected function _prepareDocument()
	{
		$app		= JFactory::getApplication();
		$menus		= $app->getMenu();
		$pathway	= $app->getPathway();
		$title		= null;
		$params 	= $app->getParams();
		$menu		= $menus->getActive();

		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object($menu)) {
			$menu_params = new JRegistry;
			$menu_params->loadJSON($menu->params);
			if (!$menu_params->get('page_title')) {
				$params->set('page_title',	JText::_('COM_JEGROUPBUY_TITLE_DEAL_PRODUCT_LIST'));
			}
		}
		else {
			$params->set('page_title',	JText::_('COM_JEGROUPBUY_TITLE_DEAL_PRODUCT_LIST'));
		}

		$title = $params->get('page_title');
		if ($app->getCfg('sitename_pagetitles', 0)) {
			$title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $title);
		}

		$this->document->setTitle($title);
	}
	function limit_characters ( $str, $n )
	{
		if ( strlen ( $str ) <= $n )
		{
			return $str;
		}
		else {
			return substr ( $str, 0, $n ) . '...';
		}
	}
}
