<?php
/**
 * @package         Modals
 * @version         9.0.6
 * 
 * @author          Peter van Westen <info@regularlabs.com>
 * @link            http://www.regularlabs.com
 * @copyright       Copyright © 2017 Regular Labs All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

namespace RegularLabs\Modals;

defined('_JEXEC') or die;

use JFactory;
use RegularLabs\Library\Article as RL_Article;
use RegularLabs\Library\Document as RL_Document;
use RegularLabs\Library\Html as RL_Html;

/**
 * Plugin that replaces stuff
 */
class Helper
{
	public function onContentPrepare($article, $context, $params)
	{
		$area    = isset($article->created_by) ? 'article' : 'other';
		$context = (($params instanceof \JRegistry) && $params->get('rl_search')) ? 'com_search.' . $params->get('readmore_limit') : $context;

		RL_Article::process($article, $context, $this, 'replaceTags', [$area, $context]);

		return $article;
	}

	public function onAfterDispatch()
	{
		Document::addHeadStuff();

		if (!$buffer = RL_Document::getBuffer())
		{
			return;
		}

		if (JFactory::getApplication()->input->getInt('ml', 0) && !JFactory::getApplication()->input->getInt('fullpage', 0))
		{
			Document::setTemplate();
		}

		self::replaceTags($buffer, 'component');

		RL_Document::setBuffer($buffer);
	}

	public function onAfterRender()
	{
		$html = JFactory::getApplication()->getBody();

		if ($html == '')
		{
			return;
		}

		// only do stuff in body
		list($pre, $body, $post) = RL_Html::getBody($html);
		self::replaceTags($body, 'body');

		Clean::cleanLeftoverJunk($pre);
		Clean::cleanLeftoverJunk($post);

		$html = $pre . $body . $post;

		Document::removeHeadStuff($html);

		JFactory::getApplication()->setBody($html);
	}

	public function replaceTags(&$string, $area = 'article', $context = '')
	{
		Replace::replaceTags($string, $area, $context);
	}
}
