Completed
Push — master ( 00b45d...3dd24a )
by Nicolaas
02:44
created

CountryPrice_Translation::canEdit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
165
     */
166
    public function Link()
167
    {
168
        return $this->getLink();
169
    }
170
171
    /**
172
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
173
     */
174
    public function getLink()
175
    {
176
        $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...
177
        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...
178
            $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...
179
        }
180
        return Director::absoluteURL($link);
181
    }
182
183
    public function requireDefaultRecords()
184
    {
185
        parent::requireDefaultRecords();
186
        if(Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) {
187
            $prices = CountryPrice::get();
188
            $ecommerceCountries = array();
189
            foreach($prices as $price) {
190
                if($countryObject = $price->CountryObject()) {
191
                    if($buyable = $price->Buyable()) {
192
                        if($buyable instanceof Product) {
193
                            $filter = array(
194
                                'EcommerceCountryID' => $countryObject->ID,
195
                                'ParentID' => $buyable->ID
196
                            );
197
                            $ecommerceCountries[$countryObject->ID] = $countryObject;
198 View Code Duplication
                            if(! CountryPrice_Translation::get()->filter($filter)->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
199
                                DB::alteration_message(
200
                                    'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code,
201
                                    'created'
202
                                );
203
                                $obj = CountryPrice_Translation::create($filter);
204
                                $obj->WithoutTranslation = true;
0 ignored issues
show
Documentation introduced by
The property WithoutTranslation does not exist on object<CountryPrice_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

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

}

Since the property has write access only, you can use the @property-write 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...
205
                                $obj->write();
206
                            }
207
                        }
208
                    }
209
                }
210
            }
211
            if(count($ecommerceCountries)) {
212
                foreach(ProductGroup::get() as $productGroup) {
213
                    foreach($ecommerceCountries as $countryID => $countryObject) {
214
                        $filter = array(
215
                            'EcommerceCountryID' => $countryObject->ID,
216
                            'ParentID' => $productGroup->ID
217
                        );
218 View Code Duplication
                        if(! CountryPrice_Translation::get()->filter($filter)->first()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
219
                            DB::alteration_message(
220
                                'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code,
221
                                'created'
222
                            );
223
                            $obj = CountryPrice_Translation::create($filter);
224
                            $obj->WithoutTranslation = true;
0 ignored issues
show
Documentation introduced by
The property WithoutTranslation does not exist on object<CountryPrice_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

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

}

Since the property has write access only, you can use the @property-write 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...
225
                            $obj->write();
226
                        }
227
                    }
228
                }
229
            }
230
        }
231
    }
232
233
}
234