Completed
Push — master ( d50167...f80385 )
by Nicolaas
01:45
created

code/modifiers/RecommendedProductsModifier.php (8 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @author Nicolaas [at] sunnysideup.co.nz
5
 * @package: ecommerce
6
 * @sub-package: ecommerce_modifiers
7
 * @description: shows a list of recommended products
8
 * the product page / dataobject need to have a function RecommendedProductsForCart
9
 * which returns an array of IDs
10
 */
11
class RecommendedProductsModifier extends OrderModifier
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
14
//--------------------------------------------------------------------  *** static variables
15
16
    private static $singular_name = "Recommended Product";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
    public function i18n_singular_name()
18
    {
19
        return _t("RecommendedProductsModifier.SINGULAR_NAME", "Recommended Product");
20
    }
21
22
    private static $plural_name = "Recommended Products";
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
    public function i18n_plural_name()
24
    {
25
        return _t("RecommendedProductsModifier.PLURAL_NAME", "Recommended Products");
26
    }
27
28
//--------------------------------------------------------------------  *** static functions
29
// ######################################## *** form functions (e. g. Showform and getform)
30
31
32
    protected $recommendedBuyables = null;
33
34
    /**
35
     * standard Modifier Method
36
     * @return Boolean
37
     */
38
    public function ShowForm()
39
    {
40
        if (!$this->recommendedBuyables) {
41
            $this->recommendedBuyables = new ArrayList();
42
            $inCartIDArray = array();
43
            if ($items = $this->Order()->Items()) {
44
                foreach ($items as $item) {
45
                    $buyable = $item->Buyable();
46
                    if ($buyable instanceof Product) {
47
                        $codeOfBuyable = $buyable->ClassName.".".$buyable->ID;
48
                        $inCartIDArray[$codeOfBuyable] = $codeOfBuyable;
49
                    }
50
                }
51
                foreach ($items as $item) {
52
                    //get recommended products
53
                    if ($item) {
54
                        $buyable = $item->Buyable();
55
                        if ($buyable instanceof Product) {
56
                            unset($recommendedProducts);
57
                            $recommendedProducts = $buyable->EcommerceRecommendedProducts();
58
                            foreach ($recommendedProducts as $recommendedProduct) {
59
                                $codeOfRecommendedProduct = $recommendedProduct->ClassName.".".$recommendedProduct->ID;
60
                                if (!in_array($codeOfRecommendedProduct, $inCartIDArray)) {
61
                                    $this->recommendedBuyables->push($recommendedProduct);
62
                                }
63
                            }
64
                        }
65
                    }
66
                }
67
            }
68
        }
69
        return $this->recommendedBuyables->count();
70
    }
71
72
    /**
73
     * Should the form be included in the editable form
74
     * on the checkout page?
75
     * @return Boolean
76
     */
77
    public function ShowFormInEditableOrderTable()
78
    {
79
        return false;
80
    }
81
82
    /**
83
     *
84
     * @return Form
0 ignored issues
show
Should the return type not be RecommendedProductsModifier_Form|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
85
     */
86
    public function getModifierForm(Controller $optionalController = null, Validator $optionalValidator = null)
87
    {
88
        if ($this->ShowForm()) {
89
            return new RecommendedProductsModifier_Form(
90
                $optionalController,
91
                'RecommendedProducts',
92
                FieldList::create(),
93
                FieldList::create(),
94
                $optionalValidator,
95
                $this->recommendedBuyables
96
            );
97
        }
98
    }
99
100
//-------------------------------------------------------------------- *** display functions
101
    public function ShowInTable()
102
    {
103
        return false;
104
    }
105
106
    public function CanRemove()
107
    {
108
        return false;
109
    }
110
111
112
// -------------------------------------------------------------------- *** table values
113
    public function LiveCalculatedTotal()
114
    {
115
        return 0;
116
    }
117
    public function LiveTableValue()
118
    {
119
        return 0;
120
    }
121
122
//-------------------------------------------------------------------- *** table titles
123
    public function LiveName()
124
    {
125
        return $this->i18n_singular_name();
126
    }
127
128
    public function Name()
0 ignored issues
show
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
129
    {
130
        if (!$this->canEdit()) {
131
            return $this->Name;
0 ignored issues
show
The property Name does not exist on object<RecommendedProductsModifier>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
        } else {
133
            return $this->LiveName();
134
        }
135
    }
136
137
//-------------------------------------------------------------------- ***  database functions
138
}
139