|
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\Traits; |
|
17
|
|
|
|
|
18
|
|
|
use JBZoo\Utils\Str; |
|
19
|
|
|
use Cake\Utility\Hash; |
|
20
|
|
|
use Cake\Core\Configure; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class HelperTrait |
|
24
|
|
|
* |
|
25
|
|
|
* @package Core\View\Helper\Traits |
|
26
|
|
|
*/ |
|
27
|
|
|
trait HelperTrait |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create icon attributes. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $options |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function _createIconAttr(array $options = []) |
|
37
|
|
|
{ |
|
38
|
|
|
$iconOptions = ['class' => '']; |
|
39
|
|
|
if (isset($options['icon'])) { |
|
40
|
|
|
if (isset($options['iconClass'])) { |
|
41
|
|
|
$iconOptions = $this->_addClass($iconOptions, $options['iconClass']); |
|
42
|
|
|
unset($options['iconClass']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$icon = $options['icon']; |
|
46
|
|
|
if (isset($options['iconInline'])) { |
|
47
|
|
|
$iconPrefix = $this->_configRead('iconPref'); |
|
|
|
|
|
|
48
|
|
|
$options = $this->_addClass($options, implode(' ', [ |
|
49
|
|
|
$this->_class('icon'), |
|
50
|
|
|
$iconPrefix, |
|
51
|
|
|
$iconPrefix . '-' . $icon |
|
52
|
|
|
])); |
|
53
|
|
|
|
|
54
|
|
|
unset($options['icon']); |
|
55
|
|
|
} else { |
|
56
|
|
|
$options['escape'] = false; |
|
57
|
|
|
$iconOptions['createIcon'] = true; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return [$options, $iconOptions]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create and get tooltip attributes. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $options |
|
68
|
|
|
* @param string $toggle |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function _getToolTipAttr(array $options = [], $toggle = 'tooltip') |
|
72
|
|
|
{ |
|
73
|
|
|
if (isset($options['tooltip'])) { |
|
74
|
|
|
|
|
75
|
|
|
$tooltip = $options['tooltip']; |
|
76
|
|
|
unset($options['tooltip']); |
|
77
|
|
|
|
|
78
|
|
|
if (is_callable($this->config('prepareTooltip'))) { |
|
|
|
|
|
|
79
|
|
|
return (array) call_user_func($this->config('prepareTooltip'), $this, $options); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$_options = [ |
|
83
|
|
|
'data-toggle' => $toggle, |
|
84
|
|
|
'data-placement' => 'top', |
|
85
|
|
|
]; |
|
86
|
|
|
|
|
87
|
|
|
if (isset($options['tooltipPos'])) { |
|
88
|
|
|
$_options['data-placement'] = (string) $options['tooltipPos']; |
|
89
|
|
|
unset($options['tooltipPos']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($tooltip === true && !isset($options['title'])) { |
|
93
|
|
|
$options['title'] = strip_tags($options['label']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (is_string($tooltip)) { |
|
97
|
|
|
$options['title'] = $tooltip; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return Hash::merge($_options, $options); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $options; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Create and get button classes. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $options |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function _getBtnClass(array $options = []) |
|
113
|
|
|
{ |
|
114
|
|
|
if (isset($options['button'])) { |
|
115
|
|
|
|
|
116
|
|
|
$button = $options['button']; |
|
117
|
|
|
unset($options['button']); |
|
118
|
|
|
|
|
119
|
|
|
if (is_callable($this->config('prepareBtnClass'))) { |
|
|
|
|
|
|
120
|
|
|
return (array) call_user_func($this->config('prepareBtnClass'), $this, $options); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($button !== true) { |
|
124
|
|
|
$classes = [$this->_configRead('btnPref')]; |
|
|
|
|
|
|
125
|
|
|
foreach ((array) $button as $button) { |
|
126
|
|
|
$classes[] = $this->_configRead('btnPref') . '-' . $button; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
$options = $this->_addClass($options, implode(' ', $classes)); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $options; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get class with union prefix. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $class |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function _class($class = 'cms') |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->_configRead('classPrefix') . '-' . Str::trim(Str::slug($class)); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Adds the given class to the element options |
|
148
|
|
|
* |
|
149
|
|
|
* @param array $options Array options/attributes to add a class to |
|
150
|
|
|
* @param string $class The class name being added. |
|
151
|
|
|
* @param string $key the key to use for class. |
|
152
|
|
|
* @return array Array of options with $key set. |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function _addClass(array $options = [], $class = null, $key = 'class') |
|
155
|
|
|
{ |
|
156
|
|
|
if (isset($options[$key]) && Str::trim($options[$key])) { |
|
157
|
|
|
$options[$key] .= ' ' . $class; |
|
158
|
|
|
} else { |
|
159
|
|
|
$options[$key] = $class; |
|
160
|
|
|
} |
|
161
|
|
|
return $options; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.