/
TypeFactory.php
113 lines (92 loc) · 3.14 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
<?php
namespace Concrete\Core\Attribute;
use Concrete\Core\Attribute\Key\Category;
use Concrete\Core\Database\DatabaseStructureManager;
use Concrete\Core\Entity\Package;
use Concrete\Core\Foundation\Environment;
use Doctrine\ORM\EntityManager;
use Concrete\Core\Entity\Attribute\Type as AttributeType;
use Gettext\Translations;
/**
* Factory class for creating and retrieving instances of the Attribute type entity.
*/
class TypeFactory
{
protected $entityManager;
protected $environment;
public function __construct(Environment $environment, EntityManager $entityManager)
{
$this->environment = $environment;
$this->entityManager = $entityManager;
}
public function getByHandle($atHandle)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
return $r->findOneBy(array('atHandle' => $atHandle));
}
public function getListByPackage(Package $package)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
return $r->findByPackage($package);
}
public function getByID($atID)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
return $r->findOneBy(array('atID' => $atID));
}
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();
}
}
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;
}
public function getList($akCategoryHandle = false)
{
$r = $this->entityManager->getRepository('\Concrete\Core\Entity\Attribute\Type');
if ($akCategoryHandle == false) {
return $r->findAll();
} else {
$category = Category::getByHandle($akCategoryHandle);
return $category->getAttributeTypes();
}
}
/**
* @deprecated
*/
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;
}
}