Completed
Pull Request — master (#24)
by Jason
21:08 queued 06:10
created

ProductDocDataExtension::updateCMSFields()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
ccs 24
cts 24
cp 1
rs 8.8571
cc 3
eloc 27
nc 4
nop 1
crap 3
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(
22
        'Products' => CatalogProduct::class,
23
    );
24
25
    /**
26
     * @var array
27
     */
28
    private static $summary_fields = array(
29
        'Name' => 'Name',
30
        'Title' => 'Title',
31
        'ProductsCt' => 'Products',
32
    );
33 1
34
    /**
35 1
     * @var array
36 1
     */
37
    private static $searchable_fields = array(
38
        'Name',
39
        'Title',
40
        'Products.ID',
41
    );
42
43
    /**
44 1
     * @return int
45
     */
46 1
    public function getProductsCt()
47
    {
48 1
        if ($this->owner->Products()->exists()) {
49 1
            return $this->owner->Products()->count();
50 1
        }
51 1
        return 0;
52 1
    }
53
54
    /**
55 1
     * @return string
56 1
     */
57 1
    public function getProductsList()
58
    {
59
        $list = '';
60
61
        if ($this->owner->Products()->exists()) {
62
            $i = 0;
63 5
            foreach($this->owner->Products() as $product) {
64
                $list .= $product->Title;
65 5
                if(++$i !== $this->owner->Products()->Count()) {
66 5
                    $list .= ", ";
67 5
                }
68 5
            }
69 5
        }
70
        return $list;
71 5
    }
72 5
73
    /**
74 5
     * @param FieldList $fields
75 5
     */
76 5
    public function updateCMSFields(FieldList $fields)
77 5
    {
78 5
        $fields->removeByName(array(
79 5
            'Link',
80
            'SortOrder',
81 5
            'Products',
82 5
        ));
83 5
84 5
        $classes = ClassInfo::subclassesFor(ProductDoc::class);
85 5
        unset($classes['ProductDoc']);
86 5
87
        $result = array();
88 5
        foreach ($classes as $class) {
89 5
            $instance = singleton($class);
90
            $pageTypeName = $instance->i18n_singular_name();
91 5
            $result[$class] = $pageTypeName;
92
        }
93 5
94 5
        $fields->addFieldToTab(
95 5
            'Root.Main',
96 5
            DropdownField::create('SetClass', 'File type', $result, $this->owner->Classname)
0 ignored issues
show
Bug introduced by
The property Classname does not seem to exist. Did you mean ClassName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
97 5
                ->setEmptyString(''),
98 5
            'Content'
99
        );
100 5
101 5
        $fields->dataFieldByName('Image')
102 5
            ->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 1
            $config->removeComponentsByType(GridFieldAddNewButton::class);
110
            $products = $this->owner->Products();
111 1
            $productsField = GridField::create('Products', 'Products', $products, $config);
112
113
            $fields->addFieldToTab('Root.Products', $productsField);
114
        }
115
    }
116
117 1
    /**
118 1
     * 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
}