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

code/modifiers/RecommendedProductsModifier.php (1 issue)

php_doc.return_type_not_inferrable

Documentation Minor

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
12
{
13
14
//--------------------------------------------------------------------  *** static variables
15
16
    private static $singular_name = "Recommended Product";
17
    public function i18n_singular_name()
18
    {
19
        return _t("RecommendedProductsModifier.SINGULAR_NAME", "Recommended Product");
20
    }
21
22
    private static $plural_name = "Recommended Products";
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
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;
132
        } else {
133
            return $this->LiveName();
134
        }
135
    }
136
137
//-------------------------------------------------------------------- ***  database functions
138
}
139