/
ListEditField.php
95 lines (82 loc) · 2.07 KB
/
ListEditField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* ListEditField is used in conjunction with ListEditForm
* to allow viewing and removing exiting list items.
* @link ListEditForm
*/
class ListEditField extends FormField
{
private static $allowed_actions = array(
'remove',
'edit'
);
private static $url_handlers = array(
'$Action/$ID' => '$Action',
);
protected $list;
public function __construct($name, DataList $list)
{
$this->list = $list;
parent::__construct($name);
}
public function Link($action = null, $id = null)
{
$link = parent::Link($action);
if ($id) {
$link = Controller::join_links($link, $id);
}
return $link;
}
/**
* This is not a data field.
* @return boolean
*/
public function hasData()
{
return false;
}
/**
* Template access for the list;
* @return DataList list
*/
public function getEditList()
{
return $this->list;
}
public function EditLink($id)
{
if ($obj = $this->list->byID($id)) {
if ($obj->canEdit(Member::currentUser())) {
return HTTP::setGetVar($this->ID(), $id);
}
}
}
public function RemoveLink($id)
{
if ($obj = $this->list->byID($id)) {
if ($obj->canDelete(Member::currentUser())) {
return $this->Link("remove", $id);
}
}
}
public function remove()
{
if ($obj = $this->list->byID($id = $this->request->param('ID'))) {
if ($obj->canDelete(Member::currentUser())) {
$this->list->removeById($id);
}
}
return Controller::curr()->redirectBack();
}
/**
* Allow DataObject specific templates,
* using the form DataObjectName_ListEditField.ss
* @return array templates
*/
public function getTemplates()
{
$templates = parent::getTemplates();
array_unshift($templates, $this->list->dataClass()."_ListEditField");
return $templates;
}
}