/
TypeFactory.php
185 lines (162 loc) · 5.26 KB
/
TypeFactory.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
<?php
namespace Concrete\Core\Attribute;
use Concrete\Core\Attribute\Category\CategoryService;
use Concrete\Core\Database\DatabaseStructureManager;
use Concrete\Core\Entity\Attribute\Type as AttributeType;
use Concrete\Core\Entity\Package;
use Concrete\Core\Foundation\Environment;
use Doctrine\ORM\EntityManager;
use Gettext\Translations;
/**
* Factory class for creating and retrieving instances of the Attribute type entity.
*/
class TypeFactory
{
/**
* @var \Doctrine\ORM\EntityManager
*/
protected $entityManager;
/**
* @var \Concrete\Core\Foundation\Environment
*/
protected $environment;
/**
* @var \Concrete\Core\Attribute\Category\CategoryService
*/
protected $categoryService;
/**
* Initialize the instance.
*
* @param \Concrete\Core\Foundation\Environment $environment
* @param \Doctrine\ORM\EntityManager $entityManager
* @param CategoryService $categoryService
*/
public function __construct(Environment $environment, EntityManager $entityManager, CategoryService $categoryService)
{
$this->environment = $environment;
$this->entityManager = $entityManager;
$this->categoryService = $categoryService;
}
/**
* Search an attribute type given its handle.
*
* @param string $atHandle
*
* @return \Concrete\Core\Entity\Attribute\Type|null
*/
public function getByHandle($atHandle)
{
$atHandle = (string) $atHandle;
if ($atHandle === '') {
return null;
}
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
return $r->findOneBy(['atHandle' => $atHandle]);
}
/**
* Get the list of attribute types defined by a package.
*
* @param \Concrete\Core\Entity\Package $package
*
* @return \Concrete\Core\Entity\Attribute\Type[]
*/
public function getListByPackage(Package $package)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
return $r->findByPackage($package);
}
/**
* Search an attribute type given its id.
*
* @param int $atID
*
* @return \Concrete\Core\Entity\Attribute\Type|null
*/
public function getByID($atID)
{
$atID = (int) $atID;
if ($atID === 0) {
return null;
}
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
return $r->findOneBy(['atID' => $atID]);
}
/**
* Create a new attribute type.
*
* @param string $atHandle The handle of the new attribute type
* @param string $atName The name of the new attribute type
* @param \Concrete\Core\Entity\Package|null $pkg The package defining the attribute type (if any)
*
* @return \Concrete\Core\Entity\Attribute\Type
*/
public function add($atHandle, $atName, $pkg = null)
{
$type = new AttributeType();
$type->setAttributeTypeName($atName);
$type->setAttributeTypeHandle($atHandle);
if ($pkg) {
$type->setPackage($pkg);
}
$this->installDatabase($type);
$this->entityManager->persist($type);
$this->entityManager->flush();
return $type;
}
/**
* Get the list of attribute types.
*
* @param string|false|null $akCategoryHandle The handle of the attribute category (if falsy, all the attribute types will be returned)
*
* @return \Concrete\Core\Entity\Attribute\Type[]
*/
public function getList($akCategoryHandle = false)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
$akCategoryHandle = (string) $akCategoryHandle;
if ($akCategoryHandle === '') {
return $r->findAll();
}
$category = $this->categoryService->getByHandle($akCategoryHandle);
return $category->getAttributeTypes();
}
/**
* @deprecated use the getList method (same arguments and same results)
*
* @param mixed $akCategoryHandle
*
* @return \Concrete\Core\Entity\Attribute\Type[]
*/
public function getAttributeTypeList($akCategoryHandle = false)
{
return $this->getList($akCategoryHandle);
}
/**
* @deprecated
*/
public function exportTranslations()
{
$translations = new Translations();
foreach ($this->getList() as $type) {
$translations->insert('AttributeTypeName', $type->getAttributeTypeName());
}
return $translations;
}
/**
* @param \Concrete\Core\Entity\Attribute\Type $type
*/
protected function installDatabase(AttributeType $type)
{
$r = $this->environment->getRecord(DIRNAME_ATTRIBUTES . '/' . $type->getAttributeTypeHandle() . '/' . FILENAME_ATTRIBUTE_DB, $type->getPackageHandle());
if ($r->exists()) {
// db.xml legacy approach
\Concrete\Core\Package\Package::installDB($r->file);
}
if (is_dir(DIR_APPLICATION . '/' . DIRNAME_CLASSES . '/' .
DIRNAME_ENTITIES)) {
// Refresh the application entities
$manager = new DatabaseStructureManager($this->entityManager);
$manager->refreshEntities();
}
}
}