-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdata.php
185 lines (160 loc) · 3.98 KB
/
data.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
defined('C5_EXECUTE') or die('Access Denied.');
class Data extends Model {
public $_table = 'Datas';
private $_indexingDisabled = false;
/**
* $data = new Data;
* $data->Load('dID=?', array(1));
* echo $data->height->getValue();
* echo $data->address->getAddress1();
* echo $data->richText->getDisplaySanitizedValue();
*/
public function __get($name) {
$ak = DataAttributeKey::getByHandle('data_' . $this->getDataType()->dtHandle . '_' . strtolower(preg_replace("/([A-Z])/", "_$1", $name)));
if (!$ak) {
return;
}
$avID = Loader::db()->GetOne('
SELECT avID
FROM DataAttributeValues
WHERE dID = ?
AND akID = ?
', array(
$this->dID,
$ak->getAttributeKeyID()
));
if (!$avID) {
return;
}
$av = DataAttributeValue::getByID($avID);
$av->setAttributeKey($ak);
return $av;
}
/**
* @param $ak DataAttributeKey
* @param $createIfNotFound boolean
* @return DataAttributeValue|null
*/
public function getAttributeValueObject($ak, $createIfNotFound = false) {
$db = Loader::db();
$av = false;
if ($avID = $db->GetOne('
SELECT avID
FROM DataAttributeValues
WHERE dID = ?
AND akID = ?
', array(
$this->dID,
$ak->getAttributeKeyID()
))) {
$av = DataAttributeValue::getByID($avID);
$av->setAttributeKey($ak);
}
if ($createIfNotFound && (!$av || !$db->GetOne('
SELECT count(avID)
FROM DataAttributeValues
WHERE avID = ?
', array($av->getAttributeValueID())))) {
$av = $ak->addAttributeValue();
}
return $av;
}
/**
* @param $ak AttributeKey|string
* @param $value mixed
*/
public function setAttribute($ak, $value) {
if (!is_object($ak)) {
$ak = DataAttributeKey::getByHandle('data_' . $this->getDataType()->dtHandle . '_' . $ak);
}
$ak->setAttribute($this, $value);
$this->reindex();
}
public function clearAttribute($ak) {
if ($dav = $this->getAttributeValueObject($ak)) {
$dav->delete();
}
$this->reindex();
}
public function Delete() {
$db = Loader::db();
$res = $db->Execute('
SELECT avID
FROM DataAttributeValues
WHERE dID = ?
', array($this->dID));
while ($row = $res->FetchRow()) {
if ($dav = DataAttributeValue::getByID($row['avID'])) {
$dav->delete();
}
}
return parent::Delete();
}
public function Insert() {
parent::Insert();
$this->reindex();
}
public function Update() {
parent::Update();
$this->reindex();
}
public function getDataType() {
$dataType = new DataType;
$dataType->Load('dtID=?', array($this->dtID));
return $dataType;
}
public function getAttributeValueObjects() {
$avos = array();
foreach ($this->getDataType()->attributes as $attribute) {
if ($avo = $this->getAttributeValueObject($attribute)) {
$avos[] = $this->getAttributeValueObject($attribute);
}
}
return $avos;
}
public function disableIndexing() {
$this->_indexingDisabled = true;
}
public function enableIndexing() {
$this->_indexingDisabled = false;
}
public function reindex() {
if ($this->_indexingDisabled) {
return;
}
$db = Loader::db();
$db->Execute('
DELETE
FROM DataSearchIndexAttributes
WHERE dID = ?
', array($this->dID));
return AttributeKey::reindex(
'DataSearchIndexAttributes',
array('dID' => $this->dID, 'dtID' => $this->getDataType()->dtID),
DataAttributeKey::getAttributes($this->dID, 'getSearchIndexValue'),
$db->Execute('
SELECT *
FROM DataSearchIndexAttributes
WHERE 1=2
')
);
}
public function import($node) {
if ($node->getName() !== 'data') {
throw new DataException(t('Invalid Element'));
}
$parent = current($node->xpath('parent::*'));
if (!$parent || empty($parent->attributes()->dtHandle)) {
throw new DataException(t('DataType handle dtHandle not found'));
}
$dataType = new DataType;
if (!$dataType->Load('dtHandle=?', array($parent->attributes()->dtHandle))) {
throw new DataException(t('DataType not found'));
}
$data = new Data;
$data->dtID = $dataType->dtID;
$data->Insert();
return $data;
}
}