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

code/model/EcommerceAlsoRecommendedDOD.php (4 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
class EcommerceAlsoRecommendedDOD extends DataExtension
6
{
7
    private static $many_many = array(
8
        'EcommerceRecommendedProducts' => 'Product'
9
    );
10
11
    private static $belongs_many_many = array(
12
        'RecommendedFor' => 'Product'
13
    );
14
15
    public function updateCMSFields(FieldList $fields)
16
    {
17
        if ($this->owner instanceof Product) {
18
            $config = GridFieldConfig_RelationEditor::create();
19
            $config
20
                ->removeComponentsByType("GridFieldEditButton")
21
                ->removeComponentsByType("GridFieldAddNewButton")
22
                ->addComponent(new GridFieldEditButtonOriginalPage());
23
            $fields->addFieldToTab('Root.Links', $recProGrid1 = new GridField('EcommerceRecommendedProducts', 'Also Recommended Products', $this->owner->EcommerceRecommendedProducts(), $config));
24
            $fields->addFieldToTab('Root.Links', $recProGrid2 = new GridField('RecommendedFor', 'Recommended For', $this->owner->RecommendedFor(), $config));
25
        }
26
    }
27
28
    /**
29
     *
30
     * small cleanup
31
     */
32
    public function onAfterWrite()
33
    {
34
        $products = $this->owner->EcommerceRecommendedProducts();
35 View Code Duplication
        if ($products->count()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            foreach ($products as $product) {
37
                if (!$product instanceof Product) {
38
                    $products->remove($product);
39
                } elseif (!$product->AllowPurchase) {
40
                    $products->remove($product);
41
                }
42
            }
43
        }
44
        $products = $this->owner->RecommendedFor();
45 View Code Duplication
        if ($products->count()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            foreach ($products as $product) {
47
                if (!$product instanceof Product) {
48
                    $products->remove($product);
49
                } elseif (!$product->AllowPurchase) {
50
                    $products->remove($product);
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * only returns the products that are for sale
58
     * if only those need to be showing.
59
     * @return DataList
60
     */
61 View Code Duplication
    public function EcommerceRecommendedProductsForSale()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) {
64
            return $this->owner->EcommerceRecommendedProducts()->filter(array("AllowPurchase" => 1));
65
        } else {
66
            return $this->owner->EcommerceRecommendedProducts();
67
        }
68
    }
69
70
    /**
71
     * only returns the products that are for sale
72
     * if only those need to be showing.
73
     * @return DataList
74
     */
75 View Code Duplication
    public function RecommendedForForSale()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        if ($this->owner->EcomConfig()->OnlyShowProductsThatCanBePurchased) {
78
            return $this->owner->RecommendedFor()->filter(array("AllowPurchase" => 1));
79
        } else {
80
            return $this->owner->RecommendedFor();
81
        }
82
    }
83
}
84