-
Notifications
You must be signed in to change notification settings - Fork 451
/
ObjectTrait.php
142 lines (124 loc) · 4.57 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Concrete\Core\Attribute;
use Concrete\Core\Attribute\Category\CategoryInterface;
use Concrete\Core\Attribute\Value\EmptyRequestAttributeValue;
use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Entity\Attribute\Value\Value as AttributeValue;
use Concrete\Core\Entity\Attribute\Value\Value\Value;
use Concrete\Core\Support\Facade\Application;
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
* @param bool $doReindexImmediately
*/
public function clearAttribute($ak, bool $doReindexImmediately = true)
{
$value = $this->getAttributeValueObject($ak);
if (is_object($value)) {
$controller = $this->getObjectAttributeCategory();
$controller->deleteValue($value);
if ($doReindexImmediately) {
$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
* @param bool $doReindexImmediately
* @return \Concrete\Core\Entity\Attribute\Value\Value
*/
public function setAttribute($ak, $value, $doReindexImmediately = true)
{
$app = Application::getFacadeApplication();
/** @var RequestCache $cache */
$cache = $app->make('cache/request');
if (is_object($ak)) {
$akHandle = $ak->getAttributeKeyHandle();
} else {
$akHandle = $ak;
}
$cache->delete('attribute/value/' . $akHandle);
$orm = \Database::connection()->getEntityManager();
$this->clearAttribute($ak, $doReindexImmediately);
// 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());
$value = false;
} else {
/**
* @var $value AttributeValue\AbstractValue
*/
$value = $controller->createAttributeValue($value);
}
}
if ($value) {
$value->setGenericValue($genericValue);
$orm->persist($value);
$orm->flush();
}
if ($doReindexImmediately) {
$category = $this->getObjectAttributeCategory();
$indexer = $category->getSearchIndexer();
if ($indexer) {
$indexer->indexEntry($category, $attributeValue, $this);
}
}
return $attributeValue;
}
}