FormEntities   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 7
dl 0
loc 42
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
B doSave() 0 14 5
1
<?php
2
3
/**
4
 * @author    Donatas Navidonskis <[email protected]>
5
 * @since     2017
6
 * @class     FormEntities
7
 *
8
 */
9
class FormEntities extends Form {
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...
10
11
    public function __construct(Controller $controller, $name, DataList $entities) {
12
        $fields = new FieldList();
13
14
        /** @var LangEntity $entity */
15
        foreach ($entities as $entity) {
16
            $fields->push(
17
                $entityField = TextField::create("Entities[{$entity->ID}]", $entity->Namespace, $entity->Value)
18
            );
19
20
            if (! empty($entity->Title)) {
21
                $entityField->setRightTitle($entity->Title);
22
                $entityField->setAttribute('title', $entity->Namespace);
23
            }
24
        }
25
26
        $actions = new FieldList([
27
            $button = new FormAction('doSave', _t('FormEntities.SAVE', 'Save')),
28
        ]);
29
30
        $button->addExtraClass('action ss-ui-action-constructive ss-ui-button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary');
31
32
        parent::__construct($controller, $name, $fields, $actions, null);
33
    }
34
35
    public function doSave(array $data = [], Form $form) {
36
        if (array_key_exists('Entities', $data) && $data['Entities'] > 0) {
37
            foreach ($data['Entities'] as $id => $value) {
38
                /** @var LangEntity $entity */
39
                $entity = LangEntity::get()->byID($id);
40
                $entity->Value = $value;
41
                $entity->write();
42
            }
43
        }
44
45
        if (! Director::is_ajax()) {
46
            return $form->getController()->redirectBack();
47
        }
48
    }
49
50
}