Completed
Pull Request — master (#88)
by Jason
02:34
created

Location::providePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
class Location extends DataObject implements PermissionProvider
4
{
5
    private static $db = array(
1 ignored issue
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...
6
        'Title' => 'Varchar(255)',
7
        'Featured' => 'Boolean',
8
        'Website' => 'Varchar(255)',
9
        'Phone' => 'Varchar(40)',
10
        'Email' => 'Varchar(255)',
11
        'EmailAddress' => 'Varchar(255)',
12
        'ShowInLocator' => 'Boolean',
13
    );
14
15
    private static $has_one = array(
1 ignored issue
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...
16
        'Category' => 'LocationCategory',
17
    );
18
19
    private static $casting = array(
1 ignored issue
show
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...
20
        'distance' => 'Int',
21
    );
22
23
    private static $default_sort = 'Title';
0 ignored issues
show
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...
24
25
    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...
26
        'ShowInLocator' => true,
27
    );
28
29
    private static $singular_name = 'Location';
1 ignored issue
show
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...
30
    private static $plural_name = 'Locations';
1 ignored issue
show
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...
31
32
    // api access via Restful Server module
33
    private static $api_access = true;
0 ignored issues
show
Unused Code introduced by
The property $api_access 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
35
    // search fields for Model Admin
36
    private static $searchable_fields = array(
1 ignored issue
show
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...
37
        'Title',
38
        'Address',
39
        'Suburb',
40
        'State',
41
        'Postcode',
42
        'Country',
43
        'Website',
44
        'Phone',
45
        'Email',
46
        'Category.ID',
47
        'ShowInLocator',
48
        'Featured',
49
    );
50
51
    // columns for grid field
52
    private static $summary_fields = array(
1 ignored issue
show
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...
53
        'Title',
54
        'Address',
55
        'Suburb',
56
        'State',
57
        'Postcode',
58
        'Country',
59
        'Category.Name',
60
        'ShowInLocator.NiceAsBoolean',
61
        'Featured.NiceAsBoolean',
62
        'Coords',
63
    );
64
65
    // Coords status for $summary_fields
66 1
    public function getCoords()
67
    {
68 1
        return ($this->Lat != 0 && $this->Lng != 0) ? 'true' : 'false';
0 ignored issues
show
Documentation introduced by
The property Lat does not exist on object<Location>. 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...
Documentation introduced by
The property Lng does not exist on object<Location>. 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
71
    // custom labels for fields
72 1
    public function fieldLabels($includerelations = true)
73
    {
74 1
        $labels = parent::fieldLabels();
75
76 1
        $labels['Title'] = 'Name';
77 1
        $labels['Suburb'] = 'City';
78 1
        $labels['Postcode'] = 'Postal Code';
79 1
        $labels['ShowInLocator'] = 'Show';
80 1
        $labels['ShowInLocator.NiceAsBoolean'] = 'Show';
81 1
        $labels['Category.Name'] = 'Category';
82 1
        $labels['Category.ID'] = 'Category';
83 1
        $labels['Email'] = 'Email';
84 1
        $labels['Featured.NiceAsBoolean'] = 'Featured';
85
        $labels['Coords'] = 'Coords';
86 1
87
        return $labels;
88
    }
89 1
90
    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...
91 1
    {
92 1
        $fields = FieldList::create(
93 1
            new TabSet(
94 1
                $name = 'Root',
95 1
                new Tab(
96 1
                    $title = 'Main',
97 1
                    HeaderField::create('ContactHD', 'Contact Information'),
98 1
                    TextField::create('Title', 'Name'),
99 1
                    TextField::create('Phone'),
100 1
                    EmailField::create('Email', 'Email'),
101 1
                    TextField::create('Website')
102 1
                        ->setAttribute('placeholder', 'http://'),
103 1
                    DropDownField::create('CategoryID', 'Category', LocationCategory::get()->map('ID', 'Title'))
104 1
                        ->setEmptyString('-- select --'),
105 1
                    CheckboxField::create('ShowInLocator', 'Show in results')
106 1
                        ->setDescription('Location will be included in results list'),
107 1
                    CheckboxField::create('Featured')
108 1
                        ->setDescription('Location will show at/near the top of the results list')
109 1
                )
110 1
            )
111
        );
112
113 1
        // allow to be extended via DataExtension
114
        $this->extend('updateCMSFields', $fields);
115
116 1
        // override Suburb field name
117
        $fields->dataFieldByName('Suburb')->setTitle('City');
118 1
119
        return $fields;
120
    }
121 5
122
    public function validate()
123 5
    {
124
        $result = parent::validate();
125 5
126
        return $result;
127
    }
128
129
    public function EmailAddress()
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...
130
    {
131
        Deprecation::notice('3.0', 'Use "$Email" instead.');
132
        if ($this->Email) {
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<Location>. 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...
133
            return $this->Email;
0 ignored issues
show
Documentation introduced by
The property Email does not exist on object<Location>. 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...
134
        } elseif ($this->EmailAddress) {
0 ignored issues
show
Documentation introduced by
The property EmailAddress does not exist on object<Location>. 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...
135
            return $this->EmailAddress;
0 ignored issues
show
Documentation introduced by
The property EmailAddress does not exist on object<Location>. 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...
136
        }
137
138
        return false;
139
    }
140
141
    public function getCustomSearchContext()
142
    {
143
        $fields = $this->scaffoldSearchFields(array(
144
            'restrictFields' => array('Address','Category.ID')
145 1
        ));
146
147 1
        $filters = array(
148
            'Address' => new PartialMatchFilter('Address'),
149
            'Suburb' => new PartialMatchFilter('Suburb'),
150 1
            'State' => new PartialMatchFilter('State'),
151
            'Postcode' => new PartialMatchFilter('Postcode'),
152 1
            'Country' => new PartialMatchFilter('Postcode'),
153
            'CategoryID' => new ExactMatchFilter('CategoryID'),
154
        );
155 1
156
        return new SearchContext(
157 1
            $this->class,
158
            $fields,
159
            $filters
160 1
        );
161
    }
162 1
163
    /**
164
     * @param Member $member
0 ignored issues
show
Documentation introduced by
Should the type for parameter $member not be false|Member?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
165
     *
166
     * @return bool
167
     */
168
    public function canView($member = false)
169
    {
170
        return true;
171
    }
172
173
    public function canEdit($member = false)
174 20
    {
175
        return Permission::check('Location_EDIT');
176 20
    }
177 20
178
    public function canDelete($member = false)
179 1
    {
180
        return Permission::check('Location_DELETE');
181
    }
182
183
    public function canCreate($member = false)
184
    {
185
        return Permission::check('Location_CREATE');
186
    }
187
188
    public function providePermissions()
189
    {
190
        return array(
191
            'Location_EDIT' => 'Edit a Location',
192
            'Location_DELETE' => 'Delete a Location',
193
            'Location_CREATE' => 'Create a Location',
194
        );
195
    }
196
197
    public function onBeforeWrite()
198
    {
199
        parent::onBeforeWrite();
200
    }
201
}
202