1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace App\Model\Table; |
14
|
|
|
|
15
|
|
|
use App\Lib\Model\Table\AppSettingTable; |
16
|
|
|
use Cake\Datasource\Exception\RecordNotFoundException; |
17
|
|
|
use Cake\Datasource\ResultSetInterface; |
18
|
|
|
use Cake\ORM\TableRegistry; |
19
|
|
|
use Cake\Validation\Validator; |
20
|
|
|
use Saito\App\Registry; |
21
|
|
|
use Saito\RememberTrait; |
22
|
|
|
use Saito\User\Permission\Permissions; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class CategoriesTable |
26
|
|
|
* |
27
|
|
|
* @package App\Model\Table |
28
|
|
|
*/ |
29
|
|
|
class CategoriesTable extends AppSettingTable |
30
|
|
|
{ |
31
|
|
|
use RememberTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function initialize(array $config) |
37
|
|
|
{ |
38
|
|
|
$this->hasMany('Entries', ['foreignKey' => 'category_id']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritDoc} |
43
|
|
|
*/ |
44
|
|
|
public function validationDefault(Validator $validator) |
45
|
|
|
{ |
46
|
|
|
$validator |
|
|
|
|
47
|
|
|
->allowEmpty('category_order', 'create') |
48
|
|
|
->add( |
49
|
|
|
'category_order', |
50
|
|
|
[ |
51
|
|
|
'isNumeric' => ['rule' => 'numeric'] |
52
|
|
|
] |
53
|
|
|
) |
54
|
|
|
->add( |
55
|
|
|
'accession', |
56
|
|
|
'validateRoleExists', |
57
|
|
|
[ |
58
|
|
|
'rule' => [$this, 'validateRoleExists'], |
59
|
|
|
] |
60
|
|
|
) |
61
|
|
|
->add( |
62
|
|
|
'accession_new_thread', |
63
|
|
|
'validateRoleExists', |
64
|
|
|
[ |
65
|
|
|
'rule' => [$this, 'validateRoleExists'], |
66
|
|
|
] |
67
|
|
|
) |
68
|
|
|
->add( |
69
|
|
|
'accession_new_posting', |
70
|
|
|
'validateRoleExists', |
71
|
|
|
[ |
72
|
|
|
'rule' => [$this, 'validateRoleExists'], |
73
|
|
|
] |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return $validator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all categories in sort order |
81
|
|
|
* |
82
|
|
|
* @return ResultSetInterface |
83
|
|
|
*/ |
84
|
|
|
public function getAllCategories(): ResultSetInterface |
85
|
|
|
{ |
86
|
|
|
$key = 'Saito.Cache.Categories'; |
87
|
|
|
|
88
|
|
|
return $this->rememberStatic( |
89
|
|
|
$key, |
90
|
|
|
function () use ($key) { |
91
|
|
|
return $this->find('all') |
92
|
|
|
->cache($key) |
93
|
|
|
->order(['category_order' => 'ASC']) |
94
|
|
|
->all(); |
95
|
|
|
} |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Merge categories |
101
|
|
|
* |
102
|
|
|
* Move all postings and then delete the source category |
103
|
|
|
* |
104
|
|
|
* @param int $sourceId id |
105
|
|
|
* @param int $targetId id |
106
|
|
|
* @return void |
107
|
|
|
* @throws RecordNotFoundException |
108
|
|
|
*/ |
109
|
|
|
public function merge($sourceId, $targetId) |
110
|
|
|
{ |
111
|
|
|
$source = $this->get($sourceId); |
112
|
|
|
$target = $this->get($targetId); |
113
|
|
|
|
114
|
|
|
if ($source->get('id') === $target->get('id')) { |
115
|
|
|
throw new \RuntimeException( |
116
|
|
|
"Can't merge category onto itself.", |
117
|
|
|
1434009121 |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$Entries = TableRegistry::get('Entries'); |
|
|
|
|
122
|
|
|
$Entries->updateAll( |
123
|
|
|
['category_id' => $target->get('id')], |
124
|
|
|
['category_id' => $source->get('id')] |
125
|
|
|
); |
126
|
|
|
$this->deleteWithAllEntries($source->get('id')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Delete a category and all postings in it |
131
|
|
|
* |
132
|
|
|
* @param int $categoryId id |
133
|
|
|
* @return void |
134
|
|
|
* @throws RecordNotFoundException |
135
|
|
|
*/ |
136
|
|
|
public function deleteWithAllEntries($categoryId) |
137
|
|
|
{ |
138
|
|
|
$category = $this->get($categoryId); |
139
|
|
|
$Entries = TableRegistry::get('Entries'); |
|
|
|
|
140
|
|
|
$Entries->deleteAll( |
141
|
|
|
['category_id' => $category->get('id')] |
142
|
|
|
); |
143
|
|
|
$this->delete($category); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Validate that a role for the category actually exists |
148
|
|
|
* |
149
|
|
|
* @param string $roleId The role-ID int |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function validateRoleExists($roleId): bool |
153
|
|
|
{ |
154
|
|
|
/** @var Permissions */ |
155
|
|
|
$permissions = Registry::get('Permissions'); |
156
|
|
|
$roles = $permissions->getRoles()->getAvailable(true); |
157
|
|
|
$roleIds = array_column($roles, 'id'); |
158
|
|
|
|
159
|
|
|
return in_array((int)$roleId, $roleIds); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.