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

code/forms/RecommendedProductsModifier_Form.php (2 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
14
{
15
    private static $image_width = 100;
16
17
    private static $something_recommended_text = "Recommended Additions";
18
19
    private static $add_button_text = "Add Selected Items";
20
21
    private static $order_item_classname = "Product_OrderItem";
22
23
    private static $product_template = "";
24
25
    public function __construct($optionalController = null, $name, FieldList $fields, FieldList $actions, $optionalValidator = null, $recommendedBuyables)
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;
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)
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