3 # FILE: ConfigSettingsUI.php
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/
16 # ---- PUBLIC INTERFACE --------------------------------------------------
27 function __construct($CfgParams, $CfgValues, $UniqueKey = NULL)
29 $this->CfgParams = $CfgParams;
30 $this->CfgValues = $CfgValues;
31 $this->UniqueKey = $UniqueKey;
41 # display nothing if there are no settings
42 if (!count($this->CfgParams)) {
return; }
44 # check whether table should be split into sections
45 $TableIsSectioned = FALSE;
46 foreach ($this->CfgParams as $Name => $Params)
48 if ($Params[
"Type"] ==
"Heading") { $TableIsSectioned = TRUE; }
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.
"\""); }
61 foreach ($this->CfgParams as $Name => $Params)
63 # if setting is actually a section heading
64 if ($Params[
"Type"] ==
"Heading")
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;
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"));
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>
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>
111 # for each configuration setting
112 $NewSettings = array();
113 foreach ($this->CfgParams as $Name => $Params)
115 # determine form field name (matches mechanism in HTML)
116 $FieldName = $this->GetFormFieldName($Name);
118 # assume the plugin value will not change
119 $DidValueChange = FALSE;
120 $OldValue = $this->CfgValues[$Name];
121 $NewSettings[$Name] = $OldValue;
123 # retrieve value based on configuration parameter type
124 switch ($Params[
"Type"])
127 # if radio buttons were used
128 if (array_key_exists(
"OnLabel", $Params)
129 && array_key_exists(
"OffLabel", $Params))
131 if (isset($_POST[$FieldName]))
133 $NewValue = ($_POST[$FieldName] ==
"1") ? TRUE : FALSE;
135 # flag that the values changed if they did
136 $DidValueChange = $this->DidValueChange(
137 $OldValue, $NewValue);
139 $NewSettings[$Name] = $NewValue;
142 # else checkbox was used
145 $NewValue = isset($_POST[$FieldName]) ? TRUE : FALSE;
147 # flag that the values changed if they did
148 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
150 $NewSettings[$Name] = $NewValue;
155 $NewValue = GetArrayValue($_POST, $FieldName, array());
157 # flag that the values changed if they did
158 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
160 $NewSettings[$Name] = $NewValue;
164 case "MetadataField":
165 $NewValue = GetArrayValue($_POST, $FieldName, array());
166 if ($NewValue ==
"-1") { $NewValue = array(); }
168 # flag that the values changed if they did
169 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
171 $NewSettings[$Name] = $NewValue;
175 if (isset($_POST[$FieldName]))
177 $NewValue = $_POST[$FieldName];
179 # flag that the values changed if they did
180 $DidValueChange = $this->DidValueChange($OldValue, $NewValue);
182 $NewSettings[$Name] = $NewValue;
187 # if value changed and there is an event to signal for changes
188 if ($DidValueChange && $this->SettingChangeEventName)
190 # add parameters for this event to existing parameters
191 $EventParams = array();
192 foreach ($EventParams as $ParamName => $ParamValue)
197 $EventParams[$ParamName] = $Name;
201 $EventParams[$ParamName] = $OldValue;
205 $EventParams[$ParamName] = $NewValue;
209 $EventParams[$ParamName] = $ParamValue;
213 $EventParams = array_merge($EventParams,
214 $this->SettingChangeEventParams);
217 $GLOBALS[
"AF"]->SignalEvent(
218 $this->SettingChangeEventName, $EventParams);
222 # return updated setting values to caller
239 $this->SettingChangeEventName = $EventName;
240 $this->SettingChangeEventParams = $EventParams;
244 # ---- PRIVATE INTERFACE -------------------------------------------------
248 private $SettingChangeEventName = NULL;
249 private $SettingChangeEventParams = array();
257 private function DisplaySetting($Name, $Value, $Params)
259 $FieldName = $this->GetFormFieldName($Name);
261 switch ($Params[
"Type"])
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).
'" />');
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)
286 if (array_key_exists(
"OnLabel", $Params)
287 && array_key_exists(
"OffLabel", $Params))
289 print(
'<input type="radio" id="'.$FieldName.
'On" name="'
290 .$FieldName.
'" value="1"'
291 .($Value ?
' checked' :
'')
292 .
' /> <label for="'.$FieldName.
'On">'.$Params[
"OnLabel"]
294 print(
'<input type="radio" id="'.$FieldName.
'Off" name="'
295 .$FieldName.
'" value="0"'
296 .($Value ?
'' :
' checked')
297 .
' /> <label for="'.$FieldName.
'Off">'.$Params[
"OffLabel"]
302 print(
'<input type="checkbox" id="'.$FieldName.
'" name="'
304 .($Value ?
' checked' :
'')
311 isset($Params[
"AllowMultiple"]) && $Params[
"AllowMultiple"];
312 $FieldName = $AllowMultiple ? $FieldName.
"[]" : $FieldName;
318 "", isset($Params[
"Rows"]) ? $Params[
"Rows"] : 1, 1,
324 case "MetadataField":
326 isset($Params[
"AllowMultiple"]) && $Params[
"AllowMultiple"];
327 $FieldName = $AllowMultiple ? $FieldName.
"[]" : $FieldName;
328 $FieldTypes = GetArrayValue($Params,
"FieldTypes");
332 print $Schema->GetFieldsAsOptionList(
333 $FieldName, $FieldTypes,
335 !$AllowMultiple, NULL,
341 isset($Params[
"AllowMultiple"]) && $Params[
"AllowMultiple"];
343 print $PrivFactory->GetItemsAsOptionList(
344 $FieldName, $Value, NULL,
345 ($AllowMultiple ? 15 : 1));
349 if (isset($Params[
"Units"]))
352 print $Params[
"Units"];
363 private function DidValueChange($OldValue, $NewValue)
365 # didn't change if they are identical
366 if ($OldValue === $NewValue)
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
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))
384 # if they both appear to be numbers and are equal
385 if (is_numeric($OldValue) && is_numeric($NewValue) && $OldValue == $NewValue)
391 if ($OldValue === TRUE && ($NewValue === 1 || $NewValue ===
"1")
392 || $NewValue === TRUE && ($OldValue === 1 || $OldValue ===
"1"))
398 if ($OldValue === FALSE && ($NewValue === 0 || $NewValue ===
"0")
399 || $NewValue === FALSE && ($OldValue === 0 || $OldValue ===
"0"))
405 if (is_array($OldValue) && is_array($NewValue))
407 # they certainly changed if the counts are different
408 if (count($OldValue) != count($NewValue))
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")))
417 foreach ($OldValue as $Key => $Value)
419 # it changed if the keys don't match
420 if (!array_key_exists($Key, $NewValue))
425 # the arrays changed if a value changed
426 if ($this->DidValueChange($Value, $NewValue[$Key]))
433 # sequential values don't have to have the same keys, just the same
437 # sort them so all the values match up if they're equal
441 foreach ($OldValue as $Key => $Value)
443 # the arrays changed if a value changed
444 if ($this->DidValueChange($Value, $NewValue[$Key]))
451 # the arrays are equal
464 private function GetFormFieldName($SettingName)
467 .($this->UniqueKey ? $this->UniqueKey.
"_" :
"")
468 .preg_replace(
"/[^a-zA-Z0-9]/",
"", $SettingName);
GetNewSettingsFromForm()
Retrieve values set by form.
Class supplying a standard user interface for viewing and setting configuration parameters.
Factory which extracts all defined privileges from the database.
DisplaySettingsTable($TableId=NULL, $TableStyle=NULL)
Display HTML table with settings parameters.
SetEventToSignalOnChange($EventName, $EventParams=array())
Set event to signal when retrieving values from form when settings have changed.
__construct($CfgParams, $CfgValues, $UniqueKey=NULL)
Class constructor.
PrintOptionList($ResultVar, $Items, $SelectedValue=NULL, $SubmitOnChange="", $Size=1, $PrintEvenIfEmpty=1, $MultipleAllowed=false, $OnChangeAction=NULL, $Width=NULL, $DisabledOptions=NULL)