1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*@author nicolaas [at] sunnysideup.co.nz |
4
|
|
|
*@package: dataobjectsorter |
5
|
|
|
*@description: allows you to edit one field in one record |
6
|
|
|
* |
7
|
|
|
**/ |
8
|
|
|
|
9
|
|
|
class DataObjectOneFieldOneRecordUpdateController extends DataObjectSortBaseClass |
10
|
|
|
{ |
11
|
|
|
private static $allowed_actions = array( |
|
|
|
|
12
|
|
|
"onefieldform" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', |
13
|
|
|
"show" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', |
14
|
|
|
"save" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION' |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* make sure to also change in routes if you change this link |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $url_segment = 'dataobjectonefieldonerecordupdate'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* get a link |
26
|
|
|
* @param string $ClassName |
27
|
|
|
* @param string $FieldName |
28
|
|
|
* @param string $recordID |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public static function popup_link_only($ClassName, $FieldName, $recordID) |
33
|
|
|
{ |
34
|
|
|
DataObjectSorterRequirements::popup_link_requirements(); |
35
|
|
|
return Injector::inst()->get('DataObjectOneFieldOneRecordUpdateController') |
36
|
|
|
->Link('show/'.$ClassName."/".$FieldName).'?id='.$recordID; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* get a link |
41
|
|
|
* @param string $ClassName |
42
|
|
|
* @param string $FieldName |
43
|
|
|
* @param string $recordID |
44
|
|
|
* @param string $linkText |
45
|
|
|
* @return string |
|
|
|
|
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public static function popup_link($ClassName, $FieldName, $recordID, $linkText = 'click here to edit') |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if ($link = self::popup_link_only($ClassName, $FieldName, $recordID)) { |
50
|
|
|
return ' |
51
|
|
|
<a href="'.$link.'" class="modalPopUp modal-popup" data-width="800" data-height="600" data-rel="window.open(\''.$link.'\', \'sortlistFor'.$ClassName.$FieldName.$recordID.'\',\'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=600,left = 440,top = 200\'); return false;">'.$linkText.'</a>'; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function init() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
//must set this first ... |
58
|
|
|
Config::inst()->update('SSViewer', 'theme_enabled', Config::inst()->get('DataObjectSorterRequirements', 'run_through_theme')); |
59
|
|
|
// Only administrators can run this method |
60
|
|
|
parent::init(); |
61
|
|
|
DataObjectSorterRequirements::popup_requirements('onefieldonerecord'); |
62
|
|
|
$url = Director::absoluteURL( |
63
|
|
|
Injector::inst()->get('DataObjectOneFieldOneRecordUpdateController')->Link('updatefield') |
64
|
|
|
); |
65
|
|
|
Requirements::customScript( |
66
|
|
|
"var DataObjectOneFieldOneRecordUpdateURL = '".$url."'", |
67
|
|
|
'DataObjectOneFieldOneRecordUpdateURL' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function onefieldform() |
72
|
|
|
{ |
73
|
|
|
Versioned::set_reading_mode('Stage.Stage'); |
74
|
|
|
$table = $this->SecureTableToBeUpdated(); |
75
|
|
|
$field = $this->SecureFieldToBeUpdated(); |
76
|
|
|
$record = $this->SecureRecordToBeUpdated(); |
77
|
|
|
$obj = $table::get()->byID($record); |
78
|
|
|
if (! $obj) { |
79
|
|
|
user_error("record could not be found!", E_USER_ERROR); |
80
|
|
|
} |
81
|
|
|
if (! $obj->canEdit()) { |
82
|
|
|
return $this->permissionFailureStandard(); |
83
|
|
|
} |
84
|
|
|
$FormField = $this->getFormField($obj, $field); |
85
|
|
|
if (!$FormField) { |
86
|
|
|
user_error("Form Field could not be Found", E_USER_ERROR); |
87
|
|
|
} |
88
|
|
|
$FormField->setValue($obj->$field); |
89
|
|
|
$form = new Form( |
90
|
|
|
$controller = $this, |
91
|
|
|
$name = "OneFieldForm", |
92
|
|
|
$fields = new FieldList( |
93
|
|
|
$FormField, |
94
|
|
|
new HiddenField("Table", "Table", $table), |
95
|
|
|
new HiddenField("Field", "Field", $field), |
96
|
|
|
new HiddenField("Record", "Record", $record) |
97
|
|
|
), |
98
|
|
|
$actions = new FieldList(new FormAction("save", "save and close")) |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
return $form; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function save($data, $form) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$table = $this->SecureTableToBeUpdated(); |
107
|
|
|
$field = $this->SecureFieldToBeUpdated(); |
108
|
|
|
$record = $this->SecureRecordToBeUpdated(); |
109
|
|
|
$obj = $table::get()->byID($record); |
110
|
|
|
if (! $obj->canEdit()) { |
111
|
|
|
return $this->permissionFailureStandard(); |
112
|
|
|
} |
113
|
|
|
$obj->$field = $data[$field]; |
114
|
|
|
$obj->write(); |
115
|
|
|
return ' |
116
|
|
|
<p>Your changes have been saved, please <a href="#" onclick="self.close(); return false;">close window</a>.</p> |
117
|
|
|
<script type="text/javascript">self.close();</script>'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|