Completed
Push — master ( fd0685...ece391 )
by Cheren
02:29
created

HtmlHelper::less()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 8.8571
cc 5
eloc 10
nc 8
nop 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
        //  Set title in html tag.
137
        if ($this->_isEscapeTitle($title, $isClear, $options)) {
138
            $title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']);
139
        }
140
141
        $options = $this->_getBtnClass($options);
142
        $options = $this->_getToolTipAttr($options);
143
144
        list($options, $iconOptions) = $this->_createIconAttr($options);
145
        if (isset($iconOptions['createIcon'])) {
146
            unset($iconOptions['createIcon']);
147
            $title = $this->icon($options['icon'], $iconOptions) . PHP_EOL . $title;
148
            unset($options['icon']);
149
        }
150
151
        if (isset($options['iconInline'])) {
152
            unset($options['iconInline']);
153
        }
154
155
        unset($options['label']);
156
        return parent::link($title, $url, $options);
157
    }
158
159
    /**
160
     * Check if need escape link title.
161
     *
162
     * @param string $title
163
     * @param bool $isClear
164
     * @param array $options
165
     * @return bool
166
     */
167
    protected function _isEscapeTitle($title, $isClear, array $options = [])
168
    {
169
        return $options['escapeTitle'] === false && !empty($title) && !$isClear;
170
    }
171
}
172