1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class LinkThroughInCMS extends Object |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private static $excluded_has_one_classes = [ |
|
|
|
|
6
|
|
|
"Image", |
7
|
|
|
"File" |
8
|
|
|
]; |
9
|
|
|
|
10
|
|
|
private static $completion_array = []; |
11
|
|
|
|
12
|
|
|
private static $tab_name_for_link = "Root.Links"; |
|
|
|
|
13
|
|
|
|
14
|
|
|
private static $tab_name_for_calc = "Root.Main"; |
|
|
|
|
15
|
|
|
|
16
|
|
|
private static $heading_for_casted_variables = "Calculated Values"; |
|
|
|
|
17
|
|
|
|
18
|
|
|
private static $excluded_casted_variables = [ |
|
|
|
|
19
|
|
|
"Title", |
20
|
|
|
"CSSClasses" |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
public static function add_links_to_fields($obj, $fields) |
24
|
|
|
{ |
25
|
|
|
if (!isset(self::$completion_array[$obj->ClassName])) { |
26
|
|
|
self::$completion_array[$obj->ClassName] = true; |
27
|
|
|
if ($obj->exists()) { |
28
|
|
|
$fieldLabels = array_merge($obj->FieldLabels(), (array)$obj->Config()->get("field_labels")); |
29
|
|
|
$hasOneLinks = $obj->Config()->get("has_one"); |
30
|
|
|
if (is_array($hasOneLinks) && count($hasOneLinks)) { |
31
|
|
|
$linkTabName = Config::inst()->get("LinkThroughInCMS", "tab_name_for_link"); |
32
|
|
|
$fields->addFieldToTab($linkTabName, new LiteralField("YouAreLookingAt", '<h2>This objects links to ...</h2>')); |
33
|
|
|
foreach ($hasOneLinks as $methodName => $className) { |
34
|
|
|
$dbFieldName = $methodName."ID"; |
35
|
|
|
$label = isset($fieldLabels[$methodName]) ? $fieldLabels[$methodName] : null; |
36
|
|
|
if ($obj->$dbFieldName && $relationObject = $obj->$methodName()) { |
37
|
|
|
if (! in_array($relationObject->ClassName, Config::inst()->get("LinkThroughInCMS", "excluded_has_one_classes"))) { |
38
|
|
|
$fields->addFieldToTab( |
39
|
|
|
$linkTabName, |
40
|
|
|
LiteralField::create( |
41
|
|
|
"LinkThroughFor".$relationObject->ClassName.$relationObject->ID, |
42
|
|
|
'<h3><a href="'.$relationObject->CMSEditLink().'" target="_blank">open related '.$label.'</a></h3>' |
43
|
|
|
) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
$castedVariables = $obj->Config()->get("casting"); |
50
|
|
|
if (is_array($castedVariables) && count($castedVariables)) { |
51
|
|
|
$calcTabName = Config::inst()->get("LinkThroughInCMS", "tab_name_for_calc"); |
52
|
|
|
$fields->addFieldToTab( |
53
|
|
|
$calcTabName, |
54
|
|
|
LiteralField::create( |
55
|
|
|
"CalculatedValuesHeader", |
56
|
|
|
'<h2>'.Config::inst()->get("LinkThroughInCMS", "heading_for_casted_variables").'</h2>' |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
foreach ($castedVariables as $castedVariableName => $castedVariableType) { |
60
|
|
|
if (!in_array($castedVariableName, Config::inst()->get("LinkThroughInCMS", "excluded_casted_variables"))) { |
61
|
|
|
$label = isset($fieldLabels[$castedVariableName]) ? $fieldLabels[$castedVariableName] : null; |
62
|
|
|
$fields->addFieldToTab( |
63
|
|
|
$calcTabName, |
64
|
|
|
ReadonlyField::create( |
65
|
|
|
$castedVariableName, |
66
|
|
|
$label |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.