<?php
/**
 * @package SP Page Builder
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2021 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/
//no direct accees
defined ('_JEXEC') or die ('Restricted access');

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Helper\ContentHelper;

class SppagebuilderViewPages extends JViewLegacy {

	public $filterForm;
	public $activeFilters = [];
	protected $items = [];
	protected $pagination;
	protected $state;
	protected $databaseIssue;

	public function display( $tpl = null ) {
		$this->items         	= $this->get('Items');
		$this->pagination    	= $this->get('Pagination');
		$this->state         	= $this->get('State');
		$this->filterForm    	= $this->get('FilterForm');
		$this->activeFilters 	= $this->get('ActiveFilters');
		$this->databaseIssue	= false;

		//Joomla Component Helper
		jimport('joomla.application.component.helper');
		$this->params = JComponentHelper::getParams('com_sppagebuilder');

		if(count( $errors = $this->get('Errors'))) {
			JError::raiseError(500,implode('<br />',$errors));
		}

		/**
		 * Enable and run maintenance audit
		 *
		 * @since	3.7.5
		*/
		$maintenancePath = JPATH_ADMINISTRATOR . '/components/com_sppagebuilder/helpers/maintenance.php';

		if (file_exists($maintenancePath))
		{
			require_once $maintenancePath;
		}

		if (class_exists('Maintenance'))
		{
			$maintenance = new Maintenance;
			$maintenance->run();
			$html = $maintenance->getBuffer('missing');;

			if (!empty($html))
			{
				$this->databaseIssue = true;
			}
		}

		$this->addToolbar();
		parent::display($tpl);
	}

	protected function addToolBar() {
		$state	= $this->get('State');
		$canDo	= ContentHelper::getActions('com_sppagebuilder');
		$user	= Factory::getUser();

		// Set the title
		ToolbarHelper::title(Text::_('COM_SPPAGEBUILDER') . ' - ' . JText::_('COM_SPPAGEBUILDER_PAGES'), 'none pbfont pbfont-pagebuilder');

		if(JVERSION < 4)
		{
			// new page button
			if ($canDo->get('core.create') || count($user->getAuthorisedCategories('com_sppagebuilder', 'core.create')) > 0)
			{
				JToolbarHelper::addNew('page.add');
			}

			// publish and unpublish button
			if ($canDo->get('core.edit.state'))
			{
				JToolbarHelper::publish('pages.publish', 'JTOOLBAR_PUBLISH', true);
				JToolbarHelper::unpublish('pages.unpublish', 'JTOOLBAR_UNPUBLISH', true);
				JToolbarHelper::checkin('pages.checkin');
			}

			// delete and trush button
			if ($this->state->get('filter.published') == -2 && $canDo->get('core.delete'))
			{
				JToolbarHelper::deleteList('', 'pages.delete', 'JTOOLBAR_EMPTY_TRASH');
			}
			elseif ($canDo->get('core.edit.state') && $canDo->get('core.delete'))
			{
				JToolbarHelper::trash('pages.trash');
			}

			if ($user->authorise('core.admin', 'com_sppagebuilder') || $user->authorise('core.options', 'com_sppagebuilder'))
			{
				JToolbarHelper::preferences('com_sppagebuilder');
			}
		}
		else
		{
			$toolbar = Toolbar::getInstance('toolbar');

			// new page button
			if ($canDo->get('core.create') || count($user->getAuthorisedCategories('com_sppagebuilder', 'core.create')) > 0)
			{
				$toolbar->addNew('page.add');
			}

			if ($canDo->get('core.edit.state'))
			{
				$dropdown = $toolbar->dropdownButton('status-group')
					->text('JTOOLBAR_CHANGE_STATUS')
					->toggleSplit(false)
					->icon('fas fa-ellipsis-h')
					->buttonClass('btn btn-action')
					->listCheck(true);

				$childBar = $dropdown->getChildToolbar();

				$childBar->publish('pages.publish')->listCheck(true);
				$childBar->unpublish('pages.unpublish')->listCheck(true);
				$childBar->checkin('pages.checkin')->listCheck(true);
				$childBar->trash('pages.trash')->listCheck(true);	
			}

			if ($this->state->get('filter.published') == -2 && $canDo->get('core.delete'))
			{
				$toolbar->delete('pages.delete')
					->text('JTOOLBAR_EMPTY_TRASH')
					->message('JGLOBAL_CONFIRM_DELETE')
					->listCheck(true);
			}

			if ($user->authorise('core.admin', 'com_sppagebuilder') || $user->authorise('core.options', 'com_sppagebuilder'))
			{
				$toolbar->preferences('com_sppagebuilder');
			}
		}
	}
}
