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 if ($SearchParams !== NULL)
30 $this->SearchParams = $SearchParams;
37 # get the list of fields that are allowed in searches for all schemas 38 $this->MFields = array();
40 foreach ($this->AllSchemas as $SCId => $Schema)
45 if ($Field->IncludeInAdvancedSearch() ||
46 $Field->IncludeInKeywordSearch() )
48 $this->MFields[]= $Field;
53 $this->Factories = array();
66 print(
'<table id="'.defaulthtmlentities($TableId).
'" ' 67 .
'class="'.defaulthtmlentities($TableStyle).
'" ' 68 .
'style="width: 100%">');
80 $Fields = $this->FlattenSearchParams(
83 # make sure the necessary javascript is required 84 $GLOBALS[
"AF"]->RequireUIFile(
"jquery-ui.js");
85 $GLOBALS[
"AF"]->RequireUIFile(
"CW-QuickSearch.js");
86 $GLOBALS[
"AF"]->RequireUIFile(
"SearchParameterSetEditingUI.js");
88 # note that all of the fields we create for these rows will be named 89 # $this->EditFormName.'[]' , combining them all into an array of results per 90 # http://php.net/manual/en/faq.html.php#faq.html.arrays 92 # css classes required by our javascript are logic_row 93 # field-row, and field-value-edit 97 foreach ($Fields as $FieldRow)
99 if (is_string($FieldRow) && $FieldRow ==
"(")
102 print(
'<tr><td colspan=2 style="padding-left: 2em;">' 103 .
'<input type="hidden" name="'.$this->EditFormName.
'[]" ' 104 .
'value="X-BEGIN-SUBGROUP-X"/>' 105 .
'<table class="cw-speui-subgroup">');
107 elseif (is_string($FieldRow) && $FieldRow ==
")")
110 $this->PrintTemplateRow();
111 print(
'<input type="hidden" name="'.$this->EditFormName.
'[]" ' 112 .
'value="X-END-SUBGROUP-X"/></table></td></tr>');
114 elseif (is_array($FieldRow) && isset($FieldRow[
"Logic"]))
116 print(
'<tr class="logic_row '.$this->EditFormName.
'">' 118 .($Depth==0?
'Top-Level Logic: ':
'Subgroup with '));
120 $ListName = $this->EditFormName.
"[]";
121 $Options = array(
"AND"=>
"AND",
"OR"=>
"OR");
122 $SelectedValue = $FieldRow[
"Logic"];
124 $OptList =
new HtmlOptionList($ListName, $Options, $SelectedValue);
125 $OptList->ClassForList(
"logic");
126 $OptList->PrintHtml();
128 print (($Depth>0?
' Logic':
'').
'</td></tr>');
130 elseif (is_array($FieldRow) && isset($FieldRow[
"FieldId"]) )
132 $FieldId = $FieldRow[
"FieldId"];
133 $Values = $FieldRow[
"Values"];
134 foreach ($Values as $CurVal)
136 print(
'<tr class="field-row '.$this->EditFormName.
'"" 137 ." style="white-space: nowrap;">' 138 .
"<td><span class=\"cw-button cw-button-elegant " 139 .
"cw-button-constrained cw-speui-delete\">X</span>" 142 # for selectable fields, we need to generate all the 143 # html elements that we might need and then depend on 144 # javascript to display only those that are relevant 146 # each field will have four elements 148 # 1. a field selector 149 $this->PrintFieldSelector($FieldId);
151 # 2. a value selector (for option and flag values) 152 $this->PrintValueSelector($FieldId, $CurVal);
158 print(
'<input type="text" class="field-value-edit" ' 159 .
'name="'.$this->EditFormName.
'[]" ' 160 .
'placeholder="(search terms)" ' 161 .
'value="'.defaulthtmlentities($SearchText).
'">');
163 # 4. an ajax search box 164 $this->PrintQuicksearch($FieldId, $SearchText);
171 # add a template row, used for adding new fields 172 $this->PrintTemplateRow();
182 if (!isset($_POST[$this->EditFormName]))
189 $GroupStack = array();
192 # extract the array of data associated with our EditFormName 193 $FormData = $_POST[$this->EditFormName];
195 # extract and set the search logic, which is always the first 196 # element in the HTML that we generate 197 $Logic = array_shift($FormData);
198 end($GroupStack)->Logic($Logic);
200 while (count($FormData))
202 # first element of each row is a field id 203 $FieldId = array_shift($FormData);
205 if ($FieldId ==
"X-BEGIN-SUBGROUP-X")
207 # add a new subgroup to our stack of subgroups 209 # extract and set the search logic 210 $Logic = array_shift($FormData);
211 end($GroupStack)->Logic($Logic);
213 elseif ($FieldId ==
"X-END-SUBGROUP-X")
215 $Subgroup = array_pop($GroupStack);
216 end($GroupStack)->AddSet($Subgroup);
220 # for selectable fields, we'll have all possible 221 # elements and will need to grab the correct ones for 222 # the currently selected field 223 $SelectVal = array_shift($FormData);
224 $TextVal = array_shift($FormData);
225 $SearchVal = array_shift($FormData);
227 if ($FieldId ==
"X-KEYWORD-X")
232 if (strlen($TextVal)==0)
241 # make sure we have factories for field types that need them 242 switch ($Field->Type())
247 if (!isset($this->Factories[$FieldId]))
249 $this->Factories[$FieldId] = $Field->GetFactory();
257 # verify that we actually have a value for our selected field 258 switch ($Field->Type())
267 # if we have no value for this field, skip displaying it 268 if (strlen($TextVal)==0)
277 # if we have no value for this field, skip displaying it 278 if (strlen($SearchVal)==0)
284 # no need to check the types where there's 285 # a SelectVal, as that cannot be left empty 290 # extract the value for our field 291 switch ($Field->Type())
307 $Item = $this->Factories[$FieldId]->GetItem(
310 # for tree fields, use the same 'is X 311 # or a child of X' construction that we 312 # use when generating search facets 315 $Val->AddParameter(array(
317 "^".$Item->Name().
" -- "), $Field);
321 $Item = $this->Factories[$FieldId]->GetItem(
323 $Val =
"=".$Item->Name();
327 list($InputId, $InputVal) = explode(
"-", $SelectVal, 2);
328 $Item = $this->Factories[$FieldId]->GetItem(
330 $Val =
"=".$Item->Name();
334 list($InputId, $InputVal) = explode(
"-", $SelectVal, 2);
335 $Val =
"=".$InputVal;
339 throw new Exception(
"Unsupported field type");
343 # add our value to the search parameters 346 end($GroupStack)->AddSet($Val);
350 end($GroupStack)->AddParameter($Val, $Field);
355 $Result = array_pop($GroupStack);
358 $this->SearchParams = $Result;
369 if ($SearchParams !== NULL)
371 $this->SearchParams = clone $SearchParams;
374 return clone $this->SearchParams;
389 if (!is_null($NewValue))
393 return $this->MaxFieldLabelLength;
408 if (!is_null($NewValue))
412 return $this->MaxValueLabelLength;
416 # ---- PRIVATE INTERFACE ------------------------------------------------- 418 private $EditFormName;
419 private $SearchParams;
423 private $MaxFieldLabelLength = 0;
424 private $MaxValueLabelLength = 0;
437 private function FlattenSearchParams($SearchParams)
442 "Logic" => $SearchParams->Logic() );
444 $SearchStrings = $SearchParams->GetSearchStrings();
445 foreach ($SearchStrings as $FieldId => $Values)
448 "FieldId" => $FieldId,
449 "Values" => $Values);
452 $KeywordStrings = $SearchParams->GetKeywordSearchStrings();
453 if (count($KeywordStrings))
456 "FieldId" =>
"X-KEYWORD-X",
457 "Values" => $KeywordStrings);
460 $Subgroups = $SearchParams->GetSubgroups();
461 if (count($Subgroups))
463 foreach ($Subgroups as $Subgroup)
466 $SubgroupItems = $this->FlattenSearchParams($Subgroup);
467 foreach ($SubgroupItems as $Item)
482 private function PrintFieldSelector($FieldId)
484 $ListName = $this->EditFormName.
"[]";
485 $SelectedValue = array();
487 # "Keyword" option is always here 488 $Options[
"X-KEYWORD-X"] =
"Keyword";
489 $OptionClass[
"X-KEYWORD-X"] =
"field-type-keyword";
490 if ($FieldId ==
"X-KEYWORD-X")
492 $SelectedValue[] =
"X-KEYWORD-X";
495 # prepare options for print 496 foreach ($this->MFields as $MField)
498 $TypeName = defaulthtmlentities(
499 str_replace(
' ',
'', strtolower($MField->TypeAsName())));
501 if (!$MField->Optional())
503 $TypeName .=
" required";
506 $FieldName = $MField->Name();
509 $FieldName = $this->AllSchemas[$MField->SchemaId()]->Name()
513 $Options[$MField->Id()] = defaulthtmlentities($FieldName);
514 $OptionClass[$MField->Id()] =
"field-type-".$TypeName;
516 if ($FieldId == $MField->Id())
518 $SelectedValue[] = $MField->Id();
522 # instantiate option list and print 523 $OptList =
new HtmlOptionList($ListName, $Options, $SelectedValue);
524 $OptList->ClassForList(
"field-subject");
525 $OptList->ClassForOptions($OptionClass);
527 $OptList->PrintHtml();
536 private function PrintValueSelector($FieldId, $CurVal)
538 # parameters of the option list 539 $ListName = $this->EditFormName.
"[]";
541 $OptionClass = array();
542 $SelectedValue = array();
544 # prepare options for print 545 foreach ($this->MFields as $MField)
550 foreach ($MField->GetPossibleValues() as $Id => $Val)
553 $Key = $MField->Id().
"-".$Id;
555 if ($MField->Id() == $FieldId)
559 $IsSelected = ($CurVal == $TgtVal) ? TRUE : FALSE ;
562 $Options[$Key] = defaulthtmlentities($Val);
563 $OptionClass[$Key] =
"field-id-".$MField->Id();
567 $SelectedValue[] = $Key;
573 # instantiate an option list and print 574 $OptList =
new HtmlOptionList($ListName, $Options, $SelectedValue);
575 $OptList->ClassForList(
"field-value-select");
576 $OptList->ClassForOptions($OptionClass);
578 $OptList->PrintHtml();
586 private function PrintQuicksearch($FieldId, $CurVal)
588 if ($FieldId !== NULL && $FieldId !=
"X-KEYWORD-X")
590 if (!isset($this->Factories[$FieldId]))
593 $Factory = $Field->GetFactory();
595 $this->Factories[$FieldId] =
596 ($Factory !== NULL) ? $Factory : FALSE;
599 $ItemId = ($this->Factories[$FieldId] !== FALSE) ?
600 $this->Factories[$FieldId]->GetItemIdByName($CurVal) :
"" ;
607 # field-value-qs css class required by our javascript. 608 # various cw-quicksearch classes required by the quicksearch javascript 609 # and ui-front class required by jquery-ui, used by qs js 610 print(
'<div class="field-value-qs cw-quicksearch ' 611 .
'cw-quicksearch-template">' 612 .
'<input class="cw-quicksearch-display ' 613 .
'cw-resourceeditor-metadatafield" ' 614 .
'placeholder="(enter some text to begin searching)" ' 615 .
'value="'.defaulthtmlentities($CurVal).
'" />' 616 .
'<input name="'.$this->EditFormName.
'[]" ' 617 .
'class="cw-quicksearch-value" type="hidden" ' 618 .
'value="'.defaulthtmlentities($ItemId).
'" />' 619 .
'<div style="display: none;" ' 620 .
'class="cw-quicksearch-menu">' 621 .
'<div class="cw-quicksearch-message ui-front"></div>' 628 private function PrintTemplateRow()
630 # field-row, template-row, field-value-edit, cw-speui-add, and 631 # cw-speui-add-subgroup css classes required by our javascript 633 "<tr class=\"field-row template-row ".$this->EditFormName.
"\"" 634 .
" style=\"white-space: nowrap;\">" 636 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 637 .
"cw-speui-delete\">X</span>" 639 $this->PrintFieldSelector(NULL);
640 $this->PrintValueSelector(NULL,
"");
641 print(
"<input type=\"text\" class=\"field-value-edit\" " 642 .
"name=\"".$this->EditFormName.
"[]\" placeholder=\"(search terms)\" " 644 $this->PrintQuicksearch(NULL,
"");
646 print(
"<tr><td colspan=2>" 647 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 648 .
"cw-speui-add\">Add Field</span>" 649 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 650 .
"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.
MaxValueLabelLength($NewValue=NULL)
Get/set the max number of characters a label of a value option list will be displayed.
static strpos()
Multibyte-aware (if supported in PHP) version of strpos().
__construct($FormFieldName, $SearchParams=NULL)
Create a UI for specifing edits to SearchParameterSets.
static substr()
Multibyte-aware (if supported in PHP) version of substr().
MaxFieldLabelLength($NewValue=NULL)
Get/set the max number of characters a label of a field option list will be displayed.
Convenience class for generating an HTML select/option form element.
Class to create a user interface for editing SearchParameterSets.