HeaderGalleryExtension   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 10
dl 0
loc 120
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B updateCMSFields() 0 26 2
B getHeaderPics() 0 18 6
A getGalleryOwner() 0 9 1
A getGalleryOfParent() 0 8 3
A getDefaultHeaderPics() 0 15 4
A LimitGalleryItems() 0 6 2
1
<?php
2
3
/**
4
 * StartGeneratedWithDataObjectAnnotator
5
 * @method DataList|HeaderGalleryImage[] HeaderGallery
6
 * EndGeneratedWithDataObjectAnnotator
7
 */
8
class HeaderGalleryExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The property $limit_header_images is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
22
    private static $delete_permission = "CMS_ACCESS_CMSMain";
0 ignored issues
show
Unused Code introduced by
The property $delete_permission is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method setUfSetup() does not seem to exist on object<GridFieldComponent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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