DiscountCouponSiteTreeDOD::updateCMSFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
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.'));');
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The property PageIDs does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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