TemplateoverviewDescription::Title()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 *@author nicolaas[at]sunnysideup.co.nz
5
 *@description: more details per template. Records are auto-added
6
 *@link: http://localhost/www.mysite.co.nz/admin/templates/TemplateoverviewDescription/13/edit
7
 **/
8
9
10
class TemplateoverviewDescription extends DataObject
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...
11
{
12
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
13
        "Title" => "Varchar",
14
        "Description" => "Text",
15
        "ToDoListHyperLink" => "Varchar(255)",
16
        "ClassNameLink" => "Varchar(120)"
17
    );
18
19
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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...
20
        "Image1" => "Image",
21
        "Image2" => "Image",
22
        "Image3" => "Image",
23
        "Image4" => "Image",
24
        "Image5" => "Image",
25
        "Image6" => "Image",
26
        "Image7" => "Image"
27
    );
28
29
    private static $searchable_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $searchable_fields 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...
30
        "ClassNameLink" => "PartialMatchFilter"
31
    );
32
33
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields 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...
34
        "ClassNameLink",
35
        "ToDoListHyperLink"
36
    );
37
38
    private static $field_labels = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $field_labels 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...
39
        "ClassNameLink" => "Page Type Name",
40
        "ToDoListHyperLink" => "Link to To Do"
41
    );
42
43
    private static $singular_name = 'Template Description';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name 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...
44
45
    private static $plural_name = 'Template Descriptions';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name 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...
46
47
    private static $default_sort = 'ClassNameLink ASC';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort 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...
48
49
    private static $indexes = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $indexes 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...
50
        "ClassNameLink" => true
51
    );
52
53
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $casting 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...
54
        "Title" => "Varchar"
55
    );
56
57
    /**
58
     * Location where we keep the template overview designs.
59
     * @var String
60
     */
61
    private static $image_folder_name = "templateoverview_advanced/designz";
0 ignored issues
show
Unused Code introduced by
The property $image_folder_name 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...
62
63
    /**
64
     * Location where we keep the template overview designs.
65
     * @var String
66
     */
67
    private static $image_source_folder = "";
0 ignored issues
show
Unused Code introduced by
The property $image_source_folder 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
    public function canCreate($member = null)
70
    {
71
        return false;
72
    }
73
74
    public function canDelete($member = null)
75
    {
76
        return true;
77
    }
78
79
    public function ClassNameLinkFancy()
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...
80
    {
81
        return implode(" ", preg_split('/(?<=\\w)(?=[A-Z])/', $this->ClassNameLink));
0 ignored issues
show
Bug introduced by
The property ClassNameLink does not seem to exist. Did you mean ClassName?

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...
82
        return preg_replace("/(?<=[^A-Z])([A-Z])/", "$1", $this->ClassNameLink);
0 ignored issues
show
Unused Code introduced by
return preg_replace('/(?... $this->ClassNameLink); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
83
    }
84
85
    public function Title()
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...
86
    {
87
        return $this->getTitle();
88
    }
89
    public function getTitle()
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...
90
    {
91
        return $this->ClassNameLinkFancy();
92
    }
93
94
    public function ModelAdminLink()
95
    {
96
        return TemplateoverviewDescriptionModelAdmin::get_full_url_segment().$this->ClassName."/EditForm/field/TemplateoverviewDescription/item/".$this->ID."/edit/";
97
    }
98
99
    public function getCMSFields()
100
    {
101
        $fields = parent::getCMSFields();
102
        $fields->removeByName("ClassNameLink");
103
        $fields->removeByName("Image1");
104
        $fields->removeByName("Image2");
105
        $fields->removeByName("Image3");
106
        $fields->removeByName("Image4");
107
        $fields->removeByName("Image5");
108
        $fields->addFieldToTab("Root.Design", new UploadField("Image1", "Design One"));
109
        $fields->addFieldToTab("Root.Design", new UploadField("Image2", "Design Two"));
110
        $fields->addFieldToTab("Root.Design", new UploadField("Image6", "Design Three"));
111
        $fields->addFieldToTab("Root.Instructions", new UploadField("Image3", "Instructions One"));
112
        $fields->addFieldToTab("Root.Instructions", new UploadField("Image4", "Instructions Two"));
113
        $fields->addFieldToTab("Root.Instructions", new UploadField("Image5", "Instructions Three"));
114
        $fields->addFieldToTab("Root.Instructions", new UploadField("Image7", "Instructions Four"));
115
        $fields->addFieldToTab("Root.Main", new HeaderField("ClassNameLinkInfo", "Details for: ".$this->ClassNameLink), "Description");
0 ignored issues
show
Bug introduced by
The property ClassNameLink does not seem to exist. Did you mean ClassName?

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...
116
        $fields->addFieldToTab("Root.Main", new LiteralField("BackLink", '<p><a href="'.$page->Link().'#sectionFor-'.$this->ClassNameLink.'">go back to template overview page</a> - dont forget to SAVE FIRST.</p>'));
0 ignored issues
show
Bug introduced by
The property ClassNameLink does not seem to exist. Did you mean ClassName?

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...
Bug introduced by
The variable $page does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
117
        return $fields;
118
    }
119
120
    public function requireDefaultRecords()
121
    {
122
        parent::requireDefaultRecords();
123
        $data = ClassInfo::subclassesFor("SiteTree");
124
        $fileList = null;
125
        if ($this->Config()->get("image_source_folder")) {
126
            $fileList = CMSHelp::get_list_of_files($this->Config()->get("image_source_folder"));
127
            if (!is_array($fileList)) {
128
                $fileList = null;
129
            } elseif (!count($fileList)) {
130
                $fileList = null;
131
            }
132
        }
133
        if ($fileList) {
134
            $destinationDir = Director::baseFolder()."/assets/".$this->Config()->get("image_folder_name")."/";
135
            $destinationFolder = Folder::find_or_make($this->Config()->get("image_folder_name"));
136
        }
137
        if ($data) {
138
            foreach ($data as $className) {
139
                $object = TemplateoverviewDescription::get()
140
                    ->filter(array("ClassNameLink" => $className))->First();
141
                if (!$object) {
142
                    $object = new TemplateoverviewDescription();
143
                    $object->ClassNameLink = $className;
0 ignored issues
show
Bug introduced by
The property ClassNameLink does not seem to exist. Did you mean ClassName?

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...
144
                    $object->write();
145
                    DB::alteration_message("adding template description for $className", "created");
146
                } else {
147
                    $otherObjects = TemplateoverviewDescription::get()
148
                    ->filter(array("ClassNameLink" => $className))->exclude(array("ID" => $object->ID));
149
                    foreach ($otherObjects as $otherObject) {
150
                        DB::alteration_message("Deleting superfluous TemplateoverviewDescription with Class Name Link: $className", "deleted");
151
                        $otherObject->delete();
152
                    }
153
                }
154
                if ($fileList) {
155
                    $i = 0;
156
                    foreach ($fileList as $fileArray) {
157
                        $explodeByDot = explode(".", $fileArray["FileName"]);
158
                        if (is_array($explodeByDot) && count($explodeByDot)) {
159
                            $base = $explodeByDot[0];
160
                            $explodeByUnderscore = explode("_", $base);
161
                            if (is_array($explodeByUnderscore) && count($explodeByUnderscore)) {
162
                                $base = $explodeByUnderscore[0];
163
                                $classNameOptionArray = array($className);
164
                                for ($j = 0; $j < 10; $j++) {
165
                                    $classNameOptionArray[] = $className.$j;
166
                                }
167
                                foreach ($classNameOptionArray as $potentialBase) {
168
                                    if ($base == $potentialBase) {
169
                                        $i++;
170
                                        $filename = "".$this->Config()->get("image_folder_name")."/".$fileArray["FileName"];
171
                                        if (!file_exists($destinationDir.$fileArray["FileName"])) {
172
                                            copy($fileArray["FullLocation"], $destinationDir.$fileArray["FileName"]);
0 ignored issues
show
Bug introduced by
The variable $destinationDir does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
173
                                        }
174
                                        $image = $Image::get()
0 ignored issues
show
Bug introduced by
The variable $Image does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
175
                                            ->filter(array("ParentID" => $destinationFolder->ID, "Name" => $fileArray["FileName"]))->First();
0 ignored issues
show
Bug introduced by
The variable $destinationFolder does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
176
                                        if (!$image) {
177
                                            $image = new Image();
178
                                            $image->ParentID = $destinationFolder->ID;
179
                                            $image->Filename = $filename;
180
                                            $image->Name = $fileArray["FileName"];
181
                                            $image->Title = $fileArray["Title"];
182
                                            $image->write();
183
                                        }
184
                                        $fieldName = "Image$i"."ID";
185
                                        if ($object->$fieldName != $image->ID) {
186
                                            $object->$fieldName = $image->ID;
187
                                            $object->write();
188
                                            DB::alteration_message("adding image to $className: ".$image->Title." (".$image->Filename.") using $fieldName field.", "created");
189
                                        }
190
                                    }
191
                                }
192
                            }
193
                        }
194
                    }
195
                } else {
196
                    DB::alteration_message("no design images found for $className", "deleted");
197
                }
198
            }
199
        }
200
        $helpDirectory = Director::baseFolder()."/".Config::inst()->get("CMSHelp", "help_file_directory_name")."/";
201
        if (!file_exists($helpDirectory)) {
202
            mkdir($helpDirectory);
203
        }
204
        $this->createManifestExcludeFile($helpDirectory);
205
206
        $devDirectory = Director::baseFolder()."/".Config::inst()->get("CMSHelp", "dev_file_directory_name")."/";
207
        if (!file_exists($devDirectory)) {
208
            mkdir($devDirectory);
209
        }
210
        $this->createManifestExcludeFile($devDirectory);
211
        $this->createHTACCESSDenyAll($devDirectory);
212
    }
213
214 View Code Duplication
    private function createManifestExcludeFile($dir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $myFile = $dir.'_manifest_exclude';
217
        if (!file_exists($myFile)) {
218
            $handle = fopen($myFile, 'w') or user_error('Cannot open file:  '.$myFile);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
219
            $data = '';
220
            fwrite($handle, $data);
221
        }
222
    }
223
224 View Code Duplication
    private function createHTACCESSDenyAll($dir)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
    {
226
        $myFile = $dir.'.htaccess';
227
        if (!file_exists($myFile)) {
228
            $handle = fopen($myFile, 'w') or user_error('Cannot open file:  '.$myFile);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
229
            $data = '
230
Order Deny,Allow
231
Deny from all
232
        ';
233
            fwrite($handle, $data);
234
        }
235
    }
236
237
    public function validate()
238
    {
239
        if ($this->ID) {
240
            if (
241
                TemplateoverviewDescription::get()
242
                    ->filter(array("ClassNameLink" => $this->ClassNameLink))
0 ignored issues
show
Bug introduced by
The property ClassNameLink does not seem to exist. Did you mean ClassName?

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...
243
                    ->exclude(array("ID" => $this->ID))
244
            ) {
245
                return new ValidationResult(false, _t("TemplateoverviewDescription.ALREADYEXISTS", "This template already exists"));
246
            }
247
        }
248
        return new ValidationResult();
249
    }
250
251
    public function onBeforeWrite()
252
    {
253
        parent::onBeforeWrite();
254
    }
255
256
    public function forTemplate()
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
        return $this->ClassNameLink;
0 ignored issues
show
Bug introduced by
The property ClassNameLink does not seem to exist. Did you mean ClassName?

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...
259
    }
260
}
261