3 # FILE: SearchParameterSetEditingUI.php 5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2016 Edward Almasy and Internet Scout Research Group 7 # http://scout.wisc.edu/cwis/ 24 public function __construct($FormFieldName, $SearchParams = NULL)
26 $this->EditFormName = $FormFieldName;
28 # iterate over the search parameter set, creating a 29 # flattened representation of it that is easier for printing 30 if ($SearchParams !== NULL)
40 # get the list of fields that are allowed in searches for all schemas 41 $this->MFields = array();
43 foreach ($this->AllSchemas as $SCId => $Schema)
48 if ($Field->IncludeInAdvancedSearch() ||
49 $Field->IncludeInKeywordSearch() )
51 $this->MFields[]= $Field;
56 $this->Factories = array();
69 print(
'<table id="'.defaulthtmlentities($TableId).
'" ' 70 .
'class="'.defaulthtmlentities($TableStyle).
'" ' 71 .
'style="width: 100%">');
83 $Fields = $this->FlattenSearchParams(
86 # make sure the necessary javascript is required 87 $GLOBALS[
"AF"]->RequireUIFile(
"jquery-ui.js");
88 $GLOBALS[
"AF"]->RequireUIFile(
"CW-QuickSearch.js");
89 $GLOBALS[
"AF"]->RequireUIFile(
"SearchParameterSetEditingUI.js");
91 # note that all of the fields we create for these rows will be named 92 # $this->EditFormName.'[]' , combining them all into an array of results per 93 # http://php.net/manual/en/faq.html.php#faq.html.arrays 95 # css classes required by our javascript are logic_row 96 # field-row, and field-value-edit 100 foreach ($Fields as $FieldRow)
102 if (is_string($FieldRow) && $FieldRow ==
"(")
105 print(
'<tr><td colspan=2 style="padding-left: 2em;">' 106 .
'<input type="hidden" name="'.$this->EditFormName.
'[]" ' 107 .
'value="X-BEGIN-SUBGROUP-X"/>' 108 .
'<table class="cw-speui-subgroup">');
110 elseif (is_string($FieldRow) && $FieldRow ==
")")
113 $this->PrintTemplateRow();
114 print(
'<input type="hidden" name="'.$this->EditFormName.
'[]" ' 115 .
'value="X-END-SUBGROUP-X"/></table></td></tr>');
117 elseif (is_array($FieldRow) && isset($FieldRow[
"Logic"]))
119 print(
'<tr class="logic_row '.$this->EditFormName.
'">' 120 .
'<td colspan="3">'.($Depth==0?
'Top-Level Logic: ':
'Subgroup with ')
121 .
'<select name="'.$this->EditFormName.
'[]" ' 123 .
'<option value="AND" ' 124 .($FieldRow[
"Logic"]==
"AND" ?
"selected ":
"")
126 .
'<option value="OR" ' 127 .($FieldRow[
"Logic"]==
"OR" ?
"selected ":
"")
129 .
'</select>'.($Depth>0?
' Logic':
'').
'</td></tr>');
131 elseif (is_array($FieldRow) && isset($FieldRow[
"FieldId"]) )
133 $FieldId = $FieldRow[
"FieldId"];
134 $Values = $FieldRow[
"Values"];
135 foreach ($Values as $CurVal)
137 print(
'<tr class="field-row '.$this->EditFormName.
'"" 138 ." style="white-space: nowrap;">' 139 .
"<td><span class=\"cw-button cw-button-elegant " 140 .
"cw-button-constrained cw-speui-delete\">X</span>" 143 # for selectable fields, we need to generate all the 144 # html elements that we might need and then depend on 145 # javascript to display only those that are relevant 147 # each field will have four elements 149 # 1. a field selector 150 $this->PrintFieldSelector($FieldId);
152 # 2. a value selector (for option and flag values) 153 $this->PrintValueSelector($FieldId, $CurVal);
155 $SearchText = (mb_strpos($CurVal,
"=")===0) ?
156 mb_substr($CurVal, 1) : $CurVal;
159 print(
'<input type="text" class="field-value-edit" ' 160 .
'name="'.$this->EditFormName.
'[]" ' 161 .
'placeholder="(search terms)" ' 162 .
'value="'.defaulthtmlentities($SearchText).
'">');
164 # 4. an ajax search box 165 $this->PrintQuicksearch($FieldId, $SearchText);
172 # add a template row, used for adding new fields 173 $this->PrintTemplateRow();
183 if (!isset($_POST[$this->EditFormName]))
190 $GroupStack = array();
193 # extract the array of data associated with our EditFormName 194 $FormData = $_POST[$this->EditFormName];
196 # extract and set the search logic, which is always the first 197 # element in the HTML that we generate 198 $Logic = array_shift($FormData);
199 end($GroupStack)->Logic($Logic);
201 while (count($FormData))
203 # first element of each row is a field id 204 $FieldId = array_shift($FormData);
206 if ($FieldId ==
"X-BEGIN-SUBGROUP-X")
208 # add a new subgroup to our stack of subgroups 210 # extract and set the search logic 211 $Logic = array_shift($FormData);
212 end($GroupStack)->Logic($Logic);
214 elseif ($FieldId ==
"X-END-SUBGROUP-X")
216 $Subgroup = array_pop($GroupStack);
217 end($GroupStack)->AddSet($Subgroup);
221 # for selectable fields, we'll have all possible 222 # elements and will need to grab the correct ones for 223 # the currently selected field 224 $SelectVal = array_shift($FormData);
225 $TextVal = array_shift($FormData);
226 $SearchVal = array_shift($FormData);
228 if ($FieldId ==
"X-KEYWORD-X")
233 if (strlen($TextVal)==0)
242 # make sure we have factories for field types that need them 243 switch ($Field->Type())
248 if (!isset($this->Factories[$FieldId]))
250 $this->Factories[$FieldId] = $Field->GetFactory();
258 # verify that we actually have a value for our selected field 259 switch ($Field->Type())
268 # if we have no value for this field, skip displaying it 269 if (strlen($TextVal)==0)
278 # if we have no value for this field, skip displaying it 279 if (strlen($SearchVal)==0)
285 # no need to check the types where there's 286 # a SelectVal, as that cannot be left empty 291 # extract the value for our field 292 switch ($Field->Type())
308 $Item = $this->Factories[$FieldId]->GetItem(
311 # for tree fields, use the same 'is X 312 # or a child of X' construction that we 313 # use when generating search facets 316 $Val->AddParameter(array(
318 "^".$Item->Name().
" -- "), $Field);
322 $Item = $this->Factories[$FieldId]->GetItem(
324 $Val =
"=".$Item->Name();
328 $Item = $this->Factories[$FieldId]->GetItem(
330 $Val =
"=".$Item->Name();
338 throw new Exception(
"Unsupported field type");
342 # add our value to the search parameters 345 end($GroupStack)->AddSet($Val);
349 end($GroupStack)->AddParameter($Val, $Field);
354 $Result = array_pop($GroupStack);
357 # set internal search params to the "Display" version, as these 358 # are the ones we shall use for display 370 if ($SearchParams !== NULL)
372 $this->SearchParams = clone $SearchParams;
375 return clone $this->SearchParams;
378 # ---- PRIVATE INTERFACE ------------------------------------------------- 380 private $EditFormName;
381 private $SearchParams;
397 private function FlattenSearchParams($SearchParams)
402 "Logic" => $SearchParams->Logic() );
404 $SearchStrings = $SearchParams->GetSearchStrings();
405 foreach ($SearchStrings as $FieldId => $Values)
408 "FieldId" => $FieldId,
409 "Values" => $Values);
412 $KeywordStrings = $SearchParams->GetKeywordSearchStrings();
413 if (count($KeywordStrings))
416 "FieldId" =>
"X-KEYWORD-X",
417 "Values" => $KeywordStrings);
420 $Subgroups = $SearchParams->GetSubgroups();
421 if (count($Subgroups))
423 foreach ($Subgroups as $Subgroup)
426 $SubgroupItems = $this->FlattenSearchParams($Subgroup);
427 foreach ($SubgroupItems as $Item)
442 private function PrintFieldSelector($FieldId)
444 # field-subject css class required by our javascript 445 print(
'<select name="'.$this->EditFormName.
'[]" ' 446 .
'class="field-subject">' 447 .
'<option class="field-type-keyword" ' 448 .($FieldId ==
"X-KEYWORD-X" ?
"selected ":
"")
449 .
'value="X-KEYWORD-X">Keyword</option>');
451 foreach ($this->MFields as $MField)
453 $TypeName = defaulthtmlentities(
454 str_replace(
' ',
'', strtolower($MField->TypeAsName())));
456 if (!$MField->Optional())
458 $TypeName .=
" required";
461 $FieldName = $MField->Name();
464 $FieldName = $this->AllSchemas[$MField->SchemaId()]->Name()
468 print(
'<option class="field-type-'.$TypeName.
'" ' 469 .($FieldId == $MField->Id() ?
"selected ":
"")
470 .
'value="'.$MField->Id().
'">' 471 .defaulthtmlentities($FieldName).
'</option>');
482 private function PrintValueSelector($FieldId, $CurVal)
484 # field-value-select class required by our javascript 485 print(
'<select name="'.$this->EditFormName.
'[]" ' 486 .
'class="field-value-select">');
487 foreach ($this->MFields as $MField)
492 foreach ($MField->GetPossibleValues() as $Id => $Val)
494 $IsSelected = $MField->Id() == $FieldId &&
495 strlen($CurVal) > 1 && $CurVal[0] ==
"=" &&
496 substr($CurVal, 1) == $Val ;
498 print(
'<option value="'.$Id.
'" ' 499 .
'class="field-id-'.$MField->Id().
'" ' 500 .($IsSelected ?
" selected" :
"")
501 .
'>'.defaulthtmlentities($Val)
514 private function PrintQuicksearch($FieldId, $CurVal)
516 if ($FieldId !== NULL && $FieldId !=
"X-KEYWORD-X")
518 if (!isset($this->Factories[$FieldId]))
521 $Factory = $Field->GetFactory();
523 $this->Factories[$FieldId] =
524 ($Factory !== NULL) ? $Factory : FALSE;
527 $ItemId = ($this->Factories[$FieldId] !== FALSE) ?
528 $this->Factories[$FieldId]->GetItemIdByName($CurVal) :
"" ;
535 # field-value-qs css class required by our javascript. 536 # various cw-quicksearch classes required by the quicksearch javascript 537 # and ui-front class required by jquery-ui, used by qs js 538 print(
'<div class="field-value-qs cw-quicksearch ' 539 .
'cw-quicksearch-template">' 540 .
'<input class="cw-quicksearch-display ' 541 .
'cw-resourceeditor-metadatafield" ' 542 .
'placeholder="(enter some text to begin searching)" ' 543 .
'value="'.defaulthtmlentities($CurVal).
'" />' 544 .
'<input name="'.$this->EditFormName.
'[]" ' 545 .
'class="cw-quicksearch-value" type="hidden" ' 546 .
'value="'.defaulthtmlentities($ItemId).
'" />' 547 .
'<div style="display: none;" ' 548 .
'class="cw-quicksearch-menu">' 549 .
'<div class="cw-quicksearch-message ui-front"></div>' 556 private function PrintTemplateRow()
558 # field-row, template-row, field-value-edit, cw-speui-add, and 559 # cw-speui-add-subgroup css classes required by our javascript 561 "<tr class=\"field-row template-row ".$this->EditFormName.
"\"" 562 .
" style=\"white-space: nowrap;\">" 564 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 565 .
"cw-speui-delete\">X</span>" 567 $this->PrintFieldSelector(NULL);
568 $this->PrintValueSelector(NULL,
"");
569 print(
"<input type=\"text\" class=\"field-value-edit\" " 570 .
"name=\"".$this->EditFormName.
"[]\" placeholder=\"(search terms)\" " 572 $this->PrintQuicksearch(NULL,
"");
574 print(
"<tr><td colspan=2>" 575 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 576 .
"cw-speui-add\">Add Field</span>" 577 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 578 .
"cw-speui-add-subgroup\">Add Subgroup</span>"
GetValuesFromFormData()
Extract values from a dynamics field edit/modification form.
DisplayAsRows()
Display the table rows for the editing form, without the surrounding.
SearchParameters($SearchParams=NULL)
Get/Set search parameters.
Set of parameters used to perform a search.
DisplayAsTable($TableId=NULL, $TableStyle=NULL)
Display editing form elements enclosed in a.
__construct($FormFieldName, $SearchParams=NULL)
Create a UI for specifing edits to SearchParameterSets.
static ConvertToDisplayParameters($SearchParams)
Get a simplified SearchParameterSet for display purposes.
Class to create a user interface for editing SearchParameterSets.