|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2022 Atlas 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; |
|
14
|
|
|
|
|
15
|
|
|
use BEdita\SDK\BEditaClientException; |
|
16
|
|
|
use Cake\Http\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Categories Controller: list, save, delete categories |
|
20
|
|
|
* |
|
21
|
|
|
* @property \App\Controller\Component\CategoriesComponent $Categories |
|
22
|
|
|
* @property \App\Controller\Component\PropertiesComponent $Properties |
|
23
|
|
|
*/ |
|
24
|
|
|
class CategoriesController extends AppController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Object type currently used |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $objectType = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritDoc |
|
35
|
|
|
*/ |
|
36
|
|
|
public function initialize(): void |
|
37
|
|
|
{ |
|
38
|
|
|
parent::initialize(); |
|
39
|
|
|
|
|
40
|
|
|
$this->loadComponent('Categories'); |
|
41
|
|
|
$this->loadComponent('Properties'); |
|
42
|
|
|
if ($this->getRequest()->getParam('object_type')) { |
|
43
|
|
|
$this->objectType = $this->getRequest()->getParam('object_type'); |
|
44
|
|
|
$this->Modules->setConfig('currentModuleName', $this->objectType); |
|
45
|
|
|
$this->Schema->setConfig('type', $this->objectType); |
|
46
|
|
|
} |
|
47
|
|
|
$this->Security->setConfig('unlockedActions', ['delete', 'save']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* List categories for the object type. |
|
52
|
|
|
* |
|
53
|
|
|
* @return \Cake\Http\Response|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public function index(): ?Response |
|
56
|
|
|
{ |
|
57
|
|
|
$this->getRequest()->allowMethod(['get']); |
|
58
|
|
|
$response = $this->Categories->index($this->objectType, $this->getRequest()->getQueryParams()); |
|
59
|
|
|
$resources = $this->Categories->map($response); |
|
60
|
|
|
$roots = $this->Categories->getAvailableRoots($resources); |
|
61
|
|
|
$categoriesTree = $this->Categories->tree($resources); |
|
62
|
|
|
$names = [$this->objectType => $this->Categories->names($this->objectType)]; |
|
63
|
|
|
|
|
64
|
|
|
$this->set(compact('resources', 'roots', 'categoriesTree', 'names')); |
|
65
|
|
|
$this->set('meta', (array)$response['meta']); |
|
66
|
|
|
$this->set('links', (array)$response['links']); |
|
67
|
|
|
$this->set('schema', $this->Schema->getSchema($this->objectType)); |
|
68
|
|
|
$this->set('properties', $this->Properties->indexList('categories')); |
|
69
|
|
|
$this->set('filter', $this->Properties->filterList('categories')); |
|
70
|
|
|
$this->set('object_types', [$this->objectType]); |
|
71
|
|
|
$this->set('objectType', $this->objectType); |
|
72
|
|
|
|
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Save category. |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Cake\Http\Response|null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function save(): ?Response |
|
82
|
|
|
{ |
|
83
|
|
|
$this->getRequest()->allowMethod(['post']); |
|
84
|
|
|
$this->viewBuilder()->setClassName('Json'); |
|
85
|
|
|
$response = $error = null; |
|
86
|
|
|
try { |
|
87
|
|
|
$data = (array)$this->getRequest()->getData(); |
|
88
|
|
|
$data['enabled'] = $data['enabled'] === 'true'; |
|
89
|
|
|
$response = $this->Categories->save($data); |
|
90
|
|
|
} catch (BEditaClientException $e) { |
|
91
|
|
|
$error = $e->getMessage(); |
|
92
|
|
|
$this->log($error, 'error'); |
|
93
|
|
|
$this->set('error', $error); |
|
94
|
|
|
} |
|
95
|
|
|
$this->set('response', $response); |
|
96
|
|
|
$this->set('error', $error); |
|
97
|
|
|
$this->setSerialize(['response', 'error']); |
|
98
|
|
|
|
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Remove single category. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $id Category ID. |
|
106
|
|
|
* @return \Cake\Http\Response|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function delete(string $id): ?Response |
|
109
|
|
|
{ |
|
110
|
|
|
$this->getRequest()->allowMethod(['post']); |
|
111
|
|
|
$this->viewBuilder()->setClassName('Json'); |
|
112
|
|
|
$response = $error = null; |
|
113
|
|
|
try { |
|
114
|
|
|
$type = $this->getRequest()->getData('object_type_name'); |
|
115
|
|
|
$response = $this->Categories->delete($id, $type); |
|
116
|
|
|
} catch (BEditaClientException $e) { |
|
117
|
|
|
$error = $e->getMessage(); |
|
118
|
|
|
$this->log($error, 'error'); |
|
119
|
|
|
$this->set('error', $error); |
|
120
|
|
|
} |
|
121
|
|
|
$this->set('response', $response); |
|
122
|
|
|
$this->set('error', $error); |
|
123
|
|
|
$this->setSerialize(['response', 'error']); |
|
124
|
|
|
|
|
125
|
|
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|