Completed
Push — develop ( 053968...47dc8d )
by Maxim
12s
created

TemplateParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * EVO CMS
4
 * Template parser
5
 *
6
 * Date: 24.06.2017
7
 *
8
 */
9
10
Class TemplateParser {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
12
    /**
13
	 * @param array $config [action, tabs, toArray]
14
	 * @param array $data
15
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be array|string? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
16
	 */
17
	public function output($config = array(), $data = array()) {
0 ignored issues
show
Coding Style introduced by
output uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
18
		global $modx;
19
20
		$output = '';
21
		$action = !empty($config['action']) ? $config['action'] : (!empty($_REQUEST['a']) ? $_REQUEST['a'] : '');
22
		$tab = isset($config['tab']) ? ' AND tab IN(' . $config['tab'] . ')' : '';
23
24
		if($action) {
25
			$sql = $modx->db->query('SELECT t1.*, IF(t1.alias=\'\',t1.name,t1.alias) AS alias, t2.category AS category_name
26
			FROM ' . $modx->getFullTableName('system_templates') . ' AS t1
27
			INNER JOIN ' . $modx->getFullTableName('categories') . ' AS t2 ON t2.id=t1.category
28
			WHERE t1.action IN(' . $action . ') ' . $tab . '
29
			ORDER BY t1.tab ASC, t1.rank ASC');
30
31
			if($modx->db->getRecordCount($sql)) {
32
				$tabs = array();
33
				while($row = $modx->db->getRow($sql)) {
34
					if(!$row['value'] && !empty($data[$row['name']])) {
35
						$row['value'] = $data[$row['name']];
36
					}
37
					$tabs[$row['tab']]['category_name'] = $row['category_name'];
38
					$tabs[$row['tab']][$row['name']] = TemplateParser::render($row);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
39
				}
40
41
				if(!empty($config['toArray'])) {
42
					$output = $tabs;
43
				} else {
44
					$output .= '<div class="tab-pane" id="pane_' . $action . '">';
45
					$output .= '
46
					<script type="text/javascript">
47
						var pane_' . $action . ' = new WebFXTabPane(document.getElementById("pane_' . $action . '"), ' . ($modx->config['remember_last_tab'] == 1 ? 'true' : 'false') . ');
48
					</script>';
49
50
					foreach($tabs as $idTab => $tab) {
51
						$output .= '<div class="tab-page" id="tab_' . $action . '_' . $idTab . '">';
52
						$output .= '
53
						<h2 class="tab">' . (!empty($config['tabs'][$idTab]) ? $config['tabs'][$idTab] : $tab['category_name']) . '</h2>
54
						<script type="text/javascript">pane_' . $action . '.addTabPage(document.getElementById("tab_' . $action . '_' . $idTab . '"));</script>';
55
						unset($tab['category_name']);
56
						foreach($tab as $item) {
57
							$output .= $item;
58
						}
59
						$output .= '</div>';
60
					}
61
					$output .= '</div>';
62
				}
63
			}
64
		}
65
66
		return $output;
67
	}
68
69
    /**
70
     * @param array $data
71
     * @return string
72
     */
73
	private function render($data) {
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
74
		global $modx, $_lang, $_country_lang;
75
76
		$data['lang.name'] = (isset($_lang[$data['alias']]) ? $_lang[$data['alias']] : $data['alias']);
77
		$data['value'] = (isset($_POST[$data['name']][$data['value']]) ? $_POST[$data['name']][$data['value']] : (isset($data['value']) ? $modx->htmlspecialchars($data['value']) : ''));
78
		$data['readonly'] = ($data['readonly'] ? ' readonly' : '');
79
80
		$output = '';
81
		$output .= '<div class="form-group row">';
82
83
		switch($data['type']) {
84
85
			case 'text':
86
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>
87
					<div class="col-sm-7">
88
					<input type="text" name="[+name+]" class="form-control" id="[+name+]" value="[+value+]" onChange="documentDirty=true;"[+readonly+] />';
89
				$output .= $data['content'];
90
				$output .= '</div>';
91
92
				break;
93
94
			case 'textarea':
95
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>
96
					<div class="col-sm-7">
97
					<textarea name="[+name+]" class="form-control" id="[+name+]" onChange="documentDirty=true;"[+readonly+]>[+value+]</textarea>';
98
				$output .= $data['content'];
99
				$output .= '</div>';
100
101
				break;
102
103
			case 'date':
104
				$data['value'] = (isset($_POST[$data['name']][$data['value']]) ? $modx->toDateFormat($_POST[$data['name']][$data['value']]) : (isset($data['value']) ? $modx->toDateFormat($data['value']) : ''));
105
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>
106
					<div class="col-sm-7">
107
					<input type="text" name="[+name+]" class="form-control DatePicker" id="[+name+]" value="[+value+]" onChange="documentDirty=true;"[+readonly+] />';
108
				$output .= $data['content'];
109
				$output .= '</div>';
110
111
				break;
112
113
			case 'select':
114
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>';
115
				$output .= '<div class="col-sm-7">';
116
				$output .= '<select name="[+name+]" class="form-control" id="[+name+]" onChange="documentDirty=true;">';
117
				if($data['name'] == 'country' && isset($_country_lang)) {
118
					$chosenCountry = isset($_POST['country']) ? $_POST['country'] : $data['country'];
119
					$output .= '<option value=""' . (!isset($chosenCountry) ? ' selected' : '') . '>&nbsp;</option>';
120
					foreach($_country_lang as $key => $value) {
121
						$output .= '<option value="' . $key . '"' . (isset($chosenCountry) && $chosenCountry == $key ? ' selected' : '') . '>' . $value . '</option>';
122
					}
123 View Code Duplication
				} else {
124
					if($data['elements']) {
125
						$elements = explode('||', $data['elements']);
126
						foreach($elements as $key => $value) {
127
							$value = explode('==', $value);
128
							$output .= '<option value="' . $value[1] . '">' . (isset($_lang[$value[0]]) ? $_lang[$value[0]] : $value[0]) . '</option>';
129
						}
130
					}
131
				}
132
				$output .= '</select>';
133
				$output .= $data['content'];
134
				$output .= '</div>';
135
136
				break;
137
138
			case 'checkbox':
139
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>';
140
				$output .= '<div class="col-sm-7">';
141
				$output .= '<input type="checkbox" name="[+name+]" class="form-control" id="[+name+]" value="[+value+]" onChange="documentDirty=true;"[+readonly+] />';
142
				if($data['elements']) {
143
					$elements = explode('||', $data['elements']);
144
					foreach($elements as $key => $value) {
145
						$value = explode('==', $value);
146
						$output .= '<br /><input type="checkbox" name="' . $value[0] . '" class="form-control" id="' . $value[0] . '" value="' . $value[1] . '" onChange="documentDirty=true;"[+readonly+] /> ' . (isset($_lang[$value[0]]) ? $_lang[$value[0]] : $value[0]);
147
					}
148
				}
149
				$output .= $data['content'];
150
				$output .= '</div>';
151
152
				break;
153
154
			case 'radio':
155
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>';
156
				$output .= '<div class="col-sm-7">';
157
				$output .= '<input type="radio" name="[+name+]" class="form-control" id="[+name+]" value="[+value+]" onChange="documentDirty=true;"[+readonly+] />';
158 View Code Duplication
				if($data['elements']) {
159
					$elements = explode('||', $data['elements']);
160
					foreach($elements as $key => $value) {
161
						$value = explode('==', $value);
162
						$output .= '<br /><input type="radio" name="[+name+]" class="form-control" id="[+name+]_' . $key . '" value="' . $value[1] . '" onChange="documentDirty=true;"[+readonly+] /> ' . (isset($_lang[$value[0]]) ? $_lang[$value[0]] : $value[0]);
163
					}
164
				}
165
				$output .= $data['content'];
166
				$output .= '</div>';
167
168
				break;
169
170
			case 'custom':
171
				$output .= '<label class="col-sm-3" for="[+name+]">[+lang.name+]</label>';
172
				$output .= '<div class="col-sm-7">';
173
				$output .= $data['content'];
174
				$output .= '</div>';
175
176
				break;
177
		}
178
179
		$output .= '</div>';
180
181
		$output = $modx->parseText($output, $data);
182
183
		return $output;
184
	}
185
186
}
187
188
?>
189
190
<?php
191
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
192
193
include_once MODX_BASE_PATH . MGR_DIR . '/media/style/' . $modx->config['manager_theme'] . '/includes/template.parser.class.inc.php';
194
195
echo TemplateParser::output(array('action' => 88), $userdata);
196
197
*/
198
?>
199
200
201
<!--
202
203
-- --------------------------------------------------------
204
-- Хост:                         127.0.0.1
205
-- Версия сервера:               5.6.29 - MySQL Community Server (GPL)
206
-- Операционная система:         Win64
207
-- HeidiSQL Версия:              9.4.0.5125
208
-- --------------------------------------------------------
209
210
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
211
/*!40101 SET NAMES utf8 */;
212
/*!50503 SET NAMES utf8mb4 */;
213
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
214
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
215
216
217
-- Дамп структуры базы данных modxnewhtml
218
CREATE DATABASE IF NOT EXISTS `modxnewhtml` /*!40100 DEFAULT CHARACTER SET utf8 */;
219
USE `modxnewhtml`;
220
221
-- Дамп структуры для таблица modxnewhtml.modx_system_templates
222
CREATE TABLE IF NOT EXISTS `modx_system_templates` (
223
`id` int(11) NOT NULL AUTO_INCREMENT,
224
  `name` varchar(255) NOT NULL DEFAULT '',
225
  `alias` varchar(255) NOT NULL DEFAULT '',
226
  `type` varchar(50) NOT NULL,
227
  `description` varchar(255) NOT NULL DEFAULT '',
228
  `help` varchar(255) NOT NULL DEFAULT '',
229
  `value` varchar(255) NOT NULL DEFAULT '',
230
  `readonly` int(1) NOT NULL DEFAULT '0',
231
  `elements` text NOT NULL,
232
  `content` text NOT NULL,
233
  `category` int(11) NOT NULL,
234
  `template` int(11) NOT NULL DEFAULT '0',
235
  `tab` int(11) NOT NULL DEFAULT '0',
236
  `rank` int(11) NOT NULL DEFAULT '0',
237
  `action` int(11) NOT NULL DEFAULT '0',
238
  PRIMARY KEY (`id`)
239
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
240
241
-- Дамп данных таблицы modxnewhtml.modx_system_templates: ~14 rows (приблизительно)
242
	/*!40000 ALTER TABLE `modx_system_templates` DISABLE KEYS */;
243
INSERT INTO `modx_system_templates` (`id`, `name`, `alias`, `type`, `description`, `help`, `value`, `readonly`, `elements`, `content`, `category`, `template`, `tab`, `rank`, `action`) VALUES
244
(0, 'fullname', 'user_full_name', 'text', '', '', '', 0, '', '', 9, 0, 1, 1, 88),
245
	(1, 'email', 'user_email', 'text', '', '', '', 0, '', '', 9, 0, 1, 2, 88),
246
	(2, 'phone', 'user_phone', 'text', '', '', '', 0, '', '', 9, 0, 1, 3, 88),
247
	(3, 'mobilephone', 'user_mobile', 'text', '', '', '', 0, '', '', 9, 0, 1, 5, 88),
248
	(4, 'fax', 'user_fax', 'text', '', '', '', 0, '', '', 9, 0, 1, 6, 88),
249
	(5, 'street', 'user_street', 'text', '', '', '', 0, '', '', 9, 0, 1, 7, 88),
250
	(6, 'city', 'user_city', 'text', '', '', '', 0, '', '', 9, 0, 1, 8, 88),
251
	(7, 'state', 'user_state', 'text', '', '', '', 0, '', '', 9, 0, 1, 9, 88),
252
	(8, 'zip', 'user_zip', 'text', '', '', '', 0, '', '', 9, 0, 1, 10, 88),
253
	(9, 'country', 'user_country', 'select', '', '', '', 0, '', '', 9, 0, 1, 11, 88),
254
	(10, 'dob', 'user_dob', 'date', '', '', '', 0, '', '', 9, 0, 1, 12, 88),
255
	(11, 'gender', 'user_gender', 'select', '', '', '', 0, '||user_male==1||user_female==2||user_other==3', '', 9, 0, 1, 13, 88),
256
	(12, 'comment', '', 'textarea', '', '', '', 0, '', '', 9, 0, 1, 14, 88),
257
	(13, 'logincount', 'user_logincount', 'custom', '', '', '', 0, '', '[+value+]', 9, 0, 1, 15, 88);
258
/*!40000 ALTER TABLE `modx_system_templates` ENABLE KEYS */;
259
260
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
261
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
262
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
263
264
-->
265