Completed
Push — master ( 2c9d27...9c0bb9 )
by Cheren
03:04
created

DocumentHelper::afterRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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