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

code/forms/RecommendedProductsModifier_Form.php (11 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
 *
5
 * you can set
6
 * RecommendedProductsModifier_Form:
7
 *   product_template: "bla"
8
 *
9
 * in your configs to have a customised product display.
10
 *
11
 *
12
 */
13
class RecommendedProductsModifier_Form extends OrderModifierForm
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...
14
{
15
    private static $image_width = 100;
0 ignored issues
show
The property $image_width 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...
16
17
    private static $something_recommended_text = "Recommended Additions";
0 ignored issues
show
The property $something_recommended_text 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...
18
19
    private static $add_button_text = "Add Selected Items";
0 ignored issues
show
The property $add_button_text 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...
20
21
    private static $order_item_classname = "Product_OrderItem";
0 ignored issues
show
The property $order_item_classname 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...
22
23
    private static $product_template = "";
0 ignored issues
show
The property $product_template 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...
24
25
    public function __construct($optionalController = null, $name, FieldList $fields, FieldList $actions, $optionalValidator = null, $recommendedBuyables)
0 ignored issues
show
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
26
    {
27
        if (! ($fields instanceof FieldList)) {
28
            $fields = FieldList::create();
29
        }
30
        $fields->push(HeaderField::create($this->config()->get("something_recommended_text")));
31
        $productFieldList = new FieldList();
32
        foreach ($recommendedBuyables as $buyable) {
33
            $template = Config::inst()->get("RecommendedProductsModifier_Form", "product_template");
34
            if ($template) {
35
                $checkboxID = $buyable->ClassName."|".$buyable->ID;
36
                $arrayData = new ArrayData(
37
                    array(
38
                        "Buyable" => $buyable,
39
                        "CheckboxID" => $checkboxID,
40
                        "Checkbox" => new CheckboxField($checkboxID, _t("RecommendedProductsModifier_Form.ADD", "add"))
41
                    )
42
                );
43
                $productFieldList->push(new LiteralField("Buyable_".$buyable->ID, $arrayData->renderWith($template)));
44
            } else {
45
                //foreach product in cart get recommended products
46
                $imageID = $buyable->ImageID;
0 ignored issues
show
$imageID is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
                $imagePart = '';
48
                if ($buyable && $buyable->ImageID > 0) {
49
                    $resizedImage = $buyable->Image()->SetWidth($this->Config()->get("image_width"));
50
                    if (is_object($resizedImage) && $resizedImage) {
51
                        $imageLink = $resizedImage->Filename;
52
                        $imagePart = '<span class="secondPart"><img src="'.$imageLink.'" alt="'.Convert::raw2att($buyable->Title).'" /></span>';
53
                    }
54
                }
55
                if (!$imagePart) {
56
                    $imagePart = '<span class="secondPart noImage">[no image available for '.$buyable->Title.']</span>';
57
                }
58
                $priceAsMoney = EcommerceCurrency::get_money_object_from_order_currency($buyable->calculatedPrice());
59
                $pricePart = '<span class="firstPart">'.$priceAsMoney->NiceLongSymbol().'</span>';
60
                $title = '<a href="'.$buyable->Link().'">'.$buyable->Title.'</a>'.$pricePart.$imagePart.'';
61
                $newField = new CheckboxField($buyable->ClassName."|".$buyable->ID, $title);
62
                $fields->push($newField);
63
            }
64
        }
65
        $fields->push(new CompositeField($productFieldList));
66
        if (! $actions instanceof FieldList) {
67
            $actions = FieldList::create();
68
        }
69
        $actions->push(FormAction::create('processOrderModifier', $this->config()->get("add_button_text")));
70
        // 6) Form construction
71
        parent::__construct($optionalController, $name, $fields, $actions, $optionalValidator);
72
        Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
73
        //Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
        //Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        Requirements::javascript("ecommerce_alsorecommended/javascript/RecommendedProductsModifier.js");
76
        Requirements::themedCSS("RecommendedProductsModifier", "ecommerce_alsrecommended");
77
    }
78
79
    public function processOrderModifier($data, $form)
0 ignored issues
show
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
    {
81
        $count = 0;
82
        $error = 0;
83
        foreach ($data as $key => $value) {
84
            if ($value == 1) {
85
                list($className, $id) = explode("|", $key);
86
                if (class_exists($className) && intval($id) == $id) {
87
                    $buyable = $className::get()->byID($id);
88
                    if ($buyable && $buyable->canPurchase()) {
89
                        $count++;
90
                        ShoppingCart::singleton()->addBuyable($buyable);
91
                    } else {
92
                        $error++;
93
                    }
94
                } else {
95
                    $error++;
96
                }
97
            }
98
        }
99
        if ($error) {
100
            ShoppingCart::singleton()->addMessage(_t("RecommendedProductsModifier_Form.ERROR_UPDATING", "There was an error updating the cart", "bad"));
101
        } elseif ($count) {
102
            ShoppingCart::singleton()->addMessage(_t("RecommendedProductsModifier_Form.CART_UPDATED", "Cart updated (".$count.")", "good"));
103
        } else {
104
            ShoppingCart::singleton()->addMessage(_t("RecommendedProductsModifier_Form.NOTHING_TO_ADD", "Nothing to add", "warning"));
105
        }
106
        Controller::curr()->redirectBack();
107
    }
108
109
110
//-------------------------------------------------------------------- *** debug
111
}
112