CWIS Developer Documentation

- Home
- Main Page
- Classes
- Files

- File List
- File Members

ConfigSettingsUI.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: ConfigSettingsUI.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2014 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9
14 class ConfigSettingsUI {
15
16 # ---- PUBLIC INTERFACE --------------------------------------------------
17
27 function __construct($CfgParams, $CfgValues, $UniqueKey = NULL)
28 {
29 $this->CfgParams = $CfgParams;
30 $this->CfgValues = $CfgValues;
31 $this->UniqueKey = $UniqueKey;
32 }
33
39 function DisplaySettingsTable($TableId = NULL, $TableStyle = NULL)
40 {
41 # display nothing if there are no settings
42 if (!count($this->CfgParams)) { return; }
43
44 # check whether table should be split into sections
45 $TableIsSectioned = FALSE;
46 foreach ($this->CfgParams as $Name => $Params)
47 {
48 if ($Params["Type"] == "Heading") { $TableIsSectioned = TRUE; }
49 }
50
51 # begin table
52 ?><table class="cw-table cw-table-fullsize cw-table-sideheaders cw-table-padded cw-table-striped <?PHP
53 if ($TableIsSectioned) { print(" cw-table-sectioned"); }
54 ?> cw-content-sysconfigtable"<?PHP
55 if ($TableId) { print(" id=\"".$TableId."\""); }
56 if ($TableStyle) { print(" style=\"".$TableStyle."\""); }
57 ?>>
58 <tbody><?PHP
59
60 # for each setting
61 foreach ($this->CfgParams as $Name => $Params)
62 {
63 # if setting is actually a section heading
64 if ($Params["Type"] == "Heading")
65 {
66 # split table section and display heading
67 if (isset($HeadingAlreadyDisplayed)) { print("</tbody><tbody>"); }
68 ?><tr><th colspan="3" scope="rowspan"><?PHP
69 print($Params["Label"]);
70 $HeadingAlreadyDisplayed = TRUE;
71 ?></th></tr><?PHP
72 }
73 else
74 {
75 $FieldName = "F_".preg_replace("/[^a-zA-Z0-9]/", "", $Name);
76 $IsTallRow = !isset($Params["Units"])
77 && !in_array($Params["Type"],
78 array("Flag", "Text", "Number"));
79 ?>
80 <tr valign="top"<?PHP
81 if ($IsTallRow) print' class="cw-content-tallrow"'; ?>>
82 <th style="padding-top: 9px;">
83 <label for="<?PHP print $FieldName;
84 ?>"><?PHP print $Params["Label"]; ?></label>
85 </th>
86 <td <?PHP if (!isset($Params["Help"])) {
87 print "colspan=\"2\""; } ?>><?PHP
88 $this->DisplaySetting($Name,
89 $this->CfgValues[$Name], $Params); ?></td>
90 <?PHP if (isset($Params["Help"])) { ?>
91 <td style="width: 50%; vertical-align: top; padding-top: 9px;"><?PHP
92 print $Params["Help"]; ?></td>
93 <?PHP } ?>
94 </tr>
95 <?PHP
96 }
97 }
98
99 # end table
100 ?></tbody>
101 </table><?PHP
102 }
103
109 function GetNewSettingsFromForm()
110 {
111 # for each configuration setting
112 $NewSettings = array();
113 foreach ($this->CfgParams as $Name => $Params)
114 {
115 # determine form field name (matches mechanism in HTML)
116 $FieldName = $this->GetFormFieldName($Name);
117
118 # assume the plugin value will not change
119 $DidValueChange = FALSE;
120 $OldValue = $this->CfgValues[$Name];
121 $NewSettings[$Name] = $OldValue;
122
123 # retrieve value based on configuration parameter type
124 switch ($Params["Type"])
125 {
126 case "Flag":
127 # if radio buttons were used
128 if (array_key_exists("OnLabel", $Params)
129 && array_key_exists("OffLabel", $Params))
130 {
131 if (isset($_POST[$FieldName]))
132 {
133 $NewValue = ($_POST[$FieldName] == "1") ? TRUE : FALSE;
134
135 # flag that the values changed if they did
136 $DidValueChange = $this->DidValueChange(
137 $OldValue, $NewValue);
138
139 $NewSettings[$Name] = $NewValue;
140 }
141 }
142 # else checkbox was used
143 else
144 {
145 $NewValue = isset($_POST[$FieldName]) ? TRUE : FALSE;
146
147 # flag that the values changed if they did
148 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
149
150 $NewSettings[$Name] = $NewValue;
151 }
152 break;
153
154 case "Option":
155 $NewValue = GetArrayValue($_POST, $FieldName, array());
156
157 # flag that the values changed if they did
158 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
159
160 $NewSettings[$Name] = $NewValue;
161 break;
162
163 case "Privileges":
164 case "MetadataField":
165 $NewValue = GetArrayValue($_POST, $FieldName, array());
166 if ($NewValue == "-1") { $NewValue = array(); }
167
168 # flag that the values changed if they did
169 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
170
171 $NewSettings[$Name] = $NewValue;
172 break;
173
174 default:
175 if (isset($_POST[$FieldName]))
176 {
177 $NewValue = $_POST[$FieldName];
178
179 # flag that the values changed if they did
180 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
181
182 $NewSettings[$Name] = $NewValue;
183 }
184 break;
185 }
186
187 # if value changed and there is an event to signal for changes
188 if ($DidValueChange && $this->SettingChangeEventName)
189 {
190 # add parameters for this event to existing parameters
191 $EventParams = array();
192 foreach ($EventParams as $ParamName => $ParamValue)
193 {
194 switch ($ParamName)
195 {
196 case "SettingName":
197 $EventParams[$ParamName] = $Name;
198 break;
199
200 case "OldValue":
201 $EventParams[$ParamName] = $OldValue;
202 break;
203
204 case "NewValue":
205 $EventParams[$ParamName] = $NewValue;
206 break;
207
208 default:
209 $EventParams[$ParamName] = $ParamValue;
210 break;
211 }
212 }
213 $EventParams = array_merge($EventParams,
214 $this->SettingChangeEventParams);
215
216 # signal event
217 $GLOBALS["AF"]->SignalEvent(
218 $this->SettingChangeEventName, $EventParams);
219 }
220 }
221
222 # return updated setting values to caller
223 return $NewSettings;
224 }
225
237 function SetEventToSignalOnChange($EventName, $EventParams = array())
238 {
239 $this->SettingChangeEventName = $EventName;
240 $this->SettingChangeEventParams = $EventParams;
241 }
242
243
244 # ---- PRIVATE INTERFACE -------------------------------------------------
245
246 private $CfgParams;
247 private $CfgValues;
248 private $SettingChangeEventName = NULL;
249 private $SettingChangeEventParams = array();
250
257 private function DisplaySetting($Name, $Value, $Params)
258 {
259 $FieldName = $this->GetFormFieldName($Name);
260
261 switch ($Params["Type"])
262 {
263 case "Text":
264 case "Number":
265 $Size = isset($Params["Size"]) ? $Params["Size"]
266 : (isset($Params["MaxVal"])
267 ? (strlen(intval($Params["MaxVal"]) + 1)) : 40);
268 $MaxLen = isset($Params["MaxLength"]) ? $Params["MaxLength"]
269 : (isset($Params["MaxVal"])
270 ? (strlen(intval($Params["MaxVal"]) + 3)) : 80);
271 print('<input type="text" size="'.$Size.'" maxlength="'
272 .$MaxLen.'" id="'.$FieldName.'" name="'.$FieldName.'" value="'
273 .htmlspecialchars($Value).'" />');
274 break;
275
276 case "Paragraph":
277 $Rows = isset($Params["Rows"]) ? $Params["Rows"] : 4;
278 $Columns = isset($Params["Columns"]) ? $Params["Columns"] : 40;
279 print('<textarea rows="'.$Rows.'" cols="'.$Columns
280 .'" id="'.$FieldName.'" name="'.$FieldName.'">'
281 .htmlspecialchars($Value)
282 .'</textarea>');
283 break;
284
285 case "Flag":
286 if (array_key_exists("OnLabel", $Params)
287 && array_key_exists("OffLabel", $Params))
288 {
289 print('<input type="radio" id="'.$FieldName.'On" name="'
290 .$FieldName.'" value="1"'
291 .($Value ? ' checked' : '')
292 .' /> <label for="'.$FieldName.'On">'.$Params["OnLabel"]
293 ."</label>\n");
294 print('<input type="radio" id="'.$FieldName.'Off" name="'
295 .$FieldName.'" value="0"'
296 .($Value ? '' : ' checked')
297 .' /> <label for="'.$FieldName.'Off">'.$Params["OffLabel"]
298 ."</label>\n");
299 }
300 else
301 {
302 print('<input type="checkbox" id="'.$FieldName.'" name="'
303 .$FieldName.'" '
304 .($Value ? ' checked' : '')
305 ." />\n");
306 }
307 break;
308
309 case "Option":
310 $AllowMultiple =
311 isset($Params["AllowMultiple"]) && $Params["AllowMultiple"];
312 $FieldName = $AllowMultiple ? $FieldName."[]" : $FieldName;
313
314 PrintOptionList(
315 $FieldName,
316 $Params["Options"],
317 $Value,
318 "", isset($Params["Rows"]) ? $Params["Rows"] : 1, 1,
319 $AllowMultiple,
320 NULL,
321 "auto");
322 break;
323
324 case "MetadataField":
325 $AllowMultiple =
326 isset($Params["AllowMultiple"]) && $Params["AllowMultiple"];
327 $FieldName = $AllowMultiple ? $FieldName."[]" : $FieldName;
328 $FieldTypes = GetArrayValue($Params, "FieldTypes");
329 $SchemaId = GetArrayValue($Params, "SchemaId", MetadataSchema::SCHEMAID_DEFAULT);
330
331 $Schema = new MetadataSchema($SchemaId);
332 print $Schema->GetFieldsAsOptionList(
333 $FieldName, $FieldTypes,
334 $Value,
335 !$AllowMultiple, NULL,
336 $AllowMultiple);
337 break;
338
339 case "Privileges":
340 $AllowMultiple =
341 isset($Params["AllowMultiple"]) && $Params["AllowMultiple"];
342 $PrivFactory = new PrivilegeFactory();
343 print $PrivFactory->GetItemsAsOptionList(
344 $FieldName, $Value, NULL,
345 ($AllowMultiple ? 15 : 1));
346 break;
347 }
348
349 if (isset($Params["Units"]))
350 {
351 ?>&nbsp;<span><?PHP
352 print $Params["Units"];
353 ?></span><?PHP
354 }
355 }
356
363 private function DidValueChange($OldValue, $NewValue)
364 {
365 # didn't change if they are identical
366 if ($OldValue === $NewValue)
367 {
368 return FALSE;
369 }
370
371 # need special cases from this point because PHP returns some odd results
372 # when performing loose equality comparisons:
373 # http://php.net/manual/en/types.comparisons.php#types.comparisions-loose
374
375 # consider NULL and an empty string to be the same. this is in case a setting
376 # is currently set to NULL and receives an empty value from the form.
377 # $_POST values are always strings
378 if (is_null($OldValue) && is_string($NewValue) && !strlen($NewValue)
379 || is_null($NewValue) && is_string($OldValue) && !strlen($OldValue))
380 {
381 return FALSE;
382 }
383
384 # if they both appear to be numbers and are equal
385 if (is_numeric($OldValue) && is_numeric($NewValue) && $OldValue == $NewValue)
386 {
387 return FALSE;
388 }
389
390 # true-like values
391 if ($OldValue === TRUE && ($NewValue === 1 || $NewValue === "1")
392 || $NewValue === TRUE && ($OldValue === 1 || $OldValue === "1"))
393 {
394 return FALSE;
395 }
396
397 # false-like values
398 if ($OldValue === FALSE && ($NewValue === 0 || $NewValue === "0")
399 || $NewValue === FALSE && ($OldValue === 0 || $OldValue === "0"))
400 {
401 return FALSE;
402 }
403
404 # arrays
405 if (is_array($OldValue) && is_array($NewValue))
406 {
407 # they certainly changed if the counts are different
408 if (count($OldValue) != count($NewValue))
409 {
410 return TRUE;
411 }
412
413 # the algorithm for associative arrays is slightly different from
414 # sequential ones. the values for associative arrays must match the keys
415 if (count(array_filter(array_keys($OldValue), "is_string")))
416 {
417 foreach ($OldValue as $Key => $Value)
418 {
419 # it changed if the keys don't match
420 if (!array_key_exists($Key, $NewValue))
421 {
422 return TRUE;
423 }
424
425 # the arrays changed if a value changed
426 if ($this->DidValueChange($Value, $NewValue[$Key]))
427 {
428 return TRUE;
429 }
430 }
431 }
432
433 # sequential values don't have to have the same keys, just the same
434 # values
435 else
436 {
437 # sort them so all the values match up if they're equal
438 sort($OldValue);
439 sort($NewValue);
440
441 foreach ($OldValue as $Key => $Value)
442 {
443 # the arrays changed if a value changed
444 if ($this->DidValueChange($Value, $NewValue[$Key]))
445 {
446 return TRUE;
447 }
448 }
449 }
450
451 # the arrays are equal
452 return FALSE;
453 }
454
455 # they changed
456 return TRUE;
457 }
458
464 private function GetFormFieldName($SettingName)
465 {
466 return "F_"
467 .($this->UniqueKey ? $this->UniqueKey."_" : "")
468 .preg_replace("/[^a-zA-Z0-9]/", "", $SettingName);
469 }
470 }
471
ConfigSettingsUI\GetNewSettingsFromForm
GetNewSettingsFromForm()
Retrieve values set by form.
Definition: ConfigSettingsUI.php:109
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
ConfigSettingsUI
Class supplying a standard user interface for viewing and setting configuration parameters.
Definition: ConfigSettingsUI.php:14
PrivilegeFactory
Factory which extracts all defined privileges from the database.
Definition: PrivilegeFactory.php:16
ConfigSettingsUI\DisplaySettingsTable
DisplaySettingsTable($TableId=NULL, $TableStyle=NULL)
Display HTML table with settings parameters.
Definition: ConfigSettingsUI.php:39
PHP
PHP
Definition: OAIClient.php:39
MetadataSchema\SCHEMAID_DEFAULT
const SCHEMAID_DEFAULT
Definition: MetadataSchema.php:52
ConfigSettingsUI\SetEventToSignalOnChange
SetEventToSignalOnChange($EventName, $EventParams=array())
Set event to signal when retrieving values from form when settings have changed.
Definition: ConfigSettingsUI.php:237
ConfigSettingsUI\__construct
__construct($CfgParams, $CfgValues, $UniqueKey=NULL)
Class constructor.
Definition: ConfigSettingsUI.php:27
PrintOptionList
PrintOptionList($ResultVar, $Items, $SelectedValue=NULL, $SubmitOnChange="", $Size=1, $PrintEvenIfEmpty=1, $MultipleAllowed=false, $OnChangeAction=NULL, $Width=NULL, $DisabledOptions=NULL)
Definition: Axis--StandardLibrary.php:122