Completed
Pull Request — master (#30)
by Jason
14:29
created

ProductDocDataExtension::getIsProductDoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
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
        return 0;
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getProductsList()
58
    {
59
        $list = '';
60
61
        if ($this->owner->Products()->exists()) {
62
            $i = 0;
63
            foreach($this->owner->Products() as $product) {
64
                $list .= $product->Title;
65
                if(++$i !== $this->owner->Products()->Count()) {
66
                    $list .= ", ";
67
                }
68
            }
69
        }
70
        return $list;
71
    }
72
73
    /**
74
     * @param FieldList $fields
75
     */
76
    public function updateCMSFields(FieldList $fields)
77
    {
78
        $fields->removeByName(array(
79
            'Link',
80
            'SortOrder',
81
            'Products',
82
        ));
83
84
        $classes = ClassInfo::subclassesFor(ProductDoc::class);
85
        unset($classes['ProductDoc']);
86
87
        $result = array();
88
        foreach ($classes as $class) {
89
            $instance = singleton($class);
90
            $pageTypeName = $instance->i18n_singular_name();
91
            $result[$class] = $pageTypeName;
92
        }
93
94
        $fields->addFieldToTab(
95
            'Root.Main',
96
            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

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