Completed
Push — master ( d10e6f...1f9c45 )
by Henry
08:18
created

Preview::routeContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
namespace Redaxscript\Modules\Preview;
3
4
use Redaxscript\Filesystem;
5
use Redaxscript\Head;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
9
/**
10
 * preview template elements
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Modules
16
 * @author Henry Ruhs
17
 */
18
19
class Preview extends Module\Module
20
{
21
	/**
22
	 * array of the module
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static $_moduleArray =
28
	[
29
		'name' => 'Preview',
30
		'alias' => 'Preview',
31
		'author' => 'Redaxmedia',
32
		'description' => 'Preview template elements',
33
		'version' => '3.3.0'
34
	];
35
36
	/**
37
	 * routeHeader
38
	 *
39
	 * @since 3.3.0
40
	 */
41
42
	public function routeHeader()
43
	{
44
		if ($this->_registry->get('firstParameter') === 'preview')
45
		{
46
			$this->_registry->set('useTitle', $this->_language->get('preview', '_preview'));
47
			$this->_registry->set('useDescription', $this->_language->get('description', '_preview'));
48
			$this->_registry->set('routerBreak', true);
49
50
			/* link */
51
52
			$link = Head\Link::getInstance();
53
			$link
54
				->init()
55
				->appendFile('modules/Preview/dist/styles/preview.min.css');
56
		}
57
	}
58
59
	/**
60
	 * routeContent
61
	 *
62
	 * @since 3.3.0
63
	 */
64
65
	public function routeContent()
66
	{
67
		if ($this->_registry->get('firstParameter') === 'preview')
68
		{
69
			echo $this->render();
70
		}
71
	}
72
73
	/**
74
	 * render
75
	 *
76
	 * @since 3.2.0
77
	 *
78
	 * @return string
79
	 */
80
81
	public function render() : string
82
	{
83
		$output = null;
84
		$partialsFilesystem = new Filesystem\File();
85
		$partialsFilesystem->init('modules/Preview/partials');
86
		$secondParameter = $this->_registry->get('secondParameter');
87
		$extension = '.phtml';
88
89
		/* collect single */
90
91
		if ($secondParameter)
92
		{
93
			$output = $this->_renderPartial($secondParameter, $partialsFilesystem->renderFile($secondParameter . $extension));
94
		}
95
96
		/* else collect all */
97
98
		else
99
		{
100
			$partialsFilesystemArray = $partialsFilesystem->getSortArray();
101
102
			/* process filesystem */
103
104
			foreach ($partialsFilesystemArray as $value)
105
			{
106
				$alias = str_replace($extension, '', $value);
107
				$output .= $this->_renderPartial($alias, $partialsFilesystem->renderFile($value));
108
			}
109
		}
110
		return $output;
111
	}
112
113
	/**
114
	 * renderPartial
115
	 *
116
	 * @since 3.2.0
117
	 *
118
	 * @param string $alias
119
	 * @param string $html
120
	 *
121
	 * @return string
122
	 */
123
124
	protected function _renderPartial(string $alias = null, string $html = null) : string
125
	{
126
		$secondParameter = $this->_registry->get('secondParameter');
127
		$parameterRoute = $this->_registry->get('parameterRoute');
128
129
		/* html elements */
130
131
		$linkElement = new Html\Element();
132
		$linkElement
133
			->init('a',
134
			[
135
				'href' => $secondParameter === $alias ? $parameterRoute . 'preview#' . $alias : $parameterRoute . 'preview/' . $alias
136
			])
137
			->text($secondParameter === $alias ? $this->_language->get('back') : $alias);
138
		$titleElement = new Html\Element();
139
		$titleElement
140
			->init('h2',
141
			[
142
				'class' => 'rs-title-preview',
143
				'id' => $alias
144
			])
145
			->html($linkElement);
146
		$boxElement = new Html\Element();
147
		$boxElement
148
			->init('div',
149
			[
150
				'class' => 'rs-is-preview rs-fn-clearfix'
151
			])
152
			->html($html);
153
154
		/* collect output */
155
156
		$output = $titleElement . $boxElement;
157
		return $output;
158
	}
159
}
160