DocumentHelper::getBodyClasses()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 4
nop 0
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 Core\Core\Plugin;
19
use JBZoo\Utils\Arr;
20
use JBZoo\Utils\Str;
21
use Cake\Event\Event;
22
use Cake\Core\Configure;
23
24
/**
25
 * Class DocumentHelper
26
 *
27
 * @package     Core\View\Helper
28
 * @property    \Core\View\Helper\HtmlHelper $Html
29
 * @property    \Core\View\Helper\AssetsHelper $Assets
30
 *
31
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
32
 */
33
class DocumentHelper extends AppHelper
34
{
35
36
    /**
37
     * Init vars.
38
     *
39
     * @var string
40
     */
41
    public $charset;
42
    public $dir;
43
    public $eol;
44
    public $locale;
45
    public $tab;
46
47
    /**
48
     * Uses helpers.
49
     *
50
     * @var array
51
     */
52
    public $helpers = [
53
        'Core.Html',
54
        'Core.Assets'
55
    ];
56
57
    /**
58
     * Is called after layout rendering is complete. Receives the layout filename as an argument.
59
     *
60
     * @param   Event $event
61
     * @param   string $layoutFile
62
     * @return  void
63
     *
64
     * @throws  \JBZoo\Utils\Exception
65
     */
66 View Code Duplication
    public function afterLayout(Event $event, $layoutFile)
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...
67
    {
68
        $pluginEvent = Plugin::getData('Core', 'View.afterLayout');
69
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.afterLayout')) {
70
            call_user_func_array($pluginEvent->find(0), [$this->getView(), $event, $layoutFile]);
71
        }
72
    }
73
74
    /**
75
     * Is called after the view has been rendered but before layout rendering has started.
76
     *
77
     * @param   Event $event
78
     * @param   string $viewFile
79
     * @return  void
80
     *
81
     * @throws  \JBZoo\Utils\Exception
82
     */
83 View Code Duplication
    public function afterRender(Event $event, $viewFile)
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...
84
    {
85
        $this->_setupMetaData();
86
87
        $pluginEvent = Plugin::getData('Core', 'View.afterRender');
88
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.afterRender')) {
89
            call_user_func_array($pluginEvent->find(0), [$this->_View, $event, $viewFile]);
90
        }
91
    }
92
93
    /**
94
     * Is called after each view file is rendered. This includes elements, views, parent views and layouts.
95
     * A callback can modify and return $content to change how the rendered content will be displayed in the browser.
96
     *
97
     * @param   Event $event
98
     * @param   string $viewFile
99
     * @param   string $content
100
     * @return  void
101
     *
102
     * @throws  \JBZoo\Utils\Exception
103
     */
104
    public function afterRenderFile(Event $event, $viewFile, $content)
105
    {
106
        $pluginEvent = Plugin::getData('Core', 'View.afterRenderFile');
107
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.afterRenderFile')) {
108
            call_user_func_array($pluginEvent->find(0), [$this->getView(), $event, $viewFile, $content]);
109
        }
110
    }
111
112
    /**
113
     * Get assets fot layout render.
114
     *
115
     * @param   string $type
116
     * @return  string
117
     */
118
    public function assets($type = 'css')
119
    {
120
        $output = [];
121
        $assets = $this->Assets->getAssets($type);
122
        foreach ($assets as $asset) {
0 ignored issues
show
Bug introduced by
The expression $assets of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
123
            $output[] = $asset['output'];
124
        }
125
126
        return implode($this->eol, $output);
127
    }
128
129
    /**
130
     * Is called before layout rendering starts. Receives the layout filename as an argument.
131
     *
132
     * @param   Event $event
133
     * @param   string $layoutFile
134
     * @return  void
135
     *
136
     * @throws  \JBZoo\Utils\Exception
137
     */
138 View Code Duplication
    public function beforeLayout(Event $event, $layoutFile)
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...
139
    {
140
        $pluginEvent = Plugin::getData('Core', 'View.beforeLayout');
141
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.beforeLayout')) {
142
            call_user_func_array($pluginEvent->find(0), [$this->getView(), $event, $layoutFile]);
143
        }
144
    }
145
146
    /**
147
     * Is called after the controller’s beforeRender method but before the controller renders view and layout.
148
     * Receives the file being rendered as an argument.
149
     *
150
     * @param   Event $event
151
     * @param   string $viewFile
152
     * @return  void
153
     *
154
     * @throws  \JBZoo\Less\Exception
155
     * @throws  \JBZoo\Utils\Exception
156
     */
157
    public function beforeRender(Event $event, $viewFile)
158
    {
159
        $this->Assets->loadPluginAssets();
160
161
        $pluginEvent = Plugin::getData('Core', 'View.beforeRender');
162
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.beforeRender')) {
163
            call_user_func_array($pluginEvent->find(0), [$this->getView(), $event, $viewFile]);
164
        }
165
    }
166
167
    /**
168
     * Is called before each view file is rendered. This includes elements, views, parent views and layouts.
169
     *
170
     * @param    Event $event
171
     * @param   string $viewFile
172
     * @return  void
173
     *
174
     * @throws  \JBZoo\Utils\Exception
175
     */
176 View Code Duplication
    public function beforeRenderFile(Event $event, $viewFile)
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...
177
    {
178
        $pluginEvent = Plugin::getData('Core', 'View.beforeRenderFile');
179
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('View.beforeRenderFile')) {
180
            call_user_func_array($pluginEvent->find(0), [$this->getView(), $event, $viewFile]);
181
        }
182
    }
183
184
    /**
185
     * Get body classes by view data.
186
     *
187
     * @return  string
188
     */
189
    public function getBodyClasses()
190
    {
191
        $view    = $this->getView();
192
        $request = $view->getRequest();
193
        $prefix  = ($request->getParam('prefix')) ? 'prefix-' . $request->getParam('prefix') : 'prefix-site';
194
195
        $classes = [
196
            $prefix,
197
            'theme-'    . Str::low($view->getTheme()),
198
            'plugin-'   . Str::low($view->getPlugin()),
199
            'view-'     . Str::low($view->getName()),
200
            'tmpl-'     . Str::low($view->getTemplate()),
201
            'layout-'   . Str::low($view->getLayout())
202
        ];
203
204
        $pass = (array) $request->getParam('pass');
205
        if (count($pass)) {
206
            $classes[] = 'item-id-' . array_shift($pass);
207
        }
208
209
        return implode(' ', $classes);
210
    }
211
212
    /**
213
     * Create head for layout.
214
     *
215
     * @return  string
216
     */
217
    public function head()
218
    {
219
        $output = [
220
            'meta'             => $this->getView()->fetch('meta'),
221
            'assets'           => $this->assets('css'),
222
            'fetch_css'        => $this->getView()->fetch('css'),
223
            'fetch_css_bottom' => $this->getView()->fetch('css_bottom'),
224
        ];
225
226
        return implode('', $output);
227
    }
228
229
    /**
230
     * Constructor hook method.
231
     *
232
     * @param   array $config
233
     */
234
    public function initialize(array $config)
235
    {
236
        parent::initialize($config);
237
238
        $this->dir     = Configure::read('Cms.docDir');
239
        $this->locale  = Configure::read('App.defaultLocale');
240
        $this->charset = Str::low(Configure::read('App.encoding'));
241
        $this->eol     = (Configure::read('debug')) ? PHP_EOL : '';
242
        $this->tab     = (Configure::read('debug')) ? Configure::read('Cms.lineTab') : '';
243
    }
244
245
    /**
246
     * Site language.
247
     *
248
     * @param   bool|true $isLang
249
     * @return  string
250
     *
251
     * @throws  \Exception
252
     */
253
    public function lang($isLang = true)
254
    {
255
        list($lang, $region) = explode('_', $this->locale);
256
        return ($isLang) ? Str::low($lang) : Str::low($region);
257
    }
258
259
    /**
260
     * Creates a link to an external resource and handles basic meta tags.
261
     *
262
     * @param   array $rows
263
     * @param   null $block
264
     * @return  null|string
265
     */
266
    public function meta(array $rows, $block = null)
267
    {
268
        $output = [];
269
        foreach ($rows as $row) {
270
            $output[] = trim($row);
271
        }
272
273
        $output = implode($this->eol, $output) . $this->eol;
274
275
        if ($block !== null) {
276
            $this->getView()->append($block, $output);
277
            return null;
278
        }
279
280
        return $output;
281
    }
282
283
    /**
284
     * Create html 5 document type.
285
     *
286
     * @return  string
287
     *
288
     * @throws  \Exception
289
     */
290
    public function type()
291
    {
292
        $lang = $this->lang();
293
        $html = [
294
            '<!doctype html>',
295
            '<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7 ie6" '
296
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
297
            '<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" '
298
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
299
            '<!--[if IE 8]><html class="no-js lt-ie9 ie8" '
300
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
301
            '<!--[if gt IE 8]><!--><html class="no-js" xmlns="http://www.w3.org/1999/xhtml" '
302
            . 'lang="' . $lang . '" dir="' . $this->dir . '" '
303
            . 'prefix="og: http://ogp.me/ns#" '
304
            . '> <!--<![endif]-->',
305
        ];
306
        return implode($this->eol, $html) . $this->eol;
307
    }
308
309
    /**
310
     * Assign data from view vars.
311
     *
312
     * @param   string $key
313
     * @return  $this
314
     */
315
    protected function _assignMeta($key)
316
    {
317
        if (Arr::key($key, $this->_View->viewVars)) {
318
            $this->getView()->assign($key, $this->_View->viewVars[$key]);
319
        }
320
321
        return $this;
322
    }
323
324
    /**
325
     * Setup view meta data.
326
     *
327
     * @return  void
328
     */
329
    protected function _setupMetaData()
330
    {
331
        $this
332
            ->_assignMeta('page_title')
333
            ->_assignMeta('meta_keywords')
334
            ->_assignMeta('meta_description');
335
    }
336
}
337