<?php

/**
 * <pre>
 * Invision Power Services
 * IP.CCS article front page management
 * 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		19th January 2010
 * @version		$Revision: 67 $
 */

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

class admin_ccs_articles_frontpage 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;

	/**
	 * Database info
	 *
	 * @access	public
	 * @var		array 			DB info
	 */	
	public $database			= array();
	
	/**
	 * Fields info
	 *
	 * @access	public
	 * @var		array 			Field info
	 */	
	public $fields				= array();
	
	/**
	 * Special mapped fields
	 *
	 * @access	public
	 * @var		array 			Fields
	 */
	public $specialFields		= array();
	
	/**
	 * Category lib
	 *
	 * @access	public
	 * @var		object
	 */
	public $categories;

	/**
	 * 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=frontpage';
		$this->form_code_js	= $this->html->form_code_js	= 'module=articles&section=frontpage';

		//-----------------------------------------
		// 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
		//-----------------------------------------
		
		$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_fp'], '11CCS10' );
		}

		//-----------------------------------------
		// And fields
		//-----------------------------------------
		
		$this->fields	= array();

		if( is_array($this->caches['ccs_fields'][ $this->database['database_id'] ]) AND count($this->caches['ccs_fields'][ $this->database['database_id'] ]) )
		{
			foreach( $this->caches['ccs_fields'][ $this->database['database_id'] ] as $_field )
			{
				$this->fields[ $_field['field_id'] ]	= $_field;
				
				$this->specialFields[ $_field['field_key'] ]	= $_field;
			}
		}
		
		//-----------------------------------------
		// And categories
		//-----------------------------------------
		
		require_once( IPSLib::getAppDir( 'ccs' ) . '/sources/databases/categories.php' );
		$this->categories	= new ccs_categories( $this->registry );
		$this->categories->hide	= false;
		$this->categories->init( $this->database );
		
		//-----------------------------------------
		// Fix language strings
		//-----------------------------------------
		
		$this->registry->ccsFunctions->fixStrings( $this->database['database_id'] );
		
		//-----------------------------------------
		// What to do?
		//-----------------------------------------
		
		switch( $this->request['do'] )
		{
			case 'frontpage':
			default:
				$this->_frontPage();
			break;
			
			case 'config':
				$this->_updateConfig();
			break;
			
			case 'remove':
				$this->_removeArticles( array( $this->request['article'] ) );
			break;
			
			case 'articles':
				$this->_removeArticles( $this->request['articles'] );
			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();
	}
	
	/**
	 * Update frontpage configuration
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function _updateConfig()
	{
		//-----------------------------------------
		// Get cache settings
		//-----------------------------------------
		
		$cache	= $this->cache->getCache('ccs_frontpage');
		
		if( !count($cache) )
		{
			$cache	= array(
							'categories'		=> '*',
							'limit'				=> 10,
							'sort'				=> 'record_updated',
							'order'				=> 'desc',
							'pinned'			=> 0,
							'template'			=> 0,
							);
		}
		
		if( $this->request['config_categories_all'] )
		{
			$cache['categories']	= '*';
		}
		else
		{
			$cache['categories']	= is_array($this->request['config_categories']) ? implode( ',', $this->request['config_categories'] ) : array();
		}
		
		$cache['limit']		= intval($this->request['config_limit']);
		$cache['sort']		= $this->request['config_sort'];
		$cache['order']		= $this->request['config_order'];
		$cache['pinned']	= intval($this->request['config_pinned']);
		$cache['template']	= intval($this->request['config_template']);
		
		$this->cache->setCache( 'ccs_frontpage', $cache, array( 'array' => 1 ) );
		
		$this->registry->output->global_message	= $this->lang->words['fp_settings_updated'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url'] . $this->form_code );
	}
	
	/**
	 * Remove articles from front page
	 *
	 * @access	protected
	 * @param	array 		Article ids to remove from front page
	 * @return	void
	 */
	protected function _removeArticles( $articles )
	{
		$articles	= IPSLib::cleanIntArray( $articles );
		
		if( !is_array($articles) OR !count($articles) )
		{
			//-----------------------------------------
			// Throw error
			//-----------------------------------------
			
			$this->registry->output->showError( $this->lang->words['no_arts_remove_fp'], '11CCS11' );
		}
		
		$update	= array(
						'field_' . $this->specialFields['article_homepage']['field_id'] => 0,
						);

		$this->DB->update( $this->database['database_database'], $update, 'primary_id_field IN(' . implode( ',', $articles ) . ')' );

		$this->registry->output->global_message	= $this->lang->words['fp_articles_removed'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url'] . $this->form_code );
	}
	
	/**
	 * Front page manager
	 * 
	 * @access protected
	 * @return void
	 */
	protected function _frontPage()
	{
		//-----------------------------------------
		// Get cache settings
		//-----------------------------------------
		
		$cache	= $this->cache->getCache('ccs_frontpage');
		
		if( !count($cache) )
		{
			$cache	= array(
							'categories'		=> '*',
							'limit'				=> 10,
							'sort'				=> 'record_updated',
							'order'				=> 'desc',
							'pinned'			=> 0,
							'template'			=> 0,
							);
		}
		
		//-----------------------------------------
		// Get possible field types
		//-----------------------------------------
		
		require_once( IPSLib::getAppDir( 'ccs' ) . '/sources/databases/fields.php' );
		$fieldsClass	= new ccs_database_fields( $this->registry );
		$fieldsClass->getHandlers();
		
		//-----------------------------------------
		// And get flagged articles
		// Note: checkbox fields are arrays natively,
		//	so we have to check for =',1,' instead of =1
		//-----------------------------------------
		
		$articles	= array();
		
		$this->DB->build( array( 'select' => '*', 'from' => $this->database['database_database'], 'where' => $this->specialFields['article_homepage']['field_id'] ? "field_{$this->specialFields['article_homepage']['field_id']}=',1,'" : '', 'order' => $cache['sort'] . ' ' . $cache['order'], 'limit' => array( 100 ) ) );
		$this->DB->execute();
		
		while( $r = $this->DB->fetch() )
		{
			$r['title']		= $fieldsClass->getFieldValue( $this->specialFields['article_title'], $r, $this->specialFields['article_title']['field_truncate'] );
			$r['body']		= $fieldsClass->getFieldValue( $this->specialFields['article_body'], $r, $this->specialFields['article_body']['field_truncate'] );
			$r['date']		= $fieldsClass->getFieldValue( $this->specialFields['article_date'], $r, $this->specialFields['article_date']['field_truncate'] );
			$r['category']	= $this->categories->categories[ $r['category_id'] ]['category_name'];
			
			$articles[]	= $r;
		}

		//-----------------------------------------
		// Get categories too
		//-----------------------------------------
		
		$this->request['category']	= $cache['categories'] != '*' ? explode( ',', $cache['categories'] ) : array();
		
		$categories	= $this->categories->getSelectMenu();
		
		//-----------------------------------------
		// Fields
		//-----------------------------------------
		
		$fields		= array();
		$fields[]	= array( 'primary_id_field', $this->lang->words['field__id'] );
		$fields[]	= array( 'member_id', $this->lang->words['field__member'] );
		$fields[]	= array( 'record_saved', $this->lang->words['field__saved'] );
		$fields[]	= array( 'record_updated', $this->lang->words['field__updated'] );
		$fields[]	= array( 'rating_real', $this->lang->words['field__rating'] );
		
		foreach( $this->fields as $r )
		{
			if( in_array( $r['field_type'], array( 'checkbox', 'multiselect', 'attachments' ) ) )
			{
				continue;
			}

			$fields[]	= array( 'field_' . $r['field_id'], $r['field_name'] );
		}
		
		//-----------------------------------------
		// And front page templates
		//-----------------------------------------
		
		$templates	= array();
		
		$this->DB->build( array( 'select' => '*', 'from' => 'ccs_page_templates', 'where' => 'template_database=4' ) );
		$this->DB->execute();
		
		while( $r = $this->DB->fetch() )
		{
			$templates[]	= array( $r['template_id'], $r['template_name'], $r['template_category'] );
		}

		$containers	= array();
		
		$this->DB->build( array( 'select' => 'container_id, container_name', 'from' => 'ccs_containers', 'where' => "container_type='arttemplate'", 'order' => 'container_order ASC' ) );
		$this->DB->execute();
		
		while( $r = $this->DB->fetch() )
		{
			$containers[ $r['container_id'] ]	= $r['container_name'];
		}

		//-----------------------------------------
		// Output
		//-----------------------------------------

		$this->registry->output->html .= $this->html->frontpageForm( $cache, $articles, $fields, $this->database, $categories, $templates, $containers );
	}
}
