-
Notifications
You must be signed in to change notification settings - Fork 333
/
AssetTableField.php
executable file
·195 lines (158 loc) · 6.47 KB
/
AssetTableField.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
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
189
190
191
192
193
194
195
<?php
/**
* A special kind of complex table field for manipulating assets.
*
* @package cms
* @subpackage assets
*/
class AssetTableField extends ComplexTableField {
protected $folder;
protected $template = "AssetTableField";
protected $permissions = array(
"edit",
"delete",
//"export",
);
/**
* Indicates whether a search is being executed on this object
*/
protected $searchingFor = null;
function __construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
$SNG_file = singleton('File');
// If search was request, filter the results here
$SQL_search = (!empty($_REQUEST['FileSearch'])) ? Convert::raw2sql($_REQUEST['FileSearch']) : null;
if($SQL_search) {
$searchFilters = array();
foreach($SNG_file->searchableFields() as $fieldName => $fieldSpec) {
if(strpos($fieldName, '.') === false) $searchFilters[] = "`$fieldName` LIKE '%{$SQL_search}%'";
}
$this->sourceFilter = '(' . implode(' OR ', $searchFilters) . ')';
$this->searchingFor = $_REQUEST['FileSearch'];
// @todo Integrate search form more closely and don't rely on deprecated
// $extraLinkParams.
$this->extraLinkParams = array(
'FileSearch' => $SQL_search
);
}
$this->sourceSort = 'Title';
$this->Markable = true;
}
function FieldHolder() {
$ret = parent::FieldHolder();
Requirements::javascript(CMS_DIR . '/javascript/AssetTableField.js');
Requirements::javascript('cms/javascript/ImageEditor/Activator.js');
return $ret;
}
function setFolder($folder) {
$this->folder = $folder;
$this->sourceFilter .= ($this->sourceFilter) ? " AND " : "";
// If you are searching for files then show all those from subfolders
if($this->searchingFor) {
$folderIDs = $nextIDSet = array($folder->ID);
$folderClasses = "'" . implode("','", ClassInfo::subclassesFor("Folder")) . "'";
while($nextIDSet) {
// TO DO: In 2.4 this should be refactored to use the new data mapper.
$nextIDSet = DB::query("SELECT ID FROM `File` WHERE ParentID IN ("
. implode(", " , $nextIDSet) . ") AND ClassName IN ($folderClasses)")->column();
if($nextIDSet) $folderIDs = array_merge($folderIDs, $nextIDSet);
}
$this->sourceFilter .= " ParentID IN (" . implode(", ", $folderIDs) . ") AND ClassName <> 'Folder'";
// Otherwise just show the direct contents
} else {
$this->sourceFilter .= " ParentID = '" . $folder->ID . "' AND ClassName <> 'Folder'";
}
}
function Folder() {
return $this->folder;
}
function sourceID() {
if($this->folder) return $this->folder->ID;
}
/**
* Get the pop-up fields for the given record.
*/
function getCustomFieldsFor($childData) {
if(!$childData) {
user_error("AssetTableField::DetailForm No record found");
return null;
}
if($childData->ParentID) {
$folder = DataObject::get_by_id('File', $childData->ParentID );
} else {
$folder = singleton('Folder');
}
$urlLink = "<div class='field readonly'>";
$urlLink .= "<label class='left'>"._t('AssetTableField.URL','URL')."</label>";
$urlLink .= "<span class='readonly'><a href='{$childData->Link()}'>{$childData->RelativeLink()}</a></span>";
$urlLink .= "</div>";
$detailFormFields = new FieldSet(
new TabSet("BottomRoot",
$mainTab = new Tab('Main',
new TextField("Title", _t('AssetTableField.TITLE','Title')),
new TextField("Name", _t('AssetTableField.FILENAME','Filename')),
new LiteralField("AbsoluteURL", $urlLink),
new ReadonlyField("FileType", _t('AssetTableField.TYPE','Type')),
new ReadonlyField("Size", _t('AssetTableField.SIZE','Size'), $childData->getSize()),
new DropdownField("OwnerID", _t('AssetTableField.OWNER','Owner'), Member::mapInCMSGroups()),
new DateField_Disabled("Created", _t('AssetTableField.CREATED','First uploaded')),
new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed'))
)
)
);
$mainTab->setTitle(_t('AssetTableField.MAIN', 'Main'));
if(is_a($childData,'Image')) {
$tab = $detailFormFields->findOrMakeTab("BottomRoot.Image", _t('AssetTableField.IMAGE', 'Image'));
$big = $childData->URL;
$thumbnail = $childData->getFormattedImage('AssetLibraryPreview')->URL;
// Hmm this required the translated string to be appended to BottomRoot to add this to the Main tab
$detailFormFields->addFieldToTab('BottomRoot.Main',
new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions'))
);
$tab->push(
new LiteralField("ImageFull",
"<img id='thumbnailImage' src='{$thumbnail}?r=" . rand(1,100000) . "' alt='{$childData->Name}' />" )
);
}
if($childData && $childData->hasMethod('BackLinkTracking')) {
$links = $childData->BackLinkTracking();
if($links->exists()) {
foreach($links as $link) {
$backlinks[] = "<li><a href=\"admin/show/$link->ID\">" . $link->Breadcrumbs(null,true). "</a></li>";
}
$backlinks = "<div style=\"clear:left\">". _t('AssetTableField.PAGESLINKING','The following pages link to this file:') ."<ul>" . implode("",$backlinks) . "</ul>";
}
if(!isset($backlinks)) $backlinks = "<p>". _t('AssetTableField.NOLINKS',"This file hasn't been linked to from any pages.") ."</p>";
$detailFormFields->addFieldToTab("BottomRoot.Links", new LiteralField("Backlinks", $backlinks));
}
// the ID field confuses the Controller-logic in finding the right view for ReferencedField
$detailFormFields->removeByName('ID');
if($childData) $childData->extend('updateCMSFields', $detailFormFields);
return $detailFormFields;
}
/**
* Provide some HTML for a search form, to be
* added above the AssetTable field, allowing
* a user to filter the current table's files
* by their filename.
*
* @return string HTML for search form
*/
function SearchForm() {
$searchFields = new FieldGroup(
new TextField('FileSearch', _t('MemberTableField.SEARCH', 'Search'), $this->searchingFor),
new HiddenField("ctf[ID]", '', $this->ID),
new HiddenField('FileFieldName', '', $this->name)
);
$actionFields = new LiteralField(
'FileFilterButton',
'<input type="submit" class="action" name="FileFilterButton" value="' . _t('MemberTableField.FILTER', 'Filter') . '" id="FileFilterButton"/>'
);
$fieldContainer = new FieldGroup(
$searchFields,
$actionFields
);
return $fieldContainer->FieldHolder();
}
}
?>