Completed
Push — master ( 16f652...622ef7 )
by James
06:19
created

CalendarEntry::getMonthDigit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
class CalendarEntry extends DataObject
4
{
5
6
    public static $db = array(
7
        "Title" => "Text",
8
        "Date" => "Date",
9
        "Time" => "Text",
10
        "Description" => "Text"
11
    );
12
13
    public static $has_one = array(
14
        "CalendarPage" => "CalendarPage",
15
        "Image" => "Image"
16
    );
17
18
    public static $summary_fields = array(
19
        "Date" => "Date",
20
        "Title" => "Title"
21
    );
22
23
    public static $default_sort = "Date ASC, Time ASC";
24
25
    public function validate()
26
    {
27
        $result = parent::validate();
28
        if (!$this->Title) {
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<CalendarEntry>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
29
            $result->error('Title is required');
30
        }
31
        if (!$this->Date) {
0 ignored issues
show
Documentation introduced by
The property Date does not exist on object<CalendarEntry>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
32
            $result->error('Date is required');
33
        }
34
        return $result;
35
    }
36
37
    public function getCMSFields()
38
    {
39
        $this->beforeUpdateCMSFields(function ($fields) {
40
            $datefield = new DateField('Date', 'Date (DD/MM/YYYY)*');
41
            $datefield->setConfig('showcalendar', true);
42
            $datefield->setConfig('dateformat', 'dd/MM/yyyy');
43
            
44
            $imageField = new UploadField('Image', 'Image');
45
            $imageField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
46
            $imageField->setConfig('allowedMaxFileNumber', 1);
47
            $imageField->setFolderName('Managed/CalendarImages');
48
            $imageField->setCanPreviewFolder(false);
49
50
            $fields->addFieldToTab('Root.Main', new TextField('Title', "Event Title*"));
51
            $fields->addFieldToTab('Root.Main', $datefield);
52
            $fields->addFieldToTab('Root.Main', new TextField('Time', "Time (HH:MM)"));
53
            $fields->addFieldToTab('Root.Main', new TextareaField('Description'));
54
            $fields->addFieldToTab('Root.Main', $imageField);
55
        });
56
57
        $fields = parent::getCMSFields();
58
59
        $this->extend('updateCMSFields', $fields);
60
61
        $fields->removeFieldFromTab("Root.Main", "CalendarPageID");
62
63
        return $fields;
64
    }
65
66
    public function getMonthDigit()
67
    {
68
        $date = strtotime($this->Date);
0 ignored issues
show
Documentation introduced by
The property Date does not exist on object<CalendarEntry>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
69
70
        return date('m', $date);
71
    }
72
    
73
    public function Link()
74
    {
75
      return $this->CalendarPage()->Link()."#".$this->ID;
0 ignored issues
show
Documentation Bug introduced by
The method CalendarPage does not exist on object<CalendarEntry>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
    }
77
78
    public function getYear()
79
    {
80
        $date = strtotime($this->Date);
0 ignored issues
show
Documentation introduced by
The property Date does not exist on object<CalendarEntry>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
81
82
        return date('Y', $date);
83
    }
84
85 View Code Duplication
    public function canCreate($members = null)
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...
86
    {
87
        $extended = $this->extendedCan(__FUNCTION__, $members);
88
        if ($extended !== null) {
89
            return $extended;
90
        }
91
92
        return true;
93
    }
94
95 View Code Duplication
    public function canEdit($members = null)
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...
96
    {
97
        $extended = $this->extendedCan(__FUNCTION__, $members);
98
        if ($extended !== null) {
99
            return $extended;
100
        }
101
102
        return true;
103
    }
104
105 View Code Duplication
    public function canDelete($members = null)
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...
106
    {
107
        $extended = $this->extendedCan(__FUNCTION__, $members);
108
        if ($extended !== null) {
109
            return $extended;
110
        }
111
112
        return true;
113
    }
114
115 View Code Duplication
    public function canView($members = null)
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...
116
    {
117
        $extended = $this->extendedCan(__FUNCTION__, $members);
118
        if ($extended !== null) {
119
            return $extended;
120
        }
121
122
        return true;
123
    }
124
}
125