function fp_render_element

6.x render.inc fp_render_element($name, $element, $use_session_submission_values_for_callback = "")
5.x render.inc fp_render_element($name, $element, $use_session_submission_values_for_callback = "")

Returns the HTML to render this form (or content) element to the screen. $name is the HTML machine name. $element is an array containing all we need to render it. If you want default values to be taken from the SESSION (because we had form_errors, say, and we want values to keep what we had between submissions) specify the callback to use in the use_session_submission_values_for_callback variable.

1 call to fp_render_element()
fp_render_array in includes/render.inc
This takes a render_array and generates the HTML for it. This usually is not called directly, but instead you should call fp_render_content() or fp_render_form()

File

includes/render.inc, line 437

Code

function fp_render_element($name, $element, $use_session_submission_values_for_callback = "") {
  $rtn = "";

  $type = @$element ["type"];
  if ($type == "") {
    $type = "markup";
  }

  // Make sure the "css name" is friendly.
  $cssname = fp_get_machine_readable($name);




  if ($type == "do_not_render") {
    return; // not supposed to render this element.
  }

  // Does the name start with a # character?  If so, do not attempt to render.
  if (substr($name, 0, 1) == "#") {
    return;
  }

  $value = @$element ["value"];
  $label = @$element ["label"];
  $options = @$element ["options"];
  $description = @$element ["description"];
  $popup_description = @$element ["popup_description"];
  $prefix = @$element ["prefix"];
  $suffix = @$element ["suffix"];
  $multiple = @$element ["multiple"];
  if ($multiple == TRUE) {
    $multiple = "multiple=multiple";
  }
  else {
    $multiple = "";
  }

  $required = @$element ["required"];
  $no_please_select = @$element ["no_please_select"];
  if (isset($element ["hide_please_select"])) {
    $no_please_select = @$element ["hide_please_select"];
  }
  $confirm = @$element ["confirm"];

  // Let's also add our cssname as a class to the element, so that even markup will get it...
  if ($type == "markup") {
    if (!isset($element ["attributes"]) || is_array($element ["attributes"])) {
      @$element ["attributes"]["class"] .= " markup-element form-element markup-element-$cssname";
    }
  }



  $attributes = @$element ["attributes"];

  if (is_array($attributes)) {
    // Convert the attributes array into a string.

    $new_attr = "";
    foreach ($attributes as $key => $val) {
      $new_attr .= " $key='$val' ";
    }
    $attributes = $new_attr;
  }

  $popup_help_link = "";
  if ($popup_description) {

    //$popup_help_link = " <a href='javascript: alert(\"" . $popup_description . "\");' class='form-popup-description'>[?]</a>";
    $popup_help_link = fp_get_js_alert_link($popup_description, "<span class='pop-q-mark'>?</span>", "form-popup-description");
  }

  $element_error_css = "";
  if (isset($_SESSION ["fp_form_errors"]) && is_array($_SESSION ["fp_form_errors"])) {
    foreach ($_SESSION ["fp_form_errors"] as $err) {
      if ($err ["name"] == $name) {
        // There is an error on this element!  Add an extra CSS element.
        $element_error_css .= "form-element-error";
      }
    }
  }

  if ($use_session_submission_values_for_callback && is_array(@$_SESSION ["fp_form_submissions"][$use_session_submission_values_for_callback]["values"])) {
    // Check the SESSION for a previous value which we should use.
    $ignore_types = array("hidden", "markup", "submit", "password");
    if (!in_array($type, $ignore_types)) {
      $value = $_SESSION ["fp_form_submissions"][$use_session_submission_values_for_callback]["values"][$name];
    }
  }


  if ($type != "markup") {
    $rtn .= "<div id='element-wrapper-$cssname' class='form-element element-type-$type'>";
  }

  if ($prefix) {
    $rtn .= $prefix;
  }


  if ($type != "markup") {
    $rtn .= "<div id='element-inner-wrapper-$cssname' class='form-element element-type-$type $element_error_css'>";
  }



  $ast = "";
  if ($required) {
    $ast = "<span class='form-required-ast'>*</span>";
  }

  // First of all, what is it's "type"?
  if ($type == "markup") {
    if (is_string($attributes) && $attributes != "") {
      $rtn .= "<div $attributes>";
    }

    // If a label is set, go ahead and display, even though its markup...
    if ($label != "") {
      $rtn .= "<label>$ast$label$popup_help_link</label>";
    }

    $rtn .= "$value";

    if (is_string($attributes) && $attributes != "") {
      $rtn .= "</div>";
    }
  }
  else if ($type != "hidden" && $type != "checkbox") {
    $rtn .= "<label>$ast$label$popup_help_link</label>";
  }

  if ($type == "textarea") {
    $rows = (isset($element ["rows"])) ? $element ["rows"] : "5";
    $rtn .= "<textarea name='$name' id='element-$cssname' rows='$rows' $attributes>$value</textarea>";
  }

  if ($type == "textfield" || $type == "text" || $type == "password") {
    if ($type == "textfield") {
      $type = "text";
    }
    $size = (isset($element ["size"])) ? $element ["size"] : "60";
    $maxlength = (isset($element ["maxlength"])) ? $element ["maxlength"] : "255";
    $value = htmlentities($value, ENT_QUOTES);
    $rtn .= "<input type='$type' name='$name' id='element-$cssname' size='$size' maxlength='$maxlength' value='$value' $attributes>";
  }

  if ($type == "hidden") {
    $value = htmlentities($value, ENT_QUOTES);
    $rtn .= "<input type='hidden' name='$name' id='element-$cssname' value='$value'>";
  }

  if ($type == "file") {
    $tname = $name;
    if ($multiple != "") {
      $tname .= "[]"; // if we allow uploading multiple files, we MUST put a [] behind it, or HTML will not upload correctly.  Weird but true.
    }
    $rtn .= "<input type='file' name='$tname' id='element-$cssname' $multiple $attributes>";
  }


  if ($type == "radios") {
    $rtn .= "<div class='form-radios form-radios-$cssname'>";
    foreach ($options as $key => $val) {
      $checked = "";
      if ($value == $key) {
        $checked = "checked=checked";
      }
      $csskey = fp_get_machine_readable($key);
      $rtn .= "<div class='radio-element radio-element-$csskey'>
                 <label class='label-for-radio'><input type='radio' name='$name' id='element-$cssname-$csskey' value='$key' $checked $attributes> $val</label>
               </div>";
    }
    $rtn .= "</div>";
  }

  if ($type == "select") {
    $rtn .= "<select name='$name' id='element-$cssname' $attributes>";
    if ($no_please_select != TRUE) {
      $rtn .= "<option value=''>- Please select -</option>";
    }

    foreach ($options as $key => $val) {
      $selected = "";
      if ($value == $key) {
        $selected = "selected";
      }
      $rtn .= "<option value='$key' $selected>$val</option>";
    }
    $rtn .= "</select>";
  }


  // Multiple checkboxes...
  if ($type == "checkboxes") {
    $rtn .= "<div class='form-checkboxes form-checkboxes-$cssname'>";
    foreach ($options as $key => $val) {
      $checked = "";
      if (is_array($value) && isset($value [$key]) && $value [$key] == $key) {

        $checked = "checked=checked";
      }
      $csskey = fp_get_machine_readable($key);
      $rtn .= "<div class='checkbox-element checkbox-element-$csskey'>
                 <label class='label-for-checkbox'><input type='checkbox' name='$name" . "[$key]' id='element-$cssname-$csskey' value='$key' $checked $attributes> $val</label>
               </div>";
    }
    $rtn .= "</div>";
  }

  // A single checkbox... The values will be with 0 (zero) or 1 (one), and boolean
  // values are accepted/saved
  if ($type == "checkbox") {
    $rtn .= "<div class='form-checkbox form-checkbox-$cssname'>";

    $checked = "";
    if ((bool) ($value) == TRUE) {
      $checked = "checked=checked";
    }
    $rtn .= "<div class='checkbox-element'>
               <label class='label-for-checkbox'><input type='checkbox' name='$name' id='element-$cssname' value='1' $checked $attributes> $label$popup_help_link</label>
             </div>";

    $rtn .= "</div>";
  }



  if ($type == "submit") {

    if ($confirm != "") {
      $confirm = htmlentities($confirm, ENT_QUOTES);
      $confirm = str_replace("\n", "\\n", $confirm);

      $attributes .= " onClick='return confirm(\"$confirm\");' ";
    }

    $rtn .= "<input type='submit' name='$name' value='$value' $attributes>";
  }

  if ($type == "button") {
    $rtn .= "<input type='button' name='$name' value='$value' $attributes>";
  }


  if ($description) {
    $rtn .= "<div class='form-element-description'>$description</div>";
  }

  if ($type != "markup") {
    $rtn .= "</div>"; // close the inner wrapper
  }



  if ($suffix) {
    $rtn .= $suffix;
  }

  if ($type != "markup") {
    $rtn .= "</div>"; // close the over-all wrapper
  }



  return $rtn;
}