<?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.controller' );
class jeGroupBuyControllerstores extends JController
{
    function __construct()
    {
		//Get View
        if(JRequest::getCmd('view') == '')
        {
            JRequest::setVar('view', 'stores');
		}
        parent::__construct();
        $this->registerTask( 'add',	        'edit' );
		$this->registerTask( 'unpublish',	'publish' );
		$this->registerTask( 'unfeatured',	'featured' );
		$this->registerTask( 'apply',       'save' );
    }

    function edit()
	{
		JRequest::setVar( 'view', 'stores');
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);
		parent::display();
	}

	function save()
	{
		global $mainframe;
		$db = JFactory::getDBO();
		$model = $this->getModel('stores');
        $post	= JRequest::get( 'post',JREQUEST_ALLOWRAW );

        if ($id=$model->store($post))
        {
        	$msg = JText::_( 'JEGROUPBUY_STORES_SAVED_SUCC' );
        }
        else
        {
        	$msg = JText::_( 'JEGROUPBUY_STORES_SAVED_UNSUCC' );
        }
        if($this->_task == 'apply')
        {
        	$link 	= 'index.php?option=com_jegroupbuy&controller=stores&task=edit&cid[]='. $id;
        }
        else
        {
        	$link = 'index.php?option=com_jegroupbuy&controller=stores';
        }
        $this->setRedirect( $link ,$msg);
	}

	function remove()
	{
		$model = $this->getModel('stores');

		if($model->remove()) {
			$msg = JText::_( 'JEGROUPBUY_STORES_DELETE_SUCCESS' );
		} else {
			return JError::raiseWarning( 500, JText::_( 'JEGROUPBUY_STORES_DELETE_FAILURE' ));
		}

		$this->setRedirect( 'index.php?option=com_jegroupbuy&controller=stores', $msg );
	}

	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db			=& JFactory::getDBO();
		$cid		= JRequest::getVar( 'cid', '', 'post', 'array' );
		$task		= JRequest::getCmd( 'task' );
		$publish = '';
		if($task == 'unpublish')
		{
			$value = "0";
			$msg = JText::_( 'JEGROUPBUY_STORES_UNPUBLISH_SUCC' );
		}else if($task == 'publish')
		{
			$value = "1";
			$msg = JText::_( 'JEGROUPBUY_STORES_PUBLISH_SUCC' );
		}

		if (empty( $cid )) {
			return 0;
		}

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

		$query = 'UPDATE #__jegroupbuy_stores'
				. ' SET published = "' . $value. '"'
				. ' WHERE id IN ( '. $cids.'  )'
				;
		//echo $query;exit;
		$db->setQuery( $query );
		$db->query();

		$link = 'index.php?option=com_jegroupbuy&controller=stores';
		$this->setRedirect( $link ,$msg);

	}

}

class JHTMLInput
{
	function checkbox($name,$value,$checked='checked')
	{
		$html = "<input type=\"checkbox\" name=\"" . $name . "\" value=\"" . $value . "\"  id=\"" . $name . "\"  onClick=\"DoTheCheck('$name')\" $checked	/>";
		return $html;
	}
}

?>