Completed
Push — develop ( 64b26a...7a7e00 )
by Schlaefer
02:28
created

JsDataHelper::notifications()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\View\Helper;
14
15
use Cake\Core\Configure;
16
use Cake\Http\ServerRequest;
17
use Cake\View\Helper\UrlHelper;
18
use Cake\View\View;
19
use Saito\JsData\JsData;
20
use Saito\JsData\Notifications;
21
use Saito\User\ForumsUserInterface;
22
23
/**
24
 * Javascript Data Helper
25
 *
26
 * @property ServerRequest $request
27
 * @property UrlHelper $Url
28
 */
29
class JsDataHelper extends AppHelper
30
{
31
    public $helpers = ['Url'];
32
33
    /**
34
     * Notifications
35
     *
36
     * @var Notifications
37
     */
38
    protected $Notifications;
39
40
    /**
41
     * get app js
42
     *
43
     * @param View $View view
44
     * @param ForumsUserInterface $CurrentUser user
45
     * @return string
46
     */
47
    public function getAppJs(View $View, ForumsUserInterface $CurrentUser)
48
    {
49
        $request = $View->getRequest();
50
51
        $js = [
52
            'app' => [
53
                'version' => Configure::read('Saito.v'),
54
                'settings' => [
55
                    'autoPageReload' => (isset($View->viewVars['autoPageReload']) ? $View->viewVars['autoPageReload'] : 0),
0 ignored issues
show
Deprecated Code introduced by
The property Cake\View\ViewVarsTrait::$viewVars has been deprecated with message: 3.7.0 Use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
56
                    'editPeriod' => (int)Configure::read(
57
                        'Saito.Settings.edit_period'
58
                    ),
59
                    'language' => Configure::read('Saito.language'),
60
                    'notificationIcon' => $this->Url->assetUrl(
61
                        'html5-notification-icon.png',
62
                        [
63
                            'pathPrefix' => Configure::read('App.imageBaseUrl'),
64
                            'fullBase' => true
65
                        ]
66
                    ),
67
                    'theme' => $View->getTheme(),
68
                    'apiroot' => $request->getAttribute('webroot') . 'api/v2/',
69
                    'webroot' => $request->getAttribute('webroot')
70
                ]
71
            ],
72
            'msg' => $this->notifications()->getAll(),
73
            'request' => [
74
                'action' => $request->getParam('action'),
75
                'controller' => $request->getParam('controller'),
76
                'isMobile' => $request->is('mobile'),
77
                'isPreview' => $request->is('preview'),
78
                'csrf' => $this->_getCsrf($View)
79
            ],
80
            'currentUser' => [
81
                'id' => (int)$CurrentUser->get('id'),
82
                'username' => $CurrentUser->get('username'),
83
                'user_show_inline' => $CurrentUser->get('inline_view_on_click') ?: false,
84
                'user_show_thread_collapsed' => $CurrentUser->get('user_show_thread_collapsed') ?: false
85
            ],
86
            'callbacks' => [
87
                'beforeAppInit' => [],
88
                'afterAppInit' => [],
89
                'afterViewInit' => []
90
            ]
91
        ];
92
        $out = 'var SaitoApp = ' . json_encode($js);
93
        $out .= '; SaitoApp.timeAppStart = new Date().getTime();';
94
95
        return $out;
96
    }
97
98
    /**
99
     * Get CSRF-config
100
     *
101
     * @param View $View View
102
     * @return array
103
     * - 'header' HTTP header for CSRF-token
104
     * - 'token' CSRF-token
105
     */
106
    protected function _getCsrf(View $View)
107
    {
108
        $key = 'csrfToken';
109
        $token = $View->getRequest()->getCookie($key);
110
        if (empty($token)) {
111
            $token = $View->getResponse()->getCookie($key)['value'];
112
        }
113
        $header = 'X-CSRF-Token';
114
115
        return compact('header', 'token');
116
    }
117
118
    /**
119
     * Gets notifications
120
     *
121
     * @return Notifications The notifications.
122
     */
123
    public function notifications(): Notifications
124
    {
125
        if (empty($this->Notifications)) {
126
            $this->Notifications = new Notifications();
127
        }
128
129
        return $this->Notifications;
130
    }
131
}
132