Passed
Push — master ( 17a49b...bad84e )
by Nicolaas
04:19
created

ModelAdminExtension::updateEditForm()   C

Complexity

Conditions 12
Paths 14

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 12
eloc 26
c 6
b 0
f 0
nc 14
nop 1
dl 0
loc 42
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\CMSNiceties\Extensions;
4
5
use SilverStripe\Admin\ModelAdmin;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Lumberjack\Forms\GridFieldSiteTreeState;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Lumberjack\...\GridFieldSiteTreeState was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SilverStripe\Versioned\Versioned;
13
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
0 ignored issues
show
Bug introduced by
The type UndefinedOffset\Sortable...s\GridFieldSortableRows was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * Class \Sunnysideup\CMSNiceties\Extensions\ModelAdminExtension.
17
 *
18
 * @property ModelAdmin|ModelAdminExtension $owner
19
 */
20
class ModelAdminExtension extends Extension
21
{
22
    private static $excluded_models_from_ssu_extension = [];
23
24
    private static $excluded_modeladmins_from_ssu_extension = [];
25
26
    private static $sort_fields_from_ssu_extension = ['SortNumber', 'Sort', 'SortOrder'];
27
28
    public function updateEditForm($form)
29
    {
30
        $owner = $this->getOwner();
31
        $excludedModelAdmins = Config::inst()->get(ModelAdmin::class, 'excluded_modeladmins_from_ssu_extension');
32
        if (! in_array(get_class($owner), $excludedModelAdmins, true)) {
33
            $excludedModels = Config::inst()->get(ModelAdmin::class, 'excluded_models_from_ssu_extension');
34
            if (! in_array($owner->modelClass, $excludedModels, true)) {
35
                // This check is simply to ensure you are on the managed model you want adjust accordingly
36
                $obj = Injector::inst()->get($owner->modelClass);
37
                if ($obj) {
38
                    $sortFields = Config::inst()->get(ModelAdmin::class, 'sort_fields_from_ssu_extension');
39
                    $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassNameHelper($owner->modelClass));
40
                    if ($gridField instanceof GridField) {
41
                        $config = $gridField->getConfig();
42
                        $dbFields = $obj->config()->get('db');
43
                        foreach ($sortFields as $sortField) {
44
                            if (isset($dbFields[$sortField])) {
45
                                // This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have
46
                                if (! $config->getComponentByType(GridFieldSortableRows::class)) {
47
                                    $obj = $owner->modelClass::singleton();
48
                                    if ($obj->hasExtension(Versioned::class)) {
49
                                        $sorter = (new GridFieldSortableRows($sortField))
50
                                            ->setUpdateVersionedStage(Versioned::LIVE);
51
                                    } else {
52
                                        $sorter = new GridFieldSortableRows($sortField);
53
                                    }
54
                                    $config->addComponent($sorter);
55
                                }
56
                                break;
57
                            }
58
                        }
59
60
                        if ($obj->hasExtension(Versioned::class) && $obj->hasStages() && class_exists(GridFieldSiteTreeState::class)) {
61
                            $config->addComponent(new GridFieldSiteTreeState());
62
                        }
63
                    }
64
                }
65
            }
66
        }
67
68
69
        return $form;
70
    }
71
72
    //
73
    // /**
74
    //  * Sanitise a model class' name for inclusion in a link
75
    //  *
76
    //  * @param string $class
77
    //  * @return string
78
    //  */
79
    protected function sanitiseClassNameHelper(string $class): string
80
    {
81
        return str_replace('\\', '-', $class);
82
    }
83
84
    // /**
85
    //  * checks if there is only one record and if no further records can be created.
86
    //  * If those conditions are true, then redirect immediately to the record
87
    //  * for the convenience of the user.
88
    //  *
89
    //  * @return void
90
    //  */
91
    // protected function updateList($list)
92
    // {
93
    //     $owner = $this->getOwner();
94
    //     if($owner->getRequest()->param('ModelClass')) {
95
    //         $obj = $list->first();
96
    //         $link = $owner->getCMSEditLinkForManagedDataObject($obj);
97
    //         // die($_SERVER['HTTP_REFERER'] ?? 'xxx');
98
    //         if($list->count() === 1) {
99
    //             $obj = $list->first();
100
    //             if($obj->canCreate() === true) {
101
    //                 $link = $owner->getCMSEditLinkForManagedDataObject($obj);
102
    //                 if($link && $owner->getRequest()->getURL() !== $link) {
103
    //                     if(Director::is_ajax()) {
104
    //                         die('<script>window.location.replace("/' . $link . '");</script>');
105
    //                     }
106
    //                 }
107
    //             }
108
    //         }
109
    //     }
110
    // }
111
}
112