Passed
Push — master ( ecaee8...5e3cc7 )
by Stefano
03:14 queued 23s
created

LayoutHelper::getCsrfToken()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
rs 9.6111
cc 5
nc 5
nop 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2018 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\View\Helper;
14
15
use App\Utility\Translate;
16
use Cake\Core\Configure;
17
use Cake\Utility\Hash;
18
use Cake\View\Helper;
19
20
/**
21
 * Helper for site layout
22
 *
23
 * @property \Cake\View\Helper\HtmlHelper $Html
24
 */
25
class LayoutHelper extends Helper
26
{
27
    /**
28
     * List of helpers used by this helper
29
     *
30
     * @var array
31
     */
32
    public $helpers = ['Html', 'Link'];
33
34
    /**
35
     * Is Dashboard
36
     *
37
     * @return bool True if visible for view
38
     */
39
    public function isDashboard(): bool
40
    {
41
        return in_array($this->_View->getName(), ['Dashboard']);
42
    }
43
44
    /**
45
     * Is Login
46
     *
47
     * @return bool True if visible for view
48
     */
49
    public function isLogin(): bool
50
    {
51
        return in_array($this->_View->getName(), ['Login']);
52
    }
53
54
    /**
55
     * Properties for various publication status
56
     *
57
     * @param array $object The object
58
     * @return string pubstatus
59
     */
60
    public function publishStatus(array $object = []): string
61
    {
62
        if (empty($object)) {
63
            return '';
64
        }
65
66
        $end = (string)Hash::get($object, 'attributes.publish_end');
67
        $start = (string)Hash::get($object, 'attributes.publish_start');
68
69
        if (!empty($end) && strtotime($end) <= time()) {
70
            return 'expired';
71
        }
72
        if (!empty($start) && strtotime($start) > time()) {
73
            return 'future';
74
        }
75
        if (!empty((string)Hash::get($object, 'meta.locked'))) {
76
            return 'locked';
77
        }
78
        if ((string)Hash::get($object, 'attributes.status') === 'draft') {
79
            return 'draft';
80
        }
81
82
        return '';
83
    }
84
85
    /**
86
     * Messages visibility
87
     *
88
     * @return bool True if visible for view
89
     */
90
    public function messages(): bool
91
    {
92
        return $this->_View->getName() != 'Login';
93
    }
94
95
    /**
96
     * Module main link
97
     *
98
     * @return string The link
99
     */
100
    public function moduleLink(): string
101
    {
102
        $currentModule = (array)$this->getView()->get('currentModule');
103
        if (!empty($currentModule) && !empty($currentModule['name'])) {
104
            $name = $currentModule['name'];
105
            $label = Hash::get($currentModule, 'label', $name);
106
107
            return $this->Html->link(
108
                $this->tr($label),
109
                ['_name' => 'modules:list', 'object_type' => $name],
110
                ['class' => sprintf('has-background-module-%s', $name)]
111
            );
112
        }
113
114
        // if no `currentModule` has been set a `moduleLink` must be set in controller otherwise current link is displayed
115
        return $this->Html->link(
116
            $this->tr($this->getView()->getName()),
117
            (array)$this->getView()->get('moduleLink'),
118
            ['class' => $this->commandLinkClass()]
119
        );
120
    }
121
122
    /**
123
     * Return style class for command link
124
     *
125
     * @return string
126
     */
127
    protected function commandLinkClass(): string
128
    {
129
        $moduleClasses = [
130
            'UserProfile' => 'has-background-black icon-user',
131
            'Import' => 'has-background-black icon-download-alt',
132
            'ObjectTypes' => 'has-background-black',
133
            'Relations' => 'has-background-black',
134
            'PropertyTypes' => 'has-background-black',
135
            'Categories' => 'has-background-black',
136
            'Applications' => 'has-background-black',
137
            'AsyncJobs' => 'has-background-black',
138
            'Config' => 'has-background-black',
139
            'Endpoints' => 'has-background-black',
140
            'Roles' => 'has-background-black',
141
        ];
142
143
        return (string)Hash::get($moduleClasses, $this->_View->getName(), 'commands-menu__module');
144
    }
145
146
    /**
147
     * Return custom element via `Properties` configuration for
148
     * a relation or property group in current module.
149
     *
150
     * @param string $item Relation or group name
151
     * @param string $type Item type: `relation` or `group`
152
     * @return string
153
     */
154
    public function customElement(string $item, string $type = 'relation'): string
155
    {
156
        $currentModule = (array)$this->getView()->get('currentModule');
157
        $name = (string)Hash::get($currentModule, 'name');
158
        if ($type === 'relation') {
159
            $path = sprintf('Properties.%s.relations._element.%s', $name, $item);
160
        } else {
161
            $path = sprintf('Properties.%s.view.%s._element', $name, $item);
162
        }
163
164
        return (string)Configure::read($path);
165
    }
166
167
    /**
168
     * Get translated val by input string, using plugins (if any) translations.
169
     *
170
     * @param string $input The input string
171
     * @return string|null
172
     */
173
    public function tr(string $input): ?string
174
    {
175
        return Translate::get($input);
176
    }
177
178
    /**
179
     * Return configuration items to create JSON BEDITA object
180
     *
181
     * @return array
182
     */
183
    public function metaConfig(): array
184
    {
185
        return [
186
            'base' => $this->Link->baseUrl(),
187
            'currentModule' => $this->getView()->get('currentModule', ['name' => 'home']),
188
            'template' => $this->getView()->getTemplate(),
189
            'modules' => array_keys($this->getView()->get('modules', [])),
190
            'plugins' => \App\Plugin::loadedAppPlugins(),
191
            'uploadable' => $this->getView()->get('uploadable', []),
192
            'locale' => \Cake\I18n\I18n::getLocale(),
193
            'csrfToken' => $this->getCsrfToken(),
194
        ];
195
    }
196
197
    /**
198
     * Get csrf token, searching in: request params, data, attributes and cookies
199
     *
200
     * @return string|null
201
     */
202
    public function getCsrfToken(): ?string
203
    {
204
        if (!empty($this->getView()->getRequest()->getParam('_csrfToken'))) {
205
            return $this->getView()->getRequest()->getParam('_csrfToken');
206
        }
207
        if (!empty($this->getView()->getRequest()->getData('_csrfToken'))) {
208
            return $this->getView()->getRequest()->getData('_csrfToken');
209
        }
210
        if (!empty($this->getView()->getRequest()->getAttribute('csrfToken'))) {
211
            return $this->getView()->getRequest()->getAttribute('csrfToken');
212
        }
213
        if (!empty($this->getView()->getRequest()->getCookie('csrfToken'))) {
214
            return $this->getView()->getRequest()->getCookie('csrfToken');
215
        }
216
217
        return null;
218
    }
219
}
220