1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Products\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField; |
6
|
|
|
use SilverStripe\Assets\File; |
7
|
|
|
use SilverStripe\Assets\Image; |
8
|
|
|
use SilverStripe\Forms\TextField; |
9
|
|
|
use SilverStripe\ORM\DataObject; |
10
|
|
|
|
11
|
|
|
class ProductFile extends DataObject |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $db = array( |
|
|
|
|
17
|
|
|
'Title' => 'Varchar(255)', |
18
|
|
|
'Content' => 'HTMLText', |
19
|
|
|
'FileLink' => 'Varchar(255)', |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $has_one = array( |
|
|
|
|
26
|
|
|
'Image' => Image::class, |
27
|
|
|
'Download' => File::class, |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private static $owns = [ |
|
|
|
|
34
|
|
|
'Image', |
35
|
|
|
'Download', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private static $table_name = 'ProductFile'; |
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private static $summary_fields = array( |
|
|
|
|
47
|
|
|
'Title' => 'Title', |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private static $searchable_fields = array( |
|
|
|
|
54
|
|
|
'Title', |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private static $default_sort = 'Title'; |
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \SilverStripe\Forms\FieldList |
64
|
|
|
*/ |
65
|
|
|
public function getCMSFields() |
66
|
|
|
{ |
67
|
|
|
$fields = parent::getCMSFields(); |
68
|
|
|
|
69
|
|
|
$file = UploadField::create('Download') |
70
|
|
|
->setAllowedMaxFileNumber(1) |
71
|
|
|
->setFolderName('Uploads/ProductFiles/File') |
72
|
|
|
->setAllowedFileCategories('document') |
73
|
|
|
->setDescription('Product file for download') |
74
|
|
|
; |
75
|
|
|
|
76
|
|
|
$fields->addFieldsToTab( |
77
|
|
|
'Root.Main', |
78
|
|
|
[ |
79
|
|
|
$file, |
80
|
|
|
TextField::create('FileLink') |
81
|
|
|
->setDescription('URL of external file. Will display on page if no file is specified.') |
82
|
|
|
->setAttribute('placeholder', 'http://'), |
83
|
|
|
], |
84
|
|
|
'Content' |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$fields->insertBefore( |
88
|
|
|
UploadField::create('Image') |
89
|
|
|
->setAllowedMaxFileNumber(1) |
90
|
|
|
->setFolderName('Uploads/ProductDocs/Images') |
91
|
|
|
->setAllowedFileCategories('image') |
92
|
|
|
->setDescription('Optional preview image of file'), |
93
|
|
|
'Content' |
|
|
|
|
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return $fields; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* if SetClass dropdown is set, create a new instance of the new class. |
101
|
|
|
*/ |
102
|
|
|
public function onAfterWrite() |
103
|
|
|
{ |
104
|
|
|
parent::onAfterWrite(); |
105
|
|
|
if (isset($_REQUEST['SetClass'])) { |
106
|
|
|
$object = $this->newClassInstance($_REQUEST['SetClass']); |
107
|
|
|
$object->write(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|