<?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
 * @forum:		  https://www.joomlart.com/forums/t/t4-builder
 * @Link:         https://demo.t4-builder.joomlart.com/
 *------------------------------------------------------------------------------
 */

// check if need separated layout for Joomla 3!
// if (($j3 = \JPB\Helper\Layout::j3(__FILE__))) {
// 	include $j3;
// 	return;
// }
defined('_JEXEC') or die;

use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Router\Route;
use Joomla\Registry\Registry;
use Joomla\CMS\HTML\HTMLHelper;

// Load the com_media language files, default to the admin file and fall back to site if one isn't found
$lang = Factory::getLanguage();
$lang->load('com_t4pagebuilder', JPATH_ADMINISTRATOR, null, false, true)
||	$lang->load('com_t4pagebuilder', JPATH_SITE, null, false, true);
if(!FrontEndT4pagebuilderHelper::isJ4()){
	HTMLHelper::_('behavior.keepalive');
	HTMLHelper::_('jquery.framework');
	HTMLHelper::_('bootstrap.framework');
}
// Create shortcut to parameters.
$params = clone $this->state->get('params');
$params->merge(new Registry($this->item));
$loadfontDefaults = file_get_contents(JPATH_ROOT . JPB_MEDIA_BUILDER . 'googlefonts/data.json');
$customFont = is_file(JPB_PATH_MEDIA_BUILDER . 'etc/'.$this->item->id.'/customfonts.json') ? json_decode(file_get_contents(JPB_PATH_MEDIA_BUILDER . 'etc/'.$this->item->id.'/customfonts.json'),true) : [];
$loadgooglefont = $params->get('loadfonts') ? $params->get('loadfonts') : $loadfontDefaults ;
$customFonts = $customFont ? $customFont['fonts'] : [];
$loadIcons = $params->get('loadconfigicons') ? $params->get('loadconfigicons') : "{\"awesome_icons\":{\"awesome_icons\":true,\"url_type\":\"cdn\",\"custom_url\":\"null\"},\"material_icons\":{\"material_icons\":true,\"url_type\":\"cdn\",\"custom_url\":\"\"}}";

$editInline = Factory::getApplication()->input->get('inline','') ? true : false;
if (!defined('JPB_LOADER')) {
	$t4base = Uri::root(true) . JPB_MEDIA_BUILDER;
	$doc = Factory::getDocument();
	$doc->addStyleSheet($t4base . 'css/editor.css');
	$ajaxUrl = Uri::base(true) . '/index.php?option=com_t4pagebuilder&view=page&format=json';
    $languages = t4pagebuilderHelper::buildLanguage();

	if(!defined('JPB_DEVMODE')) define('JPB_DEVMODE',false);
	$doc->addScriptDeclaration('
		var ajax_url="' . $ajaxUrl . '";
		var t4b = window.t4b || {}; 
      t4b.langs =' . $languages . '; 
      t4b.jversion = "'. (int) explode('.', JVERSION)[0] .'";
		var editInline="' . $editInline . '";
		var loadgooglefont=' . $loadgooglefont . ';
		var customFont=' . json_encode($customFonts) . '; 
		var site_root_url = "' . Uri::root() . '"; 
		var loadIcons = ' . json_encode($loadIcons) . '; 
		var siteUrl = "' . Uri::root() . '";
		var builder_url = "' . $t4base . '";
		var builderParam="' . JPB_PARAM . '";
		var jpb_devmode="'.JPB_DEVMODE.'";
		var isT4 ="'.$this->t4.'";
		var editor_type ="com_t4pagebuilder";
	');
	$doc->addScript($t4base . 'js/loader.js');
	$doc->addStyleSheet(Uri::root(true) .  "/administrator/components/com_t4pagebuilder/assets/css/googlefonts.css");
	$doc->addScript(Uri::root(true) .  "/administrator/components/com_t4pagebuilder/assets/js/googlefonts.js");
	define('JPB_LOADER', 1);

}
$this->configFieldsets  = array('editorConfig');
$this->hiddenFieldsets  = array('basic-limited');
$this->ignore_fieldsets = array('jmetadata', 'item_associations');

$user = Factory::getUser();

if (!$user->authorise('core.create', 'com_t4pagebuilder') || !$user->authorise('core.edit', 'com_t4pagebuilder')) {
	throw new Exception("JERROR_ALERTNOAUTHOR", 403);
}

$userid = $user->id;
$content = $this->item->page_html;
$working_content = $this->item->working_content;

$app = Factory::getApplication();
$input = $app->input;
//config view on page content
$name = $this->item->asset_name;
$asset = $this->item->asset_id;
$xml = JPATH_COMPONENT_ADMINISTRATOR . "/models/forms/page.xml";
$this->form = Form::getInstance('page',$xml);
// In case of modal
$isModal = $input->get('layout') == 'modal' ? true : false;
$layout  = $isModal ? 'modal' : 'edit';
$tmpl    = $isModal || $input->get('tmpl', '', 'cmd') === 'component' ? '&tmpl=component' : '';
$fade = ($this->item->id) ? "fade" : "show";
if(empty($this->form)) $this->form = '';
?>
<form action="<?php echo Route::_('index.php?option=com_t4pagebuilder&layout=edit&id=' . (int) $this->item->id.'&inline=1', false); ?>" method="post" name="adminForm" id="adminForm" class="form-validate">
	<div class="jpb-page-wrap">
		<div class="form-horizontal">
			<div class="row-fluid">
				<div class="span12">
					<fieldset class="adminform">
						<?php 
							// echo $this->form->getInput('pagetext');
							$modifier = $params->get('fullScreenMod', '') !== '' ? implode(' + ', $params->get('fullScreenMod'), ) . ' + ' : '';

							// check if string is encoded
							if (preg_match('/[<>\"]+/', $content, $match)) {
								$content = htmlentities($content);
							}

							// parse field name
							$fname = trim($name, '[]');
							$fname = preg_replace('/[\]\[]+/', '.', $fname);
							$fid = "jpb-field-" . str_replace('.', '-', $fname);

							// $t4id = JPB\Helper\Item::getRefId ($fname, $asset,$this->item->id);
							$t4id = $this->item->id;
							$htmlFiled = $this->form->getField('page_html');
							$htmlCss = $this->form->getField('css');
							$editorDisabled = empty($this->item->id) ? " disabled" : '';
							?>
							<input type="hidden" name="<?php echo $htmlFiled->name ?>" id="<?php echo $htmlFiled->id ?>" class="jpb-item" value="<?php echo htmlentities($htmlFiled->value) ?>" style="display: none" data-template="<?php echo $this->templateDefault;?>" data-name="<?php echo $fid ?>" />
							<input id="<?php echo $fid ?>" type="hidden" name="jpb-fields[<?php echo $fname ?>]" value="<?php echo $t4id; ?>" />
								<div class="jpb-edit" style="display: none;">
									<span class="btn btn-customize jpb-btn-edit<?php echo $editorDisabled;?>"><span class="fal fa-pencil"></span><strong>Edit</strong></span>
								</div>
							<?php echo T4pagebuilderHelper::mediafield($fid); ?>
					</fieldset>
				</div>
			</div>
		</div>
	</div>
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="return" value="<?php echo $input->getCmd('return'); ?>" />
	<input type="hidden" name="forcedLanguage" value="<?php echo $input->get('forcedLanguage', '', 'cmd'); ?>" />
	<?php echo HTMLHelper::_('form.token'); ?>
</form>
<style>
	.jpb-btn-edit,
	.jpb-preview {
		display: none;
	}
</style>