|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core\View\Helper; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\View\View; |
|
19
|
|
|
use Cake\Utility\Hash; |
|
20
|
|
|
use Cake\Core\Configure; |
|
21
|
|
|
use Core\View\Helper\Traits\HelperTrait; |
|
22
|
|
|
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class HtmlHelper |
|
26
|
|
|
* |
|
27
|
|
|
* @package Core\View\Helper |
|
28
|
|
|
* @property \Core\View\Helper\LessHelper $Less |
|
29
|
|
|
* @property \Core\View\Helper\UrlHelper $Url |
|
30
|
|
|
*/ |
|
31
|
|
|
class HtmlHelper extends CakeHtmlHelper |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
use HelperTrait; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* List of helpers used by this helper. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
public $helpers = [ |
|
42
|
|
|
'Core.Less', |
|
43
|
|
|
'Url' => ['className' => 'Core.Url'], |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* HtmlHelper constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param View $View |
|
50
|
|
|
* @param array $config |
|
51
|
|
|
*/ |
|
52
|
|
View Code Duplication |
public function __construct(View $View, array $config = []) |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
parent::__construct($View, $config); |
|
55
|
|
|
$this->_configWrite('btnPref', Configure::read('Cms.btnPref')); |
|
56
|
|
|
$this->_configWrite('iconPref', Configure::read('Cms.iconPref')); |
|
57
|
|
|
$this->_configWrite('classPrefix', Configure::read('Cms.classPrefix')); |
|
58
|
|
|
$this->_configWrite('templates.icon', '<i class="{{class}}"{{attrs}}></i>'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Create icon element. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $icon |
|
65
|
|
|
* @param array $options |
|
66
|
|
|
* @return null|string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function icon($icon = 'home', array $options = []) |
|
69
|
|
|
{ |
|
70
|
|
|
$iconPref = $this->_configRead('iconPref'); |
|
71
|
|
|
$_classes = [ |
|
72
|
|
|
$this->_class(__FUNCTION__), |
|
73
|
|
|
$iconPref, |
|
74
|
|
|
$iconPref . '-' . $icon, |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
$options = $this->_addClass($options, implode(' ', $_classes)); |
|
78
|
|
|
$classes = $options['class']; |
|
79
|
|
|
unset($options['class']); |
|
80
|
|
|
|
|
81
|
|
|
$templater = $this->templater(); |
|
82
|
|
|
return $templater->format(__FUNCTION__, [ |
|
83
|
|
|
'class' => $classes, |
|
84
|
|
|
'attrs' => $templater->formatAttributes($options), |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates a CSS stylesheets from less. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string|array $path |
|
92
|
|
|
* @param array $options |
|
93
|
|
|
* @return null|string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function less($path, array $options = []) |
|
96
|
|
|
{ |
|
97
|
|
|
$cssPath = []; |
|
98
|
|
|
|
|
99
|
|
|
if (!isset($options['force'])) { |
|
100
|
|
|
$options['force'] = false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (is_array($path)) { |
|
104
|
|
|
foreach ($path as $i) { |
|
105
|
|
|
if ($result = $this->Less->process($i, $options['force'])) { |
|
106
|
|
|
$cssPath[] = $result; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (is_string($path) && $result = $this->Less->process($path, $options['force'])) { |
|
112
|
|
|
$cssPath[] = $result; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $this->css($cssPath, $options); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Create an html link. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $title |
|
122
|
|
|
* @param null|string|array $url |
|
123
|
|
|
* @param array $options |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function link($title, $url = null, array $options = []) |
|
127
|
|
|
{ |
|
128
|
|
|
$options = $this->addClass($options, $this->_class(__FUNCTION__)); |
|
129
|
|
|
$options = Hash::merge([ |
|
130
|
|
|
'escapeTitle' => false, |
|
131
|
|
|
'clear' => false, |
|
132
|
|
|
'label' => $title, |
|
133
|
|
|
], $options); |
|
134
|
|
|
|
|
135
|
|
|
$isClear = (bool) $options['clear']; |
|
136
|
|
|
unset($options['clear']); |
|
137
|
|
|
|
|
138
|
|
|
$options = $this->_setTitleAttr($title, $options); |
|
139
|
|
|
|
|
140
|
|
|
// Set title in html tag. |
|
141
|
|
|
if ($this->_isEscapeTitle($title, $isClear, $options)) { |
|
142
|
|
|
$title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$options = $this->_getBtnClass($options); |
|
146
|
|
|
$options = $this->_getToolTipAttr($options); |
|
147
|
|
|
|
|
148
|
|
|
list($options, $iconOptions) = $this->_createIconAttr($options); |
|
149
|
|
|
if (isset($iconOptions['createIcon'])) { |
|
150
|
|
|
unset($iconOptions['createIcon']); |
|
151
|
|
|
$title = $this->icon($options['icon'], $iconOptions) . PHP_EOL . $title; |
|
152
|
|
|
unset($options['icon']); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (isset($options['iconInline'])) { |
|
156
|
|
|
unset($options['iconInline']); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
unset($options['label']); |
|
160
|
|
|
return parent::link($title, $url, $options); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Check if need escape link title. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $title |
|
167
|
|
|
* @param bool $isClear |
|
168
|
|
|
* @param array $options |
|
169
|
|
|
* @return bool |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function _isEscapeTitle($title, $isClear, array $options = []) |
|
172
|
|
|
{ |
|
173
|
|
|
return $options['escapeTitle'] === false && !empty($title) && !$isClear; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Setup default title attr. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $title |
|
180
|
|
|
* @param array $options |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function _setTitleAttr($title, array $options = []) |
|
184
|
|
|
{ |
|
185
|
|
|
if (!isset($options['title'])) { |
|
186
|
|
|
$options['title'] = strip_tags($title); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $options; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.