-
Notifications
You must be signed in to change notification settings - Fork 451
/
ObjectTrait.php
124 lines (107 loc) · 3.85 KB
/
ObjectTrait.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
<?php
namespace Concrete\Core\Attribute;
use Concrete\Core\Attribute\Category\CategoryInterface;
use Concrete\Core\Attribute\Value\EmptyRequestAttributeValue;
use Concrete\Core\Entity\Attribute\Value\Value as AttributeValue;
use Concrete\Core\Entity\Attribute\Value\Value\Value;
trait ObjectTrait
{
/**
* @return CategoryInterface
*/
abstract public function getObjectAttributeCategory();
/**
* @param $ak
* @param bool $createIfNotExists
*
* @return AttributeValue
*/
abstract public function getAttributeValueObject($ak, $createIfNotExists = false);
public function getAttribute($ak, $mode = false)
{
$value = $this->getAttributeValueObject($ak);
if (is_object($value)) {
return $value->getValue($mode);
}
}
/**
* @param $ak
* @return \Concrete\Core\Entity\Attribute\Value\Value
*/
public function getAttributeValue($ak)
{
$value = $this->getAttributeValueObject($ak);
if (is_object($value)) {
return $value;
}
}
/**
* @param AttributeKeyInterface | string $ak
*/
public function clearAttribute($ak)
{
$value = $this->getAttributeValueObject($ak);
if (is_object($value)) {
$controller = $this->getObjectAttributeCategory();
$controller->deleteValue($value);
$category = $this->getObjectAttributeCategory();
$indexer = $category->getSearchIndexer();
if ($indexer) {
$indexer->clearIndexEntry($category, $value, $this);
}
}
}
/**
* Sets the attribute of a user info object to the specified value, and saves it in the database.
*
* @param AttributeKeyInterface | string $ak
* @param mixed $value
* @return \Concrete\Core\Entity\Attribute\Value\Value
*/
public function setAttribute($ak, $value)
{
$orm = \Database::connection()->getEntityManager();
$this->clearAttribute($ak);
// Create the attribute category value.
$attributeValue = $this->getAttributeValueObject($ak, true);
$orm->persist($attributeValue);
$orm->flush();
// Create the generic value. This gets joined to any specific Attribute value objects later on.
$genericValue = new Value();
$genericValue->setAttributeKey($attributeValue->getAttributeKey());
$orm->persist($genericValue);
$orm->flush();
// Set the generic value to the attribute category value.
$attributeValue->setGenericValue($genericValue);
$orm->persist($attributeValue);
$orm->flush();
$controller = $attributeValue->getAttributeKey()->getController();
$controller->setAttributeValue($attributeValue);
if (!($value instanceof AttributeValue\AbstractValue)) {
if ($value instanceof EmptyRequestAttributeValue) {
// LEGACY SUPPORT
// If the passed $value object == EmptyRequestAttributeValue, we know we are dealing
// with a legacy attribute type that's not using Doctrine. We have not returned an
// attribute value value object.
$controller->saveForm($controller->post());
unset($value);
} else {
/**
* @var $value AttributeValue\AbstractValue
*/
$value = $controller->createAttributeValue($value);
}
}
if ($value) {
$value->setGenericValue($genericValue);
$orm->persist($value);
$orm->flush();
}
$category = $this->getObjectAttributeCategory();
$indexer = $category->getSearchIndexer();
if ($indexer) {
$indexer->indexEntry($category, $attributeValue, $this);
}
return $attributeValue;
}
}