/**
  *
  *
  */
 protected function GetHierarchyLevelData($pa_ids)
 {
     $vo_dm = Datamodel::load();
     $o_config = Configuration::load();
     $t_object = new ca_objects();
     $va_level_data = array();
     foreach ($pa_ids as $pn_id) {
         $va_params = $this->getItemIDComponents($pn_id);
         $vs_table = $va_params['table'];
         $vn_id = $va_params['id'];
         $vn_start = $va_params['start'];
         $vn_item_count = 0;
         $t_item = $vo_dm->getInstanceByTableName($vs_table, true);
         $vs_label_table_name = $t_item->getLabelTableName();
         $vs_label_display_field_name = $t_item->getLabelDisplayField();
         $vs_pk = $t_item->primaryKey();
         if ($vn_start < 0) {
             $vn_start = 0;
         }
         $va_items_for_locale = array();
         if (!$vn_id && method_exists($t_item, "getHierarchyList")) {
             $vn_id = $this->request->getParameter('root_item_id', pString);
             $va_params = $this->getItemIDComponents($vn_id);
             $vs_table = $va_params['table'];
             $vn_id = $va_params['id'];
             $vn_start = $va_params['start'];
             $t_item = $vo_dm->getInstanceByTableName($vs_table, true);
             $vs_label_table_name = $t_item->getLabelTableName();
             $vs_label_display_field_name = $t_item->getLabelDisplayField();
             $vs_pk = $t_item->primaryKey();
             $t_item->load($vn_id);
             $va_tmp = array($vs_pk => $vn_id = $t_item->get($vs_table . '.' . $vs_pk), 'item_id' => $vs_table . '-' . $vn_id, 'parent_id' => $t_item->get($vs_table . '.parent_id'), 'idno' => $t_item->get($vs_table . '.idno'), $vs_label_display_field_name => $t_item->get($vs_table . '.preferred_labels.' . $vs_label_display_field_name), 'locale_id' => $t_item->get($vs_table . '.' . 'locale_id'));
             if (!$va_tmp[$vs_label_display_field_name]) {
                 $va_tmp[$vs_label_display_field_name] = $va_tmp['idno'];
             }
             if (!$va_tmp[$vs_label_display_field_name]) {
                 $va_tmp[$vs_label_display_field_name] = '???';
             }
             if (!($vs_item_template = trim($o_config->get("{$vs_table}_hierarchy_browser_display_settings")))) {
                 $vs_item_template = "^{$vs_table}.preferred_labels.{$vs_label_display_field_name}";
             }
             $va_tmp['name'] = caProcessTemplateForIDs($vs_item_template, $vs_table, array($va_tmp[$vs_pk]));
             // Child count is only valid if has_children is not null
             $va_tmp['children'] = $t_item->get('has_children') ? (int) $t_item->get('child_count') : 1;
             // TODO: fix
             $va_items[$va_tmp[$vs_pk]][$va_tmp['locale_id']] = $va_tmp;
             $va_items_for_locale = caExtractValuesByUserLocale($va_items);
         } else {
             if ($t_item->load($vn_id)) {
                 // id is the id of the parent for the level we're going to return
                 $va_additional_wheres = array();
                 $t_label_instance = $t_item->getLabelTableInstance();
                 if ($t_label_instance && $t_label_instance->hasField('is_preferred')) {
                     $va_additional_wheres[] = "(({$vs_label_table_name}.is_preferred = 1) OR ({$vs_label_table_name}.is_preferred IS NULL))";
                 }
                 if (!is_array($va_sorts = $o_config->getList($vs_table . '_hierarchy_browser_sort_values')) || !sizeof($va_sorts)) {
                     $va_sorts = null;
                 }
                 foreach ($va_sorts as $vn_i => $vs_sort_fld) {
                     $va_tmp = explode(".", $vs_sort_fld);
                     if ($va_tmp[1] == 'preferred_labels') {
                         $va_tmp[0] = $vs_label_table_name;
                         if (!($va_tmp[1] = $va_tmp[2])) {
                             $va_tmp[1] = $vs_label_display_field_name;
                         }
                         unset($va_tmp[2]);
                         $va_sorts[$vn_i] = join(".", $va_tmp);
                     }
                 }
                 if (!in_array($vs_sort_dir = strtolower($o_config->get($vs_table . '_hierarchy_browser_sort_direction')), array('asc', 'desc'))) {
                     $vs_sort_dir = 'asc';
                 }
                 $qr_children = $t_item->getHierarchyChildrenAsQuery($t_item->getPrimaryKey(), array('additionalTableToJoin' => $vs_label_table_name, 'additionalTableJoinType' => 'LEFT', 'additionalTableSelectFields' => array($vs_label_display_field_name, 'locale_id'), 'additionalTableWheres' => $va_additional_wheres, 'returnChildCounts' => true, 'sort' => $va_sorts, 'sortDirection' => $vs_sort_dir));
                 $va_items = array();
                 if (!($vs_item_template = trim($o_config->get("{$vs_table}_hierarchy_browser_display_settings")))) {
                     $vs_item_template = "^{$vs_table}.preferred_labels.{$vs_label_display_field_name}";
                 }
                 $va_child_counts = array();
                 if (($vn_max_items_per_page = $this->request->getParameter('max', pInteger)) < 1 || $vn_max_items_per_page > 1000) {
                     $vn_max_items_per_page = null;
                 }
                 $vn_c = 0;
                 $vn_item_count = $qr_children->numRows();
                 $qr_children->seek($vn_start);
                 while ($qr_children->nextRow()) {
                     $va_tmp = array($vs_pk => $vn_id = $qr_children->get($vs_table . '.' . $vs_pk), 'item_id' => $vs_table . '-' . $vn_id, 'parent_id' => $qr_children->get($vs_table . '.parent_id'), 'idno' => $qr_children->get($vs_table . '.idno'), 'locale_id' => $qr_children->get($vs_table . '.' . 'locale_id'));
                     if (!$va_tmp[$vs_label_display_field_name]) {
                         $va_tmp[$vs_label_display_field_name] = $va_tmp['idno'];
                     }
                     if (!$va_tmp[$vs_label_display_field_name]) {
                         $va_tmp[$vs_label_display_field_name] = '???';
                     }
                     $va_tmp['name'] = caProcessTemplateForIDs($vs_item_template, $vs_table, array($va_tmp[$vs_pk]));
                     // Child count is only valid if has_children is not null
                     $va_tmp['children'] = $qr_children->get('has_children') ? (int) $qr_children->get('child_count') : 0;
                     if (is_array($va_sorts)) {
                         $vs_sort_acc = array();
                         foreach ($va_sorts as $vs_sort) {
                             $vs_sort_acc[] = $qr_children->get($vs_sort);
                         }
                         $va_tmp['sort'] = join(";", $vs_sort_acc);
                     }
                     $va_items[$va_tmp['item_id']][$va_tmp['locale_id']] = $va_tmp;
                     $vn_c++;
                     if (!is_null($vn_max_items_per_page) && $vn_c >= $vn_max_items_per_page) {
                         break;
                     }
                 }
                 if ($t_item->tableName() == 'ca_collections') {
                     $va_cross_table_items = $t_item->getRelatedItems('ca_objects');
                     $vn_item_count += sizeof($va_cross_table_items);
                     $va_ids = array();
                     foreach ($va_cross_table_items as $vn_x_item_id => $va_x_item) {
                         $va_items['ca_objects-' . $vn_x_item_id][$va_x_item['locale_id']] = $va_x_item;
                         //$va_x_item_extracted = caExtractValuesByUserLocale(array(0 => $va_x_item['labels']));
                         //$va_items[$va_x_item['object_id']][$va_x_item['locale_id']]['name'] = $va_x_item_extracted[0];
                         $va_items['ca_objects-' . $va_x_item['object_id']][$va_x_item['locale_id']]['item_id'] = 'ca_objects-' . $va_x_item['object_id'];
                         $va_items['ca_objects-' . $va_x_item['object_id']][$va_x_item['locale_id']]['parent_id'] = $vn_id;
                         unset($va_items['ca_objects-' . $vn_x_item_id][$va_x_item['locale_id']]['labels']);
                         $va_items['ca_objects-' . $va_x_item['object_id']][$va_x_item['locale_id']]['children'] = 0;
                         $va_ids[] = $va_x_item['object_id'];
                     }
                     if (!($vs_item_template = trim($o_config->get("ca_objects_hierarchy_browser_display_settings")))) {
                         $vs_item_template = "^ca_objects.preferred_labels.name";
                     }
                     if (sizeof($va_ids)) {
                         $va_child_counts = $t_object->getHierarchyChildCountsForIDs($va_ids);
                         $va_templates = caProcessTemplateForIDs($vs_item_template, 'ca_objects', $va_ids, array('returnAsArray' => true));
                         foreach ($va_child_counts as $vn_id => $vn_c) {
                             $va_items['ca_objects-' . $vn_id][$va_x_item['locale_id']]['children'] = $vn_c;
                         }
                         foreach ($va_ids as $vn_i => $vn_id) {
                             $va_items['ca_objects-' . $vn_id][$va_x_item['locale_id']]['name'] = $va_templates[$vn_i];
                         }
                     }
                 }
                 $va_items_for_locale = caExtractValuesByUserLocale($va_items);
                 $vs_rank_fld = $t_item->getProperty('RANK');
                 $va_sorted_items = array();
                 foreach ($va_items_for_locale as $vn_id => $va_node) {
                     $vs_key = preg_replace('![^A-Za-z0-9]!', '_', $va_node['name']);
                     if (isset($va_node['sort']) && $va_node['sort']) {
                         $va_sorted_items[$va_node['sort']][$vs_key] = $va_node;
                     } else {
                         if ($vs_rank_fld && ($vs_rank = (int) sprintf("%08d", $va_node[$vs_rank_fld]))) {
                             $va_sorted_items[$vs_rank][$vs_key] = $va_node;
                         } else {
                             $va_sorted_items[$vs_key][$vs_key] = $va_node;
                         }
                     }
                 }
                 ksort($va_sorted_items);
                 if ($vs_sort_dir == 'desc') {
                     $va_sorted_items = array_reverse($va_sorted_items);
                 }
                 $va_items_for_locale = array();
                 foreach ($va_sorted_items as $vs_k => $va_v) {
                     ksort($va_v);
                     if ($vs_sort_dir == 'desc') {
                         $va_v = array_reverse($va_v);
                     }
                     $va_items_for_locale = array_merge($va_items_for_locale, $va_v);
                 }
             }
         }
         $va_items_for_locale['_sortOrder'] = array_keys($va_items_for_locale);
         $va_items_for_locale['_primaryKey'] = $t_item->primaryKey();
         // pass the name of the primary key so the hierbrowser knows where to look for item_id's
         $va_items_for_locale['_itemCount'] = $vn_item_count;
         //$qr_children ? $qr_children->numRows() : 0;
         $va_level_data[$pn_id] = $va_items_for_locale;
     }
     return $va_level_data;
 }