AdvertisementDecorator::updateCMSFields()   F
last analyzed

Complexity

Conditions 16
Paths 577

Size

Total Lines 138

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 138
rs 1.5899
c 0
b 0
f 0
cc 16
nc 577
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 *@author nicolaas [at] sunnysideup.co.nz
5
 *
6
 *
7
 **/
8
9
class AdvertisementDecorator extends SiteTreeExtension
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...
10
{
11
12
    /**
13
     * load an alternative collection of JS file to power your
14
     * slideslow
15
     * see yml files for example
16
     * @var Array
17
     */
18
    private static $alternative_javascript_file_array = array();
0 ignored issues
show
Unused Code introduced by
The property $alternative_javascript_file_array 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...
19
20
    /**
21
     * load an alternative collection of JS file to power your
22
     * slideslow
23
     * see yml files for example
24
     * @var Array
25
     */
26
    private static $add_advertisements_shown_on_no_pages_to_all_pages = true;
0 ignored issues
show
Unused Code introduced by
The property $add_advertisements_shown_on_no_pages_to_all_pages 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...
27
28
    public static function add_requirements($alternativeFileLocation = null)
29
    {
30
        Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
31
        $jsFileArray = Config::inst()->get("AdvertisementDecorator", "alternative_javascript_file_array");
32
33
        if (count($jsFileArray)) {
34
            foreach ($jsFileArray as $file) {
0 ignored issues
show
Bug introduced by
The expression $jsFileArray of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
35
                Requirements::javascript($file);
36
            }
37
        } else {
38
            Requirements::javascript("advertisements/javascript/Advertisements.js");
39
            $file = "";
40
            $customJavascript = Config::inst()->get("AdvertisementDecorator", "use_custom_javascript");
41
            if ($customJavascript == 1) {
42
                $file = project()."/javascript/AdvertisementsExecutive.js";
43
            } elseif ($alternativeFileLocation) {
44
                $file = $alternativeFileLocation;
45
            }
46
            if (!$file) {
47
                $file = "advertisements/javascript/AdvertisementsExecutive.js";
48
            }
49
            Requirements::javascript($file);
50
            Requirements::themedCSS("Advertisements", "advertisements");
51
        }
52
    }
53
54
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db 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...
55
        "UseParentAdvertisements" => "Boolean"
56
    );
57
58
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one 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...
59
        "AdvertisementsFolder" => "Folder",
60
        "AdvertisementStyle" => "AdvertisementStyle"
61
    );
62
63
    private static $many_many = array(
0 ignored issues
show
Unused Code introduced by
The property $many_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...
64
        "Advertisements" => "Advertisement"
65
    );
66
67
    private static $use_custom_javascript = false;
0 ignored issues
show
Unused Code introduced by
The property $use_custom_javascript 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...
68
69
    private static $page_classes_without_advertisements = array();
0 ignored issues
show
Unused Code introduced by
The property $page_classes_without_advertisements 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...
70
71
    private static $specific_name_for_advertisements = "Advertisements";
0 ignored issues
show
Unused Code introduced by
The property $specific_name_for_advertisements 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...
72
73
    private static $page_classes_with_advertisements = array();
0 ignored issues
show
Unused Code introduced by
The property $page_classes_with_advertisements 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...
74
75
    private static $advertisements_dos = null;
0 ignored issues
show
Unused Code introduced by
The property $advertisements_dos 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...
76
77
    public function updateCMSFields(FieldList $fields)
78
    {
79
        if ($this->classHasAdvertisements($this->owner->ClassName)  && $this->owner->exists()) {
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
            $tabName = $this->MyTabName();
81
            //advertisements shown...
82
            $where = '1 = 1';
83
            if ($this->owner->AdvertisementsFolderID) {
84
                $images = Image::get()->filter("ParentID", $this->owner->AdvertisementsFolderID);
0 ignored issues
show
Bug introduced by
The property AdvertisementsFolderID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
                if ($images->count()) {
86
                    $where = "\"AdvertisementImageID\" IN (".implode(",", $images->column("ID")).")";
87
                } else {
88
                    $where = " 1 = 2";
89
                }
90
            }
91
92
            $source = Advertisement::get();
93
            if ($source && $source->count()) {
94
                $newSource = [];
95
                foreach ($source as $ad) {
96
                    $newSource[$ad->ID] = $ad->getFullTitle('width: 75px!important;');
97
                }
98
                $fields->addFieldToTab($tabName, CheckboxSetField::create(
99
                    'Advertisements',
100
                    'Select '.Config::inst()->get("Advertisement", "plural_name"),
101
                    $newSource
102
                ));
103
            }
104
105
            //$advertisementsCount = DB::query("SELECT COUNT(ID) FROM \"Advertisement\" $whereDB ;")->value();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
            $advertisements = $this->owner->Advertisements()->where($where);
107
            $txt = sprintf(_t("AdvertisementDecorator.ACTUAL", 'Current %1$s Shown'), Config::inst()->get("Advertisement", "plural_name"));
108
            $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
109
            $txt = sprintf(_t("AdvertisementDecorator.SELECT", 'Select %1$s to show ... '), Config::inst()->get("Advertisement", "plural_name"));
110
            $advertisementsGridField = new GridField('AdvertisementsList', $txt, $this->owner->Advertisements(), GridFieldConfig_RelationEditor::create());
111
            $fields->addFieldToTab($tabName, $advertisementsGridField);
112
            if (Config::inst()->get("Advertisement", "resize_images") == 'no') {
113
                $totalSize = 0;
114
                foreach ($this->owner->Advertisements() as $advertisement) {
115
                    $totalSize += $advertisement->AdvertisementImage()->getAbsoluteSize();
116
                }
117
                $seconds = round(($totalSize + 1) / 524288)    ;
118
                $fields->addFieldToTab($tabName, new LiteralField("TotalSize", '<p><em>Total download size: '.File::format_size($totalSize).', good reception 3G network download time less than ~'.($seconds+1).' seconds.</em></p>'));
119
            }
120
            if (class_exists("DataObjectSorterController")) {
121
                $shownAdvertisements = $this->owner->getManyManyComponents('Advertisements');
122
                if ($shownAdvertisements) {
123
                    $array = $shownAdvertisements->column("ID");
124
                    $idString = implode(",", $array);
125
                    $link = DataObjectSorterController::popup_link("Advertisement", $filterField = "ID", $filterValue = $idString, $linkText = "sort ".Config::inst()->get("Advertisement", "plural_name"), $titleField = "FullTitle");
126
                    $fields->addFieldToTab($tabName, new LiteralField("AdvertisementsSorter", $link));
127
                }
128
            }
129
            if ($advertisements->count()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
130
            } else {
131
                $txt = sprintf(
132
                    _t("AdvertisementDecorator.CREATE", '<p>Please <a href="admin/%1$s/">create %2$s</a> on the <a href="admin/%1$s/">%3$s tab</a> first, or see below on how to create %2$s from a folder.</p>'),
133
                    Config::inst()->get("AdvertisementAdmin", "url_segment"),
134
                    Config::inst()->get("Advertisement", "plural_name"),
135
                    Config::inst()->get("AdvertisementAdmin", "menu_title")
136
                );
137
                $fields->addFieldToTab($tabName, new LiteralField("AdvertisementsHowToCreate", $txt));
138
            }
139
            if ($parent = $this->advertisementParent()) {
140
                $txt = sprintf(_t("AdvertisementDecorator.ORUSE", 'OR  ... use %1$s from  <i>%2$s</i>.'), Config::inst()->get("Advertisement", "plural_name"), $parent->Title);
141
                $fields->addFieldToTab($tabName, new CheckboxField("UseParentAdvertisements", $txt));
142
            }
143
144
145
            $txt = _t('AdvertisementDecorator.ADVANCED', 'Advanced');
146
            $fields->addFieldToTab($tabName, $this->MyHeaderFieldLarge($txt));
147
148
            //create new advertisements
149
            $txt = sprintf(_t("AdvertisementDecorator.CREATE", 'Create new %1$s'), Config::inst()->get("Advertisement", "plural_name"));
150
            $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
151
            $txt = sprintf(
152
                _t(
153
                    "AdvertisementDecorator.CREATENEWFROMFOLDER_EXPLANATION",
154
                    'Create New %1$s from images in the folder selected - each image in the folder will be used to create a %3$s. %2$s'
155
                ),
156
                Config::inst()->get("Advertisement", "plural_name"),
157
                Advertisement::recommended_image_size_statement(),
158
                Config::inst()->get("Advertisement", "singular_name")
159
            );
160
            if (Folder::get()->count()) {
161
                $fields->addFieldToTab(
162
                    $tabName,
163
                    $treeDropdownField = new TreeDropdownField(
164
                        'AdvertisementsFolderID',
165
                        _t("AdvertisementDecorator.CREATENEWFROMFOLDER", "Create from folder"),
166
                        'Folder'
167
                    )
168
                );
169
                $treeDropdownField->setRightTitle($txt);
170
            }
171
172
            $styles = AdvertisementStyle::get();
173
            if ($styles->count()) {
174
                $fields->addFieldToTab($tabName, $this->MyHeaderField("Style"));
175
                $list = $styles->map("ID", "Title", $emptyString = _t("AdvertisementDecorator.SELECTSTYLE", "--select style--"), $sortByTitle = true);
0 ignored issues
show
Unused Code introduced by
The call to DataList::map() has too many arguments starting with $emptyString = _t('Adver...E', '--select style--').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
176
                $fields->addFieldToTab(
177
                    $tabName,
178
                    $selectStyleField = new DropdownField(
179
                        "AdvertisementStyleID",
180
                        _t("AdvertisementDecorator.STYLECREATED", "Select style"),
181
                        $list
182
                    )
183
                );
184
                $selectStyleField->setRightTitle(_t("AdvertisementDecorator.STYLECREATED_EXPLANATION", "Styles are created by your developer"));
185
            }
186
187
188
            $txt = sprintf(_t("AdvertisementDecorator.EDIT", 'Edit %1$s'), Config::inst()->get("Advertisement", "plural_name"));
0 ignored issues
show
Unused Code introduced by
$txt 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...
189
            $txt = sprintf(
190
                _t("AdvertisementDecorator.PLEASEMANAGEEXISTING", '<p>Please manage existing %1$s on the <a href="admin/%2$s/">%3$s tab</a>.</p>'),
191
                Config::inst()->get("Advertisement", "plural_name"),
192
                Config::inst()->get("AdvertisementAdmin", "url_segment"),
193
                Config::inst()->get("AdvertisementAdmin", "menu_title")
194
            );
195
            $fields->addFieldToTab($tabName, new LiteralField("ManageAdvertisements", $txt));
196
            $txt = sprintf(_t("AdvertisementDecorator.DELETE", 'Delete %1$s'), Config::inst()->get("Advertisement", "plural_name"));
197
            $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
198
            $page = SiteTree::get()->byID($this->owner->ID);
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Unused Code introduced by
$page 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...
199
200
            $txtRemove = sprintf(_t("AdvertisementDecorator.REMOVE", 'Remove all %1$s from this page (%1$s will not be deleted but are not longer associated with this page)'), Config::inst()->get("Advertisement", "plural_name"));
201
            $txtConfirmRemove = sprintf(_t("AdvertisementDecorator.CONFIRMREMOVE", 'Are you sure you want to remove all %1$s from this page?'), Config::inst()->get("Advertisement", "plural_name"));
202
            $removeallLink = 'advertisements/removealladvertisements/'.$this->owner->ID.'/';
203
            $jquery = 'if(confirm(\''.$txtConfirmRemove.'\')) {jQuery(\'#removealladvertisements\').load(\''.$removeallLink.'\');} return false;';
204
            $fields->addFieldToTab($tabName, new LiteralField("removealladvertisements", '<p class="message warning"><a href="'.$removeallLink.'" onclick="'.$jquery.'"  id="removealladvertisements"  class="ss-ui-button">'.$txtRemove.'</a></p>'));
205
206
            $txtDelete = sprintf(_t("AdvertisementDecorator.DELETE", 'Delete all %1$s from this website (but not the images associated with them)'), Config::inst()->get("Advertisement", "plural_name"));
207
            $txtConfirmDelete = sprintf(_t("AdvertisementDecorator.CONFIRMDELETE", 'Are you sure you want to delete all %1$s - there is no UNDO?'), Config::inst()->get("Advertisement", "plural_name"));
208
            $deleteallLink = 'advertisements/deletealladvertisements/'.$this->owner->ID.'/';
209
            $jquery = 'if(confirm(\''.$txtConfirmDelete.'\')) {jQuery(\'#deletealladvertisements\').load(\''.$deleteallLink.'\');} return false;';
210
            $fields->addFieldToTab($tabName, new LiteralField("deletealladvertisements", '<p class="message bad"><a href="'.$deleteallLink.'" onclick="'.$jquery.'"  id="deletealladvertisements" class="ss-ui-button">'.$txtDelete.'</a></p>'));
211
        }
212
213
        return $fields;
214
    }
215
216
    protected function MyTabName()
217
    {
218
        $code = preg_replace("/[^a-zA-Z0-9\s]/", " ", Config::inst()->get("AdvertisementAdmin", "menu_title"));
219
        $code = str_replace(" ", "", $code);
220
        return "Root.".$code;
221
    }
222
223
    protected function MyHeaderField($title)
224
    {
225
        $code = preg_replace("/[^a-zA-Z0-9\s]/", "", $title);
226
        $code = str_replace(" ", "", $code);
227
        return new LiteralField($code, "<h4 style='margin-top: 20px'>$title</h4>");
228
    }
229
230
    protected function MyHeaderFieldLarge($title)
231
    {
232
        $code = preg_replace("/[^a-zA-Z0-9\s]/", "", $title);
233
        $code = str_replace(" ", "", $code);
234
        return new LiteralField($code, "<h2 style='margin-top: 40px'>$title</h2>");
235
    }
236
237
    public function AdvertisementSet($style = null)
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...
238
    {
239
        if ($this->classHasAdvertisements($this->owner->ClassName)) {
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
240
            $browseSet = $this->owner->advertisementsToShow();
241
            if ($browseSet) {
242
                $file = null;
243
                if ($this->owner->AdvertisementStyleID) {
0 ignored issues
show
Bug introduced by
The property AdvertisementStyleID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
244
                    $style = $this->owner->AdvertisementStyle();
245
                }
246
                if ($style) {
247
                    $file = $style->FileLocation;
248
                }
249
                self::add_requirements($file);
250
251
                return $browseSet;
252
            }
253
        }
254
    }
255
256
    protected function advertisementParent()
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...
257
    {
258
        $parent = null;
259
        if ($this->owner->ParentID) {
260
            $parent = SiteTree::get()->byID($this->owner->ParentID);
0 ignored issues
show
Bug introduced by
The property ParentID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
261
        } elseif ($this->owner->URLSegment != "home") {
0 ignored issues
show
Bug introduced by
The property URLSegment does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
262
            $parent = SiteTree::get()->where("URLSegment = 'home' AND \"ClassName\" <> 'RedirectorPage'")->First();
263
            if (!$parent) {
264
                if (class_exists("HomePage")) {
265
                    $parent = HomePage::get()->First();
266
                } else {
267
                    $parent = Page::get()->filter(array("URLSegment" => "home"))->First();
268
                }
269
            }
270
        }
271
        if ($parent) {
272
            if ($this->classHasAdvertisements($parent->ClassName)) {
273
                return $parent;
274
            }
275
        }
276
    }
277
278
    public function onBeforeWrite()
279
    {
280
        parent::onBeforeWrite();
281
        $bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`";
0 ignored issues
show
Unused Code introduced by
$bt 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...
282
        if ($this->classHasAdvertisements($this->owner->ClassName)) {
0 ignored issues
show
Bug introduced by
The property ClassName does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
283
            $objects = array(0 => 0);
284
            $images = array(0 => 0);
285
            $dos1 = $this->advertisementsToShow();
286
            if ($dos1) {
287
                foreach ($dos1 as $obj) {
288
                    $images[$obj->ID] = $obj->AdvertisementImageID;
289
                    $objects[$obj->ID] = $obj->ID;
290
                }
291
            }
292
            //check for non-existing images and delete advertisements associated with it
293
294
            foreach ($images as $objectID => $imageID) {
295
                if (!Image::get()->byID($imageID)) {
296
                    $obj = Advertisement::get()->byID($objectID);
297
                    if ($obj) {
298
                        $obj->delete();
299
                        $obj->destroy();
300
                        unset($objects[$objectID]);
301
                    }
302
                }
303
            }
304
            //check if a folder has been set and create objects
305
            if ($this->owner->AdvertisementsFolderID) {
306
                $dos2 = Image::get()
307
                    ->where("\"File\".\"ParentID\" = ".$this->owner->AdvertisementsFolderID." AND \"Advertisement\".\"AdvertisementImageID\" IS NULL ")
0 ignored issues
show
Bug introduced by
The property AdvertisementsFolderID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
308
                    ->leftJoin("Advertisement", "\"Advertisement\".\"AdvertisementImageID\" = \"File\".\"ID\" ");
309
                if ($dos2->count()) {
310
                    $advertisementsToAdd = array();
0 ignored issues
show
Unused Code introduced by
$advertisementsToAdd 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...
311
                    foreach ($dos2 as $image) {
312
                        $newAdvertisement = new Advertisement();
313
                        $newAdvertisement->AdvertisementImageID = $image->ID;
0 ignored issues
show
Documentation introduced by
The property AdvertisementImageID does not exist on object<Advertisement>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
314
                        $newAdvertisement->Title = $image->Title;
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<Advertisement>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
315
                        $newAdvertisement->AutoAdded = true;
0 ignored issues
show
Documentation introduced by
The property AutoAdded does not exist on object<Advertisement>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
316
                        $newAdvertisement->write();
317
                        $objects[$newAdvertisement->ID] = $newAdvertisement->ID;
318
                    }
319
                    $this->owner->Advertisements()->addMany($objects);
320
                }
321
            }
322
            if ($this->owner->AdvertisementStyleID) {
323
                if (!AdvertisementStyle::get()->byID($this->owner->AdvertisementStyleID)) {
324
                    $this->owner->AdvertisementStyleID = 0;
0 ignored issues
show
Bug introduced by
The property AdvertisementStyleID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
325
                }
326
            }
327
            //remove advdertisements if parent is being used...
328
            if ($this->owner->UseParentAdvertisements) {
329
                if ($this->advertisementParent()) {
330
                    $combos = $this->owner->Advertisements();
331
                    if ($combos) {
332
                        $combos->removeAll();
333
                    }
334
                } else {
335
                    $this->owner->UseParentAdvertisements  = false;
0 ignored issues
show
Bug introduced by
The property UseParentAdvertisements does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
336
                }
337
            }
338
        }
339
    }
340
341
    /**
342
     *
343
     *
344
     * @return DataList
345
     */
346
    public function advertisementsToShow()
347
    {
348
        $array = array(0 => 0);
349
        if ($this->owner->exists()) {
350
            if ($this->owner->UseParentAdvertisements) {
0 ignored issues
show
Bug introduced by
The property UseParentAdvertisements does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
351
                $parent = $this->owner->advertisementParent();
352
                if ($parent) {
353
                    return $parent->advertisementsToShow();
354
                }
355
            }
356
            //from page
357
            $objects1 = $this->owner->Advertisements();
358
            if ($objects1->count()) {
359
                $array += $objects1->map('ID', 'ID')->toArray();
360
            }
361
            if (Config::inst()->get(self::class, 'add_advertisements_shown_on_no_pages_to_all_pages')) {
362
                //shown on all pages ...
363
                $objects2 = Advertisement::get()
364
                    ->leftJoin('SiteTree_Advertisements', 'Advertisement.ID = AdvertisementID')
365
                    ->where('AdvertisementID IS NULL');
366
                if ($objects2->count()) {
367
                    $array += $objects2->map('ID', 'ID')->toArray();
368
                }
369
            }
370
        }
371
        return Advertisement::get()->filter(array('ID' => $array));
372
    }
373
374
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
375
    protected function getResizedAdvertisements(){
376
377
    }
378
    */
379
380
    protected function classHasAdvertisements($className)
381
    {
382
        //assumptions:
383
        //1. in general YES
384
        //2. if list of WITH is shown then it must be in that
385
        //3. otherwise check if it is specifically excluded (WITHOUT)
386
        $result = true;
387
        $inc =  Config::inst()->get("AdvertisementDecorator", "page_classes_with_advertisements");
388
        $exc =  Config::inst()->get("AdvertisementDecorator", "page_classes_without_advertisements");
389
        if (is_array($inc) && count($inc)) {
390
            $result = false;
391
            if (in_array($className, $inc)) {
392
                $result = true;
393
            }
394
        } elseif (is_array($exc) && count($exc) && in_array($className, $exc)) {
395
            $result = false;
396
        }
397
        return $result;
398
    }
399
}
400