Completed
Push — master ( 0df295...ff7fdb )
by
unknown
02:27
created

CountryPrice_Translation::FieldsToReplace()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
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
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
        'UseOriginalTitle' => 'Boolean',
13
        'Content' => 'HTMLText',
14
        'UseOriginalContent' => 'Boolean',
15
        'WithoutTranslation' => 'Boolean'
16
    );
17
18
    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...
19
        'Title' => 'Page Title',
20
        'Content' => 'Page Content',
21
        'EcommerceCountryID' => 'Country',
22
        'WithoutTranslation' => 'Price Difference Only'
23
    );
24
25
    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...
26
        'EcommerceCountry' => 'EcommerceCountry',
27
        'Parent' => 'SiteTree'
28
    );
29
30
    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...
31
        'EcommerceCountry.Name' => 'Country',
32
        'Title' => 'Title',
33
        'WithoutTranslation.Nice' => 'Price Difference Only'
34
    );
35
36
    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...
37
        'Link' => 'Varchar'
38
    );
39
40
    /**
41
     * @var string
42
     */
43
    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...
44
45
    /**
46
     * Standard SS variable.
47
     *
48
     * @var string
49
     */
50
    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...
51
    public function i18n_singular_name()
52
    {
53
        return self::$singular_name;
54
    }
55
56
    /**
57
     * Standard SS variable.
58
     *
59
     * @var string
60
     */
61
    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...
62
    public function i18n_plural_name()
63
    {
64
        return self::$plural_name;
65
    }
66
67
    /**
68
     * CMS Fields
69
     * @return FieldList
70
     */
71
    public function getCMSFields()
72
    {
73
        $fields = parent::getCMSFields();
74
        $withoutTranslationField = $fields->dataFieldByName('WithoutTranslation');
75
        $withoutTranslationField = CheckboxField::create(
76
            'WithoutTranslation',
77
            $withoutTranslationField->Title()
78
        )
79
        ->setDescription('The page is <em>translated</em> for search engines because it has prices for this country. ');
80
81
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map()->toArray();
82
        $countryDropdownField = DropdownField::create(
83
            'EcommerceCountryID',
84
            $fields->dataFieldByName('EcommerceCountryID')->Title(),
85
            array('' => '-- make sure to select a country --')+$countries
86
        );
87
88
        $fields->removeFieldFromTab("Root.Main", 'ParentID');
89
        if($this->WithoutTranslation) {
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...
90
            return FieldList::create(
91
                array(
92
                    $countryDropdownField,
93
                    $withoutTranslationField
94
                )
95
            );
96
        } else {
97
            $fields->addFieldToTab(
98
                'Root.Main',
99
                $countryDropdownField,
100
                'Title'
101
            );
102
            $fields->addFieldToTab(
103
                'Root.Main',
104
                $withoutTranslationField,
105
                'Title'
106
            );
107
        }
108
        $dbFields = $this->inheritedDatabaseFields();
109
        foreach($dbFields as $dbField => $fieldType) {
110
            $useField = 'UseOriginal'.$dbField;
111
            if(!empty($this->$useField)) {
112
                $fields->replaceField(
113
                    $dbField,
114
                    $fields->dataFieldByName($dbField)->performReadonlyTransformation()
115
                );
116
            }
117
            if($fields->dataFieldByName($useField)){
118
                $fields->dataFieldByName($useField)->setDescription(_t('CountryPrice_Translation.IGNORE', 'Use original value for ') . $dbField);
119
            }
120
        }
121
        return $fields;
122
    }
123
124
    public function canCreate($member = null)
125
    {
126
        if (CountryPrice_EcommerceCountry::get_real_countries_list()->count()) {
127
            return parent::canCreate($member);
128
        }
129
        return false;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    protected function validate()
136
    {
137
        $validation = parent::validate();
138
        if ($validation->valid()) {
139
            if ($this->exists()) {
140
                $existing = CountryPrice_Translation::get()
141
                    ->exclude(array("ID" => $this->ID))
142
                    ->filter(
143
                        array(
144
                            "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...
145
                            "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...
146
                        )
147
                    );
148
                if ($existing->count() > 0) {
149
                    $validation->error(
150
                        'There is already an entry for this country and page'
151
                    );
152
                }
153
            }
154
        }
155
        return $validation;
156
    }
157
158
    /**
159
     *     'PageField' => 'Title'
160
     *     'TranslationField' => 'Title'
161
     *
162
     * @return ArrayList
163
     */
164
    public function FieldsToReplace()
165
    {
166
        $al = ArrayList::create();
167
        $al->push(
168
            ArrayData::create(
169
                array(
170
                    'PageField' => 'Title',
171
                    'TranslationField' => 'Title'
172
                )
173
            )
174
        );
175
        $al->push(
176
            ArrayData::create(
177
                array(
178
                    'PageField' => 'Content',
179
                    'TranslationField' => 'Content'
180
                )
181
            )
182
        );
183
        $this->extend('updateFieldsToReplace', $al);
184
        foreach($al as $fieldToReplace) {
185
            $ignoreField = 'UseOriginal' . $fieldToReplace->PageField;
186
            if(!empty($this->owner->$ignoreField)) {
0 ignored issues
show
Documentation introduced by
The property owner 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...
187
                $al->remove($fieldToReplace);
188
            }
189
        }
190
        return $al;
191
    }
192
193
    /**
194
     * @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...
195
     */
196
    public function Link()
197
    {
198
        return $this->getLink();
199
    }
200
201
    /**
202
     * @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...
203
     */
204
    public function getLink()
205
    {
206
        $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...
207
        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...
208
            $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...
209
        }
210
        return Director::absoluteURL($link);
211
    }
212
213
    public function requireDefaultRecords()
214
    {
215
        parent::requireDefaultRecords();
216
        if(Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) {
217
            $prices = CountryPrice::get();
218
            $ecommerceCountries = array();
219
            foreach($prices as $price) {
220
                if($countryObject = $price->CountryObject()) {
221
                    if($buyable = $price->Buyable()) {
222
                        if($buyable instanceof Product) {
223
                            if($buyable->ID && $countryObject->ID) {
224
                                $filter = array(
225
                                    'EcommerceCountryID' => $countryObject->ID,
226
                                    'ParentID' => $buyable->ID
227
                                );
228
                                $ecommerceCountries[$countryObject->ID] = $countryObject;
229 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...
230
                                    DB::alteration_message(
231
                                        'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code,
232
                                        'created'
233
                                    );
234
                                    $obj = CountryPrice_Translation::create($filter);
235
                                    $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...
236
                                    $obj->write();
237
                                }
238
                            }
239
                        }
240
                    }
241
                }
242
            }
243
            if(count($ecommerceCountries)) {
244
                foreach(ProductGroup::get() as $productGroup) {
245
                    foreach($ecommerceCountries as $countryID => $countryObject) {
246
                        $filter = array(
247
                            'EcommerceCountryID' => $countryObject->ID,
248
                            'ParentID' => $productGroup->ID
249
                        );
250 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...
251
                            DB::alteration_message(
252
                                'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code,
253
                                'created'
254
                            );
255
                            $obj = CountryPrice_Translation::create($filter);
256
                            $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...
257
                            $obj->write();
258
                        }
259
                    }
260
                }
261
            }
262
        }
263
    }
264
265
}
266