Completed
Push — master ( e67fa1...90cabd )
by Nicolaas
01:26
created

createImageWithStyleListAndFolder()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 9
nop 2
1
<?php
2
3
4
5
class ImageWithStyleDataExtension 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...
6
{
7
    public function AddSelectImageList($fields, $tabName, $methodName, $folderName = '')
8
    {
9
        if ($this->owner->exists()) {
10
            $this->owner->createImageWithStyleListAndFolder($methodName, $folderName);
11
            $obj = $this->owner->$methodName();
12
            if ($obj && $obj->exists()) {
13
                $link = $obj->CMSEditLink();
0 ignored issues
show
Unused Code introduced by
$link is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
14
                $title = 'edit '.$obj->Title;
0 ignored issues
show
Unused Code introduced by
$title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
15
            } else {
16
                if (! $obj) {
17
                    $obj = Injector::inst()->get('ImagesWithStyleSelection');
18
                }
19
                $link = $obj->CMSAddLink();
0 ignored issues
show
Unused Code introduced by
$link is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
20
                $title = 'add '.$obj->singular_name();
0 ignored issues
show
Unused Code introduced by
$title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
21
            }
22
            $standardListName = $this->owner->folderToListName($folderName);
23
24
            $list = ImagesWithStyleSelection::get()->map()->toArray();
25
            $myList = ImagesWithStyleSelection::get()->filter(['Title' => $standardListName]);
26
            $myListObject = null;
27
            if ($myList->count() === 1) {
28
                $myListObject = $myList->first();
29
                if ($myListObject && $myListObject->exists()) {
30
                    $myID = $myListObject->ID;
31
                    $list[$myID] = ' *** '.$list[$myID]." [RECOMMENDED] ";
32
                    asort($list);
33
                }
34
            }
35
            $fields->addFieldsToTab(
36
                'Root.'.$tabName,
37
                [
38
                    HasOneButtonField::create($methodName, $this->owner->$methodName(), $this->owner),
39
                    LiteralField::create($methodName.'_OR', '<h2>OR</h2>'),
40
                    $imageListField = DropdownField::create(
41
                        $methodName.'ID',
42
                        'Select Existing Images List',
43
                        [0 => '--- Select ---'] + $list
44
                    )
45
                ]
46
            );
47
            if ($imageListField && $myListObject) {
48
                ImagesWithStyleCMSAPI::add_links_to_folder_field($imageListField, $myListObject);
49
            }
50
            $fieldID = $tabName.'ImageSelectionID';
51
            if ($this->owner->$fieldID) {
52
                $imageList = ImagesWithStyleSelection::get()->byID($this->owner->$fieldID);
53
                if ($imageList) {
54
                    ImagesWithStyleCMSAPI::add_links_to_folder_field($imageListField, $imageList);
55
                }
56
            }
57
            if ($obj->exists() && $obj->StyledImages()->count()) {
58
                $config = GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldAddNewButton');
59
                $fields->addFieldsToTab(
60
                    'Root.'.$tabName,
61
                    [
62
                        GridField::create(
63
                            $methodName.'_Images',
64
                            'Included Are',
65
                            $obj->StyledImages(),
66
                            $config
67
                        )
68
                    ]
69
                );
70
            }
71
        } else {
72
            $fields->addFieldsToTab(
73
                'Root.'.$tabName,
74
                [
75
                    LiteralField::create(
76
                        $methodName.'_LINK',
77
                        '<h2>First save this page and then add images to it.</h2>'
78
                    )
79
                ]
80
            );
81
        }
82
    }
83
84
    public function createImageWithStyleListAndFolder($methodName, $folderName = '')
85
    {
86
        //we add plus ten because they are not always identical
87
        if (strtotime($this->owner->LastEdited) > (strtotime($this->owner->Created) + 5)) {
88
            if ($folderName === '') {
89
                $folderName = $methodName.'-for-'.$this->owner->ClassName.'-'.$this->owner->ID;
90
            }
91
            $listName = $this->owner->folderToListName($folderName);
92
93
            $array = [
94
                'Title' => $listName
95
            ];
96
            $obj = ImagesWithStyleSelection::get()->filter($array)->first();
97
            if (! $obj) {
98
                $obj = ImagesWithStyleSelection::create($array);
99
            }
100
            $obj->write();
101
            if ($folderName) {
102
                $obj->createFolder($folderName);
103
            }
104
        }
105
    }
106
107
    public function folderToListName($folderName)
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...
108
    {
109
        return str_replace('/', '-', $folderName);
110
    }
111
}
112