<?php
/**
* @Copyright Copyright (C) 2012- xml/swf
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

/**
 * Gallery Model for Gallery XML Component
 */
 
// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.model' );
 
/**
 * Gallery Model
 */
class GalleryModelGallery extends JModel
{
    /**
     * Gallery data array
     *
     * @var array
     */
    var $_data;

	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery($cat_id = 0)
	{
		$where_q = ($cat_id == 0) ? '' : ' WHERE #__gallerycatpro.catid = ' . $cat_id;
		$query = ' SELECT #__gallerycatpro.*, #__gallerycatproc.name as catname '
			   . ' FROM #__gallerycatpro '
			   . ' LEFT JOIN #__gallerycatproc'
				. '	ON #__gallerycatpro.catid = #__gallerycatproc.id '
				. $where_q
			   . ' ORDER BY #__gallerycatproc.ordnum, #__gallerycatpro.ordnum'
		;
		return $query;
	}
	/**
	 * Retrieves the Gallery data
	 * @return array Array of objects containing the data from the database
	 */
	function getData($cat_id = 0)
	{
		$mainframe = JFactory::getApplication();
		global $option;
	
		// Lets load the data if it doesn't already exist
		if (empty( $this->_data ))
		{
			$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
			$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
			$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);


			$where_q = ($cat_id == 0) ? '' : ' WHERE #__gallerycatpro.catid = ' . $cat_id;
			$query = ' SELECT #__gallerycatpro.*, #__gallerycatproc.name as catname '
				   . ' FROM #__gallerycatpro '
				   . ' LEFT JOIN #__gallerycatproc'
					. '	ON #__gallerycatpro.catid = #__gallerycatproc.id '
					. $where_q
				   . ' ORDER BY #__gallerycatproc.ordnum, #__gallerycatpro.ordnum'
			;
			$items = $this->_getList($query, $limitstart, $limit);
			 
			$total = $this->_getListCount($query);
			jimport('joomla.html.pagination');
			$pagination = new JPagination($total, $limitstart, $limit);
			
			$this->_data = array('items'=>$items, 'pagination'=>$pagination);
		}
		return $this->_data;
	}
	function &getCategories()
	{
		$query = 'SELECT id, ordnum, publish, name FROM #__gallerycatproc ORDER BY ordnum';
		$categories = $this->_getList( $query );
		return 	$categories;
	}
}
