<?php
/**
 * jeGroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2010 - 2011 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class jeGroupBuyModelproducts extends JModel
{
	function __construct()
	{
		parent::__construct();
		$array = JRequest::getVar('cid',  0, '', 'array');
		$this->setId((int)$array[0]);
    }

    function setId($id)
	{
		// Set id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}

    function &getData()
	{
		$row =& $this->getTable();
		$row->load( $this->_id );
		return $row;
	}


 	function store($data)
	{
		$row =& $this->getTable();

		// Bind the form fields to the web link table
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Create the timestamp for the date
	//	$row->date = gmdate('Y-m-d H:i:s');

		// if new item, order last in appropriate group
		if (!$row->id) {
			$where = 'catid = ' . (int) $row->catid ;
			$row->ordering = $row->getNextOrder( $where );
		}

		// Make sure the web link table is valid
		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return $row->id;
	}


	function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$db    = & JFactory::getDBO();
		$cids  = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$row   = & $this->getTable();

		JArrayHelper::toInteger( $cids );
		//$cids1 = implode( ',', $cids );

		if (count( $cids ))
		{
			foreach($cids as $cid)
			{
				if (!$row->delete( $cid ))
				{
					return false;
				}
			}
		}
		return true;
	}


	function move($direction)
	{
		$row 	= & $this->getTable();

		$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );

		if (isset( $cid[0] ))
		{
			$row->load( (int) $cid[0] );
			$row->move($direction);

			$cache = & JFactory::getCache('com_jegroupbuy');
			$cache->clean();
		}
		return true;
	}

	function saveorder($cid = array(), $order)
	{
		$row =& $this->getTable();
		$groupings = array();

		// update ordering values
		for( $i=0; $i < count($cid); $i++ )
		{
			$row->load( (int) $cid[$i] );
			// track categories
			$groupings[] = $row->catid;

			if ($row->ordering != $order[$i])
			{
				$row->ordering = $order[$i];
				if (!$row->store()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}
			}
		}

		// execute updateOrder for each parent group
		$groupings = array_unique( $groupings );
		foreach ($groupings as $group){
			$row->reorder('catid  = '.(int) $group);
		}

		return true;
	}

	function getCategoryName()
	{
		$db 	   = & JFactory::getDBO();
		$query 	   = "SELECT * FROM #__jegroupbuy_category where published  = '1' ";
		$db->setQuery( $query );
		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$rows      = $db->loadObjectList();
		$cat[] 	   = JHTML::_('select.option', '0', JText::_('SELECT_CATEGORY') );
		foreach ( $rows as $row ) {
			$cat[] = JHTML::_('select.option', $row->id, $row->name );
		}
		return $cat;
	}

	function getShopName()
	{
		$db 	   = & JFactory::getDBO();
		$query 	   = "SELECT * FROM #__jegroupbuy_stores where published = '1' ";
		$db->setQuery( $query );
		if (!$db->query())	{
			echo $db->getErrorMsg();
		}
		$rows      = $db->loadObjectList();
		$cat[] 	   = JHTML::_('select.option', '0', JText::_('SELECT_SHOP') );
		foreach ( $rows as $row ) {
			$cat[] = JHTML::_('select.option', $row->id, $row->shop_name );
		}
		return $cat;
	}

}
?>