Example #1
0
 /**
  * HTML Form element generation
  * Optional name parameter allows you to generate a form element for a field but give it a
  * name different from the field name
  * 
  * @param string $ps_field field name
  * @param string $ps_format field format
  * @param array $pa_options additional options
  * TODO: document them.
  */
 public function htmlFormElement($ps_field, $ps_format = null, $pa_options = null)
 {
     $o_db = $this->getDb();
     // init options
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     foreach (array('display_form_field_tips', 'classname', 'maxOptionLength', 'textAreaTagName', 'display_use_count', 'display_omit_items__with_zero_count', 'display_use_count_filters', 'display_use_count_filters', 'selection', 'name', 'value', 'dont_show_null_value', 'size', 'multiple', 'show_text_field_for_vars', 'nullOption', 'empty_message', 'displayMessageForFieldValues', 'DISPLAY_FIELD', 'WHERE', 'select_item_text', 'hide_select_if_only_one_option', 'field_errors', 'display_form_field_tips', 'form_name', 'no_tooltips', 'tooltip_namespace', 'extraLabelText', 'width', 'height', 'label', 'list_code', 'hide_select_if_no_options', 'id', 'lookup_url', 'progress_indicator', 'error_icon', 'maxPixelWidth', 'displayMediaVersion', 'FIELD_TYPE', 'DISPLAY_TYPE', 'choiceList', 'readonly', 'description', 'hidden') as $vs_key) {
         if (!isset($pa_options[$vs_key])) {
             $pa_options[$vs_key] = null;
         }
     }
     $va_attr = $this->getFieldInfo($ps_field);
     foreach (array('DISPLAY_WIDTH', 'DISPLAY_USE_COUNT', 'DISPLAY_SHOW_COUNT', 'DISPLAY_OMIT_ITEMS_WITH_ZERO_COUNT', 'DISPLAY_TYPE', 'IS_NULL', 'DEFAULT_ON_NULL', 'DEFAULT', 'LIST_MULTIPLE_DELIMITER', 'FIELD_TYPE', 'LIST_CODE', 'DISPLAY_FIELD', 'WHERE', 'DISPLAY_WHERE', 'DISPLAY_ORDERBY', 'LIST', 'BOUNDS_CHOICE_LIST', 'BOUNDS_LENGTH', 'DISPLAY_DESCRIPTION', 'LABEL', 'DESCRIPTION', 'SUB_LABEL', 'SUB_DESCRIPTION', 'MAX_PIXEL_WIDTH') as $vs_key) {
         if (!isset($va_attr[$vs_key])) {
             $va_attr[$vs_key] = null;
         }
     }
     if (isset($pa_options['FIELD_TYPE'])) {
         $va_attr['FIELD_TYPE'] = $pa_options['FIELD_TYPE'];
     }
     if (isset($pa_options['DISPLAY_TYPE'])) {
         $va_attr['DISPLAY_TYPE'] = $pa_options['DISPLAY_TYPE'];
     }
     $vn_display_width = isset($pa_options['width']) && $pa_options['width'] > 0 ? $pa_options['width'] : $va_attr["DISPLAY_WIDTH"];
     $vn_display_height = isset($pa_options['height']) && $pa_options['height'] > 0 ? $pa_options['height'] : $va_attr["DISPLAY_HEIGHT"];
     $va_parsed_width = caParseFormElementDimension($vn_display_width);
     $va_parsed_height = caParseFormElementDimension($vn_display_height);
     $va_dim_styles = array();
     if ($va_parsed_width['type'] == 'pixels') {
         $va_dim_styles[] = "width: " . $va_parsed_width['dimension'] . "px;";
     }
     if ($va_parsed_height['type'] == 'pixels') {
         $va_dim_styles[] = "height: " . $va_parsed_height['dimension'] . "px;";
     }
     //if ($vn_max_pixel_width) {
     //	$va_dim_styles[] = "max-width: {$vn_max_pixel_width}px;";
     //}
     $vs_dim_style = trim(join(" ", $va_dim_styles));
     $vs_field_label = isset($pa_options['label']) && strlen($pa_options['label']) > 0 ? $pa_options['label'] : $va_attr["LABEL"];
     $vs_errors = '';
     // TODO: PULL THIS FROM A CONFIG FILE
     $pa_options["display_form_field_tips"] = true;
     if (isset($pa_options['classname'])) {
         $vs_css_class_attr = ' class="' . $pa_options['classname'] . '" ';
     } else {
         $vs_css_class_attr = '';
     }
     if (!isset($pa_options['id'])) {
         $pa_options['id'] = $pa_options['name'];
     }
     if (!isset($pa_options['id'])) {
         $pa_options['id'] = $ps_field;
     }
     if (!isset($pa_options['maxPixelWidth']) || (int) $pa_options['maxPixelWidth'] <= 0) {
         $vn_max_pixel_width = $va_attr['MAX_PIXEL_WIDTH'];
     } else {
         $vn_max_pixel_width = (int) $pa_options['maxPixelWidth'];
     }
     if ($vn_max_pixel_width <= 0) {
         $vn_max_pixel_width = null;
     }
     if (!isset($pa_options["maxOptionLength"]) && isset($vn_display_width)) {
         $pa_options["maxOptionLength"] = isset($vn_display_width) ? $vn_display_width : null;
     }
     $vs_text_area_tag_name = 'textarea';
     if (isset($pa_options["textAreaTagName"]) && $pa_options['textAreaTagName']) {
         $vs_text_area_tag_name = isset($pa_options['textAreaTagName']) ? $pa_options['textAreaTagName'] : null;
     }
     if (!isset($va_attr["DISPLAY_USE_COUNT"]) || !($vs_display_use_count = $va_attr["DISPLAY_USE_COUNT"])) {
         $vs_display_use_count = isset($pa_options["display_use_count"]) ? $pa_options["display_use_count"] : null;
     }
     if (!isset($va_attr["DISPLAY_SHOW_COUNT"]) || !($vb_display_show_count = (bool) $va_attr["DISPLAY_SHOW_COUNT"])) {
         $vb_display_show_count = isset($pa_options["display_show_count"]) ? (bool) $pa_options["display_show_count"] : null;
     }
     if (!isset($va_attr["DISPLAY_OMIT_ITEMS_WITH_ZERO_COUNT"]) || !($vb_display_omit_items__with_zero_count = (bool) $va_attr["DISPLAY_OMIT_ITEMS_WITH_ZERO_COUNT"])) {
         $vb_display_omit_items__with_zero_count = isset($pa_options["display_omit_items__with_zero_count"]) ? (bool) $pa_options["display_omit_items__with_zero_count"] : null;
     }
     if (!isset($va_attr["DISPLAY_OMIT_ITEMS_WITH_ZERO_COUNT"]) || !($va_display_use_count_filters = $va_attr["DISPLAY_USE_COUNT_FILTERS"])) {
         $va_display_use_count_filters = isset($pa_options["display_use_count_filters"]) ? $pa_options["display_use_count_filters"] : null;
     }
     if (!isset($va_display_use_count_filters) || !is_array($va_display_use_count_filters)) {
         $va_display_use_count_filters = null;
     }
     if (isset($pa_options["selection"]) && is_array($pa_options["selection"])) {
         $va_selection = isset($pa_options["selection"]) ? $pa_options["selection"] : null;
     } else {
         $va_selection = array();
     }
     if (isset($pa_options["choiceList"]) && is_array($pa_options["choiceList"])) {
         $va_attr["BOUNDS_CHOICE_LIST"] = $pa_options["choiceList"];
     }
     $vs_element = $vs_subelement = "";
     if ($va_attr) {
         # --- Skip omitted fields completely
         if ($va_attr["DISPLAY_TYPE"] == DT_OMIT) {
             return "";
         }
         if (!isset($pa_options["name"]) || !$pa_options["name"]) {
             $pa_options["name"] = htmlspecialchars($ps_field, ENT_QUOTES, 'UTF-8');
         }
         $va_js = array();
         $va_handlers = array("onclick", "onchange", "onkeypress", "onkeydown", "onkeyup");
         foreach ($va_handlers as $vs_handler) {
             if (isset($pa_options[$vs_handler]) && $pa_options[$vs_handler]) {
                 $va_js[] = "{$vs_handler}='" . $pa_options[$vs_handler] . "'";
             }
         }
         $vs_js = join(" ", $va_js);
         if (!isset($pa_options["value"])) {
             // allow field value to be overriden with value from options array
             $vm_field_value = $this->get($ps_field, $pa_options);
         } else {
             $vm_field_value = $pa_options["value"];
         }
         $vm_raw_field_value = $vm_field_value;
         $vb_is_null = isset($va_attr["IS_NULL"]) ? $va_attr["IS_NULL"] : false;
         if (isset($pa_options['dont_show_null_value']) && $pa_options['dont_show_null_value']) {
             $vb_is_null = false;
         }
         if (!is_array($vm_field_value) && strlen($vm_field_value) == 0 && (!isset($vb_is_null) || !$vb_is_null || (isset($va_attr["DEFAULT_ON_NULL"]) ? $va_attr["DEFAULT_ON_NULL"] : 0))) {
             $vm_field_value = isset($va_attr["DEFAULT"]) ? $va_attr["DEFAULT"] : "";
         }
         # --- Return hidden fields
         if ($va_attr["DISPLAY_TYPE"] == DT_HIDDEN || caGetOption('hidden', $pa_options, false)) {
             return '<input type="hidden" name="' . $pa_options["name"] . '" value="' . $this->escapeHTML($vm_field_value) . '"/>';
         }
         if (isset($pa_options["size"]) && $pa_options["size"] > 0) {
             $ps_size = " size='" . $pa_options["size"] . "'";
         } else {
             if (($va_attr["DISPLAY_TYPE"] == DT_LIST_MULTIPLE || $va_attr["DISPLAY_TYPE"] == DT_LIST) && $vn_display_height > 1) {
                 $ps_size = " size='" . $vn_display_height . "'";
             } else {
                 $ps_size = '';
             }
         }
         $vs_multiple_name_extension = '';
         if ($vs_is_multiple = isset($pa_options["multiple"]) && $pa_options["multiple"] || $va_attr["DISPLAY_TYPE"] == DT_LIST_MULTIPLE ? "multiple='1'" : "") {
             $vs_multiple_name_extension = '[]';
             if (!($vs_list_multiple_delimiter = $va_attr['LIST_MULTIPLE_DELIMITER'])) {
                 $vs_list_multiple_delimiter = ';';
             }
             $va_selection = array_merge($va_selection, explode($vs_list_multiple_delimiter, $vm_field_value));
         }
         # --- Return form element
         switch ($va_attr["FIELD_TYPE"]) {
             # ----------------------------
             case FT_NUMBER:
             case FT_TEXT:
             case FT_VARS:
                 if ($va_attr["FIELD_TYPE"] == FT_VARS) {
                     if (!$pa_options['show_text_field_for_vars']) {
                         break;
                     }
                     if (!is_string($vm_field_value) && !is_numeric($vm_field_value)) {
                         $vm_value = '';
                     }
                 }
                 if ($va_attr['DISPLAY_TYPE'] == DT_COUNTRY_LIST) {
                     $vs_element = caHTMLSelect($ps_field, caGetCountryList(), array('id' => $ps_field), array('value' => $vm_field_value));
                     if ($va_attr['STATEPROV_FIELD']) {
                         $vs_element .= "<script type='text/javascript'>\n";
                         $vs_element .= "var caStatesByCountryList = " . json_encode(caGetStateList()) . ";\n";
                         $vs_element .= "\n\t\t\t\t\t\t\t\tjQuery('#{$ps_field}').click({countryID: '{$ps_field}', stateProvID: '" . $va_attr['STATEPROV_FIELD'] . "', value: '" . addslashes($this->get($va_attr['STATEPROV_FIELD'])) . "', statesByCountryList: caStatesByCountryList}, caUI.utils.updateStateProvinceForCountry);\n\t\t\t\t\t\t\t\tjQuery(document).ready(function() {\n\t\t\t\t\t\t\t\t\tcaUI.utils.updateStateProvinceForCountry({data: {countryID: '{$ps_field}', stateProvID: '" . $va_attr['STATEPROV_FIELD'] . "', value: '" . addslashes($this->get($va_attr['STATEPROV_FIELD'])) . "', statesByCountryList: caStatesByCountryList}});\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t";
                         $vs_element .= "</script>\n";
                     }
                     break;
                 }
                 if ($va_attr['DISPLAY_TYPE'] == DT_STATEPROV_LIST) {
                     $vs_element = caHTMLSelect($ps_field . '_select', array(), array('id' => $ps_field . '_select'), array('value' => $vm_field_value));
                     $vs_element .= caHTMLTextInput($ps_field . '_name', array('id' => $ps_field . '_text', 'value' => $vm_field_value));
                     break;
                 }
                 if ($vn_display_width > 0 && in_array($va_attr["DISPLAY_TYPE"], array(DT_SELECT, DT_LIST, DT_LIST_MULTIPLE))) {
                     #
                     # Generate auto generated <select> (from foreign key, from ca_lists or from field-defined choice list)
                     #
                     # TODO: CLEAN UP THIS CODE, RUNNING VARIOUS STAGES THROUGH HELPER FUNCTIONS; ALSO FORMALIZE AND DOCUMENT VARIOUS OPTIONS
                     // -----
                     // from ca_lists
                     // -----
                     if (!($vs_list_code = $pa_options['list_code'])) {
                         if (isset($va_attr['LIST_CODE']) && $va_attr['LIST_CODE']) {
                             $vs_list_code = $va_attr['LIST_CODE'];
                         }
                     }
                     if ($vs_list_code) {
                         $va_many_to_one_relations = $this->_DATAMODEL->getManyToOneRelations($this->tableName());
                         if ($va_many_to_one_relations[$ps_field]) {
                             $vs_key = 'item_id';
                         } else {
                             $vs_key = 'item_value';
                         }
                         $vs_null_option = null;
                         if (!$pa_options["nullOption"] && $vb_is_null) {
                             $vs_null_option = _t("- NONE -");
                         } else {
                             if ($pa_options["nullOption"]) {
                                 $vs_null_option = $pa_options["nullOption"];
                             }
                         }
                         $t_list = new ca_lists();
                         $va_list_attrs = array('id' => $pa_options['id']);
                         //if ($vn_max_pixel_width) { $va_list_attrs['style'] = $vs_width_style; }
                         if (method_exists($this, 'getTypeFieldName') && $ps_field == $this->getTypeFieldName()) {
                             $va_limit_list = caGetTypeListForUser($this->tableName(), array('access' => __CA_BUNDLE_ACCESS_EDIT__));
                         }
                         // NOTE: "raw" field value (value passed into method, before the model default value is applied) is used so as to allow the list default to be used if needed
                         $vs_element = $t_list->getListAsHTMLFormElement($vs_list_code, $pa_options["name"] . $vs_multiple_name_extension, $va_list_attrs, array('value' => $vm_raw_field_value, 'key' => $vs_key, 'nullOption' => $vs_null_option, 'readonly' => $pa_options['readonly'], 'restrictTypeListForTable' => $this->tableName(), 'limitToItemsWithID' => $va_limit_list ? $va_limit_list : null));
                         if (isset($pa_options['hide_select_if_no_options']) && $pa_options['hide_select_if_no_options'] && !$vs_element) {
                             $vs_element = "";
                             $ps_format = '^ERRORS^ELEMENT';
                         }
                     } else {
                         // -----
                         // from related table
                         // -----
                         $va_many_to_one_relations = $this->_DATAMODEL->getManyToOneRelations($this->tableName());
                         if (isset($va_many_to_one_relations[$ps_field]) && $va_many_to_one_relations[$ps_field]) {
                             #
                             # Use foreign  key to populate <select>
                             #
                             $o_one_table = $this->_DATAMODEL->getTableInstance($va_many_to_one_relations[$ps_field]["one_table"]);
                             $vs_one_table_primary_key = $o_one_table->primaryKey();
                             if ($o_one_table->isHierarchical()) {
                                 #
                                 # Hierarchical <select>
                                 #
                                 $va_hier = $o_one_table->getHierarchyAsList(0, $vs_display_use_count, $va_display_use_count_filters, $vb_display_omit_items__with_zero_count);
                                 if (!is_array($va_hier)) {
                                     return '';
                                 }
                                 $va_display_fields = $va_attr["DISPLAY_FIELD"];
                                 if (!in_array($vs_one_table_primary_key, $va_display_fields)) {
                                     $va_display_fields[] = $o_one_table->tableName() . "." . $vs_one_table_primary_key;
                                 }
                                 if (!is_array($va_display_fields) || sizeof($va_display_fields) < 1) {
                                     $va_display_fields = array("*");
                                 }
                                 $vs_hier_parent_id_fld = $o_one_table->getProperty("HIER_PARENT_ID_FLD");
                                 $va_options = array();
                                 if ($pa_options["nullOption"]) {
                                     $va_options[""] = array($pa_options["nullOption"]);
                                 }
                                 $va_suboptions = array();
                                 $va_suboption_values = array();
                                 $vn_selected = 0;
                                 $vm_cur_top_level_val = null;
                                 $vm_selected_top_level_val = null;
                                 foreach ($va_hier as $va_option) {
                                     if (!$va_option["NODE"][$vs_hier_parent_id_fld]) {
                                         continue;
                                     }
                                     $vn_val = $va_option["NODE"][$o_one_table->primaryKey()];
                                     $vs_selected = $vn_val == $vm_field_value ? 'selected="1"' : "";
                                     $vn_indent = $va_option["LEVEL"] - 1;
                                     $va_display_data = array();
                                     foreach ($va_display_fields as $vs_fld) {
                                         $va_bits = explode(".", $vs_fld);
                                         if ($va_bits[1] != $vs_one_table_primary_key) {
                                             $va_display_data[] = $va_option["NODE"][$va_bits[1]];
                                         }
                                     }
                                     $vs_option_label = join(" ", $va_display_data);
                                     $va_options[$vn_val] = array($vs_option_label, $vn_indent, $va_option["HITS"], $va_option['NODE']);
                                 }
                                 if (sizeof($va_options) == 0) {
                                     $vs_element = isset($pa_options['empty_message']) ? $pa_options['empty_message'] : 'No options available';
                                 } else {
                                     $vs_element = "<select name='" . $pa_options["name"] . $vs_multiple_name_extension . "' " . $vs_js . " " . $vs_is_multiple . " " . $ps_size . " id='" . $pa_options["id"] . $vs_multiple_name_extension . "' {$vs_css_class_attr}  style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' disabled="disabled" ' : '') . ">\n";
                                     if (!$pa_options["nullOption"] && $vb_is_null) {
                                         $vs_element .= "<option value=''>" . _t('- NONE -') . "</option>\n";
                                     } else {
                                         if ($pa_options["nullOption"]) {
                                             $vs_element .= "<option value=''>" . $pa_options["nullOption"] . "</option>\n";
                                         }
                                     }
                                     foreach ($va_options as $vn_val => $va_option_info) {
                                         $vs_selected = $vn_val == $vm_field_value || in_array($vn_val, $va_selection) ? "selected='selected'" : "";
                                         $vs_element .= "<option value='" . $vn_val . "' {$vs_selected}>";
                                         $vn_indent = $va_option_info[1] * 2;
                                         $vs_indent = "";
                                         if ($vn_indent > 0) {
                                             $vs_indent = str_repeat("&nbsp;", ($vn_indent - 1) * 2) . " ";
                                             $vn_indent++;
                                         }
                                         $vs_option_text = $va_option_info[0];
                                         $vs_use_count = "";
                                         if ($vs_display_use_count && $vb_display_show_count && $vn_val != "") {
                                             $vs_use_count = " (" . intval($va_option_info[2]) . ")";
                                         }
                                         $vs_display_message = '';
                                         if (is_array($pa_options['displayMessageForFieldValues'])) {
                                             foreach ($pa_options['displayMessageForFieldValues'] as $vs_df => $va_df_vals) {
                                                 if (isset($va_option_info[3][$vs_df]) && is_array($va_df_vals)) {
                                                     $vs_tmp = $va_option_info[3][$vs_df];
                                                     if (isset($va_df_vals[$vs_tmp])) {
                                                         $vs_display_message = ' ' . $va_df_vals[$vs_tmp];
                                                     }
                                                 }
                                             }
                                         }
                                         if ($pa_options["maxOptionLength"] && strlen($vs_option_text) + strlen($vs_use_count) + $vn_indent > $pa_options["maxOptionLength"]) {
                                             if (($vn_strlen = $pa_options["maxOptionLength"] - strlen($vs_indent) - strlen($vs_use_count) - 3) < $pa_options["maxOptionLength"]) {
                                                 $vn_strlen = $pa_options["maxOptionLength"];
                                             }
                                             $vs_option_text = unicode_substr($vs_option_text, 0, $vn_strlen) . "...";
                                         }
                                         $vs_element .= $vs_indent . $vs_option_text . $vs_use_count . $vs_display_message . "</option>\n";
                                     }
                                     $vs_element .= "</select>\n";
                                 }
                             } else {
                                 #
                                 # "Flat" <select>
                                 #
                                 if (!is_array($va_display_fields = $pa_options["DISPLAY_FIELD"])) {
                                     $va_display_fields = $va_attr["DISPLAY_FIELD"];
                                 }
                                 if (!is_array($va_display_fields)) {
                                     return "Configuration error: DISPLAY_FIELD directive for field '{$ps_field}' must be an array of field names in the format tablename.fieldname";
                                 }
                                 if (!in_array($vs_one_table_primary_key, $va_display_fields)) {
                                     $va_display_fields[] = $o_one_table->tableName() . "." . $vs_one_table_primary_key;
                                 }
                                 if (!is_array($va_display_fields) || sizeof($va_display_fields) < 1) {
                                     $va_display_fields = array("*");
                                 }
                                 $vs_sql = "\n\t\t\t\t\t\t\t\t\t\t\tSELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM " . $va_many_to_one_relations[$ps_field]["one_table"] . "\n\t\t\t\t\t\t\t\t\t\t\t";
                                 if (isset($pa_options["WHERE"]) && (is_array($pa_options["WHERE"]) && ($vs_where = join(" AND ", $pa_options["WHERE"]))) || is_array($va_attr["DISPLAY_WHERE"]) && ($vs_where = join(" AND ", $va_attr["DISPLAY_WHERE"]))) {
                                     $vs_sql .= " WHERE {$vs_where} ";
                                 }
                                 if (isset($va_attr["DISPLAY_ORDERBY"]) && $va_attr["DISPLAY_ORDERBY"] && ($vs_orderby = join(",", $va_attr["DISPLAY_ORDERBY"]))) {
                                     $vs_sql .= " ORDER BY {$vs_orderby} ";
                                 }
                                 $qr_res = $o_db->query($vs_sql);
                                 if ($o_db->numErrors()) {
                                     $vs_element = "Error creating menu: " . join(';', $o_db->getErrors());
                                     break;
                                 }
                                 $va_opts = array();
                                 if (isset($pa_options["nullOption"]) && $pa_options["nullOption"]) {
                                     $va_opts[$pa_options["nullOption"]] = array($pa_options["nullOption"], null);
                                 } else {
                                     if ($vb_is_null) {
                                         $va_opts[_t("- NONE -")] = array(_t("- NONE -"), null);
                                     }
                                 }
                                 if ($pa_options["select_item_text"]) {
                                     $va_opts[$pa_options["select_item_text"]] = array($pa_options["select_item_text"], null);
                                 }
                                 $va_fields = array();
                                 foreach ($va_display_fields as $vs_field) {
                                     $va_tmp = explode(".", $vs_field);
                                     $va_fields[] = $va_tmp[1];
                                 }
                                 while ($qr_res->nextRow()) {
                                     $vs_display = "";
                                     foreach ($va_fields as $vs_field) {
                                         if ($vs_field != $vs_one_table_primary_key) {
                                             $vs_display .= $qr_res->get($vs_field) . " ";
                                         }
                                     }
                                     $va_opts[] = array($vs_display, $qr_res->get($vs_one_table_primary_key), $qr_res->getRow());
                                 }
                                 if (sizeof($va_opts) == 0) {
                                     $vs_element = isset($pa_options['empty_message']) ? $pa_options['empty_message'] : 'No options available';
                                 } else {
                                     if (isset($pa_options['hide_select_if_only_one_option']) && $pa_options['hide_select_if_only_one_option'] && sizeof($va_opts) == 1) {
                                         $vs_element = "<input type='hidden' name='" . $pa_options["name"] . "' " . $vs_js . " " . $ps_size . " id='" . $pa_options["id"] . "' value='" . ($vm_field_value ? $vm_field_value : $va_opts[0][1]) . "' {$vs_css_class_attr}/>";
                                         $ps_format = '^ERRORS^ELEMENT';
                                     } else {
                                         $vs_element = "<select name='" . $pa_options["name"] . $vs_multiple_name_extension . "' " . $vs_js . " " . $vs_is_multiple . " " . $ps_size . " id='" . $pa_options["id"] . $vs_multiple_name_extension . "' {$vs_css_class_attr} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' disabled="disabled" ' : '') . ">\n";
                                         foreach ($va_opts as $va_opt) {
                                             $vs_option_text = $va_opt[0];
                                             $vs_value = $va_opt[1];
                                             $vs_selected = $vs_value == $vm_field_value || in_array($vs_value, $va_selection) ? "selected='selected'" : "";
                                             $vs_use_count = "";
                                             if ($vs_display_use_count && $vb_display_show_count && $vs_value != "") {
                                                 //$vs_use_count = "(".intval($va_option_info[2]).")";
                                             }
                                             if ($pa_options["maxOptionLength"] && strlen($vs_option_text) + strlen($vs_use_count) > $pa_options["maxOptionLength"]) {
                                                 $vs_option_text = unicode_substr($vs_option_text, 0, $pa_options["maxOptionLength"] - 3 - strlen($vs_use_count)) . "...";
                                             }
                                             $vs_display_message = '';
                                             if (is_array($pa_options['displayMessageForFieldValues'])) {
                                                 foreach ($pa_options['displayMessageForFieldValues'] as $vs_df => $va_df_vals) {
                                                     if (isset($va_opt[2][$vs_df]) && is_array($va_df_vals)) {
                                                         $vs_tmp = $va_opt[2][$vs_df];
                                                         if (isset($va_df_vals[$vs_tmp])) {
                                                             $vs_display_message = ' ' . $va_df_vals[$vs_tmp];
                                                         }
                                                     }
                                                 }
                                             }
                                             $vs_element .= "<option value='{$vs_value}' {$vs_selected}>";
                                             $vs_element .= $vs_option_text . $vs_use_count . $vs_display_message;
                                             $vs_element .= "</option>\n";
                                         }
                                         $vs_element .= "</select>\n";
                                     }
                                 }
                             }
                         } else {
                             #
                             # choice list
                             #
                             $vs_element = '';
                             // if 'LIST' is set try to stock over choice list with the contents of the list
                             if (isset($va_attr['LIST']) && $va_attr['LIST']) {
                                 // NOTE: "raw" field value (value passed into method, before the model default value is applied) is used so as to allow the list default to be used if needed
                                 $vs_element = ca_lists::getListAsHTMLFormElement($va_attr['LIST'], $pa_options["name"] . $vs_multiple_name_extension, array('class' => $pa_options['classname'], 'id' => $pa_options['id']), array('key' => 'item_value', 'value' => $vm_raw_field_value, 'nullOption' => $pa_options['nullOption'], 'readonly' => $pa_options['readonly']));
                             }
                             if (!$vs_element && (isset($va_attr["BOUNDS_CHOICE_LIST"]) && is_array($va_attr["BOUNDS_CHOICE_LIST"]))) {
                                 if (sizeof($va_attr["BOUNDS_CHOICE_LIST"]) == 0) {
                                     $vs_element = isset($pa_options['empty_message']) ? $pa_options['empty_message'] : 'No options available';
                                 } else {
                                     $vs_element = "<select name='" . $pa_options["name"] . $vs_multiple_name_extension . "' " . $vs_js . " " . $vs_is_multiple . " " . $ps_size . " id='" . $pa_options['id'] . $vs_multiple_name_extension . "' {$vs_css_class_attr} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' disabled="disabled" ' : '') . ">\n";
                                     if ($pa_options["select_item_text"]) {
                                         $vs_element .= "<option value=''>" . $this->escapeHTML($pa_options["select_item_text"]) . "</option>\n";
                                     }
                                     if (!$pa_options["nullOption"] && $vb_is_null) {
                                         $vs_element .= "<option value=''>" . _t('- NONE -') . "</option>\n";
                                     } else {
                                         if ($pa_options["nullOption"]) {
                                             $vs_element .= "<option value=''>" . $pa_options["nullOption"] . "</option>\n";
                                         }
                                     }
                                     foreach ($va_attr["BOUNDS_CHOICE_LIST"] as $vs_option => $vs_value) {
                                         $vs_selected = strval($vs_value) === strval($vm_field_value) || in_array($vs_value, $va_selection) ? "selected='selected'" : "";
                                         if ($pa_options["maxOptionLength"] && strlen($vs_option) > $pa_options["maxOptionLength"]) {
                                             $vs_option = unicode_substr($vs_option, 0, $pa_options["maxOptionLength"] - 3) . "...";
                                         }
                                         $vs_element .= "<option value='{$vs_value}' {$vs_selected}>" . $this->escapeHTML($vs_option) . "</option>\n";
                                     }
                                     $vs_element .= "</select>\n";
                                 }
                             }
                         }
                     }
                 } else {
                     if ($va_attr["DISPLAY_TYPE"] === DT_COLORPICKER) {
                         // COLORPICKER
                         $vs_element = '<input name="' . $pa_options["name"] . '" type="hidden" size="' . ($pa_options['size'] ? $pa_options['size'] : $vn_display_width) . '" value="' . $this->escapeHTML($vm_field_value) . '" ' . $vs_js . ' id=\'' . $pa_options["id"] . "' style='{$vs_dim_style}'/>\n";
                         $vs_element .= '<div id="' . $pa_options["id"] . '_colorchip" class="colorpicker_chip" style="background-color: #' . $vm_field_value . '"><!-- empty --></div>';
                         $vs_element .= "<script type='text/javascript'>jQuery(document).ready(function() { jQuery('#" . $pa_options["name"] . "_colorchip').ColorPicker({\n\t\t\t\t\t\t\t\tonShow: function (colpkr) {\n\t\t\t\t\t\t\t\t\tjQuery(colpkr).fadeIn(500);\n\t\t\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tonHide: function (colpkr) {\n\t\t\t\t\t\t\t\t\tjQuery(colpkr).fadeOut(500);\n\t\t\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tonChange: function (hsb, hex, rgb) {\n\t\t\t\t\t\t\t\t\tjQuery('#" . $pa_options["name"] . "').val(hex);\n\t\t\t\t\t\t\t\t\tjQuery('#" . $pa_options["name"] . "_colorchip').css('backgroundColor', '#' + hex);\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\tcolor: jQuery('#" . $pa_options["name"] . "').val()\n\t\t\t\t\t\t\t})}); </script>\n";
                         if (method_exists('AssetLoadManager', 'register')) {
                             AssetLoadManager::register('jquery', 'colorpicker');
                         }
                     } else {
                         # normal controls: all non-DT_SELECT display types are returned as DT_FIELD's. We could generate
                         # radio-button controls for foreign key and choice lists, but we don't bother because it's never
                         # really necessary.
                         if ($vn_display_height > 1) {
                             $vs_element = '<' . $vs_text_area_tag_name . ' name="' . $pa_options["name"] . '" rows="' . $vn_display_height . '" cols="' . $vn_display_width . '"' . ($pa_options['readonly'] ? ' readonly="readonly" disabled="disabled"' : '') . ' wrap="soft" ' . $vs_js . ' id=\'' . $pa_options["id"] . "' style='{$vs_dim_style}' " . $vs_css_class_attr . ">" . $this->escapeHTML($vm_field_value) . '</' . $vs_text_area_tag_name . '>' . "\n";
                         } else {
                             $vs_element = '<input name="' . $pa_options["name"] . '" type="text" size="' . ($pa_options['size'] ? $pa_options['size'] : $vn_display_width) . '"' . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . ' value="' . $this->escapeHTML($vm_field_value) . '" ' . $vs_js . ' id=\'' . $pa_options["id"] . "' {$vs_css_class_attr} style='{$vs_dim_style}'/>\n";
                         }
                         if (isset($va_attr['UNIQUE_WITHIN']) && is_array($va_attr['UNIQUE_WITHIN'])) {
                             $va_within_fields = array();
                             foreach ($va_attr['UNIQUE_WITHIN'] as $vs_within_field) {
                                 $va_within_fields[$vs_within_field] = $this->get($vs_within_field);
                             }
                             $vs_element .= "<span id='" . $pa_options["id"] . '_uniqueness_status' . "'></span>";
                             $vs_element .= "<script type='text/javascript'>\n\tcaUI.initUniquenessChecker({\n\t\terrorIcon: '" . $pa_options['error_icon'] . "',\n\t\tprocessIndicator: '" . $pa_options['progress_indicator'] . "',\n\t\tstatusID: '" . $pa_options["id"] . "_uniqueness_status',\n\t\tlookupUrl: '" . $pa_options['lookup_url'] . "',\n\t\tformElementID: '" . $pa_options["id"] . "',\n\t\trow_id: " . intval($this->getPrimaryKey()) . ",\n\t\ttable_num: " . $this->tableNum() . ",\n\t\tfield: '" . $ps_field . "',\n\t\twithinFields: " . json_encode($va_within_fields) . ",\n\t\t\n\t\talreadyInUseMessage: '" . addslashes(_t('Value must be unique. Please try another.')) . "'\n\t});\n</script>";
                         } else {
                             if (isset($va_attr['LOOKUP']) && $va_attr['LOOKUP']) {
                                 if (class_exists("AppController") && ($app = AppController::getInstance()) && ($req = $app->getRequest())) {
                                     AssetLoadManager::register('jquery', 'autocomplete');
                                     $vs_element .= "<script type='text/javascript'>\n\tjQuery('#" . $pa_options["id"] . "').autocomplete({ source: '" . caNavUrl($req, 'lookup', 'Intrinsic', 'Get', array('bundle' => $this->tableName() . ".{$ps_field}", "max" => 500)) . "', minLength: 3, delay: 800});\n</script>";
                                 }
                             }
                         }
                         if (isset($pa_options['usewysiwygeditor']) && $pa_options['usewysiwygeditor']) {
                             AssetLoadManager::register("ckeditor");
                             $vs_width = $vn_display_width;
                             $vs_height = $vn_display_height;
                             if (!preg_match("!^[\\d\\.]+px\$!i", $vs_width)) {
                                 $vs_width = (int) $vs_width * 6 . "px";
                             }
                             if (!preg_match("!^[\\d\\.]+px\$!i", $vs_height)) {
                                 $vs_height = (int) $vs_height * 16 . "px";
                             }
                             if (!is_array($va_toolbar_config = $this->getAppConfig()->getAssoc('wysiwyg_editor_toolbar'))) {
                                 $va_toolbar_config = array();
                             }
                             $vs_element .= "<script type='text/javascript'>jQuery(document).ready(function() {\n\t\t\t\t\t\t\t\tvar ckEditor = CKEDITOR.replace( '" . $pa_options['id'] . "',\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\ttoolbar : " . json_encode(array_values($va_toolbar_config)) . ",\n\t\t\t\t\t\t\t\t\twidth: '{$vs_width}',\n\t\t\t\t\t\t\t\t\theight: '{$vs_height}',\n\t\t\t\t\t\t\t\t\ttoolbarLocation: 'top',\n\t\t\t\t\t\t\t\t\tenterMode: CKEDITOR.ENTER_BR\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\tckEditor.on('instanceReady', function(){ \n\t\t\t\t\t\t\t\t\t ckEditor.document.on( 'keydown', function(e) {if (caUI && caUI.utils) { caUI.utils.showUnsavedChangesWarning(true); } });\n\t\t\t\t\t\t\t\t});\n \t});\t\t\t\t\t\t\t\t\t\n</script>";
                         }
                     }
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_TIMESTAMP:
                 if ($this->get($ps_field)) {
                     # is timestamp set?
                     $vs_element = $this->escapeHTML($vm_field_value);
                     # return printed date
                 } else {
                     $vs_element = "[Not set]";
                     # return text instead of 1969 date
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_DATETIME:
             case FT_HISTORIC_DATETIME:
             case FT_DATE:
             case FT_HISTORIC_DATE:
                 if (!$vm_field_value) {
                     $vm_field_value = $pa_options['value'];
                 }
                 switch ($va_attr["DISPLAY_TYPE"]) {
                     case DT_TEXT:
                         $vs_element = $vm_field_value ? $vm_field_value : "[Not set]";
                         break;
                     default:
                         $vn_max_length = $va_attr["BOUNDS_LENGTH"][1];
                         $vs_max_length = '';
                         if ($vn_max_length > 0) {
                             $vs_max_length = 'maxlength="' . $vn_max_length . '"';
                         }
                         if ($vn_display_height > 1) {
                             $vs_element = '<' . $vs_text_area_tag_name . ' name="' . $pa_options["name"] . '" rows="' . $vn_display_height . '" cols="' . $vn_display_width . '" wrap="soft" ' . $vs_js . ' ' . $vs_css_class_attr . " style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . ">" . $this->escapeHTML($vm_field_value) . '</' . $vs_text_area_tag_name . '>';
                         } else {
                             $vs_element = '<input type="text" name="' . $pa_options["name"] . '" value="' . $this->escapeHTML($vm_field_value) . "\" size='{$vn_display_width}' {$vs_max_length} {$vs_js} {$vs_css_class_attr} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                         }
                         break;
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_TIME:
                 if (!$this->get($ps_field)) {
                     $vm_field_value = "";
                 }
                 switch ($va_attr["DISPLAY_TYPE"]) {
                     case DT_TEXT:
                         $vs_element = $vm_field_value ? $vm_field_value : "[Not set]";
                         break;
                     default:
                         $vn_max_length = $va_attr["BOUNDS_LENGTH"][1];
                         $vs_max_length = '';
                         if ($vn_max_length > 0) {
                             $vs_max_length = 'maxlength="' . $vn_max_length . '"';
                         }
                         if ($vn_display_height > 1) {
                             $vs_element = '<' . $vs_text_area_tag_name . ' name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" rows="' . $vn_display_height . '" cols="' . $vn_display_width . '" wrap="soft" ' . $vs_js . ' ' . $vs_css_class_attr . " style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . ">" . $this->escapeHTML($vm_field_value) . '</' . $vs_text_area_tag_name . '>';
                         } else {
                             $vs_element = '<input type="text" name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" value="' . $this->escapeHTML($vm_field_value) . "\" size='{$vn_display_width}' {$vs_max_length} {$vs_js} {$vs_css_class_attr}' style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                         }
                         break;
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_DATERANGE:
             case FT_HISTORIC_DATERANGE:
                 switch ($va_attr["DISPLAY_TYPE"]) {
                     case DT_TEXT:
                         $vs_element = $vm_field_value ? $vm_field_value : "[Not set]";
                         break;
                     default:
                         $vn_max_length = $va_attr["BOUNDS_LENGTH"][1];
                         $vs_max_length = '';
                         if ($vn_max_length > 0) {
                             $vs_max_length = 'maxlength="' . $vn_max_length . '"';
                         }
                         if ($vn_display_height > 1) {
                             $vs_element = '<' . $vs_text_area_tag_name . ' name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" rows="' . $vn_display_height . '" cols="' . $vn_display_width . '" wrap="soft" ' . $vs_js . ' ' . $vs_css_class_attr . " style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . ">" . $this->escapeHTML($vm_field_value) . '</' . $vs_text_area_tag_name . '>';
                         } else {
                             $vs_element = '<input type="text" name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" value="' . $this->escapeHTML($vm_field_value) . "\" size='{$vn_display_width}' {$vn_max_length} {$vs_js} {$vs_css_class_attr} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                         }
                         break;
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_TIMERANGE:
                 switch ($va_attr["DISPLAY_TYPE"]) {
                     case DT_TEXT:
                         $vs_element = $vm_field_value ? $vm_field_value : "[Not set]";
                         break;
                     default:
                         $vn_max_length = $va_attr["BOUNDS_LENGTH"][1];
                         $vs_max_length = '';
                         if ($vn_max_length > 0) {
                             $vs_max_length = 'maxlength="' . $vn_max_length . '"';
                         }
                         if ($vn_display_height > 1) {
                             $vs_element = '<' . $vs_text_area_tag_name . ' name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" rows="' . $vn_display_height . '" cols="' . $vn_display_width . '" wrap="soft" ' . $vs_js . ' ' . $vs_css_class_attr . " style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . ">" . $this->escapeHTML($vm_field_value) . '</' . $vs_text_area_tag_name . '>';
                         } else {
                             $vs_element = '<input type="text" name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" value="' . $this->escapeHTML($vm_field_value) . "\" size='{$vn_display_width}' {$vs_max_length} {$vs_js} {$vs_css_class_attr}  style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                         }
                         break;
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_TIMECODE:
                 $o_tp = new TimecodeParser();
                 $o_tp->setParsedValueInSeconds($vm_field_value);
                 $vs_timecode = $o_tp->getText("COLON_DELIMITED", array("BLANK_ON_ZERO" => true));
                 $vn_max_length = $va_attr["BOUNDS_LENGTH"][1];
                 $vs_max_length = '';
                 if ($vn_max_length > 0) {
                     $vs_max_length = 'maxlength="' . $vn_max_length . '"';
                 }
                 if ($vn_display_height > 1) {
                     $vs_element = '<' . $vs_text_area_tag_name . ' name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" rows="' . $vn_display_height . '" cols="' . $vn_display_width . '" wrap="soft" ' . $vs_js . ' ' . $vs_css_class_attr . " style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . ">" . $this->escapeHTML($vs_timecode) . '</' . $vs_text_area_tag_name . '>';
                 } else {
                     $vs_element = '<input type="text" NAME="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" value="' . $this->escapeHTML($vs_timecode) . "\" size='{$vn_display_width}' {$vs_max_length} {$vs_js} {$vs_css_class_attr} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_MEDIA:
             case FT_FILE:
                 $vs_element = '<input type="file" name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" ' . $vs_js . '/>';
                 // show current media icon
                 if ($vs_version = array_key_exists('displayMediaVersion', $pa_options) ? $pa_options['displayMediaVersion'] : 'icon') {
                     $va_valid_versions = $this->getMediaVersions($ps_field);
                     if (!in_array($vs_version, $va_valid_versions)) {
                         $vs_version = $va_valid_versions[0];
                     }
                     if ($vs_tag = $this->getMediaTag($ps_field, $vs_version)) {
                         $vs_element .= $vs_tag;
                     }
                 }
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_PASSWORD:
                 $vn_max_length = $va_attr["BOUNDS_LENGTH"][1];
                 $vs_max_length = '';
                 if ($vn_max_length > 0) {
                     $vs_max_length = 'maxlength="' . $vn_max_length . '"';
                 }
                 $vs_element = '<input type="password" name="' . $pa_options["name"] . '" id="' . $pa_options["id"] . '" value="' . $this->escapeHTML($vm_field_value) . '" size="' . $vn_display_width . '" ' . $vs_max_length . ' ' . $vs_js . ' autocomplete="off" ' . $vs_css_class_attr . " style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                 break;
                 # ----------------------------
             # ----------------------------
             case FT_BIT:
                 switch ($va_attr["DISPLAY_TYPE"]) {
                     case DT_FIELD:
                         $vs_element = '<input type="text" name="' . $pa_options["name"] . "\" value='{$vm_field_value}' maxlength='1' size='2' {$vs_js} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' readonly="readonly" ' : '') . "/>";
                         break;
                     case DT_SELECT:
                         $vs_element = "<select name='" . $pa_options["name"] . "' " . $vs_js . " id='" . $pa_options["id"] . "' {$vs_css_class_attr} style='{$vs_dim_style}'" . ($pa_options['readonly'] ? ' disabled="disabled" ' : '') . ">\n";
                         foreach (array("Yes" => 1, "No" => 0) as $vs_option => $vs_value) {
                             $vs_selected = $vs_value == $vm_field_value ? "selected='selected'" : "";
                             $vs_element .= "<option value='{$vs_value}' {$vs_selected}" . ($pa_options['readonly'] ? ' disabled="disabled" ' : '') . ">" . _t($vs_option) . "</option>\n";
                         }
                         $vs_element .= "</select>\n";
                         break;
                     case DT_CHECKBOXES:
                         $vs_element = '<input type="checkbox" name="' . $pa_options["name"] . '" value="1" ' . ($vm_field_value ? 'checked="1"' : '') . ' ' . $vs_js . ($pa_options['readonly'] ? ' disabled="disabled" ' : '') . ' id="' . $pa_options["id"] . '"/>';
                         break;
                     case DT_RADIO_BUTTONS:
                         $vs_element = 'Radio buttons not supported for bit-type fields';
                         break;
                 }
                 break;
                 # ----------------------------
         }
         # Apply format
         $vs_formatting = "";
         if (isset($pa_options['field_errors']) && is_array($pa_options['field_errors']) && sizeof($pa_options['field_errors'])) {
             $va_field_errors = array();
             foreach ($pa_options['field_errors'] as $o_e) {
                 $va_field_errors[] = $o_e->getErrorDescription();
             }
             $vs_errors = join('; ', $va_field_errors);
         } else {
             $vs_errors = '';
         }
         if (is_null($ps_format)) {
             if (isset($pa_options['field_errors']) && is_array($pa_options['field_errors']) && sizeof($pa_options['field_errors'])) {
                 $ps_format = $this->_CONFIG->get('form_element_error_display_format');
             } else {
                 $ps_format = $this->_CONFIG->get('form_element_display_format');
             }
         }
         if ($ps_format != '') {
             $ps_formatted_element = $ps_format;
             $ps_formatted_element = str_replace("^ELEMENT", $vs_element, $ps_formatted_element);
             if ($vs_subelement) {
                 $ps_formatted_element = str_replace("^SUB_ELEMENT", $vs_subelement, $ps_formatted_element);
             }
             $vb_fl_display_form_field_tips = false;
             if ($pa_options["display_form_field_tips"] || !isset($pa_options["display_form_field_tips"]) && $va_attr["DISPLAY_DESCRIPTION"] || !isset($pa_options["display_form_field_tips"]) && !isset($va_attr["DISPLAY_DESCRIPTION"]) && $vb_fl_display_form_field_tips) {
                 if (preg_match("/\\^DESCRIPTION/", $ps_formatted_element)) {
                     $ps_formatted_element = str_replace("^LABEL", $vs_field_label, $ps_formatted_element);
                     $ps_formatted_element = str_replace("^DESCRIPTION", isset($pa_options["description"]) && $pa_options["description"] ? $pa_options["description"] : $va_attr["DESCRIPTION"], $ps_formatted_element);
                 } else {
                     // no explicit placement of description text, so...
                     $vs_field_id = '_' . $this->tableName() . '_' . $this->getPrimaryKey() . '_' . $pa_options["name"] . '_' . $pa_options['form_name'];
                     $ps_formatted_element = str_replace("^LABEL", '<span id="' . $vs_field_id . '">' . $vs_field_label . '</span>', $ps_formatted_element);
                     if (!isset($pa_options['no_tooltips']) || !$pa_options['no_tooltips']) {
                         TooltipManager::add('#' . $vs_field_id, "<h3>{$vs_field_label}</h3>" . (isset($pa_options["description"]) && $pa_options["description"] ? $pa_options["description"] : $va_attr["DESCRIPTION"]), $pa_options['tooltip_namespace']);
                     }
                 }
                 if (!isset($va_attr["SUB_LABEL"])) {
                     $va_attr["SUB_LABEL"] = '';
                 }
                 if (!isset($va_attr["SUB_DESCRIPTION"])) {
                     $va_attr["SUB_DESCRIPTION"] = '';
                 }
                 if (preg_match("/\\^SUB_DESCRIPTION/", $ps_formatted_element)) {
                     $ps_formatted_element = str_replace("^SUB_LABEL", $va_attr["SUB_LABEL"], $ps_formatted_element);
                     $ps_formatted_element = str_replace("^SUB_DESCRIPTION", $va_attr["SUB_DESCRIPTION"], $ps_formatted_element);
                 } else {
                     // no explicit placement of description text, so...
                     // ... make label text itself rollover for description text because no icon was specified
                     $ps_formatted_element = str_replace("^SUB_LABEL", $va_attr["SUB_LABEL"], $ps_formatted_element);
                 }
             } else {
                 $ps_formatted_element = str_replace("^LABEL", $vs_field_label, $ps_formatted_element);
                 $ps_formatted_element = str_replace("^DESCRIPTION", "", $ps_formatted_element);
                 if ($vs_subelement) {
                     $ps_formatted_element = str_replace("^SUB_LABEL", $va_attr["SUB_LABEL"], $ps_formatted_element);
                     $ps_formatted_element = str_replace("^SUB_DESCRIPTION", "", $ps_formatted_element);
                 }
             }
             $ps_formatted_element = str_replace("^ERRORS", $vs_errors, $ps_formatted_element);
             $ps_formatted_element = str_replace("^EXTRA", isset($pa_options['extraLabelText']) ? $pa_options['extraLabelText'] : '', $ps_formatted_element);
             $vs_element = $ps_formatted_element;
         } else {
             $vs_element .= "<br/>" . $vs_subelement;
         }
         return $vs_element;
     } else {
         $this->postError(716, _t("'%1' does not exist in this object", $ps_field), "BaseModel->formElement()");
         return "";
     }
     return "";
 }
Example #2
0
 /**
  * Generates HTML form element widget for preference based upon settings in preference definition file.
  * By calling this method for a series of preference names, one can quickly create an HTML-based configuration form.
  *
  * @access public
  * @param string $ps_pref Name of user preference
  * @param string $ps_format Format string containing simple tags to be replaced with preference information. Tags supported are:
  *		^LABEL = name of preference
  *		^ELEMENT = HTML code to generate form widget
  * 		If you omit $ps_format, the element code alone (content of ^ELEMENT) is returned.
  * @param array $pa_options Array of options. Support options are:
  *		field_errors = array of error messages to display on preference element
  *		useTable = if true and displayType for element in DT_CHECKBOXES checkboxes will be formatted in a table with numTableColumns columns
  *		numTableColumns = Number of columns to use when formatting checkboxes as a table. Default, if omitted, is 3
  *		genericUIList = forces FT_*_EDITOR_UI to return single UI list for table rather than by type
  *		classname = class to assign to form element
  * @return string HTML code to generate form widget
  */
 public function preferenceHtmlFormElement($ps_pref, $ps_format = null, $pa_options = null)
 {
     if ($this->isValidPreference($ps_pref)) {
         if (!is_array($pa_options)) {
             $pa_options = array();
         }
         $o_db = $this->getDb();
         $va_pref_info = $this->getPreferenceInfo($ps_pref);
         if (is_null($vs_current_value = $this->getPreference($ps_pref))) {
             $vs_current_value = $this->getPreferenceDefault($ps_pref);
         }
         $vs_output = "";
         $vs_class = "";
         $vs_classname = "";
         if (isset($pa_options['classname']) && $pa_options['classname']) {
             $vs_classname = $pa_options['classname'];
             $vs_class = " class='" . $pa_options['classname'] . "'";
         }
         foreach (array('displayType', 'displayWidth', 'displayHeight', 'length', 'formatType', 'choiceList', 'label', 'description') as $vs_k) {
             if (!isset($va_pref_info[$vs_k])) {
                 $va_pref_info[$vs_k] = null;
             }
         }
         switch ($va_pref_info["displayType"]) {
             # ---------------------------------
             case 'DT_FIELD':
                 if (($vn_display_width = $va_pref_info["displayWidth"]) < 1) {
                     $vn_display_width = 20;
                 }
                 if (($vn_display_height = $va_pref_info["displayHeight"]) < 1) {
                     $vn_display_height = 1;
                 }
                 if (isset($va_pref_info["length"]["maximum"])) {
                     $vn_max_input_length = $va_pref_info["length"]["maximum"];
                 } else {
                     $vn_max_input_length = $vn_display_width;
                 }
                 if ($vn_display_height > 1) {
                     $vs_output = "<textarea name='pref_{$ps_pref}' rows='" . $vn_display_height . "' cols='" . $vn_display_width . "'>" . htmlspecialchars($vs_current_value, ENT_QUOTES, 'UTF-8') . "</textarea>\n";
                 } else {
                     $vs_output = "<input type='text' name='pref_{$ps_pref}' size='{$vn_display_width}' maxlength='{$vn_max_input_length}'" . $vs_class . " value='" . htmlspecialchars($vs_current_value, ENT_QUOTES, 'UTF-8') . "'/>\n";
                 }
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_SELECT':
                 switch ($va_pref_info['formatType']) {
                     case 'FT_UI_LOCALE':
                         $va_locales = array();
                         if ($r_dir = opendir(__CA_APP_DIR__ . '/locale/')) {
                             while (($vs_locale_dir = readdir($r_dir)) !== false) {
                                 if ($vs_locale_dir[0] == '.') {
                                     continue;
                                 }
                                 if (sizeof($va_tmp = explode('_', $vs_locale_dir)) == 2) {
                                     $va_locales[$vs_locale_dir] = $va_tmp;
                                 }
                             }
                         }
                         $va_opts = array();
                         $t_locale = new ca_locales();
                         foreach ($va_locales as $vs_code => $va_parts) {
                             try {
                                 $vs_lang_name = Zend_Locale::getTranslation(strtolower($va_parts[0]), 'language', strtolower($va_parts[0]));
                                 $vs_country_name = Zend_Locale::getTranslation($va_parts[1], 'Country', $vs_code);
                             } catch (Exception $e) {
                                 $vs_lang_name = strtolower($va_parts[0]);
                                 $vs_country_name = $vs_code;
                             }
                             $va_opts[($vs_lang_name ? $vs_lang_name : $vs_code) . ($vs_country_name ? ' (' . $vs_country_name . ')' : '')] = $vs_code;
                         }
                         break;
                     case 'FT_LOCALE':
                         $qr_locales = $o_db->query("\n\t\t\t\t\t\t\t\tSELECT *\n\t\t\t\t\t\t\t\tFROM ca_locales\n\t\t\t\t\t\t\t\tORDER BY \n\t\t\t\t\t\t\t\t\tname\n\t\t\t\t\t\t\t");
                         $va_opts = array();
                         while ($qr_locales->nextRow()) {
                             $va_opts[$qr_locales->get('name')] = $qr_locales->get('language') . '_' . $qr_locales->get('country');
                         }
                         break;
                     case 'FT_THEME':
                         if ($r_dir = opendir($this->_CONFIG->get('themes_directory'))) {
                             $va_opts = array();
                             while (($vs_theme_dir = readdir($r_dir)) !== false) {
                                 if ($vs_theme_dir[0] == '.') {
                                     continue;
                                 }
                                 $o_theme_info = Configuration::load($this->_CONFIG->get('themes_directory') . '/' . $vs_theme_dir . '/themeInfo.conf');
                                 $va_opts[$o_theme_info->get('name')] = $vs_theme_dir;
                             }
                         }
                         break;
                     case 'FT_OBJECT_EDITOR_UI':
                     case 'FT_OBJECT_LOT_EDITOR_UI':
                     case 'FT_ENTITY_EDITOR_UI':
                     case 'FT_PLACE_EDITOR_UI':
                     case 'FT_OCCURRENCE_EDITOR_UI':
                     case 'FT_COLLECTION_EDITOR_UI':
                     case 'FT_STORAGE_LOCATION_EDITOR_UI':
                     case 'FT_OBJECT_REPRESENTATION_EDITOR_UI':
                     case 'FT_REPRESENTATION_ANNOTATION_EDITOR_UI':
                     case 'FT_SET_EDITOR_UI':
                     case 'FT_SET_ITEM_EDITOR_UI':
                     case 'FT_LIST_EDITOR_UI':
                     case 'FT_LIST_ITEM_EDITOR_UI':
                     case 'FT_LOAN_EDITOR_UI':
                     case 'FT_MOVEMENT_EDITOR_UI':
                     case 'FT_TOUR_EDITOR_UI':
                     case 'FT_TOUR_STOP_EDITOR_UI':
                     case 'FT_SEARCH_FORM_EDITOR_UI':
                     case 'FT_BUNDLE_DISPLAY_EDITOR_UI':
                     case 'FT_RELATIONSHIP_TYPE_EDITOR_UI':
                     case 'FT_USER_INTERFACE_EDITOR_UI':
                     case 'FT_USER_INTERFACE_SCREEN_EDITOR_UI':
                     case 'FT_IMPORT_EXPORT_MAPPING_EDITOR_UI':
                     case 'FT_IMPORT_EXPORT_MAPPING_GROUP_EDITOR_UI':
                         $vn_table_num = $this->_editorPrefFormatTypeToTableNum($va_pref_info['formatType']);
                         $t_instance = $this->getAppDatamodel()->getInstanceByTableNum($vn_table_num, true);
                         $va_values = $this->getPreference($ps_pref);
                         if (!is_array($va_values)) {
                             $va_values = array();
                         }
                         if (method_exists($t_instance, 'getTypeFieldName') && $t_instance->getTypeFieldName() && (!isset($pa_options['genericUIList']) || !$pa_options['genericUIList'])) {
                             $vs_output = '';
                             $va_ui_list_by_type = $this->_getUIListByType($vn_table_num);
                             $va_types = array();
                             if ((bool) $t_instance->getFieldInfo($t_instance->getTypeFieldName(), 'IS_NULL')) {
                                 $va_types['_NONE_'] = array('LEVEL' => 0, 'name_singular' => _t('NONE'), 'name_plural' => _t('NONE'));
                             }
                             $va_types += $t_instance->getTypeList(array('returnHierarchyLevels' => true));
                             if (!is_array($va_types) || !sizeof($va_types)) {
                                 $va_types = array(1 => array());
                             }
                             // force ones with no types to get processed for __all__
                             foreach ($va_types as $vn_type_id => $va_type) {
                                 $va_opts = array();
                                 // print out type-specific
                                 if (is_array($va_ui_list_by_type[$vn_type_id])) {
                                     foreach (caExtractValuesByUserLocale($va_ui_list_by_type[$vn_type_id]) as $vn_ui_id => $vs_label) {
                                         $va_opts[$vn_ui_id] = $vs_label;
                                     }
                                 }
                                 // print out generic
                                 if (is_array($va_ui_list_by_type['__all__'])) {
                                     foreach (caExtractValuesByUserLocale($va_ui_list_by_type['__all__']) as $vn_ui_id => $vs_label) {
                                         $va_opts[$vn_ui_id] = $vs_label;
                                     }
                                 }
                                 if (!is_array($va_opts) || sizeof($va_opts) == 0) {
                                     continue;
                                 }
                                 $vs_output .= "<tr><td>" . str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", (int) $va_type['LEVEL']) . $va_type['name_singular'] . "</td><td><select name='pref_{$ps_pref}_{$vn_type_id}'>\n";
                                 foreach ($va_opts as $vs_val => $vs_opt) {
                                     $vs_selected = $vs_val == $va_values[$vn_type_id] ? "SELECTED" : "";
                                     $vs_output .= "<option value='" . htmlspecialchars($vs_val, ENT_QUOTES, 'UTF-8') . "' {$vs_selected}>{$vs_opt}</option>\n";
                                 }
                                 $vs_output .= "</select></td></tr>\n";
                             }
                         } else {
                             $va_opts = $this->_getUIList($vn_table_num);
                             if (!is_array($va_opts) || sizeof($va_opts) == 0) {
                                 $vs_output = '';
                                 break 2;
                             }
                             $vs_output = "<tr><td> </td><td><select name='pref_{$ps_pref}'>\n";
                             foreach ($va_opts as $vs_val => $vs_opt) {
                                 $vs_selected = $vs_val == $vs_current_value ? "SELECTED" : "";
                                 $vs_output .= "<option value='" . htmlspecialchars($vs_val, ENT_QUOTES, 'UTF-8') . "' {$vs_selected}>" . $vs_opt . "</option>\n";
                             }
                             $vs_output .= "</select></td></tr>\n";
                         }
                         break 2;
                     default:
                         $va_opts = $va_pref_info["choiceList"];
                         break;
                 }
                 if (!is_array($va_opts) || sizeof($va_opts) == 0) {
                     $vs_output = '';
                     break;
                 }
                 $vs_output = "<select name='pref_{$ps_pref}'" . $vs_class . ">\n";
                 foreach ($va_opts as $vs_opt => $vs_val) {
                     $vs_selected = $vs_val == $vs_current_value ? "selected='1'" : "";
                     $vs_output .= "<option value='" . htmlspecialchars($vs_val, ENT_QUOTES, 'UTF-8') . "' {$vs_selected}>" . $vs_opt . "</option>\n";
                 }
                 $vs_output .= "</select>\n";
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_CHECKBOXES':
                 if ($va_pref_info["formatType"] == 'FT_BIT') {
                     $vs_selected = $vs_current_value ? "CHECKED" : "";
                     $vs_output .= "<input type='checkbox' name='pref_{$ps_pref}' value='1'" . $vs_class . " {$vs_selected}>\n";
                 } else {
                     if ($vb_use_table = isset($pa_options['useTable']) && (bool) $pa_options['useTable']) {
                         $vs_output .= "<table width='100%'>";
                     }
                     $vn_num_table_columns = isset($pa_options['numTableColumns']) && (int) $pa_options['numTableColumns'] > 0 ? (int) $pa_options['numTableColumns'] : 3;
                     $vn_c = 0;
                     foreach ($va_pref_info["choiceList"] as $vs_opt => $vs_val) {
                         if (is_array($vs_current_value)) {
                             $vs_selected = in_array($vs_val, $vs_current_value) ? "CHECKED" : "";
                         } else {
                             $vs_selected = '';
                         }
                         if ($vb_use_table && $vn_c == 0) {
                             $vs_output .= "<tr>";
                         }
                         if ($vb_use_table) {
                             $vs_output .= "<td width='" . floor(100 / $vn_num_table_columns) . "%'>";
                         }
                         $vs_output .= "<input type='checkbox' name='pref_" . $ps_pref . "[]' value='" . htmlspecialchars($vs_val, ENT_QUOTES, 'UTF-8') . "'" . $vs_class . " {$vs_selected}> " . $vs_opt . " \n";
                         if ($vb_use_table) {
                             $vs_output .= "</td>";
                         }
                         $vn_c++;
                         if ($vb_use_table && !($vn_c % $vn_num_table_columns)) {
                             $vs_output .= "</tr>\n";
                             $vn_c = 0;
                         }
                     }
                     if ($vb_use_table) {
                         $vs_output .= "</table>";
                     }
                 }
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_STATEPROV_LIST':
                 $vs_output .= caHTMLSelect("pref_{$ps_pref}_select", array(), array('id' => "pref_{$ps_pref}_select", 'class' => $vs_classname), array('value' => $vs_current_value));
                 $vs_output .= caHTMLTextInput("pref_{$ps_pref}_name", array('id' => "pref_{$ps_pref}_text", 'value' => $vs_current_value, 'class' => $vs_classname));
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_COUNTRY_LIST':
                 $vs_output .= caHTMLSelect("pref_{$ps_pref}", caGetCountryList(), array('id' => "pref_{$ps_pref}", 'class' => $vs_classname), array('value' => $vs_current_value));
                 if ($va_pref_info['stateProvPref']) {
                     $vs_output .= "<script type='text/javascript'>\n";
                     $vs_output .= "var caStatesByCountryList = " . json_encode(caGetStateList()) . ";\n";
                     $vs_output .= "\n\t\t\t\t\t\t\tjQuery('#pref_{$ps_pref}').click({countryID: 'pref_{$ps_pref}', stateProvID: 'pref_" . $va_pref_info['stateProvPref'] . "', value: '" . addslashes($this->getPreference($va_pref_info['stateProvPref'])) . "', statesByCountryList: caStatesByCountryList}, caUI.utils.updateStateProvinceForCountry);\n\t\t\t\t\t\t\tjQuery(document).ready(function() {\n\t\t\t\t\t\t\t\tcaUI.utils.updateStateProvinceForCountry({data: {countryID: 'pref_{$ps_pref}', stateProvID: 'pref_" . $va_pref_info['stateProvPref'] . "', value: '" . addslashes($this->getPreference($va_pref_info['stateProvPref'])) . "', statesByCountryList: caStatesByCountryList}});\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t";
                     $vs_output .= "</script>\n";
                 }
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_CURRENCIES':
                 $vs_output .= caHTMLSelect("pref_{$ps_pref}", caAvailableCurrenciesForConversion(), array('id' => "pref_{$ps_pref}", 'class' => $vs_classname), array('value' => $vs_current_value));
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_RADIO_BUTTONS':
                 foreach ($va_pref_info["choiceList"] as $vs_opt => $vs_val) {
                     $vs_selected = $vs_val == $vs_current_value ? "CHECKED" : "";
                     $vs_output .= "<input type='radio' name='pref_{$ps_pref}'" . $vs_class . " value='" . htmlspecialchars($vs_val, ENT_QUOTES, 'UTF-8') . "' {$vs_selected}> " . $vs_opt . " \n";
                 }
                 break;
                 # ---------------------------------
             # ---------------------------------
             case 'DT_PASSWORD':
                 if (($vn_display_width = $va_pref_info["displayWidth"]) < 1) {
                     $vn_display_width = 20;
                 }
                 if (isset($va_pref_info["length"]["maximum"])) {
                     $vn_max_input_length = $va_pref_info["length"]["maximum"];
                 } else {
                     $vn_max_input_length = $vn_display_width;
                 }
                 $vs_output = "<input type='password' name='pref_{$ps_pref}' size='{$vn_display_width}' maxlength='{$vn_max_input_length}'" . $vs_class . " value='" . htmlspecialchars($vs_current_value, ENT_QUOTES, 'UTF-8') . "'/>\n";
                 break;
                 # ---------------------------------
             # ---------------------------------
             default:
                 return "Configuration error: Invalid display type for {$ps_pref}";
                 # ---------------------------------
         }
         if (is_null($ps_format)) {
             if (isset($pa_options['field_errors']) && is_array($pa_options['field_errors']) && sizeof($pa_options['field_errors'])) {
                 $ps_format = $this->_CONFIG->get('form_element_error_display_format');
                 $va_field_errors = array();
                 foreach ($pa_options['field_errors'] as $o_e) {
                     $va_field_errors[] = $o_e->getErrorDescription();
                 }
                 $vs_errors = join('; ', $va_field_errors);
             } else {
                 $ps_format = $this->_CONFIG->get('form_element_display_format');
                 $vs_errors = '';
             }
         }
         if ($ps_format && $vs_output) {
             $vs_format = $ps_format;
             $vs_format = str_replace("^ELEMENT", $vs_output, $vs_format);
         } else {
             $vs_format = $vs_output;
         }
         $vs_format = str_replace("^EXTRA", '', $vs_format);
         if (preg_match("/\\^DESCRIPTION/", $vs_format)) {
             $vs_format = str_replace("^LABEL", _t($va_pref_info["label"]), $vs_format);
             $vs_format = str_replace("^DESCRIPTION", _t($va_pref_info["description"]), $vs_format);
         } else {
             // no explicit placement of description text, so...
             $vs_field_id = "pref_{$ps_pref}_container";
             $vs_format = str_replace("^LABEL", '<span id="' . $vs_field_id . '">' . _t($va_pref_info["label"]) . '</span>', $vs_format);
             TooltipManager::add('#' . $vs_field_id, "<h3>" . $va_pref_info["label"] . "</h3>" . $va_pref_info["description"]);
         }
         return $vs_format;
     } else {
         return "";
     }
 }