GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f9fd4d...f5c98f )
by Robert
11:43
created

View::getAssetManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\ArrayHelper;
13
use yii\helpers\Html;
14
15
/**
16
 * View represents a view object in the MVC pattern.
17
 *
18
 * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
19
 *
20
 * View is configured as an application component in [[\yii\base\Application]] by default.
21
 * You can access that instance via `Yii::$app->view`.
22
 *
23
 * You can modify its configuration by adding an array to your application config under `components`
24
 * as it is shown in the following example:
25
 *
26
 * ```php
27
 * 'view' => [
28
 *     'theme' => 'app\themes\MyTheme',
29
 *     'renderers' => [
30
 *         // you may add Smarty or Twig renderer here
31
 *     ]
32
 *     // ...
33
 * ]
34
 * ```
35
 *
36
 * For more details and usage information on View, see the [guide article on views](guide:structure-views).
37
 *
38
 * @property \yii\web\AssetManager $assetManager The asset manager. Defaults to the "assetManager" application
39
 * component.
40
 *
41
 * @author Qiang Xue <[email protected]>
42
 * @since 2.0
43
 */
44
class View extends \yii\base\View
45
{
46
    /**
47
     * @event Event an event that is triggered by [[beginBody()]].
48
     */
49
    const EVENT_BEGIN_BODY = 'beginBody';
50
    /**
51
     * @event Event an event that is triggered by [[endBody()]].
52
     */
53
    const EVENT_END_BODY = 'endBody';
54
    /**
55
     * The location of registered JavaScript code block or files.
56
     * This means the location is in the head section.
57
     */
58
    const POS_HEAD = 1;
59
    /**
60
     * The location of registered JavaScript code block or files.
61
     * This means the location is at the beginning of the body section.
62
     */
63
    const POS_BEGIN = 2;
64
    /**
65
     * The location of registered JavaScript code block or files.
66
     * This means the location is at the end of the body section.
67
     */
68
    const POS_END = 3;
69
    /**
70
     * The location of registered JavaScript code block.
71
     * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`.
72
     */
73
    const POS_READY = 4;
74
    /**
75
     * The location of registered JavaScript code block.
76
     * This means the JavaScript code block will be enclosed within `jQuery(window).load()`.
77
     */
78
    const POS_LOAD = 5;
79
    /**
80
     * This is internally used as the placeholder for receiving the content registered for the head section.
81
     */
82
    const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>';
83
    /**
84
     * This is internally used as the placeholder for receiving the content registered for the beginning of the body section.
85
     */
86
    const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>';
87
    /**
88
     * This is internally used as the placeholder for receiving the content registered for the end of the body section.
89
     */
90
    const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>';
91
92
    /**
93
     * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values
94
     * are the registered [[AssetBundle]] objects.
95
     * @see registerAssetBundle()
96
     */
97
    public $assetBundles = [];
98
    /**
99
     * @var string the page title
100
     */
101
    public $title;
102
    /**
103
     * @var array the registered meta tags.
104
     * @see registerMetaTag()
105
     */
106
    public $metaTags = [];
107
    /**
108
     * @var array the registered link tags.
109
     * @see registerLinkTag()
110
     */
111
    public $linkTags = [];
112
    /**
113
     * @var array the registered CSS code blocks.
114
     * @see registerCss()
115
     */
116
    public $css = [];
117
    /**
118
     * @var array the registered CSS files.
119
     * @see registerCssFile()
120
     */
121
    public $cssFiles = [];
122
    /**
123
     * @var array the registered JS code blocks
124
     * @see registerJs()
125
     */
126
    public $js = [];
127
    /**
128
     * @var array the registered JS files.
129
     * @see registerJsFile()
130
     */
131
    public $jsFiles = [];
132
133
    private $_assetManager;
134
135
136
    /**
137
     * Marks the position of an HTML head section.
138
     */
139 46
    public function head()
140
    {
141 46
        echo self::PH_HEAD;
142 46
    }
143
144
    /**
145
     * Marks the beginning of an HTML body section.
146
     */
147 46
    public function beginBody()
148
    {
149 46
        echo self::PH_BODY_BEGIN;
150 46
        $this->trigger(self::EVENT_BEGIN_BODY);
151 46
    }
152
153
    /**
154
     * Marks the ending of an HTML body section.
155
     */
156 49
    public function endBody()
157
    {
158 49
        $this->trigger(self::EVENT_END_BODY);
159 49
        echo self::PH_BODY_END;
160
161 49
        foreach (array_keys($this->assetBundles) as $bundle) {
162 10
            $this->registerAssetFiles($bundle);
163
        }
164 49
    }
165
166
    /**
167
     * Marks the ending of an HTML page.
168
     * @param bool $ajaxMode whether the view is rendering in AJAX mode.
169
     * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
170
     * will be rendered at the end of the view like normal scripts.
171
     */
172 49
    public function endPage($ajaxMode = false)
173
    {
174 49
        $this->trigger(self::EVENT_END_PAGE);
175
176 49
        $content = ob_get_clean();
177
178 49
        echo strtr($content, [
179 49
            self::PH_HEAD => $this->renderHeadHtml(),
180 49
            self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(),
181 49
            self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode),
182
        ]);
183
184 49
        $this->clear();
185 49
    }
186
187
    /**
188
     * Renders a view in response to an AJAX request.
189
     *
190
     * This method is similar to [[render()]] except that it will surround the view being rendered
191
     * with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]].
192
     * By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files
193
     * that are registered with the view.
194
     *
195
     * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter.
196
     * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
197
     * @param object $context the context that the view should use for rendering the view. If null,
198
     * existing [[context]] will be used.
199
     * @return string the rendering result
200
     * @see render()
201
     */
202
    public function renderAjax($view, $params = [], $context = null)
203
    {
204
        $viewFile = $this->findViewFile($view, $context);
205
206
        ob_start();
207
        ob_implicit_flush(false);
208
209
        $this->beginPage();
210
        $this->head();
211
        $this->beginBody();
212
        echo $this->renderFile($viewFile, $params, $context);
0 ignored issues
show
Bug introduced by
It seems like $viewFile defined by $this->findViewFile($view, $context) on line 204 can also be of type boolean; however, yii\base\View::renderFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
213
        $this->endBody();
214
        $this->endPage(true);
215
216
        return ob_get_clean();
217
    }
218
219
    /**
220
     * Registers the asset manager being used by this view object.
221
     * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component.
222
     */
223 29
    public function getAssetManager()
224
    {
225 29
        return $this->_assetManager ?: Yii::$app->getAssetManager();
226
    }
227
228
    /**
229
     * Sets the asset manager.
230
     * @param \yii\web\AssetManager $value the asset manager
231
     */
232 72
    public function setAssetManager($value)
233
    {
234 72
        $this->_assetManager = $value;
235 72
    }
236
237
    /**
238
     * Clears up the registered meta tags, link tags, css/js scripts and files.
239
     */
240 49
    public function clear()
241
    {
242 49
        $this->metaTags = [];
243 49
        $this->linkTags = [];
244 49
        $this->css = [];
245 49
        $this->cssFiles = [];
246 49
        $this->js = [];
247 49
        $this->jsFiles = [];
248 49
        $this->assetBundles = [];
249 49
    }
250
251
    /**
252
     * Registers all files provided by an asset bundle including depending bundles files.
253
     * Removes a bundle from [[assetBundles]] once files are registered.
254
     * @param string $name name of the bundle to register
255
     */
256 10
    protected function registerAssetFiles($name)
257
    {
258 10
        if (!isset($this->assetBundles[$name])) {
259 8
            return;
260
        }
261 10
        $bundle = $this->assetBundles[$name];
262 10
        if ($bundle) {
263 10
            foreach ($bundle->depends as $dep) {
264 8
                $this->registerAssetFiles($dep);
265
            }
266 10
            $bundle->registerAssetFiles($this);
267
        }
268 10
        unset($this->assetBundles[$name]);
269 10
    }
270
271
    /**
272
     * Registers the named asset bundle.
273
     * All dependent asset bundles will be registered.
274
     * @param string $name the class name of the asset bundle (without the leading backslash)
275
     * @param int|null $position if set, this forces a minimum position for javascript files.
276
     * This will adjust depending assets javascript file position or fail if requirement can not be met.
277
     * If this is null, asset bundles position settings will not be changed.
278
     * See [[registerJsFile]] for more details on javascript position.
279
     * @return AssetBundle the registered asset bundle instance
280
     * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected
281
     */
282 28
    public function registerAssetBundle($name, $position = null)
283
    {
284 28
        if (!isset($this->assetBundles[$name])) {
285 27
            $am = $this->getAssetManager();
286 27
            $bundle = $am->getBundle($name);
287 27
            $this->assetBundles[$name] = false;
288
            // register dependencies
289 27
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
290 27
            foreach ($bundle->depends as $dep) {
291 16
                $this->registerAssetBundle($dep, $pos);
292
            }
293 26
            $this->assetBundles[$name] = $bundle;
294 14
        } elseif ($this->assetBundles[$name] === false) {
295 1
            throw new InvalidConfigException("A circular dependency is detected for bundle '$name'.");
296
        } else {
297 13
            $bundle = $this->assetBundles[$name];
298
        }
299
300 27
        if ($position !== null) {
301 12
            $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null;
302 12
            if ($pos === null) {
303 12
                $bundle->jsOptions['position'] = $pos = $position;
304 6
            } elseif ($pos > $position) {
305 6
                throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'.");
306
            }
307
            // update position for all dependencies
308 12
            foreach ($bundle->depends as $dep) {
309 6
                $this->registerAssetBundle($dep, $pos);
310
            }
311
        }
312
313 27
        return $bundle;
314
    }
315
316
    /**
317
     * Registers a meta tag.
318
     *
319
     * For example, a description meta tag can be added like the following:
320
     *
321
     * ```php
322
     * $view->registerMetaTag([
323
     *     'name' => 'description',
324
     *     'content' => 'This website is about funny raccoons.'
325
     * ]);
326
     * ```
327
     *
328
     * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`.
329
     *
330
     * @param array $options the HTML attributes for the meta tag.
331
     * @param string $key the key that identifies the meta tag. If two meta tags are registered
332
     * with the same key, the latter will overwrite the former. If this is null, the new meta tag
333
     * will be appended to the existing ones.
334
     */
335
    public function registerMetaTag($options, $key = null)
336
    {
337
        if ($key === null) {
338
            $this->metaTags[] = Html::tag('meta', '', $options);
339
        } else {
340
            $this->metaTags[$key] = Html::tag('meta', '', $options);
341
        }
342
    }
343
344
    /**
345
     * Registers CSRF meta tags.
346
     * They are rendered dynamically to retrieve a new CSRF token for each request.
347
     *
348
     * ```php
349
     * $view->registerCsrfMetaTags();
350
     * ```
351
     *
352
     * The above code will result in `<meta name="csrf-param" content="[yii\web\Request::$csrfParam]">`
353
     * and `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page.
354
     *
355
     * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()`
356
     * from `yii.js`.
357
     *
358
     * @since 2.0.13
359
     */
360 1
    public function registerCsrfMetaTags()
361
    {
362 1
        $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return yii\helpers\Html::csrfMetaTags();');
363 1
    }
364
365
    /**
366
     * Registers a link tag.
367
     *
368
     * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon)
369
     * can be added like the following:
370
     *
371
     * ```php
372
     * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']);
373
     * ```
374
     *
375
     * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`.
376
     *
377
     * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which
378
     * has more options for this kind of link tag.
379
     *
380
     * @param array $options the HTML attributes for the link tag.
381
     * @param string $key the key that identifies the link tag. If two link tags are registered
382
     * with the same key, the latter will overwrite the former. If this is null, the new link tag
383
     * will be appended to the existing ones.
384
     */
385
    public function registerLinkTag($options, $key = null)
386
    {
387
        if ($key === null) {
388
            $this->linkTags[] = Html::tag('link', '', $options);
389
        } else {
390
            $this->linkTags[$key] = Html::tag('link', '', $options);
391
        }
392
    }
393
394
    /**
395
     * Registers a CSS code block.
396
     * @param string $css the content of the CSS code block to be registered
397
     * @param array $options the HTML attributes for the `<style>`-tag.
398
     * @param string $key the key that identifies the CSS code block. If null, it will use
399
     * $css as the key. If two CSS code blocks are registered with the same key, the latter
400
     * will overwrite the former.
401
     */
402
    public function registerCss($css, $options = [], $key = null)
403
    {
404
        $key = $key ?: md5($css);
405
        $this->css[$key] = Html::style($css, $options);
406
    }
407
408
    /**
409
     * Registers a CSS file.
410
     *
411
     * This method should be used for simple registration of CSS files. If you want to use features of
412
     * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]]
413
     * and [[registerAssetBundle()]] instead.
414
     *
415
     * @param string $url the CSS file to be registered.
416
     * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for
417
     * the supported options. The following options are specially handled and are not treated as HTML attributes:
418
     *
419
     * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on.
420
     *
421
     * @param string $key the key that identifies the CSS script file. If null, it will use
422
     * $url as the key. If two CSS files are registered with the same key, the latter
423
     * will overwrite the former.
424
     */
425 19
    public function registerCssFile($url, $options = [], $key = null)
426
    {
427 19
        $url = Yii::getAlias($url);
428 19
        $key = $key ?: $url;
429
430 19
        $depends = ArrayHelper::remove($options, 'depends', []);
431
432 19
        if (empty($depends)) {
433 19
            $this->cssFiles[$key] = Html::cssFile($url, $options);
0 ignored issues
show
Bug introduced by
It seems like $url defined by \Yii::getAlias($url) on line 427 can also be of type boolean; however, yii\helpers\BaseHtml::cssFile() does only seem to accept array|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
434
        } else {
435
            $this->getAssetManager()->bundles[$key] = Yii::createObject([
436
                'class' => AssetBundle::className(),
437
                'baseUrl' => '',
438
                'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
439
                'cssOptions' => $options,
440
                'depends' => (array) $depends,
441
            ]);
442
            $this->registerAssetBundle($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by $key ?: $url on line 428 can also be of type boolean; however, yii\web\View::registerAssetBundle() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
443
        }
444 19
    }
445
446
    /**
447
     * Registers a JS code block.
448
     * @param string $js the JS code block to be registered
449
     * @param int $position the position at which the JS script tag should be inserted
450
     * in a page. The possible values are:
451
     *
452
     * - [[POS_HEAD]]: in the head section
453
     * - [[POS_BEGIN]]: at the beginning of the body section
454
     * - [[POS_END]]: at the end of the body section
455
     * - [[POS_LOAD]]: enclosed within jQuery(window).load().
456
     *   Note that by using this position, the method will automatically register the jQuery js file.
457
     * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value.
458
     *   Note that by using this position, the method will automatically register the jQuery js file.
459
     *
460
     * @param string $key the key that identifies the JS code block. If null, it will use
461
     * $js as the key. If two JS code blocks are registered with the same key, the latter
462
     * will overwrite the former.
463
     */
464 6
    public function registerJs($js, $position = self::POS_READY, $key = null)
465
    {
466 6
        $key = $key ?: md5($js);
467 6
        $this->js[$position][$key] = $js;
468 6
        if ($position === self::POS_READY || $position === self::POS_LOAD) {
469 6
            JqueryAsset::register($this);
470
        }
471 6
    }
472
473
    /**
474
     * Registers a JS file.
475
     *
476
     * This method should be used for simple registration of JS files. If you want to use features of
477
     * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]]
478
     * and [[registerAssetBundle()]] instead.
479
     *
480
     * @param string $url the JS file to be registered.
481
     * @param array $options the HTML attributes for the script tag. The following options are specially handled
482
     * and are not treated as HTML attributes:
483
     *
484
     * - `depends`: array, specifies the names of the asset bundles that this JS file depends on.
485
     * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are:
486
     *     * [[POS_HEAD]]: in the head section
487
     *     * [[POS_BEGIN]]: at the beginning of the body section
488
     *     * [[POS_END]]: at the end of the body section. This is the default value.
489
     *
490
     * Please refer to [[Html::jsFile()]] for other supported options.
491
     *
492
     * @param string $key the key that identifies the JS script file. If null, it will use
493
     * $url as the key. If two JS files are registered with the same key at the same position, the latter
494
     * will overwrite the former. Note that position option takes precedence, thus files registered with the same key,
495
     * but different position option will not override each other.
496
     */
497 21
    public function registerJsFile($url, $options = [], $key = null)
498
    {
499 21
        $url = Yii::getAlias($url);
500 21
        $key = $key ?: $url;
501
502 21
        $depends = ArrayHelper::remove($options, 'depends', []);
503
504 21
        if (empty($depends)) {
505 21
            $position = ArrayHelper::remove($options, 'position', self::POS_END);
506 21
            $this->jsFiles[$position][$key] = Html::jsFile($url, $options);
0 ignored issues
show
Bug introduced by
It seems like $url defined by \Yii::getAlias($url) on line 499 can also be of type boolean; however, yii\helpers\BaseHtml::jsFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
507
        } else {
508
            $this->getAssetManager()->bundles[$key] = Yii::createObject([
509
                'class' => AssetBundle::className(),
510
                'baseUrl' => '',
511
                'js' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')],
512
                'jsOptions' => $options,
513
                'depends' => (array) $depends,
514
            ]);
515
            $this->registerAssetBundle($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by $key ?: $url on line 500 can also be of type boolean; however, yii\web\View::registerAssetBundle() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
516
        }
517 21
    }
518
519
    /**
520
     * Renders the content to be inserted in the head section.
521
     * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files.
522
     * @return string the rendered content
523
     */
524 49
    protected function renderHeadHtml()
525
    {
526 49
        $lines = [];
527 49
        if (!empty($this->metaTags)) {
528 1
            $lines[] = implode("\n", $this->metaTags);
529
        }
530
531 49
        if (!empty($this->linkTags)) {
532
            $lines[] = implode("\n", $this->linkTags);
533
        }
534 49
        if (!empty($this->cssFiles)) {
535 19
            $lines[] = implode("\n", $this->cssFiles);
536
        }
537 49
        if (!empty($this->css)) {
538
            $lines[] = implode("\n", $this->css);
539
        }
540 49
        if (!empty($this->jsFiles[self::POS_HEAD])) {
541 3
            $lines[] = implode("\n", $this->jsFiles[self::POS_HEAD]);
542
        }
543 49
        if (!empty($this->js[self::POS_HEAD])) {
544
            $lines[] = Html::script(implode("\n", $this->js[self::POS_HEAD]), ['type' => 'text/javascript']);
545
        }
546
547 49
        return empty($lines) ? '' : implode("\n", $lines);
548
    }
549
550
    /**
551
     * Renders the content to be inserted at the beginning of the body section.
552
     * The content is rendered using the registered JS code blocks and files.
553
     * @return string the rendered content
554
     */
555 49
    protected function renderBodyBeginHtml()
556
    {
557 49
        $lines = [];
558 49
        if (!empty($this->jsFiles[self::POS_BEGIN])) {
559 3
            $lines[] = implode("\n", $this->jsFiles[self::POS_BEGIN]);
560
        }
561 49
        if (!empty($this->js[self::POS_BEGIN])) {
562
            $lines[] = Html::script(implode("\n", $this->js[self::POS_BEGIN]), ['type' => 'text/javascript']);
563
        }
564
565 49
        return empty($lines) ? '' : implode("\n", $lines);
566
    }
567
568
    /**
569
     * Renders the content to be inserted at the end of the body section.
570
     * The content is rendered using the registered JS code blocks and files.
571
     * @param bool $ajaxMode whether the view is rendering in AJAX mode.
572
     * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
573
     * will be rendered at the end of the view like normal scripts.
574
     * @return string the rendered content
575
     */
576 49
    protected function renderBodyEndHtml($ajaxMode)
577
    {
578 49
        $lines = [];
579
580 49
        if (!empty($this->jsFiles[self::POS_END])) {
581 17
            $lines[] = implode("\n", $this->jsFiles[self::POS_END]);
582
        }
583
584 49
        if ($ajaxMode) {
585
            $scripts = [];
586
            if (!empty($this->js[self::POS_END])) {
587
                $scripts[] = implode("\n", $this->js[self::POS_END]);
588
            }
589
            if (!empty($this->js[self::POS_READY])) {
590
                $scripts[] = implode("\n", $this->js[self::POS_READY]);
591
            }
592
            if (!empty($this->js[self::POS_LOAD])) {
593
                $scripts[] = implode("\n", $this->js[self::POS_LOAD]);
594
            }
595
            if (!empty($scripts)) {
596
                $lines[] = Html::script(implode("\n", $scripts), ['type' => 'text/javascript']);
597
            }
598
        } else {
599 49
            if (!empty($this->js[self::POS_END])) {
600
                $lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']);
601
            }
602 49
            if (!empty($this->js[self::POS_READY])) {
603
                $js = "jQuery(function ($) {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
604
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
605
            }
606 49
            if (!empty($this->js[self::POS_LOAD])) {
607
                $js = "jQuery(function ($) {\n$(window).on('load', function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});\n});";
608
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
609
            }
610
        }
611
612 49
        return empty($lines) ? '' : implode("\n", $lines);
613
    }
614
}
615