|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
class DiscountCouponSiteTreeDOD extends DataExtension |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
private static $db = array( |
|
|
|
|
|
|
10
|
|
|
'PageIDs' => 'Text(700)' |
|
11
|
|
|
); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* update the CMS Fields |
|
15
|
|
|
* |
|
16
|
|
|
* @param FieldList $fields |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
public function updateCMSFields(FieldList $fields) |
|
20
|
|
|
{ |
|
21
|
|
|
$label = _t( |
|
22
|
|
|
"DiscountCouponSiteTreeDOD.SELECT_PRODUCTS_AND_SERVICES", |
|
23
|
|
|
'Select Product Categories and/or Products (if nothing is selected, the discount coupon will apply to all buyables).' |
|
24
|
|
|
); |
|
25
|
|
|
$field = new DiscountCouponSiteTreeDOD_Field( |
|
26
|
|
|
$name = "PageIDs", |
|
27
|
|
|
$title = $label, |
|
28
|
|
|
$sourceObject = "SiteTree", |
|
29
|
|
|
$keyField = "ID", |
|
30
|
|
|
$labelField = "MenuTitle" |
|
31
|
|
|
); |
|
32
|
|
|
$filter = create_function('$obj', 'return ( ( $obj InstanceOf ProductGroup || $obj InstanceOf Product) && ($obj->ParentID != '.$this->owner->ID.'));'); |
|
|
|
|
|
|
33
|
|
|
$field->setFilterFunction($filter); |
|
34
|
|
|
$fields->addFieldToTab('Root.AppliesTo', $field); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* normally returns TRUE, but returns FALSE when it, or its parent is in the list. |
|
39
|
|
|
* todo: add products in other product categories |
|
40
|
|
|
* |
|
41
|
|
|
* @param SiteTree $page |
|
42
|
|
|
* |
|
43
|
|
|
* @return boolean |
|
44
|
|
|
*/ |
|
45
|
|
|
public function canBeDiscounted(SiteTree $page) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->owner->PageIDs) { |
|
48
|
|
|
$allowedPageIDs = explode(',', $this->owner->PageIDs); |
|
49
|
|
|
$checkPages = ArrayList::create([$page]); |
|
50
|
|
|
$alreadyCheckedPageIDs = array(); |
|
51
|
|
|
while ($checkPages->Count()) { |
|
52
|
|
|
$page = $checkPages->First(); |
|
53
|
|
|
if (array_search($page->ID, $allowedPageIDs) !== false) { |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
$alreadyCheckedPageIDs[] = $page->ID; |
|
57
|
|
|
$checkPages->remove($page); |
|
58
|
|
|
|
|
59
|
|
|
// Parents list update |
|
60
|
|
|
if ($page->hasMethod('AllParentGroups')) { |
|
61
|
|
|
$parents = ArrayList::create($page->AllParentGroups()->toArray()); |
|
62
|
|
|
} else { |
|
63
|
|
|
$parents = ArrayList::create(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$parent = $page->Parent(); |
|
67
|
|
|
if ($parent && $parent->exists()) { |
|
68
|
|
|
$parents->unshift($parent); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
foreach ($parents as $parent) { |
|
72
|
|
|
if (array_search($parent->ID, $alreadyCheckedPageIDs) === false) { |
|
73
|
|
|
$checkPages->push($parent); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
$checkPages->removeDuplicates(); |
|
77
|
|
|
} |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.