|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class ProductDocDataExtension extends DataExtension |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var array |
|
7
|
|
|
*/ |
|
8
|
|
|
private static $belongs_many_many = array( |
|
9
|
|
|
'Products' => 'CatalogProduct', |
|
10
|
|
|
); |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private static $summary_fields = array( |
|
16
|
|
|
'Name' => 'Name', |
|
17
|
|
|
'Title' => 'Title', |
|
18
|
|
|
'ProductsCt' => 'Products', |
|
19
|
|
|
); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $searchable_fields = array( |
|
25
|
|
|
'Name', |
|
26
|
|
|
'Title', |
|
27
|
|
|
'Products.ID', |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return int |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getProductsCt() |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->owner->Products()->exists()) { |
|
36
|
|
|
return $this->owner->Products()->count(); |
|
37
|
|
|
} |
|
38
|
|
|
return 0; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getProductsList() |
|
45
|
|
|
{ |
|
46
|
|
|
$list = ''; |
|
47
|
|
|
|
|
48
|
|
|
if ($this->owner->Products()->exists()) { |
|
49
|
|
|
$i = 0; |
|
50
|
|
|
foreach($this->owner->Products() as $product) { |
|
51
|
|
|
$list .= $product->Title; |
|
52
|
|
|
if(++$i !== $this->owner->Products()->Count()) { |
|
53
|
|
|
$list .= ", "; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return $list; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param FieldList $fields |
|
62
|
|
|
*/ |
|
63
|
|
|
public function updateCMSFields(FieldList $fields) |
|
64
|
|
|
{ |
|
65
|
|
|
$fields->removeByName(array( |
|
66
|
|
|
'Link', |
|
67
|
|
|
'SortOrder', |
|
68
|
|
|
'Products', |
|
69
|
|
|
)); |
|
70
|
|
|
|
|
71
|
|
|
$classes = ClassInfo::subclassesFor('ProductDoc'); |
|
72
|
|
|
unset($classes['ProductDoc']); |
|
73
|
|
|
|
|
74
|
|
|
$result = array(); |
|
75
|
|
|
foreach ($classes as $class) { |
|
|
|
|
|
|
76
|
|
|
$instance = singleton($class); |
|
77
|
|
|
$pageTypeName = $instance->i18n_singular_name(); |
|
78
|
|
|
$result[$class] = $pageTypeName; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$fields->addFieldToTab( |
|
82
|
|
|
'Root.Main', |
|
83
|
|
|
DropdownField::create('SetClass', 'File type', $result, $this->owner->Classname) |
|
84
|
|
|
->setEmptyString(''), |
|
85
|
|
|
'Content' |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$fields->dataFieldByName('Image') |
|
89
|
|
|
->setFolderName('Uploads/ProductDocs/Images'); |
|
90
|
|
|
|
|
91
|
|
|
if ($this->owner->ID) { |
|
92
|
|
|
// products |
|
93
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
|
94
|
|
|
$config->removeComponentsByType('GridFieldAddExistingAutocompleter'); |
|
95
|
|
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
|
96
|
|
|
$config->removeComponentsByType('GridFieldAddNewButton'); |
|
97
|
|
|
$products = $this->owner->Products(); |
|
98
|
|
|
$productsField = GridField::create('Products', 'Products', $products, $config); |
|
99
|
|
|
|
|
100
|
|
|
$fields->addFieldToTab('Root.Products', $productsField); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Link for searchable result set |
|
106
|
|
|
* |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
public function Link() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->owner->Download()->URL; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getIsProductDoc() { |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.