Completed
Push — master ( acb1c9...dbdab7 )
by Cheren
03:07
created

DocumentHelper::_loadPluginAssets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
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\Plugin;
19
use JBZoo\Utils\Str;
20
use Cake\Event\Event;
21
use Cake\Core\Configure;
22
23
/**
24
 * Class DocumentHelper
25
 *
26
 * @package Core\View\Helper
27
 * @property \Core\View\Helper\HtmlHelper $Html
28
 * @property \Core\View\Helper\AssetsHelper $Assets
29
 */
30
class DocumentHelper extends AppHelper
31
{
32
33
    /**
34
     * Init vars.
35
     *
36
     * @var string
37
     */
38
    public $dir;
39
    public $eol;
40
    public $tab;
41
    public $locale;
42
    public $charset;
43
44
    /**
45
     * Uses helpers.
46
     *
47
     * @var array
48
     */
49
    public $helpers = [
50
        'Core.Html',
51
        'Core.Assets',
52
    ];
53
54
    /**
55
     * Constructor hook method.
56
     *
57
     * @param array $config
58
     */
59
    public function initialize(array $config)
60
    {
61
        parent::initialize($config);
62
63
        $this->dir     = Configure::read('Cms.docDir');
64
        $this->locale  = Configure::read('App.defaultLocale');
65
        $this->charset = Str::low(Configure::read('App.encoding'));
66
        $this->eol     = (Configure::read('debug')) ? PHP_EOL : '';
67
        $this->tab     = (Configure::read('debug')) ? Configure::read('Cms.lineTab') : '';
68
    }
69
70
    /**
71
     * Site language.
72
     *
73
     * @param bool|true $isLang
74
     * @return string
75
     * @throws \Exception
76
     */
77
    public function lang($isLang = true)
78
    {
79
        list($lang, $region) = explode('_', $this->locale);
80
        return ($isLang) ? Str::low($lang) : Str::low($region);
81
    }
82
83
    /**
84
     * Creates a link to an external resource and handles basic meta tags.
85
     *
86
     * @param array $rows
87
     * @param null $block
88
     * @return null|string
89
     */
90
    public function meta(array $rows, $block = null)
91
    {
92
        $output = [];
93
        foreach ($rows as $row) {
94
            $output[] = trim($row);
95
        }
96
97
        $output = implode($this->eol, $output) . $this->eol;
98
99
        if ($block !== null) {
100
            $this->_View->append($block, $output);
101
            return null;
102
        }
103
104
        return $output;
105
    }
106
107
    /**
108
     * Create html 5 document type.
109
     *
110
     * @return string
111
     * @throws \Exception
112
     */
113
    public function type()
114
    {
115
        $lang = $this->lang();
116
        $html = [
117
            '<!doctype html>',
118
            '<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7 ie6" '
119
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
120
            '<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" '
121
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
122
            '<!--[if IE 8]><html class="no-js lt-ie9 ie8" '
123
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
124
            '<!--[if gt IE 8]><!--><html class="no-js" xmlns="http://www.w3.org/1999/xhtml" '
125
            . 'lang="' . $lang . '" dir="' . $this->dir . '" '
126
            . 'prefix="og: http://ogp.me/ns#" '
127
            . '> <!--<![endif]-->',
128
        ];
129
        return implode($this->eol, $html) . $this->eol;
130
    }
131
132
    /**
133
     * Is called before each view file is rendered. This includes elements, views, parent views and layouts.
134
     *
135
     * @param Event $event
136
     * @param string $viewFile
137
     * @return void
138
     */
139
    public function beforeRenderFile(Event $event, $viewFile)
140
    {
141
        Plugin::manifestEvent('View.beforeRenderFile', $this->_View, $event, $viewFile);
142
    }
143
144
    /**
145
     * Is called after each view file is rendered. This includes elements, views, parent views and layouts.
146
     * A callback can modify and return $content to change how the rendered content will be displayed in the browser.
147
     *
148
     * @param Event $event
149
     * @param string $viewFile
150
     * @param string $content
151
     * @return void
152
     */
153
    public function afterRenderFile(Event $event, $viewFile, $content)
154
    {
155
        Plugin::manifestEvent('View.afterRenderFile', $this->_View, $event, $viewFile, $content);
156
    }
157
158
    /**
159
     * Is called after the controller’s beforeRender method but before the controller renders view and layout.
160
     * Receives the file being rendered as an argument.
161
     *
162
     * @param Event $event
163
     * @param string $viewFile
164
     * @return void
165
     */
166
    public function beforeRender(Event $event, $viewFile)
167
    {
168
        $this->Assets->loadPluginAssets();
169
        Plugin::manifestEvent('View.beforeRender', $this->_View, $event, $viewFile);
170
    }
171
172
    /**
173
     * Is called after the view has been rendered but before layout rendering has started.
174
     *
175
     * @param Event $event
176
     * @param string $viewFile
177
     * @return void
178
     */
179
    public function afterRender(Event $event, $viewFile)
180
    {
181
        $this->_setupMetaData();
182
        Plugin::manifestEvent('View.afterRender', $this->_View, $event, $viewFile);
183
    }
184
185
    /**
186
     * Is called before layout rendering starts. Receives the layout filename as an argument.
187
     *
188
     * @param Event $event
189
     * @param string $layoutFile
190
     * @return void
191
     */
192
    public function beforeLayout(Event $event, $layoutFile)
193
    {
194
        Plugin::manifestEvent('View.beforeLayout', $this->_View, $event, $layoutFile);
195
    }
196
197
    /**
198
     * Is called after layout rendering is complete. Receives the layout filename as an argument.
199
     *
200
     * @param Event $event
201
     * @param string $layoutFile
202
     * @return void
203
     */
204
    public function afterLayout(Event $event, $layoutFile)
205
    {
206
        Plugin::manifestEvent('View.beforeLayout', $this->_View, $event, $layoutFile);
207
    }
208
209
    /**
210
     * Setup view meta data.
211
     *
212
     * @return void
213
     */
214
    protected function _setupMetaData()
215
    {
216
        $this->_assignMeta('page_title')
217
            ->_assignMeta('meta_keywords')
218
            ->_assignMeta('meta_description');
219
    }
220
221
    /**
222
     * Assign data from view vars.
223
     *
224
     * @param string $key
225
     * @return $this
226
     */
227
    protected function _assignMeta($key)
228
    {
229
        if (isset($this->_View->viewVars[$key])) {
230
            $this->_View->assign($key, $this->_View->viewVars[$key]);
231
        }
232
233
        return $this;
234
    }
235
}
236