RViewBase::loadTemplate()   F
last analyzed

Complexity

Conditions 15
Paths 385

Size

Total Lines 92
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 92
rs 2.7708
cc 15
nc 385
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package     Redcore
4
 * @subpackage  View
5
 *
6
 * @copyright   Copyright (C) 2008 - 2021 redWEB.dk. All rights reserved.
7
 * @license     GNU General Public License version 2 or later, see LICENSE.
8
 */
9
10
defined('JPATH_REDCORE') or die;
11
12
/**
13
 * A view that can be rendered in full screen.
14
 *
15
 * @package     Redcore
16
 * @subpackage  View
17
 * @since       1.0
18
 */
19
abstract class RViewBase extends JViewLegacy
20
{
21
	/**
22
	 * The component title to display in the topbar layout (if using it).
23
	 * It can be html.
24
	 *
25
	 * @var string
26
	 */
27
	protected $componentTitle = '';
28
29
	/**
30
	 * Layout used to render the component
31
	 *
32
	 * @var  string
33
	 */
34
	protected $componentLayout = 'component.site';
35
36
	/**
37
	 * The topbar layout name to display.
38
	 *
39
	 * @var  boolean
40
	 */
41
	protected $topBarLayout = 'topbar.site';
42
43
	/**
44
	 * Execute and display a template script.
45
	 *
46
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
47
	 *
48
	 * @return  mixed  A string if successful, otherwise a Error object.
49
	 */
50
	public function display($tpl = null)
51
	{
52
		$render = RLayoutHelper::render(
53
			$this->componentLayout,
54
			array(
55
				'view' => $this,
56
				'tpl' => $tpl,
57
				'component_title' => $this->componentTitle,
58
				'topbar_layout' => $this->topBarLayout,
59
			)
60
		);
61
62
		if ($render instanceof Exception)
0 ignored issues
show
introduced by
$render is never a sub-type of Exception.
Loading history...
63
		{
64
			return $render;
65
		}
66
67
		echo $render;
68
69
		return true;
70
	}
71
72
	/**
73
	 * Get the view title.
74
	 *
75
	 * @return  string  The view title.
76
	 */
77
	public function getTitle()
78
	{
79
		return '';
80
	}
81
82
	/**
83
	 * Load a template file -- first look in the templates folder for an override
84
	 *
85
	 * @param   string  $tpl  The name of the template source file; automatically searches the template paths and compiles as needed.
86
	 *
87
	 * @return  string  The output of the the template script.
88
	 *
89
	 * @since   10.3
90
	 * @throws  Exception
91
	 */
92
	public function loadTemplate($tpl = null)
93
	{
94
		// Uses the parent function if no framework suffix is present
95
		if (RHtmlMedia::$frameworkSuffix == '')
96
		{
97
			return parent::loadTemplate($tpl);
98
		}
99
100
		// Clear prior output
101
		$this->_output = null;
0 ignored issues
show
Bug Best Practice introduced by
The property _output does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
103
		$template = JFactory::getApplication()->getTemplate();
104
		$layout = $this->getLayout();
105
		$layoutTemplate = $this->getLayoutTemplate();
106
107
		// Create the template file name based on the layout
108
		$file = isset($tpl) ? $layout . '_' . $tpl : $layout;
109
110
		// Clean the file name
111
		$file = preg_replace('/[^A-Z0-9_\.-]/i', '', $file);
112
		$tpl = isset($tpl) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $tpl) : $tpl;
113
114
		// Load the language file for the template
115
		$lang = JFactory::getLanguage();
116
		$lang->load('tpl_' . $template, JPATH_BASE, null, false, true)
117
		|| $lang->load('tpl_' . $template, JPATH_THEMES . "/$template", null, false, true);
118
119
		// Change the template folder if alternative layout is in different template
120
		if (isset($layoutTemplate) && $layoutTemplate != '_' && $layoutTemplate != $template)
121
		{
122
			$this->_path['template'] = str_replace($template, $layoutTemplate, $this->_path['template']);
0 ignored issues
show
Bug Best Practice introduced by
The property _path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
123
		}
124
125
		// Load the template script
126
		jimport('joomla.filesystem.path');
127
128
		// Tries first with the framework suffix
129
		$filetofind = $this->_createFileName('template', array('name' => $file . '.' . RHtmlMedia::$frameworkSuffix));
130
		$this->_template = JPath::find($this->_path['template'], $filetofind);
0 ignored issues
show
Bug Best Practice introduced by
The property _template does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
131
132
		// If no framework-speficic is found, tries with the normal alternate layout
133
		if ($this->_template == false)
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->_template of type false|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
134
		{
135
			$filetofind      = $this->_createFileName('template', array('name' => $file));
136
			$this->_template = JPath::find($this->_path['template'], $filetofind);
137
		}
138
139
		// If alternate layout can't be found, fall back to default layout, first with framework suffix
140
		if ($this->_template == false)
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->_template of type false|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
141
		{
142
			$filetofind = $this->_createFileName(
143
				'', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl) . '.' . RHtmlMedia::$frameworkSuffix)
144
			);
145
			$this->_template = JPath::find($this->_path['template'], $filetofind);
146
		}
147
148
		// If no default with suffix is found, fall back to default layout without framework suffix
149
		if ($this->_template == false)
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->_template of type false|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
150
		{
151
			$filetofind = $this->_createFileName('', array('name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl)));
152
			$this->_template = JPath::find($this->_path['template'], $filetofind);
153
		}
154
155
		if ($this->_template != false)
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->_template of type false|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
156
		{
157
			// Unset so as not to introduce into template scope
158
			unset($tpl);
159
			unset($file);
160
161
			// Never allow a 'this' property
162
			if (isset($this->this))
163
			{
164
				unset($this->this);
165
			}
166
167
			// Start capturing output into a buffer
168
			ob_start();
169
170
			// Include the requested template filename in the local scope
171
			// (this will execute the view logic).
172
			include $this->_template;
173
174
			// Done with the requested template; get the buffer and
175
			// clear it.
176
			$this->_output = ob_get_contents();
177
			ob_end_clean();
178
179
			return $this->_output;
180
		}
181
		else
182
		{
183
			throw new Exception(JText::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500);
184
		}
185
	}
186
}
187