1
|
|
|
<?php |
2
|
|
|
namespace App\Controller\Component; |
3
|
|
|
|
4
|
|
|
use App\Utility\CacheTools; |
5
|
|
|
use BEdita\WebTools\ApiClientProvider; |
6
|
|
|
use Cake\Cache\Cache; |
7
|
|
|
use Cake\Controller\Component; |
8
|
|
|
use Cake\Utility\Hash; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Categories component |
12
|
|
|
*/ |
13
|
|
|
class CategoriesComponent extends Component |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Fetch categories list. |
17
|
|
|
* |
18
|
|
|
* @param string|null $objectType The object type filter for categories. |
19
|
|
|
* @param array|null $options Query options. |
20
|
|
|
* @return array The BEdita API response for the categories list. |
21
|
|
|
*/ |
22
|
|
|
public function index(?string $objectType = null, ?array $options = []) |
23
|
|
|
{ |
24
|
|
|
$apiClient = ApiClientProvider::getApiClient(); |
25
|
|
|
|
26
|
|
|
$options = $options + [ |
27
|
|
|
'page_size' => 100, |
28
|
|
|
]; |
29
|
|
|
if (!empty($objectType)) { |
30
|
|
|
$options['filter'] = $options['filter'] ?? []; |
31
|
|
|
$options['filter']['type'] = $objectType; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $apiClient->get('/model/categories', $options); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a key/value map of categories from the BEdita categories list response. |
39
|
|
|
* |
40
|
|
|
* @param array $response The BEdita API response for the categories list. |
41
|
|
|
* @return array A map with the category ids as keys and the category attributes as values. |
42
|
|
|
*/ |
43
|
|
|
public function map(?array $response) |
44
|
|
|
{ |
45
|
|
|
return (array)Hash::combine((array)$response['data'], '{n}.id', '{n}'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create an id-based categories tree. |
50
|
|
|
* |
51
|
|
|
* @param array $map The categories map returned by the map function. |
52
|
|
|
* @return array The categories tree. |
53
|
|
|
*/ |
54
|
|
|
public function tree(?array $map) |
55
|
|
|
{ |
56
|
|
|
$tree = [ |
57
|
|
|
'_' => [], |
58
|
|
|
]; |
59
|
|
|
foreach ($map as $category) { |
60
|
|
|
if (empty($category['attributes']['parent_id'])) { |
61
|
|
|
$tree['_'][] = $category['id']; |
62
|
|
|
} else { |
63
|
|
|
$tree[$category['attributes']['parent_id']][] = $category['id']; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $tree; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get an id/label map of available category roots. |
72
|
|
|
* |
73
|
|
|
* @param array $map The categories map returned by the map function. |
74
|
|
|
* @return array The list of available roots. |
75
|
|
|
*/ |
76
|
|
|
public function getAvailableRoots(?array $map) |
77
|
|
|
{ |
78
|
|
|
$roots = ['' => '-']; |
79
|
|
|
foreach ($map as $category) { |
80
|
|
|
if (empty($category['attributes']['parent_id'])) { |
81
|
|
|
$roots[$category['id']] = empty($category['attributes']['label']) ? $category['attributes']['name'] : $category['attributes']['label']; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $roots; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Save a category using the `/model/` API. |
90
|
|
|
* |
91
|
|
|
* @param array $data Data to save. |
92
|
|
|
* @return array The BEdita API response for the saved category. |
93
|
|
|
*/ |
94
|
|
|
public function save(array $data) |
95
|
|
|
{ |
96
|
|
|
$id = Hash::get($data, 'id'); |
97
|
|
|
$type = Hash::get($data, 'object_type_name'); |
98
|
|
|
unset($data['id']); |
99
|
|
|
$body = [ |
100
|
|
|
'data' => [ |
101
|
|
|
'type' => 'categories', |
102
|
|
|
'attributes' => $data, |
103
|
|
|
], |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$apiClient = ApiClientProvider::getApiClient(); |
107
|
|
|
$endpoint = sprintf('/model/%s', 'categories'); |
108
|
|
|
$response = null; |
109
|
|
|
if (empty($id)) { |
110
|
|
|
$response = $apiClient->post($endpoint, json_encode($body)); |
111
|
|
|
} else { |
112
|
|
|
$body['data']['id'] = $id; |
113
|
|
|
|
114
|
|
|
$response = $apiClient->patch(sprintf('%s/%s', $endpoint, $id), json_encode($body)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!empty($type)) { |
118
|
|
|
$this->invalidateSchemaCache($type); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $response; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Delete a category using the `/model/` API. |
126
|
|
|
* |
127
|
|
|
* @param string|int $id The category id to delete. |
128
|
|
|
* @param string $type The object type name of the category. |
129
|
|
|
* @return array The BEdita API response for the deleted category. |
130
|
|
|
*/ |
131
|
|
|
public function delete(string $id, $type = null) |
132
|
|
|
{ |
133
|
|
|
$apiClient = ApiClientProvider::getApiClient(); |
134
|
|
|
|
135
|
|
|
$response = $apiClient->delete(sprintf('/model/%s/%s', 'categories', $id)); |
136
|
|
|
if (!empty($type)) { |
137
|
|
|
$this->invalidateSchemaCache($type); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $response; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Invalidate schema cache for forms. |
145
|
|
|
* |
146
|
|
|
* @param string $type The object type name of the category. |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
private function invalidateSchemaCache(string $type): void |
150
|
|
|
{ |
151
|
|
|
$key = CacheTools::cacheKey($type); |
152
|
|
|
Cache::delete($key, SchemaComponent::CACHE_CONFIG); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|