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"] : 4;
433  $Columns = isset($Params["Columns"]) ? $Params["Columns"] : 40;
434  print('<textarea rows="'.$Rows.'" cols="'.$Columns
435  .'" id="'.$FieldName.'" name="'.$FieldName.'"'
436  .($Params["ReadOnly"] ? " readonly" : "")
437  .($Params["UseWYSIWYG"] ? ' class="ckeditor"' : "").'>'
438  .htmlspecialchars($Value)
439  .'</textarea>');
440  if ($Params["UseWYSIWYG"])
441  {
442  $this->UsingWysiwygEditor = TRUE;
443  }
444  break;
445 
446  case self::FTYPE_FLAG:
447  if (array_key_exists("OnLabel", $Params)
448  && array_key_exists("OffLabel", $Params))
449  {
450  print('<input type="radio" id="'.$FieldName.'On" name="'
451  .$FieldName.'" value="1"'
452  .($Value ? ' checked' : '')
453  .($Params["ReadOnly"] ? ' disabled' : '')
454  .' /> <label for="'.$FieldName.'On">'.$Params["OnLabel"]
455  ."</label>\n");
456  print('<input type="radio" id="'.$FieldName.'Off" name="'
457  .$FieldName.'" value="0"'
458  .($Value ? '' : ' checked')
459  .($Params["ReadOnly"] ? ' disabled' : '')
460  .' /> <label for="'.$FieldName.'Off">'.$Params["OffLabel"]
461  ."</label>\n");
462  }
463  else
464  {
465  print('<input type="checkbox" id="'.$FieldName.'" name="'
466  .$FieldName.'" '
467  .($Value ? ' checked' : '')
468  .($Params["ReadOnly"] ? ' disabled' : '')
469  ." />\n");
470  }
471  break;
472 
473  case self::FTYPE_OPTION:
474  if ($this->IsRadioButtonField($Name))
475  {
476  $OptList = new HtmlRadioButtonSet(
477  $FieldName, $Params["Options"], $Value);
478  }
479  else
480  {
481  $OptList = new HtmlOptionList(
482  $FieldName, $Params["Options"], $Value);
483  $OptList->MultipleAllowed($Params["AllowMultiple"]);
484  $OptList->Size(isset($Params["Rows"]) ? $Params["Rows"] : 1);
485  }
486  $OptList->Disabled($Params["ReadOnly"]);
487  $OptList->PrintHtml();
488  break;
489 
490  case self::FTYPE_METADATAFIELD:
491  $FieldTypes = GetArrayValue($Params, "FieldTypes");
492  $SchemaId = GetArrayValue($Params, "SchemaId",
494  $Schema = new MetadataSchema($SchemaId);
495  print $Schema->GetFieldsAsOptionList(
496  $FieldName, $FieldTypes, $Value,
497  !$Params["AllowMultiple"] && !$Params["Required"],
498  NULL, $Params["AllowMultiple"], $Params["ReadOnly"]);
499  break;
500 
501  case self::FTYPE_PRIVILEGES:
502  # (convert legacy previously-stored values if necessary)
503  if (is_array($Value))
504  {
505  $PrivSet = new PrivilegeSet();
506  $PrivSet->AddPrivilege($Value);
507  $Value = $PrivSet;
508  }
509 
510  $Schemas = GetArrayValue($Params, "Schemas");
511  $MFields = GetArrayValue($Params, "MetadataFields", array());
512  $PEditor = new PrivilegeEditingUI($Schemas, $MFields);
513  $PEditor->DisplaySet($FieldName, $Value);
514  break;
515 
516  case self::FTYPE_SEARCHPARAMS:
517  $SPEditor = new SearchParameterSetEditingUI($FieldName, $Value);
518 
519  if (isset($Params["MaxFieldLabelLength"]))
520  {
521  $SPEditor->MaxFieldLabelLength($Params["MaxFieldLabelLength"]);
522  }
523  if (isset($Params["MaxValueLabelLength"]))
524  {
525  $SPEditor->MaxValueLabelLength($Params["MaxValueLabelLength"]);
526  }
527 
528  $SPEditor->DisplayAsTable();
529  break;
530 
531  case self::FTYPE_USER:
532  case self::FTYPE_QUICKSEARCH:
533  if (is_null($Value))
534  {
535  $Value = [];
536  }
537 
538  # set up some helpers that abstract over the
539  # differences between a USER and a QUICKSEARCH field
540  if ($Params["Type"] == self::FTYPE_USER)
541  {
542  $Search = "UserSearch";
543  $AllowMultiple = $Params["AllowMultiple"];
544  $UFactory = new CWUserFactory();
545  $NameFn = function($Key, $Val) use ($UFactory) {
546  return $UFactory->UserExists($Val) ?
547  (new CWUser($Val))->Name() : "" ;
548  };
549  $IdFn = function($Key, $Val) {
550  return $Val;
551  };
552  }
553  else
554  {
555  $MField = new MetadataField($Params["Field"]);
556  $Search = $Params["Field"];
557  $AllowMultiple = $MField->AllowMultlple();
558  $NameFn = function($Key, $Val) use ($MField) {
559  if ($MField->Type() == MetadataSchema::MDFTYPE_REFERENCE)
560  {
561  $Resource = new Resource($Key);
562  return $Resource->GetMapped("Title");
563  }
564  else
565  {
566  return $Val;
567  }
568  };
569  $IdFn = function($Key, $Val) {
570  return $Key;
571  };
572  }
573 
574  # filter out empty incoming values
575  $Value = array_filter(
576  $Value, function($x) {
577  return strlen($x)>0;
578  });
579 
580  if (count($Value))
581  {
582  # iterate over incoming values
583  foreach ($Value as $Key => $Val)
584  {
585  # pull out the corresponding name/id
586  $VName = $NameFn($Key, $Val);
587  $VId = $IdFn($Key, $Val);
588 
589  # print UI elements
590  if ($Params["ReadOnly"])
591  {
592  print "<p>".defaulthtmlentities($VName)."</p>";
593  }
594  else
595  {
597  $Search,
598  $VId,
599  defaulthtmlentities($VName),
600  FALSE,
601  $FieldName);
602  }
603  }
604  }
605 
606  if (!$Params["ReadOnly"])
607  {
608  # display a blank row for adding more values
609  # when we have no values or when we allow more
610  if (count($Value)==0 || $AllowMultiple)
611  {
613  $Search,
614  "",
615  "",
616  $AllowMultiple,
617  $FieldName);
618  }
619  }
620  break;
621 
622  case self::FTYPE_FILE:
623  $this->DisplayFileField($FieldName, $Value, $Params);
624  break;
625 
626  case self::FTYPE_IMAGE:
627  $this->DisplayImageField($FieldName, $Value, $Params);
628  break;
629  }
630 
631  if (isset($Params["Units"]))
632  {
633  ?>&nbsp;<span><?PHP
634  print $Params["Units"];
635  ?></span><?PHP
636  }
637  }
638 
645  protected function DisplayImageField($FieldName, $Value, $Params)
646  {
647  # normalize incoming value
648  $Images = is_array($Value) ? $Value
649  : (($Value === NULL) ? array() : array($Value));
650 
651  # begin value table
652  print '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
653 
654  # for each incoming value
655  $ImagesDisplayed = 0;
656  $InsertButtonHtml = "";
657  foreach ($Images as $Image)
658  {
659  # skip if image is a placeholder to indicate no images for field
660  if ($Image == self::NO_VALUE_FOR_FIELD)
661  {
662  continue;
663  }
664 
665  # load up image object if ID supplied
666  if (is_numeric($Image))
667  {
668  $Image = new SPTImage($Image);
669  }
670 
671  # skip image if it has been deleted
672  if (in_array($Image->Id(), $this->DeletedImages))
673  {
674  continue;
675  }
676 
677  # load various image attributes for use in HTML
678  $ImageUrl = defaulthtmlentities($Image->ThumbnailUrl());
679  $ImageId = $Image->Id();
680  $ImageAltTextFieldName = $FieldName."_AltText_".$ImageId;
681  $ImageAltText = defaulthtmlentities(
682  isset($_POST[$ImageAltTextFieldName])
683  ? $_POST[$ImageAltTextFieldName]
684  : $Image->AltText());
685 
686  $DeleteFieldName = $this->GetFormFieldName("ImageToDelete");
687 
688  # build up HTML for any insert buttons
689  if (isset($Params["InsertIntoField"]))
690  {
691  $InsertField = $this->GetFormFieldName($Params["InsertIntoField"]);
692  $InsertRightCommand = defaulthtmlentities(
693  "CKEDITOR.instances['".$InsertField
694  ."'].insertHtml("
695  ."'<img src=\"".$Image->PreviewUrl()."\" alt=\""
696  .htmlspecialchars($Image->AltText())
697  ."\" class=\"cw-formui-image-right\" />');");
698  $InsertLeftCommand = defaulthtmlentities(
699  "CKEDITOR.instances['".$InsertField
700  ."'].insertHtml("
701  ."'<img src=\"".$Image->PreviewUrl()."\" alt=\""
702  .htmlspecialchars($Image->AltText())
703  ."\" class=\"cw-formui-image-left\" />');");
704  $InsertButtonHtml = '<button type="button" onclick="'
705  .$InsertLeftCommand.'">Insert-L</button>'
706  .'<button type="button" onclick="'
707  .$InsertRightCommand.'">Insert-R</button>';
708  }
709 
710  # add table row for image
711  ?><tr>
712  <td><img src="<?= $ImageUrl ?>"></td>
713  <td style="white-space: nowrap;"><label for="<?=
714  $ImageAltTextFieldName ?>" class="cw-form-pseudolabel">
715  Alt Text:</label><input type="text" size="20"
716  maxlength="120" name="<?= $ImageAltTextFieldName ?>"
717  value="<?= $ImageAltText ?>"
718  placeholder=" (alternate text for image)"></td>
719  <td><?= $InsertButtonHtml ?><input type="submit" name="Submit"
720  onclick="$('#<?= $DeleteFieldName ?>').val('<?= $ImageId
721  ?>');" value="Delete"></td>
722  </tr><?PHP
723  $ImagesDisplayed++;
724 
725  # add image ID to hidden fields
726  $this->HiddenFields[$FieldName."_ID"][] = $Image->Id();
727 
728  # add container to hold ID of any image to be deleted
729  if (!isset($this->HiddenFields[$DeleteFieldName]))
730  {
731  $this->HiddenFields[$DeleteFieldName] = "";
732  }
733  }
734 
735  # if no images were displayed and an image entry was skipped
736  if (($ImagesDisplayed == 0) && count($Images))
737  {
738  # add marker to indicate no images to hidden fields
739  $this->HiddenFields[$FieldName."_ID"][] = self::NO_VALUE_FOR_FIELD;
740  }
741 
742  # add table row for new image upload
743  if ($Params["AllowMultiple"] || ($ImagesDisplayed == 0))
744  {
745  $ImageAltTextFieldName = $FieldName."_AltText_NEW";
746  ?><tr>
747  <td><input type="file" name="<?= $FieldName ?>" /></td>
748  <td style="white-space: nowrap;"><label for="<?=
749  $ImageAltTextFieldName ?>" class="cw-form-pseudolabel">
750  Alt Text:</label><input type="text" size="20"
751  maxlength="120" name="<?= $ImageAltTextFieldName ?>"
752  placeholder=" (alternate text for image)"></td>
753  <td><input type="submit" name="Submit" value="Upload" /></td>
754  </tr><?PHP
755  }
756 
757  # end value table
758  print '</table>';
759  }
760 
767  protected function DisplayFileField($FieldName, $Value, $Params)
768  {
769  # normalize incoming value
770  $Files = is_array($Value) ? $Value
771  : (($Value === NULL) ? array() : array($Value));
772 
773  # begin value table
774  print '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
775 
776  # for each incoming value
777  $FilesDisplayed = 0;
778  $InsertButtonHtml = "";
779  foreach ($Files as $File)
780  {
781  # skip if file is a placeholder to indicate no files for field
782  if ($File == self::NO_VALUE_FOR_FIELD)
783  {
784  continue;
785  }
786 
787  # load up file object if ID supplied
788  if (is_numeric($File))
789  {
790  $File = new File($File);
791  }
792 
793  # skip file if it has been deleted
794  if (in_array($File->Id(), $this->DeletedFiles))
795  {
796  continue;
797  }
798 
799  # load various attributes for use in HTML
800  $FileId = $File->Id();
801  $FileUrl = $File->GetLink();
802  $FileName = $File->Name();
803  $FileLinkTag = "<a href=\"".htmlspecialchars($FileUrl)
804  ."\" target=\"_blank\">"
805  .htmlspecialchars($FileName)."</a>";
806  $DeleteFieldName = $this->GetFormFieldName("FileToDelete");
807 
808  # build up HTML for any insert buttons
809  if (isset($Params["InsertIntoField"]))
810  {
811  $InsertField = $this->GetFormFieldName($Params["InsertIntoField"]);
812  $InsertCommand = defaulthtmlentities(
813  "CKEDITOR.instances['".$InsertField
814  ."'].insertHtml('".$FileLinkTag."');");
815  $InsertButtonHtml = '<button type="button" onclick="'
816  .$InsertCommand.'">Insert</button>';
817  }
818 
819  # add table row for file
820  ?><tr>
821  <td><?= $FileLinkTag ?></td>
822  <td><?= $InsertButtonHtml ?><input type="submit" name="Submit"
823  onclick="$('#<?= $DeleteFieldName ?>').val('<?= $FileId
824  ?>');" value="Delete"></td>
825  </tr><?PHP
826  $FilesDisplayed++;
827 
828  # add file ID to hidden fields
829  $this->HiddenFields[$FieldName."_ID"][] = $FileId;
830 
831  # add container to hold ID of any file to be deleted
832  if (!isset($this->HiddenFields[$DeleteFieldName]))
833  {
834  $this->HiddenFields[$DeleteFieldName] = "";
835  }
836  }
837 
838  # if no files were displayed and a file entry was skipped
839  if (($FilesDisplayed == 0) && count($Files))
840  {
841  # add marker to indicate no files to hidden fields
842  $this->HiddenFields[$FieldName."_ID"][] = self::NO_VALUE_FOR_FIELD;
843  }
844 
845  # add table row for new file upload
846  if ($Params["AllowMultiple"] || ($FilesDisplayed == 0))
847  {
848  ?><tr>
849  <td><input type="file" name="<?= $FieldName ?>" /></td>
850  <td><input type="submit" name="Submit" value="Upload" /></td>
851  </tr><?PHP
852  }
853 
854  # end value table
855  print '</table>';
856  }
857 
862  protected function PrintFieldHidingJavascript()
863  {
864  # for each form field
865  foreach ($this->FieldParams as $ToggledField => $Params)
866  {
867  # if field has togglers (other fields that can toggle this field)
868  if (isset($Params["DisplayIf"]))
869  {
870  # for each toggler
871  foreach ($Params["DisplayIf"] as $Toggler => $ToggleValues)
872  {
873  # add field to list of fields toggled by this toggler
874  $FieldsToggled[$Toggler][] = $ToggledField;
875 
876  # add values to list of values that toggle this field
877  if (!is_array($ToggleValues))
878  { $ToggleValues = array($ToggleValues); }
879  $FieldToggleValues[$ToggledField][$Toggler] = $ToggleValues;
880  }
881  }
882  }
883 
884  # if there were fields that toggle other fields
885  if (isset($FieldsToggled))
886  {
887  # start JavaScript code
888  ?>
889  <script type="text/javascript">
890  (function($){
891  <?PHP
892 
893  # for each toggler
894  foreach ($FieldsToggled as $Toggler => $ToggledFields)
895  {
896  # begin function called when toggler changes
897  $TogglerFFName = $this->GetFormFieldName($Toggler);
898  ?>
899  $("[id^=<?= $TogglerFFName ?>]").change(function(){
900  <?PHP
901 
902  # for each togglee (field being toggled)
903  foreach ($ToggledFields as $ToggledField)
904  {
905  # get JavaScript condition for this togglee
906  $ConditionJS = $this->GetFieldToggleConditionJS(
907  $FieldToggleValues[$ToggledField]);
908 
909  # add toggle code for togglee
910  $ToggledFieldFFName = $this->GetFormFieldName($ToggledField);
911  ?>
912  $("#row-<?= $ToggledFieldFFName ?>")[<?=
913  $ConditionJS ?> ? "show" : "hide"]();
914  <?PHP
915  }
916 
917  # end function for field changing
918  ?>
919  }).change();
920  <?PHP
921  }
922 
923  # end JavaScript code
924  ?>
925  }(jQuery));
926  </script>
927  <?PHP
928  }
929  }
930 
937  private function GetFieldToggleConditionJS($ToggleValues)
938  {
939  # for each toggler
940  foreach ($ToggleValues as $Toggler => $ToggleValues)
941  {
942  # start with fresh subcondition list
943  $SubConditions = array();
944 
945  # for each toggle value
946  $TogglerFFName = $this->GetFormFieldName($Toggler);
947  foreach ($ToggleValues as $Value)
948  {
949  # build subcondition for value
950  if ($this->FieldParams[$Toggler]["Type"] == self::FTYPE_FLAG)
951  {
952  if ($Value)
953  {
954  $SubConditions[] = "($(\"input[name=".$TogglerFFName
955  ."]\").is(\":checked:visible\"))";
956  }
957  else
958  {
959  $SubConditions[] = "(!$(\"input[name=".$TogglerFFName
960  ."]\").is(\":checked:visible\"))";
961  }
962  }
963  else
964  {
965  if ($this->IsRadioButtonField($Toggler))
966  {
967  $SubConditions[] = "($(\"input[name=".$TogglerFFName
968  ."]:checked\").val() == \"".$Value."\")";
969  }
970  else
971  {
972  $SubConditions[] = "($(\"#".$TogglerFFName
973  ."\").val() == \"".$Value."\")";
974  }
975  }
976  }
977 
978  # assemble subconditions into condition
979  if (count($SubConditions) > 1)
980  {
981  $SubConditionStrings[] = "(".implode(" || ", $SubConditions).")";
982  }
983  else
984  {
985  $SubConditionStrings[] = $SubConditions[0];
986  }
987  }
988 
989  # assemble conditions into condition string
990  $ConditionString = implode(" && ", $SubConditionStrings);
991 
992  return $ConditionString;
993  }
994 
1000  private function IsRadioButtonField($FieldName)
1001  {
1002  $Params = $this->FieldParams[$FieldName];
1003  if ($Params["Type"] == self::FTYPE_OPTION)
1004  {
1005  if (isset($Params["RadioButtons"]))
1006  {
1007  return $Params["RadioButtons"] ? TRUE : FALSE;
1008  }
1009  elseif (isset($Params["AllowMultiple"]) && $Params["AllowMultiple"])
1010  {
1011  return FALSE;
1012  }
1013  else
1014  {
1015  return (count($Params["Options"])
1016  <= self::OPTION_RADIO_BUTTON_THRESHOLD) ? TRUE : FALSE;
1017  }
1018  }
1019  else
1020  {
1021  return FALSE;
1022  }
1023  }
1024 }
PrintFieldHidingJavascript()
Print any JavaScript required to support toggling display of fields or sections.
Definition: FormUI.php:862
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:645
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:767
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.