<?php
/**
 * @copyright	Copyright (c) 2015 Skyline Technology Ltd (http://extstore.com). All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

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

/**
 * View class for a list of Popups.
 *
 * @package		Joomla.Administrator
 * @subpakage	ExtStore.AdvPopup
 */
class AdvPopupViewPopups extends JViewLegacy {
	protected $items;
	protected $pagination;
	protected $state;

	/**
	 * Display the view.
	 */
	public function display($tpl = null) {
		if (!$this->getLayout() !== 'modal') {
			AdvPopupHelper::addSubmenu('popups');
		}

		$this->state			= $this->get('State');
		$this->items			= $this->get('Items');
		$this->pagination		= $this->get('Pagination');
		$this->authors			= $this->get('Authors');
		$this->filterForm		= $this->get('FilterForm');
		$this->activeFilters	= $this->get('ActiveFilters');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		// We don't need toolbar in the modal window.
		if ($this->getLayout() !== 'modal') {
			$this->addToolbar();
			$this->sidebar	= JHtmlSidebar::render();
		}

		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 */
	protected function addToolbar() {
		require_once JPATH_COMPONENT . '/helpers/advpopup.php';

		$state	= $this->get('State');
		$canDo	= AdvPopupHelper::getActions();
		$user	= JFactory::getUser();
		$bar	= JToolBar::getInstance('toolbar');

		JToolBarHelper::title(JText::_('COM_ADVPOPUP_POPUPS_MANAGER'), 'popups.png');

		if ($canDo->get('core.create')) {
			JToolBarHelper::addNew('popup.add');
		}

		if ($canDo->get('core.edit')) {
			JToolBarHelper::editList('popup.edit');
		}

		if ($canDo->get('core.edit.state')) {
			JToolBarHelper::checkin('popups.checkin');
		}

		if ($canDo->get('core.delete')) {
			JToolBarHelper::deleteList('', 'popups.delete', 'JTOOLBAR_DELETE');
		}


		// Add a batch button
		if ($canDo->get('core.edit')) {
			JHtml::_('bootstrap.modal', 'collapseModal');
			$title = JText::_('JTOOLBAR_BATCH');

			// Instantiate a new JLayoutFile instance and render the batch button
			$layout	= new JLayoutFile('joomla.toolbar.batch');
			$dhtml	= $layout->render(array('title' => $title));
			$bar->appendButton('Custom', $dhtml, 'batch');
		}

		if ($canDo->get('core.admin')) {
			JToolBarHelper::preferences('com_advpopup');
		}
	}

	/**
	 * Returns an array of fields the table can be sorted by
	 *
	 * @return  array  Array containing the field name to sort by as the key and display text as value
	 */
	protected function getSortFields() {
		return array(
			'a.ordering'	=> JText::_('JGRID_HEADING_ORDERING'),
			'a.title'		=> JText::_('JGLOBAL_TITLE'),
			'a.access'		=> JText::_('JGRID_HEADING_ACCESS'),
			'a.language'	=> JText::_('JGRID_HEADING_LANGUAGE'),
			'a.id'			=> JText::_('JGRID_HEADING_ID')
		);
	}
}