/**
  * Get HTML form element bundle for metadata element
  *
  */
 public function getAttributeHTMLFormBundle($po_request, $ps_form_name, $pm_element_code_or_id, $ps_placement_code, $pa_bundle_settings, $pa_options)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!is_array($pa_bundle_settings)) {
         $pa_bundle_settings = array();
     }
     $vb_batch = isset($pa_options['batch']) && $pa_options['batch'] ? true : false;
     if (!($t_element = $this->_getElementInstance($pm_element_code_or_id))) {
         return false;
     }
     if ($t_element->get('parent_id')) {
         $this->postError(1930, _t('Element is not the root of the element set'), 'BaseModelWithAttributes->getAttributeHTMLFormBundle()');
         return false;
     }
     $vs_view_path = isset($pa_options['viewPath']) && $pa_options['viewPath'] ? $pa_options['viewPath'] : $po_request->getViewsDirectoryPath();
     $o_view = new View($po_request, "{$vs_view_path}/bundles/");
     $o_view->setVar('graphicsPath', $pa_options['graphicsPath']);
     // get all elements of this element set
     $va_element_set = $t_element->getElementsInSet();
     // get attributes of this element attached to this row
     $va_attributes = $this->getAttributesByElement($pm_element_code_or_id);
     $t_attr = new ca_attributes();
     $t_attr->purify($this->purify());
     $va_element_codes = array();
     $va_elements_by_container = array();
     $vb_should_output_locale_id = $t_element->getSetting('doesNotTakeLocale') ? false : true;
     $va_element_value_defaults = array();
     $va_elements_without_break_by_container = array();
     $va_elements_break_by_container = array();
     $va_element_info = array();
     foreach ($va_element_set as $va_element) {
         $va_element_info[$va_element['element_id']] = $va_element;
         if ($va_element['datatype'] == 0) {
             // containers are not active form elements
             $va_elements_break_by_container[$va_element['element_id']] = (int) $va_element["settings"]["lineBreakAfterNumberOfElements"] ? (int) $va_element["settings"]["lineBreakAfterNumberOfElements"] : -1;
             continue;
         }
         $va_label = $this->getAttributeLabelAndDescription($va_element['element_id']);
         if (!isset($va_elements_without_break_by_container[$va_element['parent_id']])) {
             $va_elements_without_break_by_container[$va_element['parent_id']] = 1;
         } else {
             $va_elements_without_break_by_container[$va_element['parent_id']] += 1;
         }
         if ($va_elements_without_break_by_container[$va_element['parent_id']] == $va_elements_break_by_container[$va_element['parent_id']] + 1) {
             $va_elements_without_break_by_container[$va_element['parent_id']] = 1;
             $vs_br = "</td></tr></table><table class=\"attributeListItem\" cellpadding=\"0px\" cellspacing=\"0px\"><tr><td class=\"attributeListItem\">";
         } else {
             $vs_br = "";
         }
         if (isset($pa_bundle_settings['usewysiwygeditor']) && strlen($pa_bundle_settings['usewysiwygeditor']) == 0) {
             unset($pa_bundle_settings['usewysiwygeditor']);
             // let null usewysiwygeditor bundle option fall back to metadata element setting
         }
         if (!is_array($va_label)) {
             $va_label = array('name' => '???', 'description' => '');
         }
         $va_elements_by_container[$va_element['parent_id']][] = $vs_br . ca_attributes::attributeHtmlFormElement($va_element, array_merge($pa_bundle_settings, array_merge($pa_options, array('label' => sizeof($va_element_set) > 1 ? $va_label['name'] : '', 'description' => $va_label['description'], 't_subject' => $this, 'request' => $po_request, 'form_name' => $ps_form_name, 'format' => ''))));
         //if the elements datatype returns true from renderDataType, then force render the element
         if (Attribute::renderDataType($va_element)) {
             return array_pop($va_elements_by_container[$va_element['element_id']]);
         }
         $va_element_ids[] = $va_element['element_id'];
         $vs_setting = Attribute::getValueDefault($va_element);
         if (strlen($vs_setting)) {
             $tmp_element = $this->_getElementInstance($va_element['element_id']);
             $va_element_value_defaults[$va_element['element_id']] = $tmp_element->getSetting($vs_setting);
         }
     }
     if ($vb_should_output_locale_id) {
         // output locale_id, if necessary, in its' own special '_locale_id' container
         $va_elements_by_container['_locale_id'] = array('hidden' => false, 'element' => $t_attr->htmlFormElement('locale_id', '^ELEMENT', array('classname' => 'labelLocale', 'id' => "{fieldNamePrefix}locale_id_{n}", 'name' => "{fieldNamePrefix}locale_id_{n}", "value" => "{locale_id}", 'no_tooltips' => true, 'dont_show_null_value' => true, 'hide_select_if_only_one_option' => true, 'WHERE' => array('(dont_use_for_cataloguing = 0)'))));
         if (stripos($va_elements_by_container['_locale_id']['element'], "'hidden'")) {
             $va_elements_by_container['_locale_id']['hidden'] = true;
         }
     }
     $o_view->setVar('t_element', $t_element);
     $o_view->setVar('t_instance', $this);
     $o_view->setVar('request', $po_request);
     $o_view->setVar('id_prefix', $ps_form_name . '_attribute_' . $t_element->get('element_id'));
     $o_view->setVar('elements', $va_elements_by_container);
     $o_view->setVar('error_source_code', 'ca_attribute_' . $t_element->get('element_code'));
     $o_view->setVar('element_ids', $va_element_ids);
     $o_view->setVar('element_info', $va_element_info);
     $o_view->setVar('element_set_label', $this->getAttributeLabel($t_element->get('element_id')));
     $o_view->setVar('element_code', $t_element->get('element_code'));
     $o_view->setVar('placement_code', $ps_placement_code);
     $o_view->setVar('render_mode', $t_element->getSetting('render'));
     // only set for list attributes (as of 26 Sept 2010 at least)
     if ($t_restriction = $this->getTypeRestrictionInstance($t_element->get('element_id'))) {
         // If batch mode force minimums to zero
         $o_view->setVar('max_num_repeats', $vb_batch ? 9999 : $t_restriction->getSetting('maxAttributesPerRow'));
         $o_view->setVar('min_num_repeats', $vb_batch ? 0 : $t_restriction->getSetting('minAttributesPerRow'));
         $o_view->setVar('min_num_to_display', $vb_batch ? 1 : $t_restriction->getSetting('minimumAttributeBundlesToDisplay'));
     }
     // these are lists of associative arrays representing attributes that were rejected in a save() action
     // during the current request. They are used to maintain the state of the form so the user can modify the
     // input that caused the error
     $o_view->setVar('failed_insert_attribute_list', $this->getFailedAttributeInserts($pm_element_code_or_id));
     $o_view->setVar('failed_update_attribute_list', $this->getFailedAttributeUpdates($pm_element_code_or_id));
     // set the list of existing attributes for the current row
     $vs_sort = $pa_bundle_settings['sort'];
     $vs_sort_dir = $pa_bundle_settings['sortDirection'];
     $va_attribute_list = $this->getAttributesByElement($t_element->get('element_id'), array('sort' => $vs_sort, 'sortDirection' => $vs_sort_dir));
     $o_view->setVar('attribute_list', $va_attribute_list);
     // pass list of element default values
     $o_view->setVar('element_value_defaults', $va_element_value_defaults);
     // pass bundle settings to view
     $o_view->setVar('settings', $pa_bundle_settings);
     // Is this being used in the batch editor?
     $o_view->setVar('batch', (bool) (isset($pa_options['batch']) && $pa_options['batch']));
     return $o_view->render('ca_attributes.php');
 }