/
CategoryProcessingHelper.php
116 lines (100 loc) · 3.66 KB
/
CategoryProcessingHelper.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
<?php
declare(strict_types=1);
/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\CategoriesModule\Helper;
use Doctrine\ORM\EntityManagerInterface;
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
use Zikula\CategoriesModule\Entity\AbstractCategoryAssignment;
use Zikula\CategoriesModule\Entity\CategoryEntity;
use Zikula\CategoriesModule\Entity\RepositoryInterface\CategoryRegistryRepositoryInterface;
use Zikula\ExtensionsModule\Api\ApiInterface\CapabilityApiInterface;
use Zikula\ExtensionsModule\Api\CapabilityApi;
/**
* Category processing helper functions for the categories module.
*/
class CategoryProcessingHelper
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var ZikulaHttpKernelInterface
*/
private $kernel;
/**
* @var CategoryRegistryRepositoryInterface
*/
private $categoryRegistryRepository;
/**
* @var CapabilityApiInterface
*/
private $capabilityApi;
public function __construct(
EntityManagerInterface $entityManager,
ZikulaHttpKernelInterface $kernel,
CategoryRegistryRepositoryInterface $categoryRegistryRepository,
CapabilityApiInterface $capabilityApi
) {
$this->entityManager = $entityManager;
$this->kernel = $kernel;
$this->categoryRegistryRepository = $categoryRegistryRepository;
$this->capabilityApi = $capabilityApi;
}
/**
* Checks whether a category may be deleted or moved.
* For this all registries are checked to see if the given category is contained in the corresponding subtree.
* If yes, the mapping table of the corresponding module is checked to see if it contains the given category.
*/
public function mayCategoryBeDeletedOrMoved(CategoryEntity $category): bool
{
// TODO #3920
return true;
// collect parents
$isOnTop = false;
$parentIds = [$category->getId()];
$directParent = $category;
while (false === $isOnTop) {
$directParent = $category->getParent();
if (null === $directParent) {
$isOnTop = true;
} else {
$parentIds[] = $directParent->getId();
}
}
// fetch registries
$registries = $this->categoryRegistryRepository->findAll();
// iterate over all registries
foreach ($registries as $registry) {
// check if the registry subtree contains our category
if (!in_array($registry->getCategory()->getId(), $parentIds, true)) {
continue;
}
// get information about responsible module
if (!$this->kernel->isBundle($registry->getModname())) {
continue;
}
$capabilities = $this->capabilityApi->getCapabilitiesOf($registry->getModname());
foreach ($capabilities[CapabilityApi::CATEGORIZABLE] as $entityClass) {
if (!is_subclass_of($entityClass, AbstractCategoryAssignment::class)) {
continue;
}
// check if this mapping table contains a reference to the given category
$mappings = $this->entityManager->getRepository($entityClass)
->findBy(['category' => $category]);
if (count($mappings) > 0) {
// existing reference found
return false;
}
}
}
return true;
}
}