|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2019 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\Controller\Model; |
|
14
|
|
|
|
|
15
|
|
|
use BEdita\SDK\BEditaClientException; |
|
16
|
|
|
use Cake\Http\Response; |
|
17
|
|
|
use Cake\Utility\Hash; |
|
18
|
|
|
use Psr\Log\LogLevel; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Object Types Model Controller: list, add, edit, remove object types |
|
22
|
|
|
* |
|
23
|
|
|
* @property \App\Controller\Component\PropertiesComponent $Properties |
|
24
|
|
|
*/ |
|
25
|
|
|
class ObjectTypesController extends ModelBaseController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Resource type currently used |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $resourceType = 'object_types'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritDoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function view($id): ?Response |
|
38
|
|
|
{ |
|
39
|
|
|
parent::view($id); |
|
40
|
|
|
|
|
41
|
|
|
// retrieve additional data |
|
42
|
|
|
$resource = (array)$this->viewBuilder()->getVar('resource'); |
|
43
|
|
|
$name = Hash::get($resource, 'attributes.name', 'undefined'); |
|
44
|
|
|
$filter = ['object_type' => $name]; |
|
45
|
|
|
try { |
|
46
|
|
|
$response = $this->apiClient->get( |
|
47
|
|
|
'/model/properties', |
|
48
|
|
|
compact('filter') + ['page_size' => 100] |
|
49
|
|
|
); |
|
50
|
|
|
} catch (BEditaClientException $e) { |
|
51
|
|
|
$this->log($e->getMessage(), LogLevel::ERROR); |
|
52
|
|
|
$this->Flash->error($e->getMessage(), ['params' => $e]); |
|
53
|
|
|
|
|
54
|
|
|
return $this->redirect(['_name' => 'model:list:' . $this->resourceType]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$objectTypeProperties = $this->prepareProperties((array)$response['data'], $name); |
|
58
|
|
|
$this->set(compact('objectTypeProperties')); |
|
59
|
|
|
$this->set('schema', $this->Schema->getSchema()); |
|
60
|
|
|
$this->set('properties', $this->Properties->viewGroups($resource, $this->resourceType)); |
|
61
|
|
|
|
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Separate properties between `inherited`, `core` and `custom` |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $data Property array |
|
69
|
|
|
* @param string $name Object type name |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function prepareProperties(array $data, string $name): array |
|
73
|
|
|
{ |
|
74
|
|
|
$inherited = $core = $custom = []; |
|
75
|
|
|
foreach ($data as $prop) { |
|
76
|
|
|
if (!is_numeric($prop['id'])) { |
|
77
|
|
|
$type = $prop['attributes']['object_type_name']; |
|
78
|
|
|
if ($type == $name) { |
|
79
|
|
|
$core[] = $prop; |
|
80
|
|
|
} else { |
|
81
|
|
|
$inherited[] = $prop; |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$custom[] = $prop; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return compact('inherited', 'core', 'custom'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Save object type. |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Cake\Http\Response|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function save(): ?Response |
|
97
|
|
|
{ |
|
98
|
|
|
$this->addCustomProperty(); |
|
99
|
|
|
$this->request = $this->request->withoutData('prop_name') |
|
100
|
|
|
->withoutData('prop_type'); |
|
101
|
|
|
|
|
102
|
|
|
return parent::save(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Add custom property |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function addCustomProperty(): void |
|
111
|
|
|
{ |
|
112
|
|
|
$name = $this->request->getData('prop_name'); |
|
113
|
|
|
$type = $this->request->getData('prop_type'); |
|
114
|
|
|
if (empty($name) || empty($type)) { |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$data = [ |
|
119
|
|
|
'type' => 'properties', |
|
120
|
|
|
'attributes' => compact('name') + [ |
|
121
|
|
|
'property_type_name' => $type, |
|
122
|
|
|
'object_type_name' => $this->request->getData('name'), |
|
123
|
|
|
], |
|
124
|
|
|
]; |
|
125
|
|
|
$this->apiClient->post('/model/properties', json_encode(compact('data'))); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|