/
StandardSetManager.php
116 lines (103 loc) · 3.46 KB
/
StandardSetManager.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
namespace Concrete\Core\Attribute;
use Concrete\Core\Entity\Attribute\Category;
use Concrete\Core\Entity\Attribute\Key\Key;
use Concrete\Core\Entity\Attribute\Set as SetEntity;
use Concrete\Core\Entity\Attribute\SetKey;
use Doctrine\ORM\EntityManager;
/**
* Handles adding and removing keys from attribute sets.
*/
class StandardSetManager implements SetManagerInterface
{
const ASET_ALLOW_NONE = 0;
const ASET_ALLOW_SINGLE = 1;
const ASET_ALLOW_MULTIPLE = 2;
protected $entityManager;
protected $allowAttributeSets = false;
protected $categoryEntity;
/**
* @return boolean
*/
public function allowAttributeSets()
{
return $this->categoryEntity->allowAttributeSets();
}
public function getUnassignedAttributeKeys()
{
$attributes = array();
foreach ($this->categoryEntity->getController()->getList() as $key) {
$query = $this->entityManager->createQuery(
'select sk from \Concrete\Core\Entity\Attribute\SetKey sk where sk.attribute_key = :key'
);
$query->setParameter('key', $key);
$query->setMaxResults(1);
$r = $query->getOneOrNullResult();
if (!is_object($r)) {
$attributes[] = $key;
}
}
return $attributes;
}
public function getAttributeSets()
{
return $this->categoryEntity->getAttributeSets();
}
/**
* @param boolean $allowAttributeSets
*/
public function setAllowAttributeSets($allowAttributeSets)
{
$this->allowAttributeSets = $allowAttributeSets;
}
public function __construct(Category $categoryEntity, EntityManager $entityManager)
{
$this->categoryEntity = $categoryEntity;
$this->entityManager = $entityManager;
}
public function addSet($handle, $name, $pkg = null, $locked = null)
{
$set = new SetEntity();
$set->setAttributeKeyCategory($this->categoryEntity);
$set->setAttributeSetHandle($handle);
$set->setAttributeSetName($name);
if ($pkg) {
$set->setPackage($pkg);
}
if ($locked) {
$set->setAttributeSetIsLocked($locked);
}
$this->entityManager->persist($set);
$this->entityManager->flush();
return $set;
}
public function addKey(SetEntity $set, Key $key)
{
$displayOrder = 0;
$keys = $set->getAttributeKeys();
if (count($keys) > 0) {
$displayOrder = count($keys);
}
$r = $this->entityManager->getRepository('Concrete\Core\Entity\Attribute\SetKey');
$setKey = $r->findOneBy(array('attribute_key' => $key, 'set' => $set));
if (!is_object($setKey)) {
$setKey = new SetKey();
$setKey->setAttributeKey($key);
$setKey->setAttributeSet($set);
$setKey->setDisplayOrder($displayOrder);
$set->getAttributeKeyCollection()->add($setKey);
$this->entityManager->persist($setKey);
$this->entityManager->flush();
}
}
public function updateAttributeSetDisplayOrder($asIDs)
{
$db = \Database::connection();
for ($i = 0; $i < count($asIDs); ++$i) {
$db->executeQuery(
"UPDATE AttributeSets SET asDisplayOrder = {$i} WHERE akCategoryID = ? AND asID = ?",
array($this->categoryEntity->getAttributeKeyCategoryID(), $asIDs[$i])
);
}
}
}