-
Notifications
You must be signed in to change notification settings - Fork 453
/
GroupFolder.php
314 lines (275 loc) · 9.44 KB
/
GroupFolder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
namespace Concrete\Core\Tree\Node\Type;
use Concrete\Core\Database\Connection\Connection;
use Concrete\Core\Tree\Node\Type\Menu\GroupFolderMenu;
use Concrete\Core\User\Group\FolderItemList;
use Concrete\Core\User\Group\GroupType;
use Concrete\Core\User\Group\Search\ColumnSet\Available;
use Concrete\Core\User\Group\Search\ColumnSet\FolderSet;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Tree\Node\Node as TreeNode;
use Concrete\Core\Tree\Node\Type\Formatter\CategoryListFormatter;
use Concrete\Core\User\User;
use Gettext\Translations;
use Symfony\Component\HttpFoundation\Request;
class GroupFolder extends TreeNode
{
const CONTAINS_GROUP_FOLDERS = 0;
const CONTAINS_GROUP_FOLDERS_AND_GROUPS = 1;
const CONTAINS_SPECIFIC_GROUPS = 2;
/**
* @return int|null
*/
protected $contains = self::CONTAINS_GROUP_FOLDERS_AND_GROUPS;
/**
* @return GroupType|null
*/
protected $selectedGroupTypes = null;
/**
* @return int
*/
public function getContains()
{
return $this->contains;
}
/**
* @param int $contains
*/
public function setContains($contains)
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
$db->replace('TreeGroupFolderNodes', [
'treeNodeID' => $this->getTreeNodeID(),
'contains' => (int)$contains,
], ['treeNodeID'], true);
$this->contains = (int)$contains;
}
/**
* @return GroupType[]
*/
public function getSelectedGroupTypes()
{
return $this->selectedGroupTypes;
}
/**
* @param GroupType[] $selectedGroupTypes
*/
public function setSelectedGroupTypes($selectedGroupTypes)
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
$db->executeQuery('DELETE FROM TreeGroupFolderNodeSelectedGroupTypes WHERE treeNodeID = ?', [
$this->treeNodeID,
]);
foreach($selectedGroupTypes as $selectedGroupType) {
$db->insert('TreeGroupFolderNodeSelectedGroupTypes', [
'treeNodeID' => $this->getTreeNodeID(),
'gtID' => $selectedGroupType->getId(),
]);
}
$this->selectedGroupTypes = $selectedGroupTypes;
}
public function getTreeNodeTranslationContext()
{
return 'TreeNodeGroupFolderName';
}
public function getPermissionResponseClassName()
{
return '\\Concrete\\Core\\Permission\\Response\\GroupFolderResponse';
}
public function getPermissionAssignmentClassName()
{
return '\\Concrete\\Core\\Permission\\Assignment\\GroupFolderAssignment';
}
public function getPermissionObjectKeyCategoryHandle()
{
return 'group_folder';
}
public function getTreeNodeTypeName()
{
return t('Folder');
}
public function getTreeNodeDisplayName($format = 'html')
{
if ($this->getTreeNodeName()) {
$name = tc($this->getTreeNodeTranslationContext(), $this->getTreeNodeName());
switch ($format) {
case 'html':
return h($name);
case 'text':
default:
return $name;
}
} elseif ($this->getTreeNodeParentID() == 0) {
return t('Folders');
}
}
public function loadDetails()
{
$app = Application::getFacadeApplication();
/** @var Connection $db */
$db = $app->make(Connection::class);
$row = $db->fetchAssoc('SELECT * FROM TreeGroupFolderNodes WHERE treeNodeID = ?', [
$this->treeNodeID,
]);
if (!empty($row)) {
$this->setPropertiesFromArray($row);
}
$this->selectedGroupTypes = [];
foreach($db->fetchAll('SELECT gtID FROM TreeGroupFolderNodeSelectedGroupTypes WHERE treeNodeID = ?', [
$this->treeNodeID,
]) as $row) {
$this->selectedGroupTypes[] = GroupType::getByID($row["gtID"]);
}
}
public function deleteDetails()
{
$app = Application::getFacadeApplication();
$db = $app->make(Connection::class);
$db->executeQuery('DELETE FROM TreeGroupFolderNodes WHERE treeNodeID = ?', [
$this->treeNodeID,
]);
$db->executeQuery('DELETE FROM TreeGroupFolderNodeSelectedGroupTypes WHERE treeNodeID = ?', [
$this->treeNodeID,
]);
}
/**
* @param TreeNode|bool $parent Node's parent folder
*
* @return TreeNode
*/
public function duplicate($parent = false)
{
$node = static::add($this->getTreeNodeName(), $parent);
$this->duplicateChildren($node);
return $node;
}
public function getTreeNodeMenu()
{
return new GroupFolderMenu($this);
}
public function getTreeNodeJSON()
{
$node = TreeNode::getTreeNodeJSON();
if ($node) {
$node->isFolder = true;
$node->icon = 'fas fa-folder';
$node->resultsThumbnailImg = $this->getListFormatter()->getIconElement();
}
return $node;
}
public function getListFormatter()
{
return new CategoryListFormatter();
}
/**
* @param string $treeNodeName Node name
* @param TreeNode|bool $parent Node's parent folder
*
* @param int $contains
* @param GroupType[] $selectedGroupTypes
* @return TreeNode|GroupFolder
*/
public static function add($treeNodeName = '', $parent = false, $contains = self::CONTAINS_GROUP_FOLDERS, $selectedGroupTypes = [])
{
$node = parent::add($parent);
$node->setTreeNodeName($treeNodeName);
$node->setContains($contains);
$node->setSelectedGroupTypes($selectedGroupTypes);
return $node;
}
public function getTreeNodeName()
{
if ($this->getTreeNodeParentID() == 0) {
return t('All Groups');
}
return parent::getTreeNodeName();
}
public function getFolderItemList(User $u, Request $request)
{
$available = new Available();
$sort = false;
$list = new FolderItemList();
$list->filterByParentFolder($this);
if ($u !== null) {
if (($column = $request->get($list->getQuerySortColumnParameter())) && ($direction = $request->get($list->getQuerySortDirectionParameter()))) {
if (is_object($available->getColumnByKey($column)) && ($direction == 'asc' || $direction == 'desc')) {
$sort = [$column, $direction];
$u->saveConfig(sprintf('folder_manager.sort.%s', $this->getTreeNodeID()), json_encode($sort));
}
} else {
$sort = $u->config(sprintf('folder_manager.sort.%s', $this->getTreeNodeID()));
if ($sort) {
/** @noinspection PhpComposerExtensionStubsInspection */
$sort = json_decode($sort);
}
}
if (is_array($sort)) {
if ($sortColumn = $available->getColumnByKey($sort[0])) {
$sortColumn->setColumnSortDirection($sort[1]);
$list->sortBySearchColumn($sortColumn);
}
}
}
return $list;
}
/**
* Get the first child folder this folder that has a specific name.
*
* @param string $name The name of the child folder
* @param bool $create Should the child folder be created if it does not exist?
*
* @return static|null return NULL if no child folder has the specified name and $create is false
*/
public function getChildFolderByName($name, $create = false)
{
$typeHandle = $this->getTreeNodeTypeHandle();
if ($this->childNodesLoaded) {
$childNodes = $this->childNodes;
} else {
$childNodesData = $this->getHierarchicalNodesOfType($typeHandle, 1, true, false, 1);
$childNodes = array_map(function ($item) {
return $item['treeNodeObject'];
}, $childNodesData);
}
$result = null;
foreach ($childNodes as $childNode) {
if ($childNode->getTreeNodeTypeHandle() === $typeHandle && $childNode->getTreeNodeName() === $name) {
$result = $childNode;
break;
}
}
if ($result === null && $create) {
$result = static::add($name, $this);
}
return $result;
}
/**
* Get a descendent folder of this folder given its path.
*
* @param array $names The names of the child folders (1st item: child folder, 2nd item: grand-child folder, ...)
* @param bool $create Should the descendent folders be created if they don't exist?
*
* @return static|null return NULL if the descendent folder has not been found and $create is false
*/
public function getChildFolderByPath(array $names, $create = false)
{
if (count($names) === 0) {
$result = $this;
} else {
$childName = array_shift($names);
$result = $this->getChildFolderByName($childName, $create);
if ($result !== null) {
$result = $result->getChildFolderByPath($names, $create);
}
}
return $result;
}
public function exportTranslations(Translations $translations)
{
return false;
}
}