|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
*@author nicolaas [at] sunnysideup.co.nz |
|
4
|
|
|
*@todo: |
|
5
|
|
|
* pagination |
|
6
|
|
|
* use scaffolding method (of some sort) to get right field type |
|
7
|
|
|
* (and many other things) |
|
8
|
|
|
* |
|
9
|
|
|
*@package: dataobjectsorter |
|
10
|
|
|
*@description: allows you to quickly review and update one field for all records |
|
11
|
|
|
* e.g. update price for all products |
|
12
|
|
|
* URL is like this |
|
13
|
|
|
* dataobjectonefieldupdate/update/$Action/$ID/$OtherID |
|
14
|
|
|
* dataobjectonefieldupdate/[show]/[updatefield]/[tablename]/[fieldname] |
|
15
|
|
|
* |
|
16
|
|
|
**/ |
|
17
|
|
|
|
|
18
|
|
|
class DataObjectOneFieldUpdateController extends DataObjectSortBaseClass |
|
19
|
|
|
{ |
|
20
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
21
|
|
|
"updatefield" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', |
|
22
|
|
|
"show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION' |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* make sure to also change in routes if you change this link |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private static $url_segment = 'dataobjectonefieldupdate'; |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
private static $page_size = 50; |
|
33
|
|
|
|
|
34
|
|
|
private static $field = null; |
|
35
|
|
|
|
|
36
|
|
|
private static $_objects = null; |
|
37
|
|
|
|
|
38
|
|
|
private static $_objects_without_field = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $ClassName |
|
43
|
|
|
* @param string $FieldName |
|
44
|
|
|
* @param string $where |
|
45
|
|
|
* @param string $sort |
|
46
|
|
|
* @param string $titleField |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function popup_link_only($ClassName, $FieldName, $where = '', $sort = '', $titleField = "Title") |
|
51
|
|
|
{ |
|
52
|
|
|
DataObjectSorterRequirements::popup_link_requirements(); |
|
53
|
|
|
$params = array(); |
|
54
|
|
|
if ($where) { |
|
55
|
|
|
$params["where"] = "where=".urlencode($where); |
|
56
|
|
|
} |
|
57
|
|
|
if ($sort) { |
|
58
|
|
|
$params["sort"] = "sort=".urlencode($sort); |
|
59
|
|
|
} |
|
60
|
|
|
if ($titleField) { |
|
61
|
|
|
$params["titlefield"] = "titlefield=".urlencode($titleField); |
|
62
|
|
|
} |
|
63
|
|
|
return Injector::inst()->get('DataObjectOneFieldUpdateController') |
|
64
|
|
|
->Link('show/'.$ClassName."/".$FieldName).'?'.implode("&", $params); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $ClassName |
|
70
|
|
|
* @param string $FieldName |
|
71
|
|
|
* @param string $where |
|
72
|
|
|
* @param string $sort |
|
73
|
|
|
* @param string $linkText |
|
74
|
|
|
* @param string $titleField |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
View Code Duplication |
public static function popup_link($ClassName, $FieldName, $where = '', $sort = '', $linkText = 'click here to edit', $titleField = "Title") |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$link = self::popup_link_only($ClassName, $FieldName, $where, $sort, $titleField = "Title"); |
|
81
|
|
|
if ($link) { |
|
82
|
|
|
return ' |
|
83
|
|
|
<a href="'.$link.'" class="modalPopUp modal-popup" data-width="800" data-height="600" data-rel="window.open(\''.$link.'\', \'sortlistFor'.$ClassName.$FieldName.'\',\'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600,left = 440,top = 200\'); return false;">'.$linkText.'</a>'; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
View Code Duplication |
public function init() |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
//must set this first ... |
|
91
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', Config::inst()->get('DataObjectSorterRequirements', 'run_through_theme')); |
|
92
|
|
|
parent::init(); |
|
93
|
|
|
DataObjectSorterRequirements::popup_requirements('onefield'); |
|
94
|
|
|
$url = Director::absoluteURL( |
|
95
|
|
|
Injector::inst()->get('DataObjectOneFieldUpdateController') |
|
96
|
|
|
->Link('updatefield') |
|
97
|
|
|
); |
|
98
|
|
|
Requirements::customScript( |
|
99
|
|
|
"var DataObjectOneFieldUpdateURL = '".$url."'", |
|
100
|
|
|
'DataObjectOneFieldUpdateURL' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function updatefield($request = null) |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
Versioned::set_reading_mode('Stage.Stage'); |
|
107
|
|
|
$updateMessage = ""; |
|
108
|
|
|
$updateCount = 0; |
|
109
|
|
|
$table = $request->param("ID"); |
|
110
|
|
|
$field = $request->param("OtherID"); |
|
111
|
|
|
$titleField = $request->getVar('titlefield'); |
|
112
|
|
|
$ids = explode(",", $request->getVar("id")); |
|
113
|
|
|
$newValue = $request->getVar("value"); |
|
114
|
|
|
if ($memberID = Member::currentUserID()) { |
|
|
|
|
|
|
115
|
|
|
if (class_exists($table) && count($ids) > 0 && ($newValue || $newValue == 0)) { |
|
116
|
|
|
foreach ($ids as $id) { |
|
117
|
|
|
if (intval($id)) { |
|
118
|
|
|
if ($obj = $table::get()->byID($id)) { |
|
119
|
|
|
if ($obj->hasDatabaseField($field)) { |
|
120
|
|
|
if ($obj->canEdit()) { |
|
121
|
|
|
$obj->$field = $newValue; |
|
122
|
|
|
if ($obj instanceof SiteTree) { |
|
123
|
|
|
$obj->writeToStage("Stage"); |
|
124
|
|
|
$obj->publish("Stage", "Live"); |
|
|
|
|
|
|
125
|
|
|
} else { |
|
126
|
|
|
$obj->write(); |
|
127
|
|
|
} |
|
128
|
|
|
if ($titleField && $obj->hasDatabaseField($titleField)) { |
|
129
|
|
|
$title = $obj->$titleField; |
|
130
|
|
|
} elseif ($obj->hasMethod("Title")) { |
|
131
|
|
|
$title = $obj->Title(); |
|
132
|
|
|
} elseif ($obj->hasMethod("getTitle")) { |
|
133
|
|
|
$title = $obj->getTitle(); |
|
134
|
|
|
} elseif ($title = $obj->Title) { |
|
135
|
|
|
//do nothing |
|
136
|
|
|
} elseif ($title = $obj->Name) { |
|
137
|
|
|
//do nothing |
|
138
|
|
|
} else { |
|
139
|
|
|
$title = $obj->ID; |
|
140
|
|
|
} |
|
141
|
|
|
$dbField = $obj->stat('db'); |
|
|
|
|
|
|
142
|
|
|
$newValueObject = $obj->dbObject($field); |
|
143
|
|
|
if ($newValueObject->hasMethod('Nice')) { |
|
144
|
|
|
$newValueFancy = $newValueObject->Nice(); |
|
145
|
|
|
} else { |
|
146
|
|
|
$newValueFancy = $newValueObject->Raw(); |
|
147
|
|
|
} |
|
148
|
|
|
$updateCount++; |
|
149
|
|
|
$updateMessage .= "Record updated: <i class=\"fieldTitle\">$field</i> for <i class=\"recordTitle\">".$title ."</i> updated to <i class=\"newValue\">".$newValueFancy."</i><br />"; |
|
150
|
|
|
} |
|
151
|
|
|
} else { |
|
152
|
|
|
user_error("field does not exist", E_USER_ERROR); |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
user_error("could not find record: $table, $id ", E_USER_ERROR); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
if ($updateCount > 1) { |
|
160
|
|
|
return "$updateCount records Updated"; |
|
161
|
|
|
} else { |
|
162
|
|
|
return $updateMessage; |
|
163
|
|
|
} |
|
164
|
|
|
} else { |
|
165
|
|
|
user_error("data object specified: '$table' or id count: '".count($ids)."' or newValue: '$newValue' is not valid", E_USER_ERROR); |
|
166
|
|
|
} |
|
167
|
|
|
} else { |
|
168
|
|
|
user_error("you need to be logged in to make the changes", E_USER_ERROR); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
//used in template |
|
173
|
|
|
public function DataObjectsToBeUpdated() |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
|
|
Versioned::set_reading_mode('Stage.Stage'); |
|
176
|
|
|
if (self::$_objects === null) { |
|
177
|
|
|
$table = $this->SecureTableToBeUpdated(); |
|
178
|
|
|
$field = $this->SecureFieldToBeUpdated(); |
|
179
|
|
|
$where = ''; |
|
180
|
|
View Code Duplication |
if (isset($this->requestParams["where"]) && $this->requestParams["where"]) { |
|
|
|
|
|
|
181
|
|
|
$where = urldecode($this->requestParams["where"]); |
|
182
|
|
|
} |
|
183
|
|
|
$sort = ''; |
|
184
|
|
View Code Duplication |
if (isset($this->requestParams["sort"]) && $this->requestParams["sort"]) { |
|
|
|
|
|
|
185
|
|
|
$sort = urldecode($this->requestParams["sort"]); |
|
186
|
|
|
} |
|
187
|
|
|
$titleField = 'Title'; |
|
|
|
|
|
|
188
|
|
|
if (isset($this->requestParams["titlefield"]) && $this->requestParams["titlefield"]) { |
|
189
|
|
|
$titleField = urldecode($this->requestParams["titlefield"]); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
$start = 0; |
|
192
|
|
|
if (isset($this->requestParams["start"])) { |
|
193
|
|
|
$start = intval($this->requestParams["start"]); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
if (isset($_GET["debug"])) { |
|
197
|
|
|
print_r("SELECT * FROM $table $where SORT BY $sort LIMIT $start, ". Config::inst()->get("DataObjectOneFieldUpdateController", "page_size")); |
|
198
|
|
|
} |
|
199
|
|
|
$dataList = $table::get()->where($where)->sort($sort)->limit(1000); |
|
200
|
|
|
$ids = array(); |
|
201
|
|
|
if ($dataList->count()) { |
|
202
|
|
|
foreach ($dataList as $obj) { |
|
203
|
|
|
if ($obj->canEdit() && $obj->canView()) { |
|
204
|
|
|
$ids[$obj->ID] = $obj->ID; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
$dataList = $table::get()->filter(array('ID' => $ids))->sort($sort)->limit(1000); |
|
209
|
|
|
$_objects = new PaginatedList($dataList, $this->request); |
|
|
|
|
|
|
210
|
|
|
$_objects->setPageLength(Config::inst()->get("DataObjectOneFieldUpdateController", "page_size")); |
|
211
|
|
|
$arrayList = ArrayList::create(); |
|
212
|
|
|
if ($_objects->count()) { |
|
213
|
|
|
foreach ($_objects as $obj) { |
|
214
|
|
|
$obj->FormField = $obj->dbObject($field)->scaffoldFormField(); |
|
215
|
|
|
$obj->FormField->setName($obj->ClassName."/".$obj->ID); |
|
216
|
|
|
//3.0TODO Check that I work vvv. |
|
217
|
|
|
$obj->FormField->addExtraClass("updateField"); |
|
218
|
|
|
$obj->FieldToBeUpdatedValue = $obj->$field; |
|
219
|
|
|
$obj->FormField->setValue($obj->$field); |
|
220
|
|
|
$title = $obj->getTitle(); |
|
221
|
|
|
$arrayList->push(new ArrayData(array("FormField" => $obj->FormField, "MyTitle" => $title))); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
self::$_objects = $arrayList; |
|
225
|
|
|
self::$_objects_without_field = $_objects; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return self::$_objects; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* retun a list of objects |
|
233
|
|
|
* we need it like this for pagination.... |
|
234
|
|
|
* @return DataList |
|
|
|
|
|
|
235
|
|
|
*/ |
|
236
|
|
|
public function PaginatedListItems() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->DataObjectsToBeUpdated(); |
|
239
|
|
|
return self::$_objects_without_field; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|