|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
*@author nicolaas[at]sunnysideup.co.nz |
|
5
|
|
|
*@description contains a list of form field and their explantions |
|
6
|
|
|
* |
|
7
|
|
|
**/ |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class FormFieldExplanationExtension extends Extension |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
public static $allowed_actions = array("addfieldexplanation"); |
|
13
|
|
|
|
|
14
|
|
|
public static function add_explanations($form, $datarecord) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
|
17
|
|
|
$js = '
|
|
18
|
|
|
var formFieldExplanationErrorMessage = new Array();'; |
|
19
|
|
|
$dos = DataObject::get("FormFieldExplanation", "{$bt}ParentID{$bt} = ".$datarecord->ID); |
|
20
|
|
|
$explanations = array(); |
|
21
|
|
|
if ($dos) { |
|
22
|
|
|
foreach ($dos as $do) { |
|
23
|
|
|
if ($do->Explanation) { |
|
24
|
|
|
$explanations[$do->Name]["Explanation"] = $do->Explanation; |
|
25
|
|
|
} |
|
26
|
|
|
if ($do->CustomErrorMessage) { |
|
27
|
|
|
$explanations[$do->Name]["CustomErrorMessage"] = $do->CustomErrorMessage; |
|
28
|
|
|
} |
|
29
|
|
|
if ($do->CustomErrorMessageAdditional) { |
|
30
|
|
|
$explanations[$do->Name]["CustomErrorMessageAdditional"] = $do->CustomErrorMessageAdditional; |
|
31
|
|
|
} |
|
32
|
|
|
if ($do->AlternativeFieldLabel) { |
|
33
|
|
|
$explanations[$do->Name]["AlternativeFieldLabel"] = $do->AlternativeFieldLabel; |
|
34
|
|
|
} |
|
35
|
|
|
if ($do->ID) { |
|
36
|
|
|
$explanations[$do->Name]["ID"] = $do->ID; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
$dos = $do = null; |
|
|
|
|
|
|
41
|
|
|
$dataFields = $form->fields(); |
|
42
|
|
|
$extraFields = new FieldSet(); |
|
43
|
|
|
if ($dataFields) { |
|
44
|
|
|
foreach ($dataFields as $field) { |
|
45
|
|
|
if ($field instanceof CompositeField) { |
|
46
|
|
|
self::find_composite_fields($field, $extraFields); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
if ($dataFields) { |
|
51
|
|
|
foreach ($dataFields as $field) { |
|
52
|
|
|
self::process_field($field, $explanations, $datarecord, $js); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
if ($extraFields) { |
|
56
|
|
|
foreach ($extraFields as $field) { |
|
57
|
|
|
self::process_field($field, $explanations, $datarecord, $js); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
// block prototype validation |
|
61
|
|
|
Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js"); |
|
62
|
|
|
Requirements::block("sapphire/javascript/Validator.js"); |
|
|
|
|
|
|
63
|
|
|
Requirements::javascript("formfieldexplanations/javascript/Silvertripe-2.3-Validator.js"); |
|
64
|
|
|
Requirements::javascript("formfieldexplanations/javascript/formfieldexplanations.js"); |
|
65
|
|
|
Requirements::customScript($js, "FormFieldExplanationExtension"); |
|
|
|
|
|
|
66
|
|
|
Requirements::themedCSS("formfieldexplanations"); |
|
67
|
|
|
return $form; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected static function find_composite_fields($compositeField, &$extraFields) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$dataFields = $compositeField->FieldSet(); |
|
73
|
|
|
if ($dataFields) { |
|
74
|
|
|
foreach ($dataFields as $field) { |
|
75
|
|
|
if ($field instanceof CompositeField) { |
|
76
|
|
|
self::find_composite_fields($field, $extraFields); |
|
77
|
|
|
} else { |
|
78
|
|
|
$extraFields->push($field); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return $extraFields; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected static function process_field($field, $explanations, $datarecord, &$js) |
|
86
|
|
|
{ |
|
87
|
|
|
$dos = $do = null; |
|
|
|
|
|
|
88
|
|
|
if ($name = $field->Name()) { |
|
89
|
|
|
$message = ''; |
|
90
|
|
|
if (isset($explanations[$name])) { |
|
91
|
|
|
if (isset($explanations[$name]["Explanation"])) { |
|
92
|
|
|
$message .= $explanations[$name]["Explanation"]; |
|
93
|
|
|
} |
|
94
|
|
|
if ($datarecord->canEdit() && isset($explanations[$name]["ID"])) { |
|
95
|
|
|
$message .= ' | '.self::CMSLink($datarecord->ID, $explanations[$name]["ID"]); |
|
96
|
|
|
} |
|
97
|
|
|
} elseif ($datarecord->canEdit() && $name) { |
|
98
|
|
|
$title = $field->Title(); |
|
99
|
|
|
if (!$title) { |
|
100
|
|
|
$title = $name; |
|
101
|
|
|
} |
|
102
|
|
|
$cleanTitle = strip_tags($title); |
|
103
|
|
|
if (class_exists("UserDefinedForm")) { |
|
104
|
|
|
$cleanTitle = str_replace(UserDefinedForm::$required_identifier, "", $cleanTitle); |
|
105
|
|
|
$cleanTitle = str_replace("+", "", $cleanTitle); |
|
106
|
|
|
} |
|
107
|
|
|
$message .= ' | <a href="'.$datarecord->Link().'addfieldexplanation/'.urlencode($name).'/'.urlencode($cleanTitle).'/" class="addFieldExplanation">customise field</a>'; |
|
108
|
|
|
} |
|
109
|
|
|
$do = true; |
|
110
|
|
|
switch ($field->class) { |
|
111
|
|
|
case "HeaderField": |
|
112
|
|
|
$do = false; |
|
113
|
|
|
break; |
|
114
|
|
|
default: |
|
115
|
|
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
$id = $field->id(); |
|
118
|
|
|
$message = str_replace("/", "\/", Convert::raw2js($message)); |
|
119
|
|
|
if ($do && $message && $name && $id) { |
|
120
|
|
|
$js .= "
|
|
121
|
|
|
formfieldexplanations.add_info('".$name."', '".$message."', '".$id."');"; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
$errorMessage = ''; |
|
125
|
|
|
if (isset($explanations[$name]["CustomErrorMessage"])) { |
|
126
|
|
|
$errorMessage = $explanations[$name]["CustomErrorMessage"]; |
|
127
|
|
|
if (isset($explanations[$name]["CustomErrorMessageAdditional"])) { |
|
128
|
|
|
$errorMessage .= '<span class="additionalValidationErrorMessage">'.$explanations[$name]["CustomErrorMessageAdditional"].'</span>'; |
|
129
|
|
|
} |
|
130
|
|
|
$field->addExtraClass("customErrorMessage"); |
|
131
|
|
|
$field->setCustomValidationMessage($errorMessage); |
|
132
|
|
|
} |
|
133
|
|
|
if ($field->Required() && $errorMessage) { |
|
134
|
|
|
$js .= "
|
|
135
|
|
|
formFieldExplanationErrorMessage['$name'] = '".str_replace("/", "\/", Convert::raw2js($errorMessage))."';"; |
|
136
|
|
|
} |
|
137
|
|
|
if (isset($explanations[$name]["AlternativeFieldLabel"])) { |
|
138
|
|
|
$js .= "
|
|
139
|
|
|
formfieldexplanations.replace_title('".$name."', '".str_replace("/", "\/", Convert::raw2js($explanations[$name]["AlternativeFieldLabel"]))."', '".$id."');"; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function addfieldexplanation(HTTPRequest $HTTPRequest) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$bt = defined('DB::USE_ANSI_SQL') ? "\"" : "`"; |
|
146
|
|
|
$fieldName = $HTTPRequest->param("ID"); |
|
147
|
|
|
$fieldTitle = $HTTPRequest->param("OtherID"); |
|
148
|
|
|
$obj = DataObject::get_one("FormFieldExplanation", "{$bt}Name{$bt} = '".$fieldName."' AND ParentID = ".$this->owner->ID); |
|
149
|
|
|
if (!$obj) { |
|
150
|
|
|
$obj = new FormFieldExplanation(); |
|
151
|
|
|
} |
|
152
|
|
|
$obj->Name = $fieldName; |
|
153
|
|
|
$obj->Title = $fieldTitle; |
|
154
|
|
|
$obj->Explanation = "explanation to be added"; |
|
155
|
|
|
$obj->ParentID = $this->owner->ID; |
|
156
|
|
|
$obj->write(); |
|
157
|
|
|
if (Director::is_ajax()) { |
|
158
|
|
|
return self::CMSLink($this->owner->ID, $obj->ID); |
|
159
|
|
|
} else { |
|
160
|
|
|
Director::redirectBack(); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function editfieldexplanation(HTTPRequest $HTTPRequest) |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
$customiseArray = array( |
|
|
|
|
|
|
167
|
|
|
"Title" => "Test", |
|
168
|
|
|
"Form" => "FormTest" |
|
169
|
|
|
); |
|
170
|
|
|
//TO DO!!!!! link with DataObjectsorter |
|
|
|
|
|
|
171
|
|
|
return array(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
protected static function CMSLink($pageID, $itemID) |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
if (class_exists("DataObjectOneRecordUpdateController")) { |
|
177
|
|
|
$link = DataObjectOneRecordUpdateController::popup_link( |
|
178
|
|
|
$className = "FormFieldExplanation", |
|
179
|
|
|
$recordID = $itemID, |
|
180
|
|
|
$linkText = "edit customisation" |
|
181
|
|
|
); |
|
182
|
|
|
return $link; |
|
183
|
|
|
} else { |
|
184
|
|
|
return '<a href="admin/show/'.$pageID.'" class="editFieldExplanation">edit description in CMS</a>'; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
protected function array2json($array) |
|
189
|
|
|
{ |
|
190
|
|
|
foreach ($array as $key => $value) { |
|
191
|
|
|
if (is_array($value)) { |
|
192
|
|
|
$result[] = "$key:" . $this->array2json($value); |
|
|
|
|
|
|
193
|
|
|
} else { |
|
194
|
|
|
$value = (is_bool($value)) ? $value : "\"$value\""; |
|
195
|
|
|
$result[] = "$key:$value \n"; |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
return (isset($result)) ? "{\n".implode(', ', $result) ."} \n": '{}'; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|