Completed
Pull Request — master (#30)
by Jason
17:47 queued 02:47
created

ProductDocDataExtension::updateCMSFields()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 26
nc 4
nop 1
dl 0
loc 38
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
namespace Dynamic\ProductCatalog\ORM;
4
5
use Dynamic\ProductCatalog\Docs\ProductDoc;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\Forms\DropdownField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
11
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
13
use SilverStripe\ORM\DataExtension;
14
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
15
16
class ProductDocDataExtension extends DataExtension
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $belongs_many_many = array(
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
22
        'Products' => CatalogProduct::class,
23
    );
24
25
    /**
26
     * @var array
27
     */
28
    private static $summary_fields = array(
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
29
        'Name' => 'Name',
30
        'Title' => 'Title',
31
        'ProductsCt' => 'Products',
32
    );
33
34
    /**
35
     * @var array
36
     */
37
    private static $searchable_fields = array(
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
38
        'Name',
39
        'Title',
40
        'Products.ID',
41
    );
42
43
    /**
44
     * @return int
45
     */
46
    public function getProductsCt()
47
    {
48
        if ($this->owner->Products()->exists()) {
49
            return $this->owner->Products()->count();
50
        }
51
52
        return 0;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getProductsList()
59
    {
60
        $list = '';
61
62
        if ($this->owner->Products()->exists()) {
63
            $i = 0;
64
            foreach ($this->owner->Products() as $product) {
65
                $list .= $product->Title;
66
                if (++$i !== $this->owner->Products()->Count()) {
67
                    $list .= ', ';
68
                }
69
            }
70
        }
71
72
        return $list;
73
    }
74
75
    /**
76
     * @param FieldList $fields
77
     */
78
    public function updateCMSFields(FieldList $fields)
79
    {
80
        $fields->removeByName(array(
81
            'Link',
82
            'SortOrder',
83
            'Products',
84
        ));
85
86
        $classes = ClassInfo::subclassesFor(ProductDoc::class);
87
        unset($classes['ProductDoc']);
88
89
        $result = array();
90
        foreach ($classes as $class) {
91
            $instance = singleton($class);
92
            $pageTypeName = $instance->i18n_singular_name();
93
            $result[$class] = $pageTypeName;
94
        }
95
96
        $fields->addFieldToTab(
97
            'Root.Main',
98
            DropdownField::create('SetClass', 'File type', $result, $this->owner->Classname)
0 ignored issues
show
Bug introduced by
'SetClass' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
            DropdownField::create(/** @scrutinizer ignore-type */ 'SetClass', 'File type', $result, $this->owner->Classname)
Loading history...
99
                ->setEmptyString(''),
100
            'Content'
101
        );
102
103
        $fields->dataFieldByName('Image')
104
            ->setFolderName('Uploads/ProductDocs/Images');
105
106
        if ($this->owner->ID) {
107
            // products
108
            $config = GridFieldConfig_RelationEditor::create();
109
            $config->removeComponentsByType(GridFieldAddExistingAutocompleter::class);
110
            $config->addComponent(new GridFieldAddExistingSearchButton());
111
            $config->removeComponentsByType(GridFieldAddNewButton::class);
112
            $products = $this->owner->Products();
113
            $productsField = GridField::create('Products', 'Products', $products, $config);
114
115
            $fields->addFieldToTab('Root.Products', $productsField);
116
        }
117
    }
118
119
    /**
120
     * Link for searchable result set.
121
     *
122
     * @return mixed
123
     */
124
    public function Link()
125
    {
126
        return $this->owner->Download()->URL;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function getIsProductDoc()
133
    {
134
        return true;
135
    }
136
}
137