Completed
Push — master ( 1e7dc0...d5c54b )
by Cheren
02:27
created

HtmlHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 16
c 5
b 0
f 3
lcom 2
cbo 6
dl 0
loc 159
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A icon() 0 19 1
B less() 0 20 5
B link() 0 36 4
A _isEscapeTitle() 0 4 3
A _setTitleAttr() 0 8 2
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
    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
                $cssPath[] = $this->Less->process($i, $options['force']);
106
            }
107
        }
108
109
        if (is_string($path)) {
110
            $cssPath[] = $this->Less->process($path, $options['force']);
111
        }
112
113
        return $this->css($cssPath, $options);
114
    }
115
116
    /**
117
     * Create an html link.
118
     *
119
     * @param string $title
120
     * @param null|string|array $url
121
     * @param array $options
122
     * @return string
123
     */
124
    public function link($title, $url = null, array $options = [])
125
    {
126
        $options = $this->addClass($options, $this->_class(__FUNCTION__));
127
        $options = Hash::merge([
128
            'escapeTitle' => false,
129
            'clear'       => false,
130
            'label'       => $title,
131
        ], $options);
132
133
        $isClear = (bool) $options['clear'];
134
        unset($options['clear']);
135
136
        $options = $this->_setTitleAttr($title, $options);
137
138
        //  Set title in html tag.
139
        if ($this->_isEscapeTitle($title, $isClear, $options)) {
140
            $title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']);
141
        }
142
143
        $options = $this->_getBtnClass($options);
144
        $options = $this->_getToolTipAttr($options);
145
146
        list($options, $iconOptions) = $this->_createIconAttr($options);
147
        if (isset($iconOptions['createIcon'])) {
148
            unset($iconOptions['createIcon']);
149
            $title = $this->icon($options['icon'], $iconOptions) . PHP_EOL . $title;
150
            unset($options['icon']);
151
        }
152
153
        if (isset($options['iconInline'])) {
154
            unset($options['iconInline']);
155
        }
156
157
        unset($options['label']);
158
        return parent::link($title, $url, $options);
159
    }
160
161
    /**
162
     * Check if need escape link title.
163
     *
164
     * @param string $title
165
     * @param bool $isClear
166
     * @param array $options
167
     * @return bool
168
     */
169
    protected function _isEscapeTitle($title, $isClear, array $options = [])
170
    {
171
        return $options['escapeTitle'] === false && !empty($title) && !$isClear;
172
    }
173
174
    /**
175
     * Setup default title attr.
176
     *
177
     * @param string $title
178
     * @param array $options
179
     * @return array
180
     */
181
    protected function _setTitleAttr($title, array $options = [])
182
    {
183
        if (!isset($options['title'])) {
184
            $options['title'] = strip_tags($title);
185
        }
186
187
        return $options;
188
    }
189
}
190