1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Products\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Products\Model\ProductFile; |
6
|
|
|
use Dynamic\Products\Page\Product; |
7
|
|
|
use SilverStripe\Core\ClassInfo; |
8
|
|
|
use SilverStripe\Dev\Debug; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\GridField\GridField; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter; |
13
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor; |
15
|
|
|
use SilverStripe\ORM\DataExtension; |
16
|
|
|
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton; |
17
|
|
|
|
18
|
|
|
class ProductFileDataExtension extends DataExtension |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
24
|
|
|
'Products' => Product::class, |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private static $summary_fields = array( |
|
|
|
|
31
|
|
|
'Title' => 'Title', |
32
|
|
|
'ProductsCt' => 'Products', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private static $searchable_fields = array( |
|
|
|
|
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
|
|
|
)); |
84
|
|
|
|
85
|
|
|
$classes = ClassInfo::subclassesFor(ProductFile::class); |
86
|
|
|
|
87
|
|
|
unset($classes['dynamic\products\model\productfile']); |
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) |
99
|
|
|
->setEmptyString(''), |
100
|
|
|
'Content' |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$fields->dataFieldByName('Image') |
104
|
|
|
->setFolderName('Uploads/ProductFiles/Images'); |
105
|
|
|
|
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
|
|
|
* Link for searchable result set. |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
|
|
public function getLink() |
122
|
|
|
{ |
123
|
|
|
return $this->owner->Download()->URL; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function getIsProductFile() |
130
|
|
|
{ |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|