|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class CMSEditLinkAPI extends Object |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
private static $_cache = array(); |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* common usage ... |
|
11
|
|
|
* |
|
12
|
|
|
* public function CMSEditLink($action = null) |
|
13
|
|
|
* { |
|
14
|
|
|
* return CMSEditLinkAPI::find_edit_link_for_object($this, $action); |
|
15
|
|
|
* }; |
|
16
|
|
|
* |
|
17
|
|
|
* @param DataObject | string $objectOrClassName |
|
18
|
|
|
* @param string $action |
|
|
|
|
|
|
19
|
|
|
* |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function find_edit_link_for_object($objectOrClassName, $action = null) |
|
23
|
|
|
{ |
|
24
|
|
|
if($objectOrClassName instanceof DataObject && $objectOrClassName->exists()) { |
|
25
|
|
|
$modelName = $objectOrClassName->ClassName; |
|
26
|
|
|
$id = $objectOrClassName->ID; |
|
27
|
|
|
} else { |
|
28
|
|
|
$modelName = $objectOrClassName; |
|
29
|
|
|
$objectOrClassName = Injector::inst()->get($modelName); |
|
|
|
|
|
|
30
|
|
|
$id = 0; |
|
31
|
|
|
} |
|
32
|
|
|
$key = $modelName.'_'.$action; |
|
33
|
|
|
if(!isset(self::$_cache[$key])) { |
|
34
|
|
|
foreach(ClassInfo::subclassesFor('ModelAdmin') as $i => $class) { |
|
|
|
|
|
|
35
|
|
|
if($class == 'ModelAdmin') {continue;} |
|
36
|
|
|
if(ClassInfo::classImplements($class, 'TestOnly')) {continue;} |
|
37
|
|
|
$myAdminClassName = $class; |
|
38
|
|
|
$modelAdminclassObject = Injector::inst()->get($myAdminClassName); |
|
39
|
|
|
$models = $modelAdminclassObject->getManagedModels(); |
|
40
|
|
|
foreach($models as $key => $model) { |
|
41
|
|
|
if($key === $modelName || $model === $modelName) { |
|
42
|
|
|
$myManagerClass = $class; |
|
43
|
|
|
$myManagerObject = $modelAdminclassObject; |
|
44
|
|
|
break 2; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
if(isset($myManagerClass) && isset($myManagerObject)) { |
|
49
|
|
|
if(!$action) { |
|
|
|
|
|
|
50
|
|
|
$action = $modelName.'/EditForm/field/'.$modelName.'/item/0/'; |
|
51
|
|
|
self::$_cache[$key] = Controller::join_links( |
|
52
|
|
|
Director::baseURL(), |
|
53
|
|
|
$myManagerObject->Link($action) |
|
54
|
|
|
); |
|
55
|
|
|
} else { |
|
56
|
|
|
return Controller::join_links( |
|
57
|
|
|
Director::baseURL(), |
|
58
|
|
|
$myManagerObject->Link($action) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return str_replace('item/0/', 'item/'.$id.'/', self::$_cache[$key]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
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.