Completed
Push — master ( dbdab7...02a364 )
by Cheren
02:29
created

HtmlHelper::getAssets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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 Core\View\Helper\Traits\IncludeTrait;
23
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper;
24
25
/**
26
 * Class HtmlHelper
27
 *
28
 * @package Core\View\Helper
29
 * @property \Core\View\Helper\LessHelper $Less
30
 * @property \Core\View\Helper\UrlHelper $Url
31
 * @property \Core\View\Helper\DocumentHelper $Document
32
 */
33
class HtmlHelper extends CakeHtmlHelper
34
{
35
36
    use HelperTrait, IncludeTrait;
37
38
    /**
39
     * List of helpers used by this helper.
40
     *
41
     * @var array
42
     */
43
    public $helpers = [
44
        'Core.Less',
45
        'Core.Document',
46
        'Url' => ['className' => 'Core.Url'],
47
    ];
48
49
    /**
50
     * Hold assets data.
51
     *
52
     * @var array
53
     */
54
    protected $_assets = [
55
        'styles'  => [],
56
        'scripts' => [],
57
    ];
58
59
    /**
60
     * HtmlHelper constructor.
61
     *
62
     * @param View $View
63
     * @param array $config
64
     */
65 View Code Duplication
    public function __construct(View $View, array $config = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
66
    {
67
        parent::__construct($View, $config);
68
        $this->_configWrite('btnPref', Configure::read('Cms.btnPref'));
69
        $this->_configWrite('iconPref', Configure::read('Cms.iconPref'));
70
        $this->_configWrite('classPrefix', Configure::read('Cms.classPrefix'));
71
        $this->_configWrite('templates.icon', '<i class="{{class}}"{{attrs}}></i>');
72
    }
73
74
    /**
75
     * Get sort assets included list.
76
     *
77
     * @param string $key
78
     * @return array|null
79
     */
80
    public function getAssets($key)
81
    {
82
        $value = Hash::get($this->_assets, $key);
83
        if (is_string($value)) {
84
            return $value;
85
        }
86
87
        return Hash::sort($value, '{n}.weight', 'asc');
88
    }
89
90
    /**
91
     * Creates a link element for CSS stylesheets.
92
     *
93
     * @param array|string $path
94
     * @param array $options
95
     * @return null|string
96
     */
97
    public function css($path, array $options = [])
98
    {
99
        $options += ['rel' => 'stylesheet'];
100
        return $this->_include($path, $options, 'css');
101
    }
102
103
    /**
104
     * Create icon element.
105
     *
106
     * @param string $icon
107
     * @param array $options
108
     * @return null|string
109
     */
110
    public function icon($icon = 'home', array $options = [])
111
    {
112
        $iconPref = $this->_configRead('iconPref');
113
        $_classes = [
114
            $this->_class(__FUNCTION__),
115
            $iconPref,
116
            $iconPref . '-' . $icon,
117
        ];
118
119
        $options = $this->_addClass($options, implode(' ', $_classes));
120
        $classes = $options['class'];
121
        unset($options['class']);
122
123
        $templater = $this->templater();
124
        return $templater->format(__FUNCTION__, [
125
            'class' => $classes,
126
            'attrs' => $templater->formatAttributes($options),
127
        ]);
128
    }
129
130
    /**
131
     * Creates a CSS stylesheets from less.
132
     *
133
     * @param string|array $path
134
     * @param array $options
135
     * @return null|string
136
     */
137
    public function less($path, array $options = [])
138
    {
139
        $cssPath = [];
140
141
        if (!isset($options['force'])) {
142
            $options['force'] = false;
143
        }
144
145
        if (is_array($path)) {
146
            foreach ($path as $i) {
147
                if ($result = $this->Less->process($i, $options['force'])) {
148
                    $cssPath[] = $result;
149
                }
150
            }
151
        }
152
153
        if (is_string($path) && $result = $this->Less->process($path, $options['force'])) {
154
            $cssPath[] = $result;
155
        }
156
157
        return $this->css($cssPath, $options);
158
    }
159
160
    /**
161
     * Create an html link.
162
     *
163
     * @param string $title
164
     * @param null|string|array $url
165
     * @param array $options
166
     * @return string
167
     */
168
    public function link($title, $url = null, array $options = [])
169
    {
170
        $options = $this->addClass($options, $this->_class(__FUNCTION__));
171
        $options = Hash::merge([
172
            'escapeTitle' => false,
173
            'clear'       => false,
174
            'label'       => $title,
175
        ], $options);
176
177
        $isClear = (bool) $options['clear'];
178
        unset($options['clear']);
179
180
        $options = $this->_setTitleAttr($title, $options);
181
182
        //  Set title in html tag.
183
        if ($this->_isEscapeTitle($title, $isClear, $options)) {
184
            $title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']);
185
        }
186
187
        $options = $this->_getBtnClass($options);
188
        $options = $this->_getToolTipAttr($options);
189
190
        list($title, $options) = $this->_createIcon($this, $title, $options);
191
        unset($options['label']);
192
193
        return parent::link($title, $url, $options);
194
    }
195
196
    /**
197
     * Returns one or many `<script>` tags depending on the number of scripts given.
198
     *
199
     * @param array|string $path
200
     * @param array $options
201
     * @return null|string
202
     */
203
    public function script($path, array $options = [])
204
    {
205
        return $this->_include($path, $options, 'script');
206
    }
207
208
    /**
209
     * Check if need escape link title.
210
     *
211
     * @param string $title
212
     * @param bool $isClear
213
     * @param array $options
214
     * @return bool
215
     */
216
    protected function _isEscapeTitle($title, $isClear, array $options = [])
217
    {
218
        return $options['escapeTitle'] === false && !empty($title) && !$isClear;
219
    }
220
221
    /**
222
     * Setup default title attr.
223
     *
224
     * @param string $title
225
     * @param array $options
226
     * @return array
227
     */
228
    protected function _setTitleAttr($title, array $options = [])
229
    {
230
        if (!isset($options['title'])) {
231
            $options['title'] = strip_tags($title);
232
        }
233
234
        return $options;
235
    }
236
}
237