Completed
Push — master ( ea460f...fd0685 )
by Cheren
02:34
created

HtmlHelper::link()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 34
rs 8.5806
cc 4
eloc 22
nc 8
nop 3
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
 */
29
class HtmlHelper extends CakeHtmlHelper
30
{
31
32
    use HelperTrait;
33
34
    /**
35
     * HtmlHelper constructor.
36
     *
37
     * @param View $View
38
     * @param array $config
39
     */
40
    public function __construct(View $View, array $config = [])
41
    {
42
        parent::__construct($View, $config);
43
        $this->_configWrite('btnPref', Configure::read('Cms.btnPref'));
44
        $this->_configWrite('iconPref', Configure::read('Cms.iconPref'));
45
        $this->_configWrite('classPrefix', Configure::read('Cms.classPrefix'));
46
        $this->_configWrite('templates.icon', '<i class="{{class}}"{{attrs}}></i>');
47
    }
48
49
    /**
50
     * Create icon element.
51
     *
52
     * @param string $icon
53
     * @param array $options
54
     * @return null|string
55
     */
56
    public function icon($icon = 'home', array $options = [])
57
    {
58
        $iconPref = $this->_configRead('iconPref');
59
        $_classes = [
60
            $this->_class(__FUNCTION__),
61
            $iconPref,
62
            $iconPref . '-' . $icon,
63
        ];
64
65
        $options = $this->_addClass($options, implode(' ', $_classes));
66
        $classes = $options['class'];
67
        unset($options['class']);
68
69
        $templater = $this->templater();
70
        return $templater->format(__FUNCTION__, [
71
            'class' => $classes,
72
            'attrs' => $templater->formatAttributes($options),
73
        ]);
74
    }
75
76
    /**
77
     * Create an html link.
78
     *
79
     * @param string $title
80
     * @param null|string|array $url
81
     * @param array $options
82
     * @return string
83
     */
84
    public function link($title, $url = null, array $options = [])
85
    {
86
        $options = $this->addClass($options, $this->_class(__FUNCTION__));
87
        $options = Hash::merge([
88
            'escapeTitle' => false,
89
            'clear'       => false,
90
            'label'       => $title,
91
        ], $options);
92
93
        $isClear = (bool) $options['clear'];
94
        unset($options['clear']);
95
96
        //  Set title in html tag.
97
        if ($this->_isEscapeTitle($title, $isClear, $options)) {
98
            $title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']);
99
        }
100
101
        $options = $this->_getBtnClass($options);
102
        $options = $this->_getToolTipAttr($options);
103
104
        list($options, $iconOptions) = $this->_createIconAttr($options);
105
        if (isset($iconOptions['createIcon'])) {
106
            unset($iconOptions['createIcon']);
107
            $title = $this->icon($options['icon'], $iconOptions) . PHP_EOL . $title;
108
            unset($options['icon']);
109
        }
110
111
        if (isset($options['iconInline'])) {
112
            unset($options['iconInline']);
113
        }
114
115
        unset($options['label']);
116
        return parent::link($title, $url, $options);
117
    }
118
119
    /**
120
     * Check if need escape link title.
121
     *
122
     * @param string $title
123
     * @param bool $isClear
124
     * @param array $options
125
     * @return bool
126
     */
127
    protected function _isEscapeTitle($title, $isClear, array $options = [])
128
    {
129
        return $options['escapeTitle'] === false && !empty($title) && !$isClear;
130
    }
131
}
132