<?php
/**
 *------------------------------------------------------------------------------
 * @package       T4 Page Builder for Joomla!
 *------------------------------------------------------------------------------
 * @copyright     Copyright (C) 2004-2020 JoomlArt.com. All Rights Reserved.
 * @license       GNU General Public License version 2 or later; see LICENSE.txt
 * @authors       JoomlArt, JoomlaBamboo, (contribute to this project at github
 *                & Google group to become co-author)
 *------------------------------------------------------------------------------
 */


defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Language\Text;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Access\Exception\NotAllowed as JAccessExceptionNotallowed;

if (!Factory::getUser()->authorise('core.edit', 'com_t4pagebuilder')) {
  throw new JAccessExceptionNotallowed(Text::_('JERROR_ALERTNOAUTHOR'), 403);
}
JLoader::registerNamespace('JPB', __DIR__ . '/libs', false, false, 'psr4');
JLoader::register('T4pagebuilderHelper', __DIR__ . '/helpers/t4pagebuilder.php');
if (file_exists(__DIR__ . '/define.php')) {
	require_once __DIR__ . '/define.php';
}

//check tables
T4pagebuilderHelper::checkTables();

if (version_compare(JVERSION, '4', 'ge')) {
    HTMLHelper::_('jquery.framework');
}

$lang = Factory::getLanguage();
$extension = 'com_categories';
$base_dir = JPATH_SITE;
$language_tag = 'en-GB';
$reload = true;
$lang->load($extension, $base_dir, $language_tag, $reload);
$doc = Factory::getDocument();
$doc->addStylesheet(JPB_PATH_BASE . 'assets/css/ja-pagebuilder.css');
$script[] = 'var url_base = "' . Uri::base(true) . '";';
$script[] = "jQuery(document).ready(function($){";

if (version_compare(JVERSION, '5', '>=')){
    $script[] = "		$('body').addClass('j4 j5');";
}elseif(version_compare(JVERSION, '4', '>=')){
    $script[] = "		$('body').addClass('j4');";
}else{
    $script[] = "		$('body').addClass('j3');";
}
$script[] = "});";

$doc->addScriptDeclaration(implode("\n", $script));
$controller = BaseController::getInstance('T4pagebuilder', array('base_path' => JPATH_COMPONENT_ADMINISTRATOR));
$controller->execute(Factory::getApplication()->input->get('task'));
$controller->redirect();
