1<?php
2/**
3 * Joomla! Content Management System
4 *
5 * @copyright  Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
6 * @license    GNU General Public License version 2 or later; see LICENSE.txt
7 */
8
9namespace Joomla\CMS\Document\Renderer\Html;
10
11defined('JPATH_PLATFORM') or die;
12
13use Joomla\CMS\Document\DocumentRenderer;
14use Joomla\CMS\Helper\ModuleHelper;
15use Joomla\CMS\Log\Log;
16use Joomla\CMS\Layout\LayoutHelper;
17use Joomla\Registry\Registry;
18
19/**
20 * HTML document renderer for a single module
21 *
22 * @since  3.5
23 */
24class ModuleRenderer extends DocumentRenderer
25{
26	/**
27	 * Renders a module script and returns the results as a string
28	 *
29	 * @param   string  $module   The name of the module to render
30	 * @param   array   $attribs  Associative array of values
31	 * @param   string  $content  If present, module information from the buffer will be used
32	 *
33	 * @return  string  The output of the script
34	 *
35	 * @since   3.5
36	 */
37	public function render($module, $attribs = array(), $content = null)
38	{
39		if (!is_object($module))
40		{
41			$title = isset($attribs['title']) ? $attribs['title'] : null;
42
43			$module = ModuleHelper::getModule($module, $title);
44
45			if (!is_object($module))
46			{
47				if (is_null($content))
48				{
49					return '';
50				}
51
52				/**
53				 * If module isn't found in the database but data has been pushed in the buffer
54				 * we want to render it
55				 */
56				$tmp = $module;
57				$module = new \stdClass;
58				$module->params = null;
59				$module->module = $tmp;
60				$module->id = 0;
61				$module->user = 0;
62			}
63		}
64
65		// Set the module content
66		if (!is_null($content))
67		{
68			$module->content = $content;
69		}
70
71		// Get module parameters
72		$params = new Registry($module->params);
73
74		// Use parameters from template
75		if (isset($attribs['params']))
76		{
77			$template_params = new Registry(html_entity_decode($attribs['params'], ENT_COMPAT, 'UTF-8'));
78			$params->merge($template_params);
79			$module = clone $module;
80			$module->params = (string) $params;
81		}
82
83		// Default for compatibility purposes. Set cachemode parameter or use JModuleHelper::moduleCache from within the module instead
84		$cachemode = $params->get('cachemode', 'oldstatic');
85
86		if ($params->get('cache', 0) == 1 && \JFactory::getConfig()->get('caching') >= 1 && $cachemode != 'id' && $cachemode != 'safeuri')
87		{
88			// Default to itemid creating method and workarounds on
89			$cacheparams = new \stdClass;
90			$cacheparams->cachemode = $cachemode;
91			$cacheparams->class = 'JModuleHelper';
92			$cacheparams->method = 'renderModule';
93			$cacheparams->methodparams = array($module, $attribs);
94
95			return ModuleHelper::ModuleCache($module, $params, $cacheparams);
96		}
97
98		return ModuleHelper::renderModule($module, $attribs);
99	}
100}
101