<?php
/**
 * Content Templater List View
 *
 * @package     Content Templater
 * @version     1.5.4
 *
 * @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 VIEW object class
jimport( 'joomla.application.component.view' );

/**
 * Content Templater List View
 */
class ContentTemplaterViewList extends JView
{
	/**
	 * Custom Constructor
	 */
	function __construct( $config = array() )
	{
		/** set up global variable for sorting etc
		 * $context is used in VIEW abd in MODEL
		 **/
		global $context;
		$context = 'list.list.';

		parent::__construct( $config );
	}

	/**
	 * Display the view
	 * take data from MODEL and put them into
	 * reference variables
	 */

	function display( $tpl = null )
	{
		$mainframe =& JFactory::getApplication();
		$viewLayout	= JRequest::getCmd( 'layout', 'default' );

		$config =& JComponentHelper::getParams( 'com_contenttemplater' );

		$document =& JFactory::getDocument();
		$document->addStyleSheet( JURI::root( true ).'/administrator/components/com_contenttemplater/css/style.css' );

		if ( $viewLayout == 'import' ) {
			// Set document title
			$document->setTitle( JText::_( 'Content Templater - Import Templates' ) );
			// Set ToolBar title
			JToolBarHelper::title( JText::_( 'Import Templates' ), 'contenttemplater.png' );
			// Set toolbar items for the page
			JToolBarHelper::back();
		} else {
			// Set document title
			$document->setTitle( JText::_( 'Content Templater Items' ) );
			// Set ToolBar title
			JToolBarHelper::title( JText::_( 'List' ), 'contenttemplater.png' );
			// Set toolbar items for the page
			JToolBarHelper::addNewX();
			JToolBarHelper::customX( 'copy', 'copy.png', 'copy_f2.png', 'Copy' );
			JToolBarHelper::editListX();
			JToolBarHelper::deleteList( JText::_( 'VALIDDELETEITEMS' ) );
			JToolBarHelper::publishList();
			JToolBarHelper::unpublishList();
			JToolBarHelper::customX( 'export', 'export.png', 'export_f2.png', 'Export' ) ;
			JToolBarHelper::custom( 'import', 'import.png', 'import_f2.png', 'Import', false, false ) ;
			JToolBarHelper::preferences( 'com_contenttemplater', '360' );
			$bar = & JToolBar::getInstance('toolbar');
			// Add a configuration button
			jimport('joomla.language.help');
			$url = JHelp::createURL( 'help', 'com_contenttemplater' );
			$bar->appendButton( 'Popup', 'help', 'Help', $url, '570', '280' );
		}

		// Set ToolBar title
		$uri	=& JFactory::getURI();

		// get filter options
		$option	= 'contenttemplater';
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'.filter_order',		'filter_order',		'ordering',							'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.filter_order_Dir',	'filter_order_Dir',	'',									'word' );
		$filter_state		= $mainframe->getUserStateFromRequest( $option.'.filter_state',		'filter_state',		$config->get( 'state', '' ),		'word' );
		$search				= $mainframe->getUserStateFromRequest( $option.'.search',			'search',			'',									'string' );
		$search				= JString::strtolower( $search );
		$limit				= $mainframe->getUserStateFromRequest( $option.'.limit',			'limit',			$mainframe->getCfg( 'list_limit' ),	'int' );
		$limitstart			= JRequest::getCmd( 'limitstart' );

		// remember the actual order and column
		$lists['order'] = $filter_order;
		$lists['order_Dir'] = $filter_order_Dir;
		$lists['state']	= JHTML::_( 'grid.state',  $filter_state );
		$lists['search'] = $search;

		$this->_models['list']->setState( 'state', $filter_state );
		$this->_models['list']->setState( 'limit', $limit );
		$this->_models['list']->setState( 'limitstart', $limitstart );
		$this->_models['list']->setState( 'search', $search );

		$items =& $this->get( 'AllParams' );

		$pageNav =& $this->get( 'Pagination' );

		// save a reference into view
		$this->assignRef( 'user',		JFactory::getUser() );
		$this->assignRef( 'lists',		$lists );
		$this->assignRef( 'items',		$items );
		$this->assignRef( 'pagination',	$pageNav );
		$this->assignRef( 'request_url',	$uri->toString() );

		// call parent display
		parent::display( $tpl );
	}

	function maxlen( $string='', $maxlen=60 )
	{
		if ( strlen( $string ) > $maxlen ) {
			$string = substr( $string, 0, $maxlen-3 ).'...';
		}
		return $string;
	}
}