CWIS Developer Documentation
FormUI.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: FormUI.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2016-2017 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
134 class FormUI extends FormUI_Base
135 {
136 
137  # ---- PUBLIC INTERFACE --------------------------------------------------
138 
145  public function DisplayFormTable($TableId = NULL, $TableStyle = NULL,
146  $TableCssClass = NULL)
147  {
148  # display nothing if there are no fields
149  if (!count($this->FieldParams)) { return; }
150 
151  # check whether table should be split into sections
152  $TableIsSectioned = FALSE;
153  foreach ($this->FieldParams as $Name => $Params)
154  {
155  if ($Params["Type"] == self::FTYPE_HEADING)
156  { $TableIsSectioned = TRUE; }
157  }
158 
159  # begin table
160  // @codingStandardsIgnoreStart
161  ?><table class="cw-table cw-table-fullsize cw-table-sideheaders cw-table-padded cw-table-striped<?PHP
162  if ($TableIsSectioned) { print(" cw-table-sectioned"); }
163  if (!is_null($TableCssClass)) { print(" ".$TableCssClass); }
164  ?> cw-content-sysconfigtable"<?PHP
165  if ($TableId) { print(" id=\"".$TableId."\""); }
166  if ($TableStyle) { print(" style=\"".$TableStyle."\""); }
167  ?>>
168  <tbody><?PHP
169  // @codingStandardsIgnoreEnd
170 
171  # for each field
172  foreach ($this->FieldParams as $Name => $Params)
173  {
174  # generate name for field
175  $FormFieldName = $this->GetFormFieldName($Name);
176 
177  # if field is actually a section heading
178  if ($Params["Type"] == self::FTYPE_HEADING)
179  {
180  # split table section and display heading
181  if (isset($HeadingAlreadyDisplayed)) { print("</tbody><tbody>"); }
182  ?><tr id="section-<?= $FormFieldName
183  ?>"><th colspan="3" scope="rowspan"><?=
184  $Params["Label"] ?></th></tr><?PHP
185  $HeadingAlreadyDisplayed = TRUE;
186  }
187  else
188  {
189  # determine if row may have taller content
190  $ShortRowFieldTypes = array(
191  self::FTYPE_FLAG,
192  self::FTYPE_METADATAFIELD,
193  self::FTYPE_NUMBER,
194  self::FTYPE_PASSWORD,
195  self::FTYPE_TEXT,
196  self::FTYPE_URL,
197  self::FTYPE_USER,
198  );
199  $IsTallRow =
200  ($Params["Type"] == self::FTYPE_QUICKSEARCH) ||
201  (!isset($Params["Units"])
202  && !in_array($Params["Type"], $ShortRowFieldTypes)
203  && (($Params["Type"] != self::FTYPE_OPTION)
204  || (isset($Params["Rows"])
205  && ($Params["Rows"] > 1))) );
206 
207  # load up value(s) to go into field
208  $Value = $this->GetFieldValue($Name);
209 
210  # set up CSS classes for table row
211  $RowClass = "cw-formui-fieldtype-".strtolower($Params["Type"]);
212  if ($Params["Type"] == "MetadataField")
213  {
214  $RowClass .= " cw-formui-schemaid-"
215  .GetArrayValue($Params, "SchemaId",
217  }
218  $RowClass .= $IsTallRow ? " cw-content-tallrow" : "";
219  $RowClassAttrib = ' class="'.$RowClass.'"';
220 
221  # set up CSS classes for row header cell
222  $HeaderClass = $IsTallRow ? "cw-content-tallrow-th" : "";
223  $HeaderClassAttrib = strlen($HeaderClass)
224  ? ' class="'.$HeaderClass.'"' : "";
225 
226  # set up CSS classes for row label
227  $LabelClass = "cw-form-pseudolabel"
228  .(isset(self::$ErrorMessages[$Name])
229  ? " cw-form-error" : "");
230 
231  # set up min/max note if applicable
232  unset($RangeNotePieces);
233  if (isset($Params["MinVal"]))
234  {
235  $RangeNotePieces[] = "Minimum: <i>".$Params["MinVal"]."</i>";
236  }
237  if (isset($Params["MaxVal"]))
238  {
239  $RangeNotePieces[] = "Maximum: <i>".$Params["MaxVal"]."</i>";
240  }
241  if (isset($Params["RecVal"]))
242  {
243  $RangeNotePieces[] = "Recommended: <i>".$Params["RecVal"]."</i>";
244  }
245  if (isset($RangeNotePieces))
246  {
247  $RangeNote = "(".implode(", ", $RangeNotePieces).")";
248  }
249 
250  // @codingStandardsIgnoreStart
251  ?>
252  <tr<?= ($IsTallRow ? " valign=\"top\"" : "").$RowClassAttrib
253  ?> id="row-<?= $FormFieldName ?>">
254  <th<?= $HeaderClassAttrib ?>>
255  <label for="<?= $FormFieldName
256  ?>" class="<?= $LabelClass ?>"><?=
257  $Params["Label"] ?></label>
258  </th>
259  <td <?PHP if (!isset($Params["Help"]) && !isset($RangeNotePieces)) {
260  print "colspan=\"2\""; } ?>><?PHP
261  $this->DisplayFormField(
262  $Name, $Value, $Params); ?></td>
263  <?PHP if (isset($Params["Help"])) { ?>
264  <td class="cw-content-help-cell"><?= $Params["Help"] ?></td>
265  <?PHP } elseif (isset($RangeNotePieces)) { ?>
266  <td class="cw-content-help-cell"><?= $RangeNote ?></td>
267  <?PHP } ?>
268  </tr>
269  <?PHP
270  }
271  }
272 
273  # end table
274  ?></tbody>
275  </table><?PHP
276 
277  # add any hidden form fields
278  print $this->GetHiddenFieldsHtml();
279 
280  # add any needed JavaScript for toggled fields
282 
283  # pull in WYSIWYG editor setup if needed
284  if ($this->UsingWysiwygEditor)
285  {
286  require_once($GLOBALS["AF"]->GUIFile("CKEditorSetup.php"));
287  }
288  }
289  // @codingStandardsIgnoreEnd
290 
294  public static function DisplayErrorBlock()
295  {
296  if (count(self::$ErrorMessages))
297  {
298  print "<ul class=\"cw-form-error\">\n";
299  $DisplayedMsgs = array();
300  foreach (self::$ErrorMessages as $Field => $Msgs)
301  {
302  foreach ($Msgs as $Msg)
303  {
304  if (!in_array($Msg, $DisplayedMsgs))
305  {
306  print "<li>".$Msg."</li>\n";
307  $DisplayedMsgs[] = $Msg;
308  }
309  }
310  }
311  print "</ul>\n";
312  }
313  }
314 
319  public function HandleDeletes()
320  {
321  parent::HandleDeletes();
322 
323  $TextFieldsToCheck = array();
324 
325  # check for text fields that may contain images
326  foreach ($this->FieldParams as $Name => $Params)
327  {
328  if (isset($Params["InsertIntoField"])
329  && (($Params["Type"] == self::FTYPE_FILE)
330  || ($Params["Type"] == self::FTYPE_IMAGE)))
331  {
332  $TextFieldsToCheck[] = $Params["InsertIntoField"];
333  }
334  }
335 
336  # load images to check
337  foreach ($this->DeletedImages as $ImageId)
338  {
339  $Images[$ImageId] = new SPTImage($ImageId);
340  }
341 
342  # load files to check
343  foreach ($this->DeletedFiles as $FileId)
344  {
345  $Files[$FileId] = new File($FileId);
346  }
347 
348  # for each text field potentially containing references to deleted items
349  foreach ($TextFieldsToCheck as $FieldName)
350  {
351  # get HTML form field name for field
352  $FormFieldName = $this->GetFormFieldName($FieldName);
353 
354  # for each deleted image
355  foreach ($this->DeletedImages as $ImageId)
356  {
357  # strip out any tags referencing that image from field content
358  $_POST[$FormFieldName] = preg_replace(
359  "%<img [^>]*src=\""
360  .$Images[$ImageId]->PreviewUrl()."\"[^>]*>%",
361  "",
362  $this->GetFieldValue($FieldName));
363  }
364 
365  # for each deleted file
366  foreach ($this->DeletedFiles as $FileId)
367  {
368  # strip out any tags we inserted that reference that file
369  $FileLink = $Files[$FileId]->GetLink();
370  $_POST[$FormFieldName] = preg_replace(
371  "%<a [^>]*href=\""
372  .preg_quote(htmlspecialchars($FileLink), '%')
373  ."\"[^>]*>(.*?)</a>%",
374  "\1",
375  $this->GetFieldValue($FieldName));
376  }
377  }
378  }
379 
380 
381 
382  # ---- PRIVATE INTERFACE -------------------------------------------------
383 
384  protected $UsingWysiwygEditor = FALSE;
385 
391 
398  protected function DisplayFormField($Name, $Value, $Params)
399  {
400  $FieldName = $this->GetFormFieldName($Name,
401  ($Params["Type"] != self::FTYPE_PRIVILEGES));
402 
403  switch ($Params["Type"])
404  {
405  case self::FTYPE_TEXT:
406  case self::FTYPE_NUMBER:
407  case self::FTYPE_URL:
408  case self::FTYPE_PASSWORD:
409  $DefaultSize = ($Params["Type"] == self::FTYPE_NUMBER) ? 6 : 40;
410  $DefaultMaxLen = ($Params["Type"] == self::FTYPE_NUMBER) ? 12 : 80;
411  $Size = isset($Params["Size"]) ? $Params["Size"]
412  : (isset($Params["MaxVal"])
413  ? (strlen(intval($Params["MaxVal"]) + 1))
414  : $DefaultSize);
415  $MaxLen = isset($Params["MaxLength"]) ? $Params["MaxLength"]
416  : (isset($Params["MaxVal"])
417  ? (strlen(intval($Params["MaxVal"]) + 3))
418  : $DefaultMaxLen);
419  $Placeholder = isset($Params["Placeholder"])
420  ? $Params["Placeholder"]
421  : "(".strtolower($Params["Label"]).")";
422  $InputType = ($Params["Type"] == self::FTYPE_PASSWORD)
423  ? "password" : "text";
424  print('<input type="'.$InputType.'" size="'.$Size.'" maxlength="'
425  .$MaxLen.'" id="'.$FieldName.'" name="'.$FieldName.'"'
426  .' value="'.htmlspecialchars($Value).'"'
427  .' placeholder=" '.htmlspecialchars($Placeholder).'"'
428  .($Params["ReadOnly"] ? " readonly" : "").' />');
429  break;
430 
431  case self::FTYPE_PARAGRAPH:
432  $Rows = isset($Params["Rows"]) ? $Params["Rows"]
433  : (isset($Params["Height"]) ? $Params["Height"] : 4);
434  $Columns = isset($Params["Columns"]) ? $Params["Columns"]
435  : (isset($Params["Width"]) ? $Params["Width"] : 40);
436  print('<textarea rows="'.$Rows.'" cols="'.$Columns
437  .'" id="'.$FieldName.'" name="'.$FieldName.'"'
438  .($Params["ReadOnly"] ? " readonly" : "")
439  .($Params["UseWYSIWYG"] ? ' class="ckeditor"' : "").'>'
440  .htmlspecialchars($Value)
441  .'</textarea>');
442  if ($Params["UseWYSIWYG"])
443  {
444  $this->UsingWysiwygEditor = TRUE;
445  }
446  break;
447 
448  case self::FTYPE_FLAG:
449  if (array_key_exists("OnLabel", $Params)
450  && array_key_exists("OffLabel", $Params))
451  {
452  print('<input type="radio" id="'.$FieldName.'On" name="'
453  .$FieldName.'" value="1"'
454  .($Value ? ' checked' : '')
455  .($Params["ReadOnly"] ? ' disabled' : '')
456  .' /> <label for="'.$FieldName.'On">'.$Params["OnLabel"]
457  ."</label>\n");
458  print('<input type="radio" id="'.$FieldName.'Off" name="'
459  .$FieldName.'" value="0"'
460  .($Value ? '' : ' checked')
461  .($Params["ReadOnly"] ? ' disabled' : '')
462  .' /> <label for="'.$FieldName.'Off">'.$Params["OffLabel"]
463  ."</label>\n");
464  }
465  else
466  {
467  print('<input type="checkbox" id="'.$FieldName.'" name="'
468  .$FieldName.'" '
469  .($Value ? ' checked' : '')
470  .($Params["ReadOnly"] ? ' disabled' : '')
471  ." />\n");
472  }
473  break;
474 
475  case self::FTYPE_OPTION:
476  if ($this->IsRadioButtonField($Name))
477  {
478  $OptList = new HtmlRadioButtonSet(
479  $FieldName, $Params["Options"], $Value);
480  }
481  else
482  {
483  $OptList = new HtmlOptionList(
484  $FieldName, $Params["Options"], $Value);
485  $OptList->MultipleAllowed($Params["AllowMultiple"]);
486  $OptList->Size(isset($Params["Rows"]) ? $Params["Rows"] : 1);
487  }
488  $OptList->Disabled($Params["ReadOnly"]);
489  $OptList->PrintHtml();
490  break;
491 
492  case self::FTYPE_METADATAFIELD:
493  $FieldTypes = GetArrayValue($Params, "FieldTypes");
494  $SchemaId = GetArrayValue($Params, "SchemaId",
496  $Schema = new MetadataSchema($SchemaId);
497  print $Schema->GetFieldsAsOptionList(
498  $FieldName, $FieldTypes, $Value,
499  !$Params["AllowMultiple"] && !$Params["Required"],
500  NULL, $Params["AllowMultiple"], $Params["ReadOnly"]);
501  break;
502 
503  case self::FTYPE_PRIVILEGES:
504  # (convert legacy previously-stored values if necessary)
505  if (is_array($Value))
506  {
507  $PrivSet = new PrivilegeSet();
508  $PrivSet->AddPrivilege($Value);
509  $Value = $PrivSet;
510  }
511 
512  $Schemas = GetArrayValue($Params, "Schemas");
513  $MFields = GetArrayValue($Params, "MetadataFields", array());
514  $PEditor = new PrivilegeEditingUI($Schemas, $MFields);
515  $PEditor->DisplaySet($FieldName, $Value);
516  break;
517 
518  case self::FTYPE_SEARCHPARAMS:
519  $SPEditor = new SearchParameterSetEditingUI($FieldName, $Value);
520 
521  if (isset($Params["MaxFieldLabelLength"]))
522  {
523  $SPEditor->MaxFieldLabelLength($Params["MaxFieldLabelLength"]);
524  }
525  if (isset($Params["MaxValueLabelLength"]))
526  {
527  $SPEditor->MaxValueLabelLength($Params["MaxValueLabelLength"]);
528  }
529 
530  $SPEditor->DisplayAsTable();
531  break;
532 
533  case self::FTYPE_USER:
534  case self::FTYPE_QUICKSEARCH:
535  if (is_null($Value))
536  {
537  $Value = [];
538  }
539 
540  # set up some helpers that abstract over the
541  # differences between a USER and a QUICKSEARCH field
542  if ($Params["Type"] == self::FTYPE_USER)
543  {
544  $Search = "UserSearch";
545  $AllowMultiple = $Params["AllowMultiple"];
546  $UFactory = new CWUserFactory();
547  $NameFn = function($Key, $Val) use ($UFactory) {
548  return $UFactory->UserExists($Val) ?
549  (new CWUser($Val))->Name() : "" ;
550  };
551  $IdFn = function($Key, $Val) {
552  return $Val;
553  };
554  }
555  else
556  {
557  $MField = new MetadataField($Params["Field"]);
558  $Search = $Params["Field"];
559  $AllowMultiple = $MField->AllowMultlple();
560  $NameFn = function($Key, $Val) use ($MField) {
561  if ($MField->Type() == MetadataSchema::MDFTYPE_REFERENCE)
562  {
563  $Resource = new Resource($Key);
564  return $Resource->GetMapped("Title");
565  }
566  else
567  {
568  return $Val;
569  }
570  };
571  $IdFn = function($Key, $Val) {
572  return $Key;
573  };
574  }
575 
576  # filter out empty incoming values
577  $Value = array_filter(
578  $Value, function($x) {
579  return strlen($x)>0;
580  });
581 
582  if (count($Value))
583  {
584  # iterate over incoming values
585  foreach ($Value as $Key => $Val)
586  {
587  # pull out the corresponding name/id
588  $VName = $NameFn($Key, $Val);
589  $VId = $IdFn($Key, $Val);
590 
591  # print UI elements
592  if ($Params["ReadOnly"])
593  {
594  print "<p>".defaulthtmlentities($VName)."</p>";
595  }
596  else
597  {
599  $Search,
600  $VId,
601  defaulthtmlentities($VName),
602  FALSE,
603  $FieldName);
604  }
605  }
606  }
607 
608  if (!$Params["ReadOnly"])
609  {
610  # display a blank row for adding more values
611  # when we have no values or when we allow more
612  if (count($Value)==0 || $AllowMultiple)
613  {
615  $Search,
616  "",
617  "",
618  $AllowMultiple,
619  $FieldName);
620  }
621  }
622  break;
623 
624  case self::FTYPE_FILE:
625  $this->DisplayFileField($FieldName, $Value, $Params);
626  break;
627 
628  case self::FTYPE_IMAGE:
629  $this->DisplayImageField($FieldName, $Value, $Params);
630  break;
631  }
632 
633  if (isset($Params["Units"]))
634  {
635  ?>&nbsp;<span><?PHP
636  print $Params["Units"];
637  ?></span><?PHP
638  }
639  }
640 
647  protected function DisplayImageField($FieldName, $Value, $Params)
648  {
649  # normalize incoming value
650  $Images = is_array($Value) ? $Value
651  : (($Value === NULL) ? array() : array($Value));
652 
653  # begin value table
654  print '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
655 
656  # for each incoming value
657  $ImagesDisplayed = 0;
658  $InsertButtonHtml = "";
659  foreach ($Images as $Image)
660  {
661  # skip if image is a placeholder to indicate no images for field
662  if ($Image == self::NO_VALUE_FOR_FIELD)
663  {
664  continue;
665  }
666 
667  # load up image object if ID supplied
668  if (is_numeric($Image))
669  {
670  $Image = new SPTImage($Image);
671  }
672 
673  # skip image if it has been deleted
674  if (in_array($Image->Id(), $this->DeletedImages))
675  {
676  continue;
677  }
678 
679  # load various image attributes for use in HTML
680  $ImageUrl = defaulthtmlentities($Image->ThumbnailUrl());
681  $ImageId = $Image->Id();
682  $ImageAltTextFieldName = $FieldName."_AltText_".$ImageId;
683  $ImageAltText = defaulthtmlentities(
684  isset($_POST[$ImageAltTextFieldName])
685  ? $_POST[$ImageAltTextFieldName]
686  : $Image->AltText());
687 
688  $DeleteFieldName = $this->GetFormFieldName("ImageToDelete");
689 
690  # build up HTML for any insert buttons
691  if (isset($Params["InsertIntoField"]))
692  {
693  $InsertField = $this->GetFormFieldName($Params["InsertIntoField"]);
694  $InsertRightCommand = defaulthtmlentities(
695  "CKEDITOR.instances['".$InsertField
696  ."'].insertHtml("
697  ."'<img src=\"".$Image->PreviewUrl()."\" alt=\""
698  .htmlspecialchars($Image->AltText())
699  ."\" class=\"cw-formui-image-right\" />');");
700  $InsertLeftCommand = defaulthtmlentities(
701  "CKEDITOR.instances['".$InsertField
702  ."'].insertHtml("
703  ."'<img src=\"".$Image->PreviewUrl()."\" alt=\""
704  .htmlspecialchars($Image->AltText())
705  ."\" class=\"cw-formui-image-left\" />');");
706  $InsertButtonHtml = '<button type="button" onclick="'
707  .$InsertLeftCommand.'">Insert-L</button>'
708  .'<button type="button" onclick="'
709  .$InsertRightCommand.'">Insert-R</button>';
710  }
711 
712  # add table row for image
713  ?><tr>
714  <td><img src="<?= $ImageUrl ?>"></td>
715  <td style="white-space: nowrap;"><label for="<?=
716  $ImageAltTextFieldName ?>" class="cw-form-pseudolabel">
717  Alt Text:</label><input type="text" size="20"
718  maxlength="120" name="<?= $ImageAltTextFieldName ?>"
719  value="<?= $ImageAltText ?>"
720  placeholder=" (alternate text for image)"></td>
721  <td><?= $InsertButtonHtml ?><input type="submit" name="Submit"
722  onclick="$('#<?= $DeleteFieldName ?>').val('<?= $ImageId
723  ?>');" value="Delete"></td>
724  </tr><?PHP
725  $ImagesDisplayed++;
726 
727  # add image ID to hidden fields
728  $this->HiddenFields[$FieldName."_ID"][] = $Image->Id();
729 
730  # add container to hold ID of any image to be deleted
731  if (!isset($this->HiddenFields[$DeleteFieldName]))
732  {
733  $this->HiddenFields[$DeleteFieldName] = "";
734  }
735  }
736 
737  # if no images were displayed and an image entry was skipped
738  if (($ImagesDisplayed == 0) && count($Images))
739  {
740  # add marker to indicate no images to hidden fields
741  $this->HiddenFields[$FieldName."_ID"][] = self::NO_VALUE_FOR_FIELD;
742  }
743 
744  # add table row for new image upload
745  if ($Params["AllowMultiple"] || ($ImagesDisplayed == 0))
746  {
747  $ImageAltTextFieldName = $FieldName."_AltText_NEW";
748  ?><tr>
749  <td><input type="file" name="<?= $FieldName ?>" /></td>
750  <td style="white-space: nowrap;"><label for="<?=
751  $ImageAltTextFieldName ?>" class="cw-form-pseudolabel">
752  Alt Text:</label><input type="text" size="20"
753  maxlength="120" name="<?= $ImageAltTextFieldName ?>"
754  placeholder=" (alternate text for image)"></td>
755  <td><input type="submit" name="Submit" value="Upload" /></td>
756  </tr><?PHP
757  }
758 
759  # end value table
760  print '</table>';
761  }
762 
769  protected function DisplayFileField($FieldName, $Value, $Params)
770  {
771  # normalize incoming value
772  $Files = is_array($Value) ? $Value
773  : (($Value === NULL) ? array() : array($Value));
774 
775  # begin value table
776  print '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
777 
778  # for each incoming value
779  $FilesDisplayed = 0;
780  $InsertButtonHtml = "";
781  foreach ($Files as $File)
782  {
783  # skip if file is a placeholder to indicate no files for field
784  if ($File == self::NO_VALUE_FOR_FIELD)
785  {
786  continue;
787  }
788 
789  # load up file object if ID supplied
790  if (is_numeric($File))
791  {
792  $File = new File($File);
793  }
794 
795  # skip file if it has been deleted
796  if (in_array($File->Id(), $this->DeletedFiles))
797  {
798  continue;
799  }
800 
801  # load various attributes for use in HTML
802  $FileId = $File->Id();
803  $FileUrl = $File->GetLink();
804  $FileName = $File->Name();
805  $FileLinkTag = "<a href=\"".htmlspecialchars($FileUrl)
806  ."\" target=\"_blank\">"
807  .htmlspecialchars($FileName)."</a>";
808  $DeleteFieldName = $this->GetFormFieldName("FileToDelete");
809 
810  # build up HTML for any insert buttons
811  if (isset($Params["InsertIntoField"]))
812  {
813  $InsertField = $this->GetFormFieldName($Params["InsertIntoField"]);
814  $InsertCommand = defaulthtmlentities(
815  "CKEDITOR.instances['".$InsertField
816  ."'].insertHtml('".$FileLinkTag."');");
817  $InsertButtonHtml = '<button type="button" onclick="'
818  .$InsertCommand.'">Insert</button>';
819  }
820 
821  # add table row for file
822  ?><tr>
823  <td><?= $FileLinkTag ?></td>
824  <td><?= $InsertButtonHtml ?><input type="submit" name="Submit"
825  onclick="$('#<?= $DeleteFieldName ?>').val('<?= $FileId
826  ?>');" value="Delete"></td>
827  </tr><?PHP
828  $FilesDisplayed++;
829 
830  # add file ID to hidden fields
831  $this->HiddenFields[$FieldName."_ID"][] = $FileId;
832 
833  # add container to hold ID of any file to be deleted
834  if (!isset($this->HiddenFields[$DeleteFieldName]))
835  {
836  $this->HiddenFields[$DeleteFieldName] = "";
837  }
838  }
839 
840  # if no files were displayed and a file entry was skipped
841  if (($FilesDisplayed == 0) && count($Files))
842  {
843  # add marker to indicate no files to hidden fields
844  $this->HiddenFields[$FieldName."_ID"][] = self::NO_VALUE_FOR_FIELD;
845  }
846 
847  # add table row for new file upload
848  if ($Params["AllowMultiple"] || ($FilesDisplayed == 0))
849  {
850  ?><tr>
851  <td><input type="file" name="<?= $FieldName ?>" /></td>
852  <td><input type="submit" name="Submit" value="Upload" /></td>
853  </tr><?PHP
854  }
855 
856  # end value table
857  print '</table>';
858  }
859 
864  protected function PrintFieldHidingJavascript()
865  {
866  # for each form field
867  foreach ($this->FieldParams as $ToggledField => $Params)
868  {
869  # if field has togglers (other fields that can toggle this field)
870  if (isset($Params["DisplayIf"]))
871  {
872  # for each toggler
873  foreach ($Params["DisplayIf"] as $Toggler => $ToggleValues)
874  {
875  # add field to list of fields toggled by this toggler
876  $FieldsToggled[$Toggler][] = $ToggledField;
877 
878  # add values to list of values that toggle this field
879  if (!is_array($ToggleValues))
880  { $ToggleValues = array($ToggleValues); }
881  $FieldToggleValues[$ToggledField][$Toggler] = $ToggleValues;
882  }
883  }
884  }
885 
886  # if there were fields that toggle other fields
887  if (isset($FieldsToggled))
888  {
889  # start JavaScript code
890  ?>
891  <script type="text/javascript">
892  (function($){
893  <?PHP
894 
895  # for each toggler
896  foreach ($FieldsToggled as $Toggler => $ToggledFields)
897  {
898  # begin function called when toggler changes
899  $TogglerFFName = $this->GetFormFieldName($Toggler);
900  ?>
901  $("[id^=<?= $TogglerFFName ?>]").change(function(){
902  <?PHP
903 
904  # for each togglee (field being toggled)
905  foreach ($ToggledFields as $ToggledField)
906  {
907  # get JavaScript condition for this togglee
908  $ConditionJS = $this->GetFieldToggleConditionJS(
909  $FieldToggleValues[$ToggledField]);
910 
911  # add toggle code for togglee
912  $ToggledFieldFFName = $this->GetFormFieldName($ToggledField);
913  ?>
914  $("#row-<?= $ToggledFieldFFName ?>")[<?=
915  $ConditionJS ?> ? "show" : "hide"]();
916  <?PHP
917  }
918 
919  # end function for field changing
920  ?>
921  }).change();
922  <?PHP
923  }
924 
925  # end JavaScript code
926  ?>
927  }(jQuery));
928  </script>
929  <?PHP
930  }
931  }
932 
939  private function GetFieldToggleConditionJS($ToggleValues)
940  {
941  # for each toggler
942  foreach ($ToggleValues as $Toggler => $ToggleValues)
943  {
944  # start with fresh subcondition list
945  $SubConditions = array();
946 
947  # for each toggle value
948  $TogglerFFName = $this->GetFormFieldName($Toggler);
949  foreach ($ToggleValues as $Value)
950  {
951  # build subcondition for value
952  if ($this->FieldParams[$Toggler]["Type"] == self::FTYPE_FLAG)
953  {
954  if ($Value)
955  {
956  $SubConditions[] = "($(\"input[name=".$TogglerFFName
957  ."]\").is(\":checked:visible\"))";
958  }
959  else
960  {
961  $SubConditions[] = "(!$(\"input[name=".$TogglerFFName
962  ."]\").is(\":checked:visible\"))";
963  }
964  }
965  else
966  {
967  if ($this->IsRadioButtonField($Toggler))
968  {
969  $SubConditions[] = "($(\"input[name=".$TogglerFFName
970  ."]:checked\").val() == \"".$Value."\")";
971  }
972  else
973  {
974  $SubConditions[] = "($(\"#".$TogglerFFName
975  ."\").val() == \"".$Value."\")";
976  }
977  }
978  }
979 
980  # assemble subconditions into condition
981  if (count($SubConditions) > 1)
982  {
983  $SubConditionStrings[] = "(".implode(" || ", $SubConditions).")";
984  }
985  else
986  {
987  $SubConditionStrings[] = $SubConditions[0];
988  }
989  }
990 
991  # assemble conditions into condition string
992  $ConditionString = implode(" && ", $SubConditionStrings);
993 
994  return $ConditionString;
995  }
996 
1002  private function IsRadioButtonField($FieldName)
1003  {
1004  $Params = $this->FieldParams[$FieldName];
1005  if ($Params["Type"] == self::FTYPE_OPTION)
1006  {
1007  if (isset($Params["RadioButtons"]))
1008  {
1009  return $Params["RadioButtons"] ? TRUE : FALSE;
1010  }
1011  elseif (isset($Params["AllowMultiple"]) && $Params["AllowMultiple"])
1012  {
1013  return FALSE;
1014  }
1015  else
1016  {
1017  return (count($Params["Options"])
1018  <= self::OPTION_RADIO_BUTTON_THRESHOLD) ? TRUE : FALSE;
1019  }
1020  }
1021  else
1022  {
1023  return FALSE;
1024  }
1025  }
1026 }
PrintFieldHidingJavascript()
Print any JavaScript required to support toggling display of fields or sections.
Definition: FormUI.php:864
Base class (covering non-presentation elements) supplying a standard user interface for presenting an...
Definition: FormUI_Base.php:14
Metadata schema (in effect a Factory class for MetadataField).
GetHiddenFieldsHtml()
Get HTML for hidden form fields associated with form processing.
DisplayFormField($Name, $Value, $Params)
Display HTML form field for specified field.
Definition: FormUI.php:398
User interface element for editing PrivilegeSets.
Set of privileges used to access resource information or other parts of the system.
static PrintQuickSearchField($FieldId, $CurrentValue, $CurrentDisplayValue, $CloneAfter=FALSE, $FormFieldName=NULL)
Print the blank text field quick search field for the QuickSearch JS object.
CWIS-specific user factory class.
Child class (covering presentation elements only) supplying a standard user interface for presenting ...
Definition: FormUI.php:134
DisplayImageField($FieldName, $Value, $Params)
Display image form field for specified field.
Definition: FormUI.php:647
Convenience class for generating a set of HTML radio button form elements.
Encapsulates a full-size, preview, and thumbnail image.
Definition: SPTImage.php:13
HandleDeletes()
Handle image deletion, removing deleted images from text fields where they may have been inserted...
Definition: FormUI.php:319
Object representing a locally-defined type of metadata field.
const OPTION_RADIO_BUTTON_THRESHOLD
FTYPE_OPTION fields with this many or fewer options will display as radio buttons by default...
Definition: FormUI.php:390
Represents a "resource" in CWIS.
Definition: Resource.php:13
static DisplayErrorBlock()
Display HTML block with error messages (if any).
Definition: FormUI.php:294
GetFormFieldName($FieldName, $IncludePrefix=TRUE)
Get HTML form field name for specified field.
DisplayFileField($FieldName, $Value, $Params)
Display file form field for specified field.
Definition: FormUI.php:769
GetFieldValue($FieldName)
Get value for form field.
$UsingWysiwygEditor
Definition: FormUI.php:384
Class representing a stored (usually uploaded) file.
Definition: File.php:13
DisplayFormTable($TableId=NULL, $TableStyle=NULL, $TableCssClass=NULL)
Display HTML table for form.
Definition: FormUI.php:145
CWIS-specific user class.
Definition: CWUser.php:13
Convenience class for generating an HTML select/option form element.
Class to create a user interface for editing SearchParameterSets.