CMSTricks   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 59
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C add_links_to_fields() 0 46 15
1
<?php
2
3
/**
4
 * Creates links through for
5
 *
6
 */
7
8
class CMSTricks extends Object
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...
9
{
10
    private static $excluded_has_one_classes = array("Image", "File");
0 ignored issues
show
Unused Code introduced by
The property $excluded_has_one_classes 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
12
    private static $completion_array = array();
13
14
    private static $tab_name_for_link = "Root.Links";
0 ignored issues
show
Unused Code introduced by
The property $tab_name_for_link 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...
15
16
    private static $tab_name_for_calc = "Root.Main";
0 ignored issues
show
Unused Code introduced by
The property $tab_name_for_calc 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...
17
18
    private static $excluded_casted_variables = array("Title", "CSSClasses");
0 ignored issues
show
Unused Code introduced by
The property $excluded_casted_variables 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
20
    public static function add_links_to_fields($obj, $fields)
21
    {
22
        if (!isset(self::$completion_array[$obj->ClassName])) {
23
            self::$completion_array[$obj->ClassName] = true;
24
            if ($obj->exists()) {
25
                $fieldLabels = array_merge($obj->FieldLabels(), (array)$obj->Config()->get("field_labels"));
26
                $hasOneLinks = $obj->Config()->get("has_one");
27
                if (is_array($hasOneLinks) && count($hasOneLinks)) {
28
                    $linkTabName = Config::inst()->get("CMSTricks", "tab_name_for_link");
29
                    $fields->addFieldToTab($linkTabName, new LiteralField("YouAreLookingAt", '<h2>This objects links to ...</h2>'));
30
                    foreach ($hasOneLinks as $methodName => $className) {
31
                        $dbFieldName = $methodName."ID";
32
                        $label = isset($fieldLabels[$methodName]) ? $fieldLabels[$methodName] : null;
33
                        if ($obj->$dbFieldName && $relationObject = $obj->$methodName()) {
34
                            if (!in_array($relationObject->ClassName, Config::inst()->get("CMSTricks", "excluded_has_one_classes"))) {
35
                                $fields->addFieldToTab(
36
                                    $linkTabName,
37
                                    new LiteralField(
38
                                        "LinkThroughFor".$relationObject->ClassName.$relationObject->ID,
39
                                        '<h3><a href="'.$relationObject->CMSEditLink().'" target="_blank">open related '.$label.'</a></h3>'
40
                                    )
41
                                );
42
                            }
43
                        }
44
                    }
45
                }
46
                $castedVariables = $obj->Config()->get("casting");
47
                if (is_array($castedVariables) && count($castedVariables)) {
48
                    $calcTabName = Config::inst()->get("CMSTricks", "tab_name_for_calc");
49
                    $fields->addFieldToTab($calcTabName, new LiteralField("CalculatedValues", '<h2>Calculated Values ...</h2>'));
50
                    foreach ($castedVariables as $castedVariableName => $castedVariableType) {
51
                        if (!in_array($castedVariableName, Config::inst()->get("CMSTricks", "excluded_casted_variables"))) {
52
                            $label = isset($fieldLabels[$castedVariableName]) ? $fieldLabels[$castedVariableName] : null;
53
                            $fields->addFieldToTab(
54
                                $calcTabName,
55
                                new ReadonlyField(
56
                                    $castedVariableName,
57
                                    $label
58
                                )
59
                            );
60
                        }
61
                    }
62
                }
63
            }
64
        }
65
    }
66
}
67