1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* StartGeneratedWithDataObjectAnnotator |
5
|
|
|
* @method DataList|HeaderGalleryImage[] HeaderGallery |
6
|
|
|
* EndGeneratedWithDataObjectAnnotator |
7
|
|
|
*/ |
8
|
|
|
class HeaderGalleryExtension extends DataExtension |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private static $has_many = array( |
|
|
|
|
12
|
|
|
'HeaderGallery' => 'HeaderGalleryImage' |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Limit the number of displayed header images |
17
|
|
|
* If set to 1 we have a single upload field |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
private static $limit_header_images = 0; |
|
|
|
|
21
|
|
|
|
22
|
|
|
private static $delete_permission = "CMS_ACCESS_CMSMain"; |
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
public function updateCMSFields(FieldList $fields) |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var GridFieldConfig $conf |
29
|
|
|
*/ |
30
|
|
|
$conf = GridFieldConfig_RecordEditor::create(10); |
31
|
|
|
$conf->addComponent(new GridFieldSortableRows('SortOrder')); |
32
|
|
|
$conf->addComponent(new GridFieldGalleryTheme('Attachment')); |
33
|
|
|
$conf->addComponent(new GridFieldBulkUpload()); |
34
|
|
|
$conf->getComponentByType('GridFieldBulkUpload')->setUfSetup('setFolderName', 'header'); |
|
|
|
|
35
|
|
|
|
36
|
|
|
if (class_exists('GridFieldPaginatorWithShowAll')) { |
37
|
|
|
$conf->removeComponentsByType('GridFieldPaginator'); |
38
|
|
|
$conf->addComponent(new GridFieldPaginatorWithShowAll(10)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$fields->addFieldToTab( |
42
|
|
|
"Root." . _t('HeaderGalleryExtension.GalleryTabName', 'Header Gallery'), |
43
|
|
|
Gridfield::create( |
44
|
|
|
'HeaderGallery', |
45
|
|
|
_t('HeaderGalleryExtension.GalleryFieldTitle', 'Gallery in header'), |
46
|
|
|
$this->owner->HeaderGallery(), |
47
|
|
|
$conf |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Helper for getting the header gallery in the template |
54
|
|
|
* Tries to find the current gallery, falls back to parent pages and home-page or SiteConfig if it has a standard gallery |
55
|
|
|
* @return null|void |
56
|
|
|
*/ |
57
|
|
|
public function getHeaderPics() |
58
|
|
|
{ |
59
|
|
|
$owner = $this->getGalleryOwner(); |
60
|
|
|
|
61
|
|
|
if (!$owner->ID) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($owner->ID) && $owner->HeaderGallery()->count() > 0) { |
66
|
|
|
return $owner->HeaderGallery(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($owner->ParentID && $gallery = $this->getGalleryOfParent($owner)) { |
70
|
|
|
return $gallery; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->getDefaultHeaderPics(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getGalleryOwner() |
77
|
|
|
{ |
78
|
|
|
$owner = $this->owner; |
79
|
|
|
|
80
|
|
|
//overwrite owner in Translatable or Subsite setups |
81
|
|
|
$this->owner->extend('updateGalleryOwner', $owner); |
82
|
|
|
|
83
|
|
|
return $owner; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getGalleryOfParent($owner) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$Parent = $owner->Parent(); |
89
|
|
|
|
90
|
|
|
return (is_object($Parent) && $Parent->ID != 0) |
91
|
|
|
? $Parent->getHeaderPics() |
92
|
|
|
: null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Helper to get the default header pics from e.g. SiteConfig |
97
|
|
|
* @return DataList|null|SS_Limitable |
98
|
|
|
*/ |
99
|
|
|
public function getDefaultHeaderPics() |
100
|
|
|
{ |
101
|
|
|
$default = null; |
102
|
|
|
|
103
|
|
|
$this->owner->extend('updateDefaultHeaderPics', $default); |
104
|
|
|
|
105
|
|
|
if (!$default && class_exists('SiteConfig')) { |
106
|
|
|
$siteconfig = SiteConfig::current_site_config(); |
107
|
|
|
if ($siteconfig->hasMethod('HeaderGallery')) { |
108
|
|
|
$default = $this->LimitGalleryItems($siteconfig->HeaderGallery()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $default; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* limits the number of displayed items |
117
|
|
|
* |
118
|
|
|
* @param DataList $galleryList |
119
|
|
|
* @return DataList|SS_Limitable |
120
|
|
|
*/ |
121
|
|
|
protected function LimitGalleryItems(DataList $galleryList) |
122
|
|
|
{ |
123
|
|
|
$limit = Config::inst()->get('HeaderGalleryExtension', 'limit_header_images'); |
124
|
|
|
|
125
|
|
|
return $limit ? $galleryList->limit($limit) : $galleryList; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.