CountryPrice_Translation::canCreate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
4
5
class CountryPrice_Translation extends DataObject
6
{
7
    private static $automatically_create_dummy_translations_for_products_and_productgroups = true;
8
9
    private static $dependencies = array(
10
        'CountryURLProvider' => '%$CountryURLProvider',
11
    );
12
13
    /**
14
     * automatically populated by the dependency manager.
15
     *
16
     * @var CountryURLProvider
17
     */
18
    public $CountryURLProvider = null;
19
20
    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...
21
        'Title' => 'Varchar(200)',
22
        'UseOriginalTitle' => 'Boolean',
23
        'Content' => 'HTMLText',
24
        'UseOriginalContent' => 'Boolean',
25
        'WithoutTranslation' => 'Boolean'
26
    );
27
28
    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...
29
        'Title' => 'Page Title',
30
        'Content' => 'Page Content',
31
        'EcommerceCountryID' => 'Country',
32
        'WithoutTranslation' => 'Price Difference Only'
33
    );
34
35
    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...
36
        'EcommerceCountry' => 'EcommerceCountry',
37
        'Parent' => 'SiteTree'
38
    );
39
40
    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...
41
        'EcommerceCountry.Name' => 'Country',
42
        'Title' => 'Title',
43
        'WithoutTranslation.Nice' => 'Price Difference Only'
44
    );
45
46
    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...
47
        'Link' => 'Varchar'
48
    );
49
50
51
    /**
52
     * Standard SS variable.
53
     *
54
     * @var string
55
     */
56
    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...
57
    public function i18n_singular_name()
58
    {
59
        return self::$singular_name;
60
    }
61
62
    /**
63
     * Standard SS variable.
64
     *
65
     * @var string
66
     */
67
    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...
68
    public function i18n_plural_name()
69
    {
70
        return self::$plural_name;
71
    }
72
73
    /**
74
     * name of the object that provides the country URL
75
     *
76
     * @return string
77
     */
78
    public static function get_country_url_provider()
79
    {
80
        $obj = Injector::inst()->get('CountryPrice_Translation');
81
82
        return $obj->CountryURLProvider;
83
    }
84
85
    /**
86
     * CMS Fields
87
     * @return FieldList
88
     */
89
    public function getCMSFields()
90
    {
91
        $fields = parent::getCMSFields();
92
        $withoutTranslationField = $fields->dataFieldByName('WithoutTranslation');
93
        $withoutTranslationField = CheckboxField::create(
94
            'WithoutTranslation',
95
            $withoutTranslationField->Title()
96
        )
97
        ->setDescription('The page is <em>translated</em> for search engines because it has prices for this country. ');
98
99
        $countries = CountryPrice_EcommerceCountry::get_real_countries_list()->map()->toArray();
100
        $countryDropdownField = DropdownField::create(
101
            'EcommerceCountryID',
102
            $fields->dataFieldByName('EcommerceCountryID')->Title(),
103
            array('' => '-- make sure to select a country --')+$countries
104
        );
105
106
        // //$fields->removeFieldFromTab("Root.Main", 'ParentID');
107
        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...
108
            return FieldList::create(
109
                array(
110
                    $countryDropdownField,
111
                    $withoutTranslationField
112
                )
113
            );
114
        } else {
115
            $fields->addFieldToTab(
116
                'Root.Main',
117
                $countryDropdownField,
118
                'Title'
119
            );
120
            $fields->addFieldToTab(
121
                'Root.Main',
122
                $withoutTranslationField,
123
                'Title'
124
            );
125
        }
126
        $dbFields = $this->inheritedDatabaseFields();
127
        foreach ($dbFields as $dbField => $fieldType) {
128
            $useField = 'UseOriginal'.$dbField;
129
            if (!empty($this->$useField)) {
130
                $fields->replaceField(
131
                    $dbField,
132
                    $fields->dataFieldByName($dbField)->performReadonlyTransformation()
133
                );
134
            }
135
            if ($fields->dataFieldByName($useField)) {
136
                $fields->dataFieldByName($useField)->setDescription(_t('CountryPrice_Translation.IGNORE', 'Use untranslated value for ') . $dbField);
137
            }
138
        }
139
        if ($this->exists() && $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...
140
            $fields->addFieldToTab(
141
                'Root.ParentPage',
142
                CMSEditLinkField::create(
143
                    $name = 'MyParent',
144
                    $title = 'My Parent Page',
145
                    $linkedObject = $this->Parent()
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...
146
                )
147
            );
148
        }
149
        return $fields;
150
    }
151
152
153
154
    public function canCreate($member = null)
155
    {
156
        if (CountryPrice_EcommerceCountry::get_real_countries_list()->count()) {
157
            return parent::canCreate($member);
158
        }
159
        return false;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    protected function validate()
166
    {
167
        $validation = parent::validate();
168
        if ($validation->valid()) {
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
                $validation->error(
171
                    'You can not create a translation without seleting a country.'
172
                );
173
            }
174
            if (! $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...
175
                $validation->error(
176
                    'You can not create a translation without attaching it to a page.'
177
                );
178
            }
179
180
            $existing = CountryPrice_Translation::get()
181
                ->exclude(array("ID" => $this->ID))
182
                ->filter(
183
                    array(
184
                        "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...
185
                        "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...
186
                    )
187
                );
188
            if ($existing->count() > 0) {
189
                $validation->error(
190
                    'There is already an entry for this page for this country.'
191
                );
192
            }
193
        }
194
        return $validation;
195
    }
196
197
    /**
198
     *     'PageField' => 'Title'
199
     *     'TranslationField' => 'Title'
200
     *
201
     * @return ArrayList
202
     */
203
    public function FieldsToReplace()
204
    {
205
        $al = ArrayList::create();
206
        $al->push(
207
            ArrayData::create(
208
                array(
209
                    'PageField' => 'Title',
210
                    'TranslationField' => 'Title'
211
                )
212
            )
213
        );
214
        $al->push(
215
            ArrayData::create(
216
                array(
217
                    'PageField' => 'Content',
218
                    'TranslationField' => 'Content'
219
                )
220
            )
221
        );
222
        $this->extend('updateFieldsToReplace', $al);
223
        foreach ($al as $fieldToReplace) {
224
            $ignoreField = 'UseOriginal' . $fieldToReplace->PageField;
225
            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...
226
                $al->remove($fieldToReplace);
227
            }
228
        }
229
        return $al;
230
    }
231
232
    /**
233
     * @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...
234
     */
235
    public function Link()
236
    {
237
        return $this->getLink();
238
    }
239
240
    /**
241
     * @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...
242
     */
243
    public function getLink()
244
    {
245
        $standardLink = $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...
246
        $linkWithNewCountryCode = '';
247
        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...
248
            $linkWithNewCountryCode = CountryPrice_Translation::get_country_url_provider()
0 ignored issues
show
Bug introduced by
The method replaceCountryCodeInUrl cannot be called on \CountryPrice_Translatio..._country_url_provider() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
249
                ->replaceCountryCodeInUrl(
250
                    $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...
251
                    $standardLink
252
                );
253
        }
254
        if ($linkWithNewCountryCode) {
255
            $link = $linkWithNewCountryCode;
256
        } else {
257
            $link = $standardLink;
258
        }
259
260
        return Director::absoluteURL($link);
261
    }
262
263
    public function requireDefaultRecords()
264
    {
265
        parent::requireDefaultRecords();
266
        //get rid of rogue entries:
267
        DB::query('DELETE FROM CountryPrice_Translation WHERE EcommerceCountryID = 0 OR ParentID = 0');
268
        if (Config::inst()->get('CountryPrice_Translation', 'automatically_create_dummy_translations_for_products_and_productgroups')) {
269
            $prices = CountryPrice::get();
270
            $ecommerceCountries = array();
271
            foreach ($prices as $price) {
272
                if ($countryObject = $price->CountryObject()) {
273
                    if ($buyable = $price->Buyable()) {
274
                        if ($buyable instanceof Product) {
275
                            if ($buyable->ID && $countryObject->ID) {
276
                                $filter = array(
277
                                    'EcommerceCountryID' => $countryObject->ID,
278
                                    'ParentID' => $buyable->ID
279
                                );
280
                                $ecommerceCountries[$countryObject->ID] = $countryObject;
281 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...
282
                                    DB::alteration_message(
283
                                        'Creating fake translation for '.$buyable->Title.' for country '.$countryObject->Code,
284
                                        'created'
285
                                    );
286
                                    $obj = CountryPrice_Translation::create($filter);
287
                                    $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...
288
                                    $obj->write();
289
                                }
290
                            }
291
                        }
292
                    }
293
                }
294
            }
295
            if (count($ecommerceCountries)) {
296
                foreach (ProductGroup::get() as $productGroup) {
297
                    foreach ($ecommerceCountries as $countryID => $countryObject) {
298
                        $filter = array(
299
                            'EcommerceCountryID' => $countryObject->ID,
300
                            'ParentID' => $productGroup->ID
301
                        );
302 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...
303
                            DB::alteration_message(
304
                                'Creating fake translation for '.$productGroup->Title.' for country '.$countryObject->Code,
305
                                'created'
306
                            );
307
                            $obj = CountryPrice_Translation::create($filter);
308
                            $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...
309
                            $obj->write();
310
                        }
311
                    }
312
                }
313
            }
314
        }
315
    }
316
}
317