<?php

/**
 * <pre>
 * Invision Power Services
 * IP.CCS database template help
 * Last Updated: $Date: 2009-09-16 21:05:09 -0400 (Wed, 16 Sep 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		25th Sept 2009
 * @version		$Revision: 85 $
 */

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

class admin_ccs_templates_help 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;
	
	/**
	 * HTML library
	 *
	 * @access	public
	 * @var		object
	 */
	public $html;

	/**
	 * 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_templates' );
		
		//-----------------------------------------
		// Set up stuff
		//-----------------------------------------
		
		$this->form_code	= $this->html->form_code	= 'module=templates&amp;section=help';
		$this->form_code_js	= $this->html->form_code_js	= 'module=templates&section=help';

		//-----------------------------------------
		// 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() );
		
		//-----------------------------------------
		// What to do?
		//-----------------------------------------
		
		switch( $this->request['do'] )
		{
			case 'list':
			default:
				$this->showDatabases();
			break;			

			case 'tags':
				$this->_showTags();
			break;
		}
		
		//-----------------------------------------
		// Pass to CP output hander
		//-----------------------------------------
		
		$this->registry->getClass('output')->printPopupWindow();
	}
	
	/**
	 * Show the tags available
	 *
	 * @access	protected
	 * @param	array 		Database info (already pulled elsewhere)
	 * @return	void
	 */
	protected function _showTags( $database=array() )
	{
		//-----------------------------------------
		// Get database
		//-----------------------------------------
		
		if( !count($database) )
		{
			$database	= $this->DB->buildAndFetch( array( 'select' => '*', 'from' => 'ccs_databases', 'where' => 'database_id=' . intval($this->request['id']) ) );
		}

		if( !$database['database_id'] )
		{
			$this->registry->output->error( $this->lang->words['dbhelp_no_db'] );
		}
		
		$this->registry->ccsFunctions->fixStrings( $database['database_id'] );
		
		//-----------------------------------------
		// Get fields
		//-----------------------------------------
		
		$fields	= array();

		$this->DB->build( array( 'select' => '*', 'from' => 'ccs_database_fields', 'where' => 'field_database_id=' . $database['database_id'], 'order' => 'field_position ASC' ) );
		$this->DB->execute();
		
		while( $r = $this->DB->fetch() )
		{
			$fields[]	= $r;
		}
		
		//-----------------------------------------
		// Output
		//-----------------------------------------
		
		$this->registry->output->html .= $this->html->listDatabaseTags( $database, $fields );
	}

	/**
	 * List the databases
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function showDatabases()
	{
		//-----------------------------------------
		// If it's articles, just show help
		//-----------------------------------------
		
		if( $this->request['type'] > 3 )
		{
			$database	= $this->DB->buildAndFetch( array( 'select' => '*', 'from' => 'ccs_databases', 'where' => 'database_is_articles=1' ) );
			
			return $this->_showTags( $database );
		}
		
		//-----------------------------------------
		// Get our databases
		//-----------------------------------------
		
		$databases	= array();

		$this->DB->build( array( 'select' => '*', 'from' => 'ccs_databases', 'where' => 'database_is_articles=0', 'order' => 'database_name ASC' ) );
		$this->DB->execute();
		
		while( $r = $this->DB->fetch() )
		{
			$databases[]	= $r;
		}

		//-----------------------------------------
		// Output
		//-----------------------------------------
		
		$this->registry->output->html .= $this->html->listDatabases( $databases );
	}
}
