Completed
Push — master ( fb0bef...1ce86f )
by Nicolaas
02:37
created

CountryPrice_Translation::getLink()   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
    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...
32
        'Link' => 'Varchar'
33
    );
34
35
    /**
36
     * @var string
37
     */
38
    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...
39
40
    /**
41
     * Standard SS variable.
42
     *
43
     * @var string
44
     */
45
    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...
46
    public function i18n_singular_name()
47
    {
48
        return self::$singular_name;
49
    }
50
51
    /**
52
     * Standard SS variable.
53
     *
54
     * @var string
55
     */
56
    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...
57
    public function i18n_plural_name()
58
    {
59
        return self::$plural_name;
60
    }
61
62
    /**
63
     * CMS Fields
64
     * @return FieldList
65
     */
66
    public function getCMSFields()
67
    {
68
        $fields = parent::getCMSFields();
69
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map()->toArray();
70
        $fields->addFieldToTab(
71
            'Root.Main',
72
            DropdownField::create(
73
                'EcommerceCountryID',
74
                $fields->dataFieldByName('EcommerceCountryID')->Title(),
75
                array('' => '-- make sure to select a country --')+$countries
76
            ),
77
            'Title'
78
        );
79
        $withoutTranslationField = $fields->dataFieldByName('WithoutTranslation');
80
        $fields->addFieldToTab(
81
            'Root.Main',
82
            CheckboxField::create(
83
                'WithoutTranslation',
84
                $withoutTranslationField->Title()
85
            )->setRightTitle('The page is <em>translated</em> for search engines because it has prices for this country. '),
86
            'Title'
87
        );
88
        $fields->removeFieldFromTab("Root.Main", 'ParentID');
89
        return $fields;
90
    }
91
92
    public function canCreate($member = null)
93
    {
94
        if (CountryPrice_EcommerceCountry::get_real_countries_list()->count()) {
95
            return parent::canCreate($member);
96
        }
97
        return false;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function validate()
104
    {
105
        $validation = parent::validate();
106
        if ($validation->valid()) {
107
            if ($this->exists()) {
108
                $existing = CountryPrice_Translation::get()
109
                    ->exclude(array("ID" => $this->ID))
110
                    ->filter(
111
                        array(
112
                            "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...
113
                            "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...
114
                        )
115
                    );
116
                if ($existing->count() > 0) {
117
                    $validation->error(
118
                        'There is already an entry for this country and page'
119
                    );
120
                }
121
            }
122
        }
123
        return $validation;
124
    }
125
126
    /**
127
     *     'PageField' => 'Title'
128
     *     'TranslationField' => 'Title'
129
     *
130
     * @return ArrayList
131
     */
132
    public function FieldsToReplace()
133
    {
134
        $al = ArrayList::create();
135
        $al->push(
136
            ArrayData::create(
137
                array(
138
                    'PageField' => 'Title',
139
                    'TranslationField' => 'Title'
140
                )
141
            )
142
        );
143
        $al->push(
144
            ArrayData::create(
145
                array(
146
                    'PageField' => 'Content',
147
                    'TranslationField' => 'Content'
148
                )
149
            )
150
        );
151
        $this->extend('updateFieldsToReplace', $al);
152
        return $al;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    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...
159
    {
160
        return $this->getLink();
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    function getLink()
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...
167
    {
168
        $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...
169
        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...
170
            $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...
171
        }
172
        return Director::absoluteURL($link);
173
    }
174
}
175