<?php
/**
 * NoNumber! Controller
 *
 * @package     NoNumber! Extension Manager
 * @version     1.4.0
 *
 * @author      Peter van Westen <peter@nonumber.nl>
 * @link        http://www.nonumber.nl
 * @copyright   Copyright (C) 2010 NoNumber! All Rights Reserved
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// Import CONTROLLER object class
jimport( 'joomla.application.component.controller' );

/**
 * NoNumber! Licenses Controller
 */
class NoNumberManagerController extends JController
{
	/**
	 * admin_url - url of the main component administrator
	 *
	 * @var string
	 */
	var $_admin_url = null;

	/**
	 * Custom Constructor
	 */
	function __construct( $default = array() )
	{
		parent::__construct( $default );

		// initialize class property
		$this->_admin_url = 'index.php?option=com_nonumbermanager';
	}

	/**
	 * Display Method
	 * Call the method and display the requested view
	 */
	function display( $cachable=false )
	{
		$document =& JFactory::getDocument();

		$view =& $this->getView( 'default', $document->getType() );

		// Get/Create the model
		$model =& $this->getModel( 'model' );
		if ( $model ) {
			// Push the model into the view ( as default )
			$view->setModel( $model, true );
		}

		$view->setLayout( 'default' );
		$view->display();
	}

	/**
	 * Save Method
	 * Save the selected item specified by id
	 * and set Redirection to the list of items
	 */
	function save()
	{
		$extension = JRequest::getVar( 'extension', '' );
		$code = JRequest::getVar( 'code', '' );

		if ( $extension ) {
			$db =& JFactory::getDBO();
			$sql = "REPLACE INTO #__nonumber_licenses
				( `extension`, `code` )
				VALUES ( ".$db->quote($extension).", ".$db->quote($code)." );
			";
			$db->setQuery( $sql );
			$db->query();
			$msg = JText::_( 'Code saved' );
		} else {
			$msg = JText::_( 'Failed to save code' );
		}

		echo $msg;
		exit;
	}
}