Passed
Pull Request — master (#708)
by Stefano
03:44
created

LayoutHelper::metaConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 14
c 1
b 1
f 0
dl 0
loc 18
rs 9.7998
cc 3
nc 3
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\Utility\Inflector;
19
use Cake\View\Helper;
20
21
/**
22
 * Helper for site layout
23
 *
24
 * @property \Cake\View\Helper\HtmlHelper $Html
25
 */
26
class LayoutHelper extends Helper
27
{
28
    /**
29
     * List of helpers used by this helper
30
     *
31
     * @var array
32
     */
33
    public $helpers = ['Html', 'Link'];
34
35
    /**
36
     * Is Dashboard
37
     *
38
     * @return bool True if visible for view
39
     */
40
    public function isDashboard(): bool
41
    {
42
        return in_array($this->_View->getName(), ['Dashboard']);
43
    }
44
45
    /**
46
     * Is Login
47
     *
48
     * @return bool True if visible for view
49
     */
50
    public function isLogin(): bool
51
    {
52
        return in_array($this->_View->getName(), ['Login']);
53
    }
54
55
    /**
56
     * Properties for various publication status
57
     *
58
     * @param array $object The object
59
     * @return string pubstatus
60
     */
61
    public function publishStatus(array $object = []): string
62
    {
63
        if (empty($object)) {
64
            return '';
65
        }
66
67
        $end = (string)Hash::get($object, 'attributes.publish_end');
68
        $start = (string)Hash::get($object, 'attributes.publish_start');
69
70
        if (!empty($end) && strtotime($end) <= time()) {
71
            return 'expired';
72
        }
73
        if (!empty($start) && strtotime($start) > time()) {
74
            return 'future';
75
        }
76
        if (!empty((string)Hash::get($object, 'meta.locked'))) {
77
            return 'locked';
78
        }
79
        if ((string)Hash::get($object, 'attributes.status') === 'draft') {
80
            return 'draft';
81
        }
82
83
        return '';
84
    }
85
86
    /**
87
     * Messages visibility
88
     *
89
     * @return bool True if visible for view
90
     */
91
    public function messages(): bool
92
    {
93
        return $this->_View->getName() != 'Login';
94
    }
95
96
    /**
97
     * Module main link
98
     *
99
     * @return string The link
100
     */
101
    public function moduleLink(): string
102
    {
103
        $currentModule = (array)$this->getView()->get('currentModule');
104
        if (!empty($currentModule) && !empty($currentModule['name'])) {
105
            $name = $currentModule['name'];
106
            $label = Hash::get($currentModule, 'label', $name);
107
108
            return $this->Html->link(
109
                $this->tr($label),
110
                ['_name' => 'modules:list', 'object_type' => $name],
111
                ['class' => sprintf('has-background-module-%s', $name)]
112
            );
113
        }
114
115
        // if no `currentModule` has been set a `moduleLink` must be set in controller otherwise current link is displayed
116
        return $this->Html->link(
117
            $this->tr($this->getView()->getName()),
118
            (array)$this->getView()->get('moduleLink'),
119
            ['class' => $this->commandLinkClass()]
120
        );
121
    }
122
123
    /**
124
     * Return style class for command link
125
     *
126
     * @return string
127
     */
128
    protected function commandLinkClass(): string
129
    {
130
        $moduleClasses = [
131
            'UserProfile' => 'has-background-black icon-user',
132
            'Import' => 'has-background-black icon-download-alt',
133
            'ObjectTypes' => 'has-background-black',
134
            'Relations' => 'has-background-black',
135
            'PropertyTypes' => 'has-background-black',
136
            'Categories' => 'has-background-black',
137
            'Applications' => 'has-background-black',
138
            'AsyncJobs' => 'has-background-black',
139
            'Config' => 'has-background-black',
140
            'Endpoints' => 'has-background-black',
141
            'Roles' => 'has-background-black',
142
        ];
143
144
        return (string)Hash::get($moduleClasses, $this->_View->getName(), 'commands-menu__module');
145
    }
146
147
    /**
148
     * Return custom element via `Properties` configuration for
149
     * a relation or property group in current module.
150
     *
151
     * @param string $item Relation or group name
152
     * @param string $type Item type: `relation` or `group`
153
     * @return string
154
     */
155
    public function customElement(string $item, string $type = 'relation'): string
156
    {
157
        $currentModule = (array)$this->getView()->get('currentModule');
158
        $name = (string)Hash::get($currentModule, 'name');
159
        if ($type === 'relation') {
160
            $path = sprintf('Properties.%s.relations._element.%s', $name, $item);
161
        } else {
162
            $path = sprintf('Properties.%s.view.%s._element', $name, $item);
163
        }
164
165
        return (string)Configure::read($path);
166
    }
167
168
    /**
169
     * Get translated val by input string, using plugins (if any) translations.
170
     *
171
     * @param string $input The input string
172
     * @return string|null
173
     */
174
    public function tr(string $input): ?string
175
    {
176
        return Translate::get($input);
177
    }
178
179
    /**
180
     * Return configuration items to create JSON BEDITA object
181
     *
182
     * @return array
183
     */
184
    public function metaConfig(): array
185
    {
186
        $csrfToken = null;
187
        if (!empty($this->getView()->getRequest()->getParam('_csrfToken'))) {
188
            $csrfToken = $this->getView()->getRequest()->getParam('_csrfToken');
189
        } elseif (!empty($this->getView()->getRequest()->getData('_csrfToken'))) {
190
            $csrfToken = $this->getView()->getRequest()->getData('_csrfToken');
191
        }
192
193
        return [
194
            'base' => $this->Link->baseUrl(),
195
            'currentModule' => $this->getView()->get('currentModule', ['name' => 'home']),
196
            'template' => $this->getView()->getTemplate(),
197
            'modules' => array_keys($this->getView()->get('modules', [])),
198
            'plugins' => \App\Plugin::loadedAppPlugins(),
199
            'uploadable' => $this->getView()->get('uploadable', []),
200
            'locale' => \Cake\I18n\I18n::getLocale(),
201
            'csrfToken' => $csrfToken,
202
        ];
203
    }
204
}
205