forked from hugowetterberg/cbisimport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCbisTemplate.php
More file actions
230 lines (215 loc) · 6.72 KB
/
CbisTemplate.php
File metadata and controls
230 lines (215 loc) · 6.72 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Class that handles all template information from CBIS.
*
* @package cbisimport
*/
class CbisTemplate {
const GENERAL = 91;
private static $templates = array();
private $name;
private $attributes;
private $parents;
/**
* Private constructor, use the static function
* CbisTemplate::getTemplate() instead.
*
* @param int $id
*/
private function __construct($id) {
$this->attributes = array();
$this->loadDefinition($id);
}
/**
* Gets the template with the given id.
*
* @param int $id
* @return CbisTemplate
*/
public static function getTemplate($id) {
if (!isset(self::$templates[$id])) {
self::$templates[$id] = new CbisTemplate($id);
}
return self::$templates[$id];
}
/**
* Recursively loads the template and it's parent templates.
*
* @param int $id
* @return void
*/
private function loadDefinition($id) {
if ($id != CbisTemplate::GENERAL) {
$template = cbisimport_get_template($id);
if ($template) {
$this->name = $template->Name;
foreach (cbisimport_array($template->Attributes->TemplateAttribute) as $attribute) {
$this->attributes[$attribute->AttributeId] = $attribute;
}
}
$this->template = $template;
}
else {
$this->name = t('General');
$templates = cbisimport_get_templates();
foreach ($templates as $template) {
foreach (cbisimport_array($template->Attributes->TemplateAttribute) as $attribute) {
$this->attributes[$attribute->AttributeId] = $attribute;
}
}
$this->template = (object)array(
'Name' => $this->name,
);
}
}
/**
* Checks if the template is an instance of a specific template. That is if
* the template is the template in question, or if it inherits from it.
*
* @param int $template_id
* @return bool
*/
public function isInstanceOf($template_id) {
$match = $this->template->Id == $template_id;
$match = $match || $this->template->ParentId == $template_id;
if (!$match && $this->template->ParentId) {
$parent = self::getTemplate($this->template->ParentId);
$match = $parent->isInstanceOf($template_id);
}
return $match;
}
/**
* Convenience function that loads the required template and sanitizes the
* product.
*
* @param object|array $product
* The raw product data or an array of products.
* @return array
* The sanitized product or products.
*/
public static function sanitize($product) {
$result = NULL;
if (is_array($product)) {
$result = array();
foreach ($product as $p) {
$tpl = self::getTemplate(CbisTemplate::GENERAL);
$result[$product->Id] = $tpl->sanitizeProduct($p);
}
}
else {
$tpl = self::getTemplate(CbisTemplate::GENERAL);
$result = $tpl->sanitizeProduct($product);
}
return $result;
}
/**
* Names and normalizes the product attributes and other data.
*
* @param object $product
* @return array
* The sanitized product.
*/
public function sanitizeProduct($product) {
$sane = array();
foreach ((array)$product as $name => $value) {
switch ($name) {
case 'Attributes':
$value = $this->sanitizeProductAttributes($value->AttributeData);
break;
case 'Occasions':
$occasions = cbisimport_array($value->OccasionObject);
$value = array();
foreach ($occasions as $occasion) {
$o = strtotime('0001-01-01T00:00:00');
$occasion->StartDate = $this->normalizeDate($occasion->StartDate);
$occasion->EndDate = $this->normalizeDate($occasion->EndDate);
$occasion->StartTime = $this->getTimeOffset($occasion->StartTime);
$occasion->EndTime = $this->getTimeOffset($occasion->EndTime);
$occasion->EntryTime = strtotime($occasion->EntryTime);
if ($occasion->EntryTime == $o) {
$occasion->EntryTime = NULL;
}
$value[$occasion->Id] = $occasion;
}
break;
case 'PublishedDate':
case 'RevisionDate':
case 'ExpirationDate':
if (!empty($value)) {
$value = strtotime($value);
}
break;
}
$sane[$name] = $value;
}
$sane['TemplateName'] = $this->name;
$sane['TemplateParents'] = $this->parents;
return $sane;
}
/**
* Add timezone and DST
* 3600 is the timezone offset in sweden. Don't use date('Z'), it will mess up the the time because of DST
*/
private function normalizeDate($date) {
$n_date = strtotime($date) + date('Z', $date);
$n_date += date('I', $n_date) * 3600;
return $n_date;
}
private function getTimeOffset($isotime) {
$t = date('Y-m-d') . 'T' . substr($isotime, 11, 8) . date('P');
$o = strtotime($t) - strtotime(date('Y-m-d\T00:00:00P'));
return $o;
}
/**
* Maps in the attribute values to their system names and flattens their
* value structure where appropriate.
*
* @param array $attributeData
* @return array
* The sanitized attributes.
*/
private function sanitizeProductAttributes($attributeData) {
$attributes = array();
foreach ($attributeData as $attribute) {
$def = $this->attributes[$attribute->AttributeId];
// Copytext, not in CBIS template. THIS IS A HACK -DA
if($attribute->AttributeId == 274) {
$def->AttributeSystemName = 'Copytext';
}
if ($def) {
switch ($attribute->MetaType) {
case 'Media':
$attributes[$def->AttributeSystemName] = cbisimport_array($attribute->Value->MediaList->MediaObject);
break;
case 'LocationDistances':
$attributes[$def->AttributeSystemName] = cbisimport_array($attribute->Value->LocationDistance);
break;
case 'String':
$text = $attribute->Value->Data;
$text = preg_replace('/<br\/?>/', "\n", $text);
$text = preg_replace('/\n{3,}/', "\n\n", $text);
$text = strip_tags($text, '<a>');
$text = html_entity_decode($text);
$text = trim($text);
$attributes[$def->AttributeSystemName] = $text;
break;
default:
if (isset($attribute->Value->Data)) {
$attributes[$def->AttributeSystemName] = $attribute->Value->Data;
}
else {
$attributes[$def->AttributeSystemName] = $attribute->Value;
}
break;
}
if (isset($attribute->Language)) {
$attributes['_language'][$def->AttributeSystemName] = $attribute->Language;
}
}
else {
$attributes['_unknown'][] = $attribute;
}
}
return $attributes;
}
}