Completed
Push — master ( b7c5b8...080abf )
by Nicolaas
01:40
created

code/modifiers/MinMaxModifier.php (2 issues)

unused private properties.

Unused Code 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_stockcontrol
7
 * @description: makes sure that a buyable quantity in cart stays between a min and a max
8
 */
9
class MinMaxModifier extends OrderModifier
10
{
11
12
//--------------------------------------------------------------------*** static variables
13
14
    private static $db = array(
0 ignored issues
show
The property $db 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...
15
        "Adjustments" => "HTMLText"
16
    );
17
18
    private static $singular_name = "Stock Adjustment";
19
    public function i18n_singular_name()
20
    {
21
        self::$singular_name;
22
    }
23
24
    private static $plural_name = "Stock Adjustments";
25
    public function i18n_plural_name()
26
    {
27
        return self::$plural_name;
28
    }
29
30
    private static $title = "MinMaxModifier";
0 ignored issues
show
The property $title 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...
31
32
    private static $default_min_quantity = 1;
33
34
    private static $default_max_quantity = 9999;
35
36
    private static $min_field = "MinQuantity";
37
38
    private static $max_field = "MaxQuantity";
39
40
    private static $adjustment_message = "Based on stock availability, quantities have been adjusted as follows: ";
41
42
    private static $sorry_message = "Sorry, your selected value not is available.";
43
44
    private static $use_stock_quantities = true;
45
46
    private static $ids_of_items_adjusted = array();
47
48
//-------------------------------------------------------------------- *** static functions
49
50
    public static function show_form()
51
    {
52
        self::apply_min_max();
53
        return false;
54
    }
55
56
    public static function get_form($controller)
57
    {
58
        return false;
59
    }
60
61
//-------------------------------------------------------------------- *** cms fuctions
62
63
    public function getCMSFields()
64
    {
65
        $fields = parent::getCMSFields();
66
        $fields->removeByName("Adjustments");
67
        $fields->addFieldToTab("Root.Debug", new ReadonlyField("AdjustmentsShown", "Adjustments", $this->Adjustments));
68
        return $fields;
69
    }
70
//-------------------------------------------------------------------- *** display functions
71
    public function CanBeRemoved()
72
    {
73
        return false;
74
    }
75
76
    public function ShowInTable()
77
    {
78
        return false;
79
    }
80
81
82
//--------------------------------------------------------------------*** table values
83
    public function LiveCalculatedTotal()
84
    {
85
        self::apply_min_max();
86
        return 0;
87
    }
88
89
    public function LiveTableValue()
90
    {
91
        return "";
92
    }
93
94
95
//--------------------------------------------------------------------*** table titles
96
    public function LiveName()
97
    {
98
        return "";
99
    }
100
101
//-------------------------------------------------------------------- *** calculations
102
    public static function apply_min_max()
103
    {
104
        if (self::$min_field || self::$max_field  || self::$default_min_quantity || self::$default_max_quantity) {
105
            $msgArray = array();
106
            $minFieldName = self::$min_field;
107
            $maxFieldName = self::$max_field;
108
            $currentOrder = ShoppingCart::current_order();
109
            if ($currentOrder->IsSubmitted()) {
110
                //too late!
111
                return;
112
            }
113
            $items = $currentOrder->Items();
114
            $i = 0;
115
            if ($items) {
116
                foreach ($items as $item) {
117
                    $buyable = $item->Buyable();
118
                    if ($buyable) {
119
                        $quantity = $item->Quantity;
120
                        $absoluteMin = self::$default_min_quantity;
121
                        $absoluteMax = self::$default_max_quantity;
122
                        $parent = $buyable->Parent();
123 View Code Duplication
                        if ($minFieldName) {
124
                            if (isset($buyable->$minFieldName) && $buyable->$minFieldName > 0) {
125
                                $absoluteMin = $buyable->$minFieldName;
126
                            }
127
                            //product variations
128
                            elseif (!isset($buyable->$minFieldName)) {
129
                                if ($parent && isset($parent->$minFieldName) && $parent->$minFieldName > 0) {
130
                                    $absoluteMin = $parent->$minFieldName;
131
                                }
132
                            }
133
                        }
134 View Code Duplication
                        if ($maxFieldName) {
135
                            if (isset($buyable->$maxFieldName) && $buyable->$maxFieldName > 0) {
136
                                $absoluteMax = $buyable->$maxFieldName;
137
                            }
138
                            //product variations
139
                            elseif (!isset($buyable->$maxFieldName)) {
140
                                if ($parent && isset($parent->$maxFieldName) && $parent->$maxFieldName > 0) {
141
                                    $absoluteMax = $parent->$maxFieldName;
142
                                }
143
                            }
144
                        }
145
                        if ($buyable->UnlimitedStock) {
146
                            //nothing more to do
147
                        } elseif (self::$use_stock_quantities) {
148
                            $maxStockQuantity = $buyable->getActualQuantity();
149
                            if ($absoluteMax > $maxStockQuantity) {
150
                                $absoluteMax = $maxStockQuantity;
151
                            }
152
                            if ($absoluteMin > $maxStockQuantity) {
153
                                $absoluteMax = 0;
154
                                $maxStockQuantity = 0;
155
                            }
156
                        }
157
                        $absoluteMin = intval($absoluteMin) - 0;
158
                        $absoluteMax = intval($absoluteMax) - 0;
159
                        $newValue = $quantity;
160
                        if ($quantity < $absoluteMin && $absoluteMin > 0) {
161
                            debug::log("adjusting for MIN: $quantity < $absoluteMin");
162
                            $newValue = $absoluteMin;
163
                        }
164
                        if ($quantity > $absoluteMax && $absoluteMax > 0) {
165
                            debug::log("adjusting for MAX: $quantity > $absoluteMax");
166
                            $newValue = $absoluteMax;
167
                        }
168
                        if ($quantity != $newValue) {
169
                            $item->Quantity = $newValue;
170
                            ShoppingCart::singleton()->setQuantity($buyable, $newValue);
171
                            $msgArray[$i] = $buyable->Title." changed from ".$quantity." to ".$newValue;
172
                            $i++;
173
                            $quantity = $newValue;
174
                            self::$ids_of_items_adjusted[$item->ID] = $item->ID;
175
                        }
176
                        if (Director::is_ajax()) {
177
                            //do nothing
178
                        } else {
179
                            //IS THIS WORKING?
180
                            $fieldName = $item->AJAXDefinitions()->QuantityFieldName();
181
                            $js = 'MinMaxModifier.add_item("input[name=\''.$fieldName.'\']", '.intval($absoluteMin).', '.intval($absoluteMax).', "'.addslashes(self::$sorry_message).'");';
182
                            Requirements::javascript("ecommerce_stockcontrol/javascript/MinMaxModifier.js");
183
                            Requirements::customScript($js, $fieldName);
184
                        }
185
                    }
186
                }
187
            }
188
        }
189
        if (count($msgArray)) {
190
            if (self::$adjustment_message) {
191
                $msg = self::$adjustment_message."\n".implode("\n", $msgArray);
192
                if ($msg && !Director::is_ajax()) {
193
                    Requirements::customScript('alert("'.Convert::raw2js($msg).'");', "MinMaxModifierAlert");
194
                }
195
                //$this->Adjustments = $msg;
196
            }
197
        }
198
    }
199
200
    public function updateForAjax(array $js)
201
    {
202
        parent::updateForAjax($js);
203
        self::apply_min_max();
204
        if (is_array(self::$ids_of_items_adjusted) && count(self::$ids_of_items_adjusted)) {
205
            $items = OrderItem::get()->filter(array('ID' => self::$ids_of_items_adjusted));
206
            if ($items->count()) {
207
                foreach ($items as $item) {
208
                    $item->updateForAjax($js);
209
                }
210
            }
211
        }
212
        return $js;
213
    }
214
215
216
//--------------------------------------------------------------------*** database functions
217
}
218