Passed
Push — master ( b298f7...a19e37 )
by Antony
02:10
created

ProductCategoryExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 32
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace AntonyThorpe\SilverShopProductModel;
4
5
use AntonyThorpe\SilverShopProductModel\ProductModel;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
9
use SilverStripe\Forms\HeaderField;
10
use SilverStripe\Forms\LabelField;
11
use SilverStripe\ORM\ArrayList;
12
use SilverStripe\ORM\DataExtension;
13
use SilverStripe\ORM\GroupedList;
14
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
15
16
/**
17
 * Extends SilverShop\Page\ProductCategory to enable the adding of Models
18
 */
19
class ProductCategoryExtension extends DataExtension
20
{
21
    private static $has_many = array (
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
22
        'ProductModels' => ProductModel::class
23
    );
24
25
    public function updateCMSFields(FieldList $fields)
26
    {
27
        $fields->addFieldToTab(
28
            'Root.' . _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'),
29
            HeaderField::create(
30
                'ModelHeading',
31
                _t(\SilverShop\Page\ProductCategory::class . 'ModelsHeading', 'Specify the models for this Product Category')
32
            )
33
        );
34
        $fields->addFieldToTab(
35
            'Root.' . _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'),
36
            LabelField::create(
37
                'ModelLabel',
38
                _t(
39
                    \SilverShop\Page\ProductCategory::class . "ModelsLabel",
40
                    "The below entries determine the order of Models, and their associated products, on the Product Category Pages.  Also, sets the Models to appear in dropdown on each Product's page."
41
                )
42
            )
43
        );
44
45
        $fields->addFieldToTab(
46
            'Root.' . _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'),
47
            GridField::create(
48
                'Models',
49
                _t(\SilverShop\Page\ProductCategory::class . 'Models', 'Models'),
50
                $this->owner->ProductModels()->sort('Sort', 'ASC'),
51
                $config = GridFieldConfig_RecordEditor::create()
52
            )
53
        );
54
        // Add reorder capabilties
55
        if ($this->owner->ProductModels()->count() >= 2) {
56
            $config->addComponent(new GridFieldOrderableRows('Sort'));
57
        }
58
    }
59
60
    /**
61
     * Create a grouped list for presentation in a table
62
     * @return GroupedList
63
     */
64
    public function getGroupedProductsByModel()
65
    {
66
        $list = $this->owner->ProductsShowable();
67
        $sortedList = new ArrayList();
68
69
        foreach ($this->owner->ProductModels()->sort('Sort') as $model) {
70
            foreach ($list as $product) {
71
                if ($product->Model == $model->Title) {
72
                    $sortedList->push($product);
73
                }
74
            }
75
        }
76
77
        return GroupedList::create($sortedList);
78
    }
79
}
80