Completed
Push — master ( 898373...fb0bef )
by Nicolaas
04:30
created

CountryPrice_Translation::Link()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
4
5
class CountryPrice_Translation 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...
6
{
7
    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...
8
        'Title' => 'Varchar(200)',
9
        'Content' => 'HTMLText',
10
        'WithoutTranslation' => 'Boolean'
11
    );
12
13
    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...
14
        'Title' => 'Page Title',
15
        'Content' => 'Page Content',
16
        'EcommerceCountryID' => 'Country',
17
        'WithoutTranslation' => 'Without Translation'
18
    );
19
20
    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...
21
        'EcommerceCountry' => 'EcommerceCountry',
22
        'Parent' => 'SiteTree'
23
    );
24
25
    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...
26
        'EcommerceCountry.Name' => 'Country',
27
        'Title' => 'Title',
28
        'WithoutTranslation.Nice' => 'Price Difference Only'
29
    );
30
31
    /**
32
     * @var string
33
     */
34
    private static $locale_get_parameter = 'ecomlocale';
0 ignored issues
show
Unused Code introduced by
The property $locale_get_parameter 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...
35
36
    /**
37
     * Standard SS variable.
38
     *
39
     * @var string
40
     */
41
    private static $singular_name = 'Translation';
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...
42
    public function i18n_singular_name()
43
    {
44
        return self::$singular_name;
45
    }
46
47
    /**
48
     * Standard SS variable.
49
     *
50
     * @var string
51
     */
52
    private static $plural_name = 'Translations';
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...
53
    public function i18n_plural_name()
54
    {
55
        return self::$plural_name;
56
    }
57
58
    /**
59
     * CMS Fields
60
     * @return FieldList
61
     */
62
    public function getCMSFields()
63
    {
64
        $fields = parent::getCMSFields();
65
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map()->toArray();
66
        $fields->addFieldToTab(
67
            'Root.Main',
68
            DropdownField::create(
69
                'EcommerceCountryID',
70
                $fields->dataFieldByName('EcommerceCountryID')->Title(),
71
                array('' => '-- make sure to select a country --')+$countries
72
            ),
73
            'Title'
74
        );
75
        $withoutTranslationField = $fields->dataFieldByName('WithoutTranslation');
76
        $fields->addFieldToTab(
77
            'Root.Main',
78
            CheckboxField::create(
79
                'WithoutTranslation',
80
                $withoutTranslationField->Title()
81
            )->setRightTitle('The page is <em>translated</em> for search engines because it has prices for this country. '),
82
            'Title'
83
        );
84
        $fields->removeFieldFromTab("Root.Main", 'ParentID');
85
        return $fields;
86
    }
87
88
    public function canCreate($member = null)
89
    {
90
        if (CountryPrice_EcommerceCountry::get_real_countries_list()->count()) {
91
            return parent::canCreate($member);
92
        }
93
        return false;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    protected function validate()
100
    {
101
        $validation = parent::validate();
102
        if ($validation->valid()) {
103
            if ($this->exists()) {
104
                $existing = CountryPrice_Translation::get()
105
                    ->exclude(array("ID" => $this->ID))
106
                    ->filter(
107
                        array(
108
                            "EcommerceCountryID" => $this->EcommerceCountryID,
0 ignored issues
show
Documentation introduced by
The property EcommerceCountryID does not exist on object<CountryPrice_Translation>. 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...
109
                            "ParentID" => $this->ParentID
0 ignored issues
show
Documentation introduced by
The property ParentID does not exist on object<CountryPrice_Translation>. 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...
110
                        )
111
                    );
112
                if ($existing->count() > 0) {
113
                    $validation->error(
114
                        'There is already an entry for this country and page'
115
                    );
116
                }
117
            }
118
        }
119
        return $validation;
120
    }
121
122
    /**
123
     *     'PageField' => 'Title'
124
     *     'TranslationField' => 'Title'
125
     *
126
     * @return ArrayList
127
     */
128
    public function FieldsToReplace()
129
    {
130
        $al = ArrayList::create();
131
        $al->push(
132
            ArrayData::create(
133
                array(
134
                    'PageField' => 'Title',
135
                    'TranslationField' => 'Title'
136
                )
137
            )
138
        );
139
        $al->push(
140
            ArrayData::create(
141
                array(
142
                    'PageField' => 'Content',
143
                    'TranslationField' => 'Content'
144
                )
145
            )
146
        );
147
        $this->extend('updateFieldsToReplace', $al);
148
        return $al;
149
    }
150
151
    function Link()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
    {
153
        $link = $this->Parent()->Link();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on CountryPrice_Translation. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
154
        if($this->EcommerceCountryID) {
0 ignored issues
show
Documentation introduced by
The property EcommerceCountryID does not exist on object<CountryPrice_Translation>. 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...
155
            $link .= '?'.$this->Config()->get('locale_get_parameter').'='.$this->EcommerceCountry()->Code;
0 ignored issues
show
Documentation Bug introduced by
The method EcommerceCountry does not exist on object<CountryPrice_Translation>? 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...
156
        }
157
        return $link;
158
    }
159
}
160