Completed
Push — master ( 3b65eb...dc665d )
by Nicolaas
11:00 queued 02:51
created

EcommerceTaskSetDefaultProductGroupValues.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
4
/**
5
 * @description: resets fields in the product group class to "inherit" in case their value does not exist.
6
 *
7
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
8
 * @package: ecommerce
9
 * @sub-package: tasks
10
 * @inspiration: Silverstripe Ltd, Jeremy
11
 **/
12
class EcommerceTaskSetDefaultProductGroupValues extends BuildTask
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...
13
{
14
    protected $title = 'Set Default Product Group Values';
15
16
    protected $description = 'Set default product group values such as DefaultSortOrder.';
17
18
    protected $fieldsToCheck = array(
19
        'SORT' => 'DefaultSortOrder',
20
        'FILTER' => 'DefaultFilter',
21
        'DISPLAY' => 'DisplayStyle',
22
    );
23
24
    public function run($request)
25
    {
26
        $productGroup = ProductGroup::get()->First();
27
        if ($productGroup) {
28
            foreach ($this->fieldsToCheck as $method => $fieldName) {
29
                $acceptableValuesArray = array_flip($productGroup->getUserPreferencesOptionsForDropdown($method));
30
                $this->checkField($fieldName, $acceptableValuesArray, 'inherit');
31
            }
32
        } else {
33
            DB::alteration_message('There are no ProductGroup pages to correct', 'created');
34
        }
35
    }
36
37
    protected function checkField($fieldName, $acceptableValuesArray, $resetValue)
38
    {
39
        $faultyProductGroups = ProductGroup::get()
40
            ->exclude(array($fieldName => $acceptableValuesArray));
41
        if ($faultyProductGroups->count()) {
42
            foreach ($faultyProductGroups as $faultyProductGroup) {
43
                $faultyProductGroup->$fieldName = $resetValue;
44
                $faultyProductGroup->writeToStage('Stage');
45
                $faultyProductGroup->publish('Stage', 'Live');
46
                DB::alteration_message("Reset $fieldName for ".$faultyProductGroup->Title, 'created');
47
            }
48
        } else {
49
            DB::alteration_message("Could not find any faulty records for ProductGroup.$fieldName");
50
        }
51
    }
52
}
53