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.
'">' 121 .($Depth==0?
'Top-Level Logic: ':
'Subgroup with '));
123 $ListName = $this->EditFormName.
"[]";
124 $Options = array(
"AND"=>
"AND",
"OR"=>
"OR");
125 $SelectedValue = $FieldRow[
"Logic"];
127 $OptList =
new HtmlOptionList($ListName, $Options, $SelectedValue);
128 $OptList->ClassForList(
"logic");
129 $OptList->PrintHtml();
131 print (($Depth>0?
' Logic':
'').
'</td></tr>');
133 elseif (is_array($FieldRow) && isset($FieldRow[
"FieldId"]) )
135 $FieldId = $FieldRow[
"FieldId"];
136 $Values = $FieldRow[
"Values"];
137 foreach ($Values as $CurVal)
139 print(
'<tr class="field-row '.$this->EditFormName.
'"" 140 ." style="white-space: nowrap;">' 141 .
"<td><span class=\"cw-button cw-button-elegant " 142 .
"cw-button-constrained cw-speui-delete\">X</span>" 145 # for selectable fields, we need to generate all the 146 # html elements that we might need and then depend on 147 # javascript to display only those that are relevant 149 # each field will have four elements 151 # 1. a field selector 152 $this->PrintFieldSelector($FieldId);
154 # 2. a value selector (for option and flag values) 155 $this->PrintValueSelector($FieldId, $CurVal);
161 print(
'<input type="text" class="field-value-edit" ' 162 .
'name="'.$this->EditFormName.
'[]" ' 163 .
'placeholder="(search terms)" ' 164 .
'value="'.defaulthtmlentities($SearchText).
'">');
166 # 4. an ajax search box 167 $this->PrintQuicksearch($FieldId, $SearchText);
174 # add a template row, used for adding new fields 175 $this->PrintTemplateRow();
185 if (!isset($_POST[$this->EditFormName]))
192 $GroupStack = array();
195 # extract the array of data associated with our EditFormName 196 $FormData = $_POST[$this->EditFormName];
198 # extract and set the search logic, which is always the first 199 # element in the HTML that we generate 200 $Logic = array_shift($FormData);
201 end($GroupStack)->Logic($Logic);
203 while (count($FormData))
205 # first element of each row is a field id 206 $FieldId = array_shift($FormData);
208 if ($FieldId ==
"X-BEGIN-SUBGROUP-X")
210 # add a new subgroup to our stack of subgroups 212 # extract and set the search logic 213 $Logic = array_shift($FormData);
214 end($GroupStack)->Logic($Logic);
216 elseif ($FieldId ==
"X-END-SUBGROUP-X")
218 $Subgroup = array_pop($GroupStack);
219 end($GroupStack)->AddSet($Subgroup);
223 # for selectable fields, we'll have all possible 224 # elements and will need to grab the correct ones for 225 # the currently selected field 226 $SelectVal = array_shift($FormData);
227 $TextVal = array_shift($FormData);
228 $SearchVal = array_shift($FormData);
230 if ($FieldId ==
"X-KEYWORD-X")
235 if (strlen($TextVal)==0)
244 # make sure we have factories for field types that need them 245 switch ($Field->Type())
250 if (!isset($this->Factories[$FieldId]))
252 $this->Factories[$FieldId] = $Field->GetFactory();
260 # verify that we actually have a value for our selected field 261 switch ($Field->Type())
270 # if we have no value for this field, skip displaying it 271 if (strlen($TextVal)==0)
280 # if we have no value for this field, skip displaying it 281 if (strlen($SearchVal)==0)
287 # no need to check the types where there's 288 # a SelectVal, as that cannot be left empty 293 # extract the value for our field 294 switch ($Field->Type())
310 $Item = $this->Factories[$FieldId]->GetItem(
313 # for tree fields, use the same 'is X 314 # or a child of X' construction that we 315 # use when generating search facets 318 $Val->AddParameter(array(
320 "^".$Item->Name().
" -- "), $Field);
324 $Item = $this->Factories[$FieldId]->GetItem(
326 $Val =
"=".$Item->Name();
330 $Item = $this->Factories[$FieldId]->GetItem(
332 $Val =
"=".$Item->Name();
340 throw new Exception(
"Unsupported field type");
344 # add our value to the search parameters 347 end($GroupStack)->AddSet($Val);
351 end($GroupStack)->AddParameter($Val, $Field);
356 $Result = array_pop($GroupStack);
359 # set internal search params to the "Display" version, as these 360 # are the ones we shall use for display 372 if ($SearchParams !== NULL)
374 $this->SearchParams = clone $SearchParams;
377 return clone $this->SearchParams;
392 if (!is_null($NewValue))
396 return $this->MaxFieldLabelLength;
411 if (!is_null($NewValue))
415 return $this->MaxValueLabelLength;
419 # ---- PRIVATE INTERFACE ------------------------------------------------- 421 private $EditFormName;
422 private $SearchParams;
426 private $MaxFieldLabelLength = 0;
427 private $MaxValueLabelLength = 0;
440 private function FlattenSearchParams($SearchParams)
445 "Logic" => $SearchParams->Logic() );
447 $SearchStrings = $SearchParams->GetSearchStrings();
448 foreach ($SearchStrings as $FieldId => $Values)
451 "FieldId" => $FieldId,
452 "Values" => $Values);
455 $KeywordStrings = $SearchParams->GetKeywordSearchStrings();
456 if (count($KeywordStrings))
459 "FieldId" =>
"X-KEYWORD-X",
460 "Values" => $KeywordStrings);
463 $Subgroups = $SearchParams->GetSubgroups();
464 if (count($Subgroups))
466 foreach ($Subgroups as $Subgroup)
469 $SubgroupItems = $this->FlattenSearchParams($Subgroup);
470 foreach ($SubgroupItems as $Item)
485 private function PrintFieldSelector($FieldId)
487 $ListName = $this->EditFormName.
"[]";
488 $SelectedValue = array();
490 # "Keyword" option is always here 491 $Options[
"X-KEYWORD-X"] =
"Keyword";
492 $OptionClass[
"X-KEYWORD-X"] =
"field-type-keyword";
493 if ($FieldId ==
"X-KEYWORD-X")
495 $SelectedValue[] =
"X-KEYWORD-X";
498 # prepare options for print 499 foreach ($this->MFields as $MField)
501 $TypeName = defaulthtmlentities(
502 str_replace(
' ',
'', strtolower($MField->TypeAsName())));
504 if (!$MField->Optional())
506 $TypeName .=
" required";
509 $FieldName = $MField->Name();
512 $FieldName = $this->AllSchemas[$MField->SchemaId()]->Name()
516 $Options[$MField->Id()] = defaulthtmlentities($FieldName);
517 $OptionClass[$MField->Id()] =
"field-type-".$TypeName;
519 if ($FieldId == $MField->Id())
521 $SelectedValue[] = $MField->Id();
525 # instantiate option list and print 526 $OptList =
new HtmlOptionList($ListName, $Options, $SelectedValue);
527 $OptList->ClassForList(
"field-subject");
528 $OptList->ClassForOptions($OptionClass);
530 $OptList->PrintHtml();
539 private function PrintValueSelector($FieldId, $CurVal)
541 # parameters of the option list 542 $ListName = $this->EditFormName.
"[]";
544 $OptionClass = array();
545 $SelectedValue = array();
547 # prepare options for print 548 foreach ($this->MFields as $MField)
553 foreach ($MField->GetPossibleValues() as $Id => $Val)
555 $IsSelected = $MField->Id() == $FieldId &&
556 strlen($CurVal) > 1 && $CurVal[0] ==
"=" &&
557 substr($CurVal, 1) == $Val ;
559 $Options[$Id] = defaulthtmlentities($Val);
560 $OptionClass[$Id] =
"field-id-".$MField->Id();
564 $SelectedValue[] = $Id;
570 # instantiate an option list and print 571 $OptList =
new HtmlOptionList($ListName, $Options, $SelectedValue);
572 $OptList->ClassForList(
"field-value-select");
573 $OptList->ClassForOptions($OptionClass);
575 $OptList->PrintHtml();
583 private function PrintQuicksearch($FieldId, $CurVal)
585 if ($FieldId !== NULL && $FieldId !=
"X-KEYWORD-X")
587 if (!isset($this->Factories[$FieldId]))
590 $Factory = $Field->GetFactory();
592 $this->Factories[$FieldId] =
593 ($Factory !== NULL) ? $Factory : FALSE;
596 $ItemId = ($this->Factories[$FieldId] !== FALSE) ?
597 $this->Factories[$FieldId]->GetItemIdByName($CurVal) :
"" ;
604 # field-value-qs css class required by our javascript. 605 # various cw-quicksearch classes required by the quicksearch javascript 606 # and ui-front class required by jquery-ui, used by qs js 607 print(
'<div class="field-value-qs cw-quicksearch ' 608 .
'cw-quicksearch-template">' 609 .
'<input class="cw-quicksearch-display ' 610 .
'cw-resourceeditor-metadatafield" ' 611 .
'placeholder="(enter some text to begin searching)" ' 612 .
'value="'.defaulthtmlentities($CurVal).
'" />' 613 .
'<input name="'.$this->EditFormName.
'[]" ' 614 .
'class="cw-quicksearch-value" type="hidden" ' 615 .
'value="'.defaulthtmlentities($ItemId).
'" />' 616 .
'<div style="display: none;" ' 617 .
'class="cw-quicksearch-menu">' 618 .
'<div class="cw-quicksearch-message ui-front"></div>' 625 private function PrintTemplateRow()
627 # field-row, template-row, field-value-edit, cw-speui-add, and 628 # cw-speui-add-subgroup css classes required by our javascript 630 "<tr class=\"field-row template-row ".$this->EditFormName.
"\"" 631 .
" style=\"white-space: nowrap;\">" 633 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 634 .
"cw-speui-delete\">X</span>" 636 $this->PrintFieldSelector(NULL);
637 $this->PrintValueSelector(NULL,
"");
638 print(
"<input type=\"text\" class=\"field-value-edit\" " 639 .
"name=\"".$this->EditFormName.
"[]\" placeholder=\"(search terms)\" " 641 $this->PrintQuicksearch(NULL,
"");
643 print(
"<tr><td colspan=2>" 644 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 645 .
"cw-speui-add\">Add Field</span>" 646 .
"<span class=\"cw-button cw-button-elegant cw-button-constrained " 647 .
"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.
static ConvertToDisplayParameters($SearchParams)
Get a simplified SearchParameterSet for display purposes.
Convenience class for generating an HTML select/option form element.
Class to create a user interface for editing SearchParameterSets.