Completed
Push — master ( cfdfa6...b435f2 )
by Jason
28s
created

SlideImage::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 1

Importance

Changes 12
Bugs 0 Features 3
Metric Value
c 12
b 0
f 3
dl 0
loc 32
ccs 26
cts 26
cp 1
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
crap 1
1
<?php
2
3
class SlideImage extends DataObject implements PermissionProvider
4
{
5
    //TODO: move to config.yml
6
    private static $defaults = array(
1 ignored issue
show
Unused Code introduced by
The property $defaults 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...
7
        'ShowSlide' => true,
8
    );
9
10 1
    public function getCMSFields()
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...
11
    {
12 1
        $fields = parent::getCMSFields();
13 1
        $ImageField = new UploadField('Image', 'Image');
14 1
        $ImageField->getValidator()->allowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
15 1
        $ImageField->setFolderName('Uploads/SlideImages');
16 1
        $ImageField->setConfig('allowedMaxFileNumber', 1);
17 1
        $ImageField->getValidator()->setAllowedMaxFileSize(FLEXSLIDER_IMAGE_FILE_SIZE_LIMIT);
18
19 1
        $fields->removeByName(array('ShowSlide'));
20
21 1
        $fields->addFieldsToTab('Root.Main', array(
22 1
            TextField::create('Name')
23 1
                ->setDescription('for internal reference only'),
24 1
            TextField::create('Title')
25 1
                ->setDescription('optional, used in template'),
26 1
            TextareaField::create('Description')
27 1
                ->setDescription('optional, used in template'),
28 1
            TreeDropdownField::create('PageLinkID', 'Choose a page to link to:', 'SiteTree'),
29 1
            $ImageField,
30 1
            CheckboxField::create('ShowSlide')->setTitle('Show Slide')
31 1
                ->setDescription('Include this slide in the slider. Uncheck to hide'),
32 1
        ));
33 1
        $fields->removeByName(array(
34 1
            'SortOrder',
35 1
            'PageID',
36 1
        ));
37
38 1
        $this->extend('updateCMSFields', $fields);
39
40 1
        return $fields;
41
    }
42
43 6
    public function validate()
44
    {
45 6
        $result = parent::validate();
46
47 6
        if (!$this->Name) {
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<SlideImage>. 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...
48 1
            $result->error('A Name is required before you can save');
49 1
        }
50
51 6
        if (!$this->ImageID) {
0 ignored issues
show
Documentation introduced by
The property ImageID does not exist on object<SlideImage>. 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...
52 2
            $result->error('An Image is required before you can save');
53 2
        }
54
55 6
        return $result;
56
    }
57
58 1
    public function providePermissions()
59
    {
60
        return array(
61 1
            'Slide_EDIT' => 'Slide Edit',
62 1
            'Slide_DELETE' => 'Slide Delete',
63 1
            'Slide_CREATE' => 'Slide Create',
64 1
        );
65
    }
66 1
    public function canCreate($member = null)
67
    {
68 1
        return Permission::check('Slide_CREATE');
69
    }
70
71 1
    public function canEdit($member = null)
72
    {
73 1
        return Permission::check('Slide_EDIT');
74
    }
75
76 1
    public function canDelete($member = null)
77
    {
78 1
        return Permission::check('Slide_DELETE');
79
    }
80
81 1
    public function canView($member = null)
82
    {
83 1
        return true;
84
    }
85
}
86