AffinityTitleRule.php
7.41 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
require_once(__DIR__ . "/../ecommerce-adapters/AffinityDataLayer.php");
class AffinityTitleTemplateUnit {
const TYPE_STRING = 'str';
const TYPE_CUSTOM_ATTRIBUTE = 'customattr';
const TYPE_PRODUCT_FIELD = 'pfield';
public $type;
public $value; //string value | attribute name | field name
}
class AffinityTitleRule {
const className = "AffinityTitleRule";
public $id;
public $arrTitleTemplateUnit = array();
public $arrCategoriesToApply = array(); //arrEcommerceCategoryObj
public $arrProductsToApply = array(); //arrAffinityProduct
public static $titleRules;
/*
* Template Unit
*/
public function addTemplateUnit($type, $value, $position = -1) {
$obj = new AffinityTitleTemplateUnit();
$obj->type = $type;
$obj->value = $value;
$position = ($position === -1) ? count($this->arrTitleTemplateUnit) : $position;
array_splice($this->arrTitleTemplateUnit, $position, 0, $obj);
$this->save();
}
public function editTemplateUnit($position, $newValue) {
if(empty($this->arrTitleTemplateUnit[$position])) {
throw new Exception("Template Unit on the position $position doesn't exist!");
}
$obj = $this->arrTitleTemplateUnit[$position];
$obj->newValue = $newValue;
$this->save();
}
public function deleteTemplateUnit($position) {
if(empty($this->arrTitleTemplateUnit[$position])) {
throw new Exception("Template Unit to delete (on the position $position) doesn't exist!");
}
array_splice($this->arrTitleTemplateUnit, $position, 1);
$this->save();
}
/*
* End Template Unit
*/
/*
* Category Relationship
*/
public function addCategory($id) {
$this->arrCategoriesToApply[$id] = AffinityEcommerceCategory::get($id);
$this->save();
}
public function deleteCategory($id) {
unset($this->arrCategoriesToApply[$id]);
$this->save();
}
/*
* End Category Relationship
*/
/*
* Product Relationship
*/
public function addProduct($id) {
$this->arrCategoriesToApply[$id] = AffinityEcommerceCategory::get($id);
$this->save();
}
public function deleteProduct($id) {
unset($this->arrCategoriesToApply[$id]);
$this->save();
}
/*
* End Product Relationship
*/
/*
* Persisting Lifecycle
*/
public function save() {
$this->arrTitleTemplateUnit = json_encode($this->arrTitleTemplateUnit);
$arrCategoriesToApplyIds = array_keys($this->arrCategoriesToApply);
$this->arrCategoriesToApply = json_encode($arrCategoriesToApplyIds);
$arrProductsToApplyIds = array_keys($this->arrProductsToApply);
$this->arrProductsToApply = json_encode($this->arrProductsToApply);
return AffinityDataLayer::save($this);
}
public static function get($id) {
$objRetrieved = AffinityDataLayer::get(AffinityTitleRule::className, $id);
$this->arrTitleTemplateUnit = json_decode($this->arrTitleTemplateUnit);
$arrCategoriesToApplyIds = array_keys($this->arrCategoriesToApply);
$this->arrCategoriesToApply = json_decode($this->arrCategoriesToApply);
$arrProductsToApplyIds = json_decode($this->arrProductsToApply);
$this->arrProductsToApply = json_decode($this->arrProductsToApply);
return $objRetrieved;
}
public static function create() {
$obj = new AffinityTitleRule();
$obj->addTemplateUnit(AffinityTitleTemplateUnit::TYPE_PRODUCT_FIELD, "title");
$obj->save();
return $obj;
}
public static function delete($id) {
AffinityDataLayer::delete(AffinityTitleRule::className, $id);
}
/*
<rules>
<rule type="string">New</rule>
<rule type="attr">title</rule>
</rules>
*/
static function parseRule($string='') {
if (empty($string)) {
$string = $this->_titleTemplate;
}
$rules = simplexml_load_string($string);
$arr = array();
foreach ($rules as $rule) {
$arr[] = array('type' => (string)$rule['type'], 'value' => (string)$rule);
}
return $arr;
}
static function trunc() {
global $wpdb;
$wpdb->query("DELETE FROM ".$wpdb->prefix."ebayaffinity_titlerules");
}
function upsert($forceinsert=false) {
global $wpdb;
if ($forceinsert) {
$query = $wpdb->prepare(
"INSERT INTO ".$wpdb->prefix."ebayaffinity_titlerules (id, is_default, rule) VALUES (%d, %d, %s)",
$this->_id, $this->_is_default, $this->_titleTemplate
);
} else if (!empty($this->_id)) {
$query = $wpdb->prepare(
"UPDATE '.$wpdb->prefix.'ebayaffinity_titlerules SET rule=%s WHERE id = %d",
$this->_titleTemplate, $this->_id
);
} else {
$query = $wpdb->prepare(
"INSERT INTO '.$wpdb->prefix.'ebayaffinity_titlerules (rule) VALUES (%s)",
$this->_titleTemplate
);
}
if ($wpdb->query($query)) {
if (!empty($wpdb->insert_id)) {
$this->id = $wpdb->insert_id;
} else {
$this->id = 0;
}
return $this->id;
} else {
return -1;
}
}
static function getAllRules() {
global $wpdb;
return $wpdb->get_results("SELECT id, is_default, rule FROM ".$wpdb->prefix."ebayaffinity_titlerules ORDER BY id");
}
static function getRule($id) {
if (empty($id)) {
return false;
}
if (empty(self::$titleRules)) {
self::$titleRules = self::getAllRules();
}
foreach (self::$titleRules as $titleRule) {
if ($titleRule->id == $id) {
return $titleRule;
}
}
return false;
}
static function getDefaultRule() {
if (empty(self::$titleRules)) {
self::$titleRules = self::getAllRules();
}
foreach (self::$titleRules as $titleRule) {
if (!empty($titleRule->is_default)) {
return $titleRule;
}
}
return false;
}
static function generate($product) {
if (empty(self::$titleRules)) {
self::$titleRules = self::getAllRules();
}
$rule = self::getRule(get_post_meta($product->id, '_affinity_titlerule', true));
if (empty($rule)) {
$terms = get_the_terms($product->id, 'product_cat');
if (is_array($terms)) {
foreach ($terms as $k=>$term) {
$rule = self::getRule(get_term_meta($term->term_id, '_affinity_titlerule', true));
break;
}
}
}
if (empty($rule)) {
$rule = self::getDefaultRule();
}
if (empty($rule)) {
return array(0, $product->get_title());
}
$build = array();
$taxonomies = wc_get_attribute_taxonomies();
$taxonomies_arr = array();
foreach ($taxonomies as $taxonomy) {
$taxonomies_arr[$taxonomy->attribute_name] = $taxonomy->attribute_label;
}
$attributes = $product->get_attributes();
$ats = array();
foreach ($attributes as $k=>$attribute) {
if (empty($attribute['is_variation'])) {
$value = $product->get_attribute($attribute['name']);
if (empty($attribute['is_taxonomy'])) {
$ats[$k] = $value;
} else {
$ats[$k] = $value;
}
}
}
$arr = self::parseRule($rule->rule);
foreach ($arr as $el) {
if ($el['type'] === 'string') {
$build[] = $el['value'];
} else if ($el['type'] === 'attr') {
if ($el['value'] === 'title') {
$build[] = $product->get_title();
} else if ($el['value'] === 'fake-fake-condition') {
$build[] = 'New';
} else {
if (!empty($ats[$el['value']])) {
$build[] = $ats[$el['value']];
}
}
}
}
return array($rule->id, implode(' ', $build));
}
}