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
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private static $searchable_fields = array( |
|
|
|
|
38
|
|
|
'Name', |
39
|
|
|
'Title', |
40
|
|
|
'Products.ID', |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
1 |
|
public function getProductsCt() |
47
|
|
|
{ |
48
|
1 |
|
if ($this->owner->Products()->exists()) { |
49
|
1 |
|
return $this->owner->Products()->count(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return 0; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
1 |
|
public function getProductsList() |
59
|
|
|
{ |
60
|
1 |
|
$list = ''; |
61
|
|
|
|
62
|
1 |
|
if ($this->owner->Products()->exists()) { |
63
|
1 |
|
$i = 0; |
64
|
1 |
|
foreach ($this->owner->Products() as $product) { |
65
|
1 |
|
$list .= $product->Title; |
66
|
1 |
|
if (++$i !== $this->owner->Products()->Count()) { |
67
|
1 |
|
$list .= ', '; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $list; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param FieldList $fields |
77
|
|
|
*/ |
78
|
5 |
|
public function updateCMSFields(FieldList $fields) |
79
|
|
|
{ |
80
|
5 |
|
$fields->removeByName(array( |
81
|
5 |
|
'Link', |
82
|
|
|
'SortOrder', |
83
|
|
|
'Products', |
84
|
|
|
)); |
85
|
|
|
|
86
|
5 |
|
$classes = ClassInfo::subclassesFor(ProductDoc::class); |
87
|
5 |
|
unset($classes['ProductDoc']); |
88
|
|
|
|
89
|
5 |
|
$result = array(); |
90
|
5 |
|
foreach ($classes as $class) { |
91
|
5 |
|
$instance = singleton($class); |
92
|
5 |
|
$pageTypeName = $instance->i18n_singular_name(); |
93
|
5 |
|
$result[$class] = $pageTypeName; |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
$fields->addFieldToTab( |
97
|
5 |
|
'Root.Main', |
98
|
5 |
|
DropdownField::create('SetClass', 'File type', $result, $this->owner->Classname) |
99
|
5 |
|
->setEmptyString(''), |
100
|
5 |
|
'Content' |
101
|
|
|
); |
102
|
|
|
|
103
|
5 |
|
$fields->dataFieldByName('Image') |
104
|
5 |
|
->setFolderName('Uploads/ProductDocs/Images'); |
105
|
|
|
|
106
|
5 |
|
if ($this->owner->ID) { |
|
|
|
|
107
|
|
|
// products |
108
|
|
|
/* |
|
|
|
|
109
|
|
|
$config = GridFieldConfig_RelationEditor::create(); |
110
|
|
|
$config->removeComponentsByType(GridFieldAddExistingAutocompleter::class); |
111
|
|
|
$config->addComponent(new GridFieldAddExistingSearchButton()); |
112
|
|
|
$config->removeComponentsByType(GridFieldAddNewButton::class); |
113
|
|
|
$products = $this->owner->Products(); |
114
|
|
|
$productsField = GridField::create('Products', 'Products', $products, $config); |
115
|
|
|
|
116
|
|
|
$fields->addFieldToTab('Root.Products', $productsField); |
117
|
|
|
*/ |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Link for searchable result set. |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
1 |
|
public function Link() |
127
|
|
|
{ |
128
|
1 |
|
return $this->owner->Download()->URL; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
1 |
|
public function getIsProductDoc() |
135
|
|
|
{ |
136
|
1 |
|
return true; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|