-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathHasManyFileDataObjectManager.php
More file actions
188 lines (147 loc) · 5.54 KB
/
HasManyFileDataObjectManager.php
File metadata and controls
188 lines (147 loc) · 5.54 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
class HasManyFileDataObjectManager extends FileDataObjectManager
{
public $joinField;
public $addTitle;
public $RelationType = "HasMany";
protected $htmlListEndName = 'CheckedList';
protected $htmlListField = 'selected';
public $template = 'RelationFileDataObjectManager';
public $itemClass = 'HasManyFileDataObjectManager_Item';
protected $relationAutoSetting = false;
protected $markingPermission;
/**
* Most of the code below was copied from HasManyComplexTableField.
* Painful, but necessary, until PHP supports multiple inheritance.
*/
function __construct($controller, $name, $sourceClass, $fileFieldName = null, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "")
{
parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
$this->Markable = true;
if($controllerClass = $this->controllerClass()) {
$this->joinField = $this->getParentIdName($controllerClass, $this->sourceClass);
} else {
user_error("Can't figure out the data class of $controller", E_USER_WARNING);
}
}
/**
* Try to determine the DataObject that this field is built on top of
*/
function controllerClass() {
if($this->controller instanceof DataObject) return $this->controller->class;
elseif($this->controller instanceof ContentController) return $this->controller->data()->class;
}
public function setMarkingPermission($perm)
{
$this->markingPermission = $perm;
}
public function hasMarkingPermission()
{
if(is_bool($this->markingPermission))
return $this->markingPermission;
elseif($this->markingPermission)
return Permission::check($this->markingPermission);
return true;
}
function getQuery($limitClause = null) {
if($this->customQuery) {
$query = $this->customQuery;
$query->select[] = "\"{$this->sourceClass}\".\"ID\" AS \"ID\"";
$query->select[] = "\"{$this->sourceClass}\".\"ClassName\" AS \"ClassName\"";
$query->select[] = "\"{$this->sourceClass}\".\"ClassName\" AS \"RecordClassName\"";
}
else {
$query = singleton($this->sourceClass)->extendedSQL($this->sourceFilter, $this->sourceSort, $limitClause, $this->sourceJoin);
// Add more selected fields if they are from joined table.
$SNG = singleton($this->sourceClass);
foreach($this->FieldList() as $k => $title) {
if(! $SNG->hasField($k) && ! $SNG->hasMethod('get' . $k))
$query->select[] = $k;
}
}
return clone $query;
}
public function setParentClass($class)
{
parent::setParentClass($class);
$this->joinField = $this->getParentIdName($class, $this->sourceClass);
}
function sourceItems() {
if($this->sourceItems)
return $this->sourceItems;
$limitClause = '';
if(isset($_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ]) && is_numeric($_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ]))
$limitClause = $_REQUEST[ 'ctf' ][ $this->Name() ][ 'start' ] . ", $this->pageSize";
else
$limitClause = "0, $this->pageSize";
$dataQuery = $this->getQuery($limitClause);
$records = $dataQuery->execute();
$items = new DataObjectSet();
foreach($records as $record) {
if(! is_object($record)) {
$class = $this->sourceClass;
$record = new $class($record);
}
$items->push($record);
}
$dataQuery = $this->getQuery();
$records = $dataQuery->execute();
$unpagedItems = new DataObjectSet();
foreach($records as $record) {
if(! is_object($record))
$record = new DataObject($record);
$unpagedItems->push($record);
}
$this->unpagedSourceItems = $unpagedItems;
$this->totalCount = ($this->unpagedSourceItems) ? $this->unpagedSourceItems->TotalItems() : null;
return $items;
}
function getControllerID() {
return $this->controller->ID;
}
public function SortableClass()
{
return $this->sourceClass();
}
function saveInto(DataObject $record) {
$fieldName = $this->name;
$saveDest = $record->$fieldName();
if(! $saveDest)
user_error("HasManyDataObjectManager::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
$items = array();
if($list = $this->value[ $this->htmlListField ]) {
if($list != 'undefined')
$items = explode(',', trim($list,","));
}
$saveDest->setByIDList($items);
}
function ExtraData() {
$items = array();
foreach($this->unpagedSourceItems as $item) {
if($item->{$this->joinField} == $this->controller->ID)
$items[] = $item->ID;
}
$list = implode(',', $items);
$value = ",";
$value .= !empty($list) ? $list."," : "";
$inputId = $this->id() . '_' . $this->htmlListEndName;
return <<<HTML
<input id="$inputId" name="{$this->name}[{$this->htmlListField}]" type="hidden" value="{$value}"/>
HTML;
}
}
class HasManyFileDataObjectManager_Item extends FileDataObjectManager_Item {
function MarkingCheckbox() {
$name = $this->parent->Name() . '[]';
$joinVal = $this->item->{$this->parent->joinField};
$parentID = $this->parent->getControllerID();
$disabled = $this->parent->hasMarkingPermission() ? "" : "disabled='disabled'";
if($this->parent->IsReadOnly || ($joinVal > 0 && $joinVal != $parentID))
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" disabled=\"disabled\"/>";
else if($joinVal == $parentID)
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" checked=\"checked\" $disabled />";
else
return "<input class=\"checkbox\" type=\"checkbox\" name=\"$name\" value=\"{$this->item->ID}\" $disabled />";
}
}
?>