<?php

/**
 * <pre>
 * Invision Power Services
 * IP.CCS article configuration
 * Last Updated: $Date: 2009-08-23 21:54:28 -0400 (Sun, 23 Aug 2009) $
 * </pre>
 *
 * @author 		$Author: bfarber $
 * @copyright	(c) 2001 - 2009 Invision Power Services, Inc.
 * @license		http://www.invisionpower.com/community/board/license.html
 * @package		IP.Content
 * @link		http://www.invisionpower.com
 * @since		2nd February 2010
 * @version		$Revision: 67 $
 */

if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly.";
	exit();
}

class admin_ccs_articles_configure extends ipsCommand
{
	/**
	 * Shortcut for url
	 *
	 * @access	private
	 * @var		string			URL shortcut
	 */
	private $form_code;
	
	/**
	 * Shortcut for url (javascript)
	 *
	 * @access	private
	 * @var		string			JS URL shortcut
	 */
	private $form_code_js;
	
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */	
	private $html;
	
	/**
	 * Main database handler
	 *
	 * @access	private
	 * @var		object			Database action file
	 */
	private $databaseHandler;
	
	/**
	 * Database info
	 *
	 * @access	public
	 * @var		array 			DB info
	 */	
	public $database			= array();

	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry ) 
	{
		//-----------------------------------------
		// Load HTML
		//-----------------------------------------
		
		$this->html = $this->registry->output->loadTemplate( 'cp_skin_articles' );
		
		//-----------------------------------------
		// Set up stuff
		//-----------------------------------------
		
		$this->form_code	= $this->html->form_code	= 'module=articles&amp;section=configure';
		$this->form_code_js	= $this->html->form_code_js	= 'module=articles&section=configure';

		//-----------------------------------------
		// Load Language
		//-----------------------------------------
		
		ipsRegistry::getClass('class_localization')->loadLanguageFile( array( 'admin_lang' ) );

		//-----------------------------------------
		// Grab extra CSS
		//-----------------------------------------
		
		$this->registry->output->addToDocumentHead( 'importcss', $this->settings['skin_app_url'] . 'css/ccs.css' );

		$_global = $this->registry->output->loadTemplate( 'cp_skin_ccsglobal' );
		
		$this->registry->output->addToDocumentHead( 'inlinecss', $_global->getCss() );
		
		//-----------------------------------------
		// Get database data
		//-----------------------------------------
		
		$this->database	= $this->DB->buildAndFetch( array( 'select' => '*', 'from' => 'ccs_databases', 'where' => 'database_is_articles=1' ) );
		
		if( !$this->database['database_id'] )
		{
			$this->registry->output->showError( $this->lang->words['no_db_id_cats'], '11CCS7' );
		}
		
		$this->request['id']	= $this->database['database_id'];
		
		//-----------------------------------------
		// Get main database handler
		//-----------------------------------------
		
		require_once( IPSLib::getAppDir('ccs') . '/modules_admin/databases/databases.php' );
		$this->databaseHandler	= new admin_ccs_databases_databases( $this->registry );
		$this->databaseHandler->makeRegistryShortcuts( $this->registry );
		
		$this->databaseHandler->form_code		= $this->form_code;
		$this->databaseHandler->form_code_js	= $this->form_code_js;
		$this->databaseHandler->html			= $this->html;
		$this->databaseHandler->containerType	= 'arttemplate';

		//-----------------------------------------
		// Fix language strings
		//-----------------------------------------
		
		$this->registry->ccsFunctions->fixStrings( $this->database['database_id'] );
				
		//-----------------------------------------
		// What to do?
		//-----------------------------------------
		
		switch( $this->request['do'] )
		{
			default:
			case 'home':
			case 'add':
			case 'edit':
			case 'doAdd':
			case 'delete':
				$this->_databaseForm();
			break;

			case 'doEdit':
				$this->_databaseSave();
			break;
		}
		
		//-----------------------------------------
		// Pass to CP output hander
		//-----------------------------------------
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}

	/**
	 * Save a new database/edited database
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function _databaseSave()
	{
		$this->databaseHandler->_databaseSave( 'edit' );
	}
	
	/**
	 * Form to add/edit a database
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function _databaseForm()
	{
		$this->databaseHandler->_databaseForm( 'edit' );
	}
}
