Completed
Push — master ( 633f87...d85777 )
by Cheren
03:16
created

HtmlHelper::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
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\ORM\Entity;
20
use JBZoo\Utils\Arr;
21
use Cake\View\Helper;
22
use Cake\Utility\Hash;
23
use Cake\Core\Configure;
24
use Core\View\Helper\Traits\HelperTrait;
25
use Core\View\Helper\Traits\IncludeTrait;
26
use Core\View\Helper\Traits\MaterializeCssTrait;
27
use Cake\View\Helper\HtmlHelper as CakeHtmlHelper;
28
29
/**
30
 * Class HtmlHelper
31
 *
32
 * @package     Core\View\Helper
33
 * @property    \Core\View\Helper\LessHelper $Less
34
 * @property    \Core\View\Helper\UrlHelper $Url
35
 * @property    \Core\View\Helper\DocumentHelper $Document
36
 */
37
class HtmlHelper extends CakeHtmlHelper
38
{
39
40
    use HelperTrait, IncludeTrait, MaterializeCssTrait;
41
42
    /**
43
     * List of helpers used by this helper.
44
     *
45
     * @var array
46
     */
47
    public $helpers = [
48
        'Core.Less',
49
        'Core.Document',
50
        'Url' => ['className' => 'Core.Url']
51
    ];
52
53
    /**
54
     * Hold assets data.
55
     *
56
     * @var array
57
     */
58
    protected $_assets = [
59
        'styles'  => [],
60
        'scripts' => []
61
    ];
62
63
    /**
64
     * HtmlHelper constructor.
65
     *
66
     * @param   View $View
67
     * @param   array $config
68
     */
69
    public function __construct(View $View, array $config = [])
70
    {
71
        $this->_defaultConfig = Hash::merge([
72
            'materializeCss' => false,
73
            'btnPref'        => Configure::read('Cms.btnPref'),
74
            'iconPref'       => Configure::read('Cms.iconPref'),
75
            'classPrefix'    => Configure::read('Cms.classPrefix'),
76
            'templates'      => [
77
                'icon' => '<i class="{{class}}"{{attrs}}></i>'
78
            ]
79
        ], $this->_defaultConfig);
80
81
        parent::__construct($View, $config);
82
    }
83
84
    /**
85
     * Constructor hook method. Implement this method to avoid having to overwrite the constructor and call parent.
86
     *
87
     * @param   array $config The configuration settings provided to this helper.
88
     * @return  void
89
     */
90
    public function initialize(array $config)
91
    {
92
        $isMaterializeCss = $this->getConfig('materializeCss', false);
93
94
        if ($isMaterializeCss === true) {
95
            $this->_configWrite('prepareBtnClass', function (Helper $form, $options, $button) {
96
                return $this->_prepareBtn($form, $options, $button);
97
            });
98
99
            $this->_configWrite('prepareTooltip', function (Helper $html, $options, $tooltip) {
100
                return $this->_prepareTooltip($html, $options, $tooltip);
101
            });
102
        }
103
104
        parent::initialize($config);
105
    }
106
107
    /**
108
     * Creates a link element for CSS stylesheets.
109
     *
110
     * @param   array|string $path
111
     * @param   array $options
112
     * @return  null|string
113
     */
114
    public function css($path, array $options = [])
115
    {
116
        $options += ['rel' => 'stylesheet'];
117
        return $this->_include($path, $options, 'css');
118
    }
119
120
    /**
121
     * Get sort assets included list.
122
     *
123
     * @param   string $key
124
     * @return  array|null|string
125
     */
126
    public function getAssets($key)
127
    {
128
        $value = Hash::get($this->_assets, $key);
129
        if (is_string($value)) {
130
            return $value;
131
        }
132
133
        return Hash::sort((array) $value, '{n}.weight', 'asc');
134
    }
135
136
    /**
137
     * Create icon element.
138
     *
139
     * @param   string $icon
140
     * @param   array $options
141
     * @return  null|string
142
     */
143
    public function icon($icon = 'home', array $options = [])
144
    {
145
        $iconPref = $this->_configRead('iconPref');
146
        $_classes = [
147
            $this->_class(__FUNCTION__),
148
            $iconPref,
149
            $iconPref . '-' . $icon,
150
        ];
151
152
        $options = $this->_addClass($options, implode(' ', $_classes));
153
        $classes = $options['class'];
154
        unset($options['class']);
155
156
        $templater = $this->templater();
157
        return $templater->format(__FUNCTION__, [
158
            'class' => $classes,
159
            'attrs' => $templater->formatAttributes($options),
160
        ]);
161
    }
162
163
    /**
164
     * Creates a CSS stylesheets from less.
165
     *
166
     * @param   string|array $path
167
     * @param   array $options
168
     * @return  null|string
169
     *
170
     * @throws  \JBZoo\Less\Exception
171
     */
172
    public function less($path, array $options = [])
173
    {
174
        $cssPath = [];
175
176
        if (!Arr::key('force', $options)) {
177
            $options['force'] = false;
178
        }
179
180
        if (is_array($path)) {
181
            foreach ($path as $i) {
182
                if ($result = $this->Less->process($i, $options['force'])) {
183
                    $cssPath[] = $result;
184
                }
185
            }
186
        }
187
188
        if (is_string($path) && $result = $this->Less->process($path, $options['force'])) {
189
            $cssPath[] = $result;
190
        }
191
192
        return $this->css($cssPath, $options);
193
    }
194
195
    /**
196
     * Create an html link.
197
     *
198
     * @param   string $title
199
     * @param   null|string|array $url
200
     * @param   array $options
201
     * @return  string
202
     */
203
    public function link($title, $url = null, array $options = [])
204
    {
205
        $options = $this->addClass($options, $this->_class(__FUNCTION__));
206
        $options = Hash::merge([
207
            'escapeTitle' => false,
208
            'clear'       => false,
209
            'label'       => $title,
210
        ], $options);
211
212
        $isClear = (bool) $options['clear'];
213
        unset($options['clear']);
214
215
        $options = $this->_setTitleAttr($title, $options);
216
217
        //  Set title in html tag.
218
        if ($this->_isEscapeTitle($title, $isClear, $options)) {
219
            $title = $this->tag('span', $title, ['class' => $this->_class(__FUNCTION__) . '-title']);
220
        }
221
222
        $options = $this->_getBtnClass($options);
223
        $options = $this->_getToolTipAttr($options);
224
225
        list($title, $options) = $this->_createIcon($this, $title, $options);
226
        unset($options['label']);
227
228
        return parent::link($title, $url, $options);
229
    }
230
231
    /**
232
     * Returns one or many `<script>` tags depending on the number of scripts given.
233
     *
234
     * @param   array|string $path
235
     * @param   array $options
236
     * @return  null|string
237
     */
238
    public function script($path, array $options = [])
239
    {
240
        return $this->_include($path, $options, 'script');
241
    }
242
243
    /**
244
     * Create status icon or link.
245
     *
246
     * @param   int $status
247
     * @param   array $url
248
     * @param   string $icon
249
     * @return  null|string
250
     */
251
    public function status($status = 0, array $url = [], $icon = 'circle')
252
    {
253
        $class = (int) $status === 1 ? 'ck-green' : 'ck-red';
254
        if (count($url) === 0) {
255
            return $this->icon($icon, ['class' => $class]);
256
        }
257
258
        return $this->link(null, 'javascript:void(0);', [
259
            'icon'     => $icon,
260
            'class'    => $class,
261
            'data-url' => $this->Url->build($url)
262
        ]);
263
    }
264
265
    /**
266
     * Create and render ajax toggle element.
267
     *
268
     * @param   Entity $entity
269
     * @param   array $url
270
     * @param   array $data
271
     * @return  string
272
     */
273
    public function toggle(Entity $entity, array $url = [], array $data = [])
274
    {
275
        if (count($url) === 0) {
276
            $url = [
277
                'action'     => 'toggle',
278
                'prefix'     => $this->request->getParam('prefix'),
279
                'plugin'     => $this->request->getParam('plugin'),
280
                'controller' => $this->request->getParam('controller'),
281
                (int) $entity->get('id'),
282
                (int) $entity->get('status')
283
            ];
284
        }
285
286
        $data = Hash::merge([
287
            'url'    => $url,
288
            'entity' => $entity,
289
        ], $data);
290
291
        return $this->_View->element('Core.' .  __FUNCTION__, $data);
292
    }
293
294
    /**
295
     * Check if need escape link title.
296
     *
297
     * @param   string $title
298
     * @param   bool $isClear
299
     * @param   array $options
300
     * @return  bool
301
     */
302
    protected function _isEscapeTitle($title, $isClear, array $options = [])
303
    {
304
        return $options['escapeTitle'] === false && !empty($title) && !$isClear;
305
    }
306
307
    /**
308
     * Setup default title attr.
309
     *
310
     * @param   string $title
311
     * @param   array $options
312
     * @return  array
313
     */
314
    protected function _setTitleAttr($title, array $options = [])
315
    {
316
        if (!Arr::key('title', $options)) {
317
            $options['title'] = strip_tags($title);
318
        }
319
320
        return $options;
321
    }
322
}
323