/**
  * Given a item_id (request parameter 'id') returns a list of direct children for use in the hierarchy browser
  * Returned data is JSON format
  */
 public function getFacetHierarchyLevel()
 {
     $va_access_values = caGetUserAccessValues($this->request);
     $ps_facet_name = $this->request->getParameter('facet', pString);
     $this->opo_browse->setTypeRestrictions(array($this->opn_type_restriction_id));
     if (!is_array($va_facet_info = $this->opo_browse->getInfoForFacet($ps_facet_name))) {
         return null;
     }
     $va_facet = $this->opo_browse->getFacet($ps_facet_name, array('sort' => 'name', 'checkAccess' => $va_access_values));
     $pa_ids = explode(";", $ps_ids = $this->request->getParameter('id', pString));
     if (!sizeof($pa_ids)) {
         $pa_ids = array(null);
     }
     $va_level_data = 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;
     }
     $t_model = $this->opo_datamodel->getInstanceByTableName($this->ops_tablename, true);
     $o_config = Configuration::load();
     if (!is_array($va_sorts = $o_config->getList($this->ops_tablename . '_hierarchy_browser_sort_values')) || !sizeof($va_sorts)) {
         $va_sorts = array();
     }
     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($this->ops_tablename . '_hierarchy_browser_sort_direction')), array('asc', 'desc'))) {
         $vs_sort_dir = 'asc';
     }
     $va_expanded_facet = array();
     $t_item = new ca_list_items();
     foreach ($va_facet as $vn_id => $va_facet_item) {
         $va_expanded_facet[$vn_id] = true;
         $va_ancestors = $t_item->getHierarchyAncestors($vn_id, array('idsOnly' => true));
         if (is_array($va_ancestors)) {
             foreach ($va_ancestors as $vn_ancestor_id) {
                 $va_expanded_facet[$vn_ancestor_id] = true;
             }
         }
     }
     foreach ($pa_ids as $pn_id) {
         $va_json_data = array('_primaryKey' => 'item_id');
         $va_tmp = explode(":", $pn_id);
         $vn_id = $va_tmp[0];
         $vn_start = (int) $va_tmp[1];
         if ($vn_start < 0) {
             $vn_start = 0;
         }
         switch ($va_facet_info['type']) {
             case 'attribute':
                 // is it a list attribute?
                 $t_element = new ca_metadata_elements();
                 if ($t_element->load(array('element_code' => $va_facet_info['element_code']))) {
                     if ($t_element->get('datatype') == 3) {
                         // 3=list
                         $t_list = new ca_lists();
                         if (!$vn_id) {
                             $vn_id = $t_list->getRootListItemID($t_element->get('list_id'));
                         }
                         $t_item = new ca_list_items($vn_id);
                         $va_children = $t_item->getHierarchyChildren(null, array('idsOnly' => true));
                         $va_child_counts = $t_item->getHierarchyChildCountsForIDs($va_children);
                         $qr_res = caMakeSearchResult('ca_list_items', $va_children);
                         $vs_pk = $t_model->primaryKey();
                         if ($qr_res) {
                             while ($qr_res->nextHit()) {
                                 $vn_parent_id = $qr_res->get('ca_list_items.parent_id');
                                 $vn_item_id = $qr_res->get('ca_list_items.item_id');
                                 if (!isset($va_expanded_facet[$vn_item_id])) {
                                     continue;
                                 }
                                 $va_item = array();
                                 $va_item['item_id'] = $vn_item_id;
                                 $va_item['name'] = $qr_res->get('ca_list_items.preferred_labels');
                                 $va_item['children'] = isset($va_child_counts[$vn_item_id]) && $va_child_counts[$vn_item_id] ? $va_child_counts[$vn_item_id] : 0;
                                 $va_json_data[$vn_item_id] = $va_item;
                             }
                         }
                     }
                 }
                 break;
             case 'label':
                 // label facet
                 $va_facet_info['table'] = $this->ops_tablename;
                 // fall through to default case
             // fall through to default case
             default:
                 if (!$vn_id) {
                     $va_hier_ids = $this->opo_browse->getHierarchyIDsForFacet($ps_facet_name, array('checkAccess' => $va_access_values));
                     $t_item = $this->opo_datamodel->getInstanceByTableName($va_facet_info['table']);
                     $t_item->load($vn_id);
                     $vn_id = $vn_root = $t_item->getHierarchyRootID();
                     $va_hierarchy_list = $t_item->getHierarchyList(true);
                     $vn_last_id = null;
                     $vn_c = 0;
                     foreach ($va_hierarchy_list as $vn_i => $va_item) {
                         if (!in_array($vn_i, $va_hier_ids)) {
                             continue;
                         }
                         // only show hierarchies that have items in browse result
                         if ($vn_start <= $vn_c) {
                             $va_item['item_id'] = $va_item[$t_item->primaryKey()];
                             if (!isset($va_facet[$va_item['item_id']]) && $vn_root == $va_item['item_id']) {
                                 continue;
                             }
                             unset($va_item['parent_id']);
                             unset($va_item['label']);
                             $va_json_data[$va_item['item_id']] = $va_item;
                             $vn_last_id = $va_item['item_id'];
                         }
                         $vn_c++;
                         if (!is_null($vn_max_items_per_page) && $vn_c >= $vn_max_items_per_page + $vn_start) {
                             break;
                         }
                     }
                     if (sizeof($va_json_data) == 2) {
                         // if only one hierarchy root (root +  _primaryKey in array) then don't bother showing it
                         $vn_id = $vn_last_id;
                         unset($va_json_data[$vn_last_id]);
                     }
                 }
                 if ($vn_id) {
                     $vn_c = 0;
                     foreach ($va_facet as $vn_i => $va_item) {
                         if ($va_item['parent_id'] == $vn_id) {
                             if ($vn_start <= $vn_c) {
                                 $va_item['item_id'] = $va_item['id'];
                                 $va_item['name'] = $va_item['label'];
                                 $va_item['children'] = $va_item['child_count'];
                                 unset($va_item['label']);
                                 unset($va_item['child_count']);
                                 unset($va_item['id']);
                                 $va_json_data[$va_item['item_id']] = $va_item;
                             }
                             $vn_c++;
                             if (!is_null($vn_max_items_per_page) && $vn_c >= $vn_max_items_per_page + $vn_start) {
                                 break;
                             }
                         }
                     }
                 }
                 break;
         }
         $vs_rank_fld = $t_item->getProperty('RANK');
         $va_sorted_items = array();
         foreach ($va_json_data as $vn_id => $va_node) {
             if (!is_array($va_node)) {
                 continue;
             }
             $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_json_data = array();
         $va_sorted_items = array_slice($va_sorted_items, $vn_start, $vn_max_items_per_page);
         foreach ($va_sorted_items as $vs_k => $va_v) {
             ksort($va_v);
             if ($vs_sort_dir == 'desc') {
                 $va_v = array_reverse($va_v);
             }
             $va_json_data = array_merge($va_json_data, $va_v);
         }
         $va_json_data['_itemCount'] = sizeof($va_json_data);
         $va_json_data['_sortOrder'] = array_keys($va_json_data);
         $va_json_data['_primaryKey'] = $t_model->primaryKey();
         // pass the name of the primary key so the hierbrowser knows where to look for item_id's
         $va_level_data[$pn_id] = $va_json_data;
     }
     if (!trim($this->request->getParameter('init', pString))) {
         $this->opo_result_context->setParameter($ps_facet_name . '_browse_last_id', $pn_id);
         $this->opo_result_context->saveContext();
     }
     $this->view->setVar('facet_list', $va_level_data);
     return $this->render('Browse/facet_hierarchy_level_json.php');
 }
 /**
  * Given a item_id (request parameter 'id') returns a list of ancestors for use in the hierarchy browser
  * Returned data is JSON format
  */
 public function getFacetHierarchyAncestorList()
 {
     $pn_id = $this->request->getParameter('id', pInteger);
     $ps_facet_name = $this->request->getParameter('facet', pString);
     if (!is_array($va_facet_info = $this->opo_browse->getInfoForFacet($ps_facet_name))) {
         return null;
     }
     $va_ancestors = array();
     switch ($va_facet_info['type']) {
         case 'attribute':
             // is it a list attribute?
             $t_element = new ca_metadata_elements();
             if ($t_element->load(array('element_code' => $va_facet_info['element_code']))) {
                 if ($t_element->get('datatype') == 3) {
                     // 3=list
                     if (!$pn_id) {
                         $t_list = new ca_lists();
                         $pn_id = $t_list->getRootListItemID($t_element->get('list_id'));
                     }
                     $t_item = new ca_list_items($pn_id);
                     if ($t_item->getPrimaryKey()) {
                         $va_ancestors = array_reverse($t_item->getHierarchyAncestors(null, array('includeSelf' => true, 'idsOnly' => true)));
                         array_shift($va_ancestors);
                     }
                 }
             }
             break;
         case 'label':
             // label facet
             $va_facet_info['table'] = $this->ops_tablename;
             // fall through to default case
         // fall through to default case
         default:
             $t_item = $this->opo_datamodel->getInstanceByTableName($va_facet_info['table']);
             $t_item->load($pn_id);
             if (method_exists($t_item, "getHierarchyList")) {
                 $va_access_values = caGetUserAccessValues($this->request);
                 $va_facet = $this->opo_browse->getFacet($ps_facet_name, array('sort' => 'name', 'checkAccess' => $va_access_values));
                 $va_hierarchy_list = $t_item->getHierarchyList(true);
                 $vn_hierarchies_in_use = 0;
                 foreach ($va_hierarchy_list as $vn_i => $va_item) {
                     if (isset($va_facet[$va_item[$t_item->primaryKey()]])) {
                         $vn_hierarchies_in_use++;
                         if ($vn_hierarchies_in_use > 1) {
                             break;
                         }
                     }
                 }
             }
             if ($t_item->getPrimaryKey()) {
                 $va_ancestors = array_reverse($t_item->getHierarchyAncestors(null, array('includeSelf' => true, 'idsOnly' => true)));
                 if (!is_array($va_ancestors)) {
                     $va_ancestors = array();
                 }
             }
             if ($vn_hierarchies_in_use <= 1) {
                 array_shift($va_ancestors);
             }
             break;
     }
     $this->view->setVar('ancestors', $va_ancestors);
     return $this->render('Browse/facet_hierarchy_ancestors_json.php');
 }
Example #3
0
/**
 * Fetch the id of the root item in list
 *
 * @param string $ps_list_code List code
 * @param array $pa_options Options include:
 *		transaction = transaction to execute queries within. [Default=null]
 * @return int item_id of the root list item or null if no default item was found
 */
function caGetListRootID($ps_list_code, $pa_options = null)
{
    $t_list = new ca_lists();
    if ($o_trans = caGetOption('transaction', $pa_options, null)) {
        $t_list->setTransaction($o_trans);
    }
    return $t_list->getRootListItemID($ps_list_code);
}
Example #4
0
 /**
  * Given a item_id (request parameter 'id') returns a list of ancestors for use in the hierarchy browser
  * Returned data is JSON format
  */
 public function getFacetHierarchyAncestorList()
 {
     $pn_id = $this->request->getParameter('id', pInteger);
     $va_access_values = caGetUserAccessValues($this->request);
     $ps_facet_name = $this->request->getParameter('facet', pString);
     $this->view->setVar("facet_name", $ps_facet_name);
     $this->view->setVar("key", $this->request->getParameter('key', pString));
     $ps_browse_type = $this->request->getParameter('browseType', pString);
     if (!($va_browse_info = caGetInfoForBrowseType($ps_browse_type))) {
         // invalid browse type – throw error
         die("Invalid browse type");
     }
     $this->view->setVar("browse_type", $ps_browse_type);
     $vs_class = $va_browse_info['table'];
     $o_browse = caGetBrowseInstance($vs_class);
     if (!is_array($va_facet_info = $o_browse->getInfoForFacet($ps_facet_name))) {
         return null;
     }
     if ($ps_cache_key = $this->request->getParameter('key', pString)) {
         $o_browse->reload($ps_cache_key);
     }
     $va_ancestors = array();
     switch ($va_facet_info['type']) {
         case 'attribute':
             // is it a list attribute?
             $t_element = new ca_metadata_elements();
             if ($t_element->load(array('element_code' => $va_facet_info['element_code']))) {
                 if ($t_element->get('datatype') == 3) {
                     // 3=list
                     if (!$pn_id) {
                         $t_list = new ca_lists();
                         $pn_id = $t_list->getRootListItemID($t_element->get('list_id'));
                     }
                     $t_item = new ca_list_items($pn_id);
                     if ($t_item->getPrimaryKey()) {
                         $vs_primary_key = $t_item->primaryKey();
                         $this->view->setVar("primary_key", $vs_primary_key);
                         $vs_display_fld = $t_item->getLabelDisplayField();
                         $this->view->setVar("display_field", $vs_display_fld);
                         $vs_label_table_name = $t_item->getLabelTableName();
                         $va_ancestors = array_reverse($t_item->getHierarchyAncestors(null, array('includeSelf' => true, 'additionalTableToJoin' => $vs_label_table_name, 'additionalTableJoinType' => 'LEFT', 'additionalTableSelectFields' => array($vs_display_fld, 'locale_id'), 'additionalTableWheres' => array('(' . $vs_label_table_name . '.is_preferred = 1 OR ' . $vs_label_table_name . '.is_preferred IS NULL)'))));
                         array_shift($va_ancestors);
                     }
                 }
             }
             break;
         case 'label':
             // label facet
             $va_facet_info['table'] = $this->ops_tablename;
             // fall through to default case
         // fall through to default case
         default:
             $t_item = $this->request->datamodel->getInstanceByTableName($va_facet_info['table']);
             $t_item->load($pn_id);
             if (method_exists($t_item, "getHierarchyList")) {
                 $va_access_values = caGetUserAccessValues($this->request);
                 $va_facet = $o_browse->getFacet($ps_facet_name, array('sort' => 'name', 'checkAccess' => $va_access_values));
                 $va_hierarchy_list = $t_item->getHierarchyList(true);
                 $vn_hierarchies_in_use = 0;
                 foreach ($va_hierarchy_list as $vn_i => $va_item) {
                     if (isset($va_facet[$va_item[$t_item->primaryKey()]])) {
                         $vn_hierarchies_in_use++;
                         if ($vn_hierarchies_in_use > 1) {
                             break;
                         }
                     }
                 }
             }
             if ($t_item->getPrimaryKey()) {
                 $vs_primary_key = $t_item->primaryKey();
                 $this->view->setVar("primary_key", $vs_primary_key);
                 $vs_display_fld = $t_item->getLabelDisplayField();
                 $this->view->setVar("display_field", $vs_display_fld);
                 $vs_label_table_name = $t_item->getLabelTableName();
                 $va_ancestors = array_reverse($t_item->getHierarchyAncestors(null, array('includeSelf' => true, 'additionalTableToJoin' => $vs_label_table_name, 'additionalTableJoinType' => 'LEFT', 'additionalTableSelectFields' => array($vs_display_fld, 'locale_id'), 'additionalTableWheres' => array('(' . $vs_label_table_name . '.is_preferred = 1 OR ' . $vs_label_table_name . '.is_preferred IS NULL)'))));
             }
             if ($vn_hierarchies_in_use <= 1) {
                 array_shift($va_ancestors);
             }
             break;
     }
     $this->view->setVar('ancestors', $va_ancestors);
     switch ($this->request->getParameter('returnAs', pString)) {
         case "json":
             return $this->render('Browse/facet_hierarchy_ancestors_json.php');
             break;
             # ------------------------------------------------
         # ------------------------------------------------
         case "html":
         default:
             return $this->render('Browse/facet_hierarchy_ancestors_html.php');
             break;
             # ------------------------------------------------
     }
 }
Example #5
0
 /**
  * Returns items contained in list, including their labels. This method will returned the list items sorted according to the default_sort field setting of
  * the list they belong to. Note that correct order when sorting by label is only guaranteed if 'extractValuesByUserLocale' is set to true [default is false]. 
  * This is due to the list return format: since each item is indexed by item_id first, it can only have a single position in the return structure. If multiple labels are returned
  * for an item then the item will only be in the correct sort order for one of the labels in most cases. To ensure proper sort order by label text, labels must be restricted to a 
  * single locale.
  * 
  * @param $pm_list_name_or_id mixed - list_code or list_id of desired list
  * @param $pa_options array - optional array of options. Supported options include:
  *			returnHierarchyLevels =		if true list is returned with 'LEVEL' field set to hierarchical level of item, and items are returned in order such that if you loop through the returned list and indent each item according to its level you get a nicely formatted hierarchical display. Default is false.
  * 			extractValuesByUserLocale = if true then values are processed to be appropriate for current user locale; default is false:  return values for all locales
  *			directChildrenOnly =	 	if true, only children immediately below the specified item are returned; [default is false]
  * 			includeSelf =	if true, the specified item is included in the returned set of items; [default is false]
  *			type_id = 		optional list item type to limit returned items by; default is to not limit by type (eg. type_id = null)
  *			item_id =		optional item_id to use as root of hierarchy for returned items; if this is not set (the default) then all items in the list are returned
  *			sort =			if set to a __CA_LISTS_SORT_BY_*__ constant, will force the list to be sorted by that criteria overriding the sort order set in the ca_lists.default_sort field
  *			idsOnly = 		if true, only the primary key id values of the list items are returned
  *			enabledOnly =	return only enabled list items [default=false]
  *			labelsOnly = 	if true only labels in the current locale are returns in an array key'ed on item_id
  *
  * @return array List of items indexed first on item_id and then on locale_id of label
  */
 public function getItemsForList($pm_list_name_or_id, $pa_options = null)
 {
     $vn_list_id = $this->_getListID($pm_list_name_or_id);
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['returnHierarchyLevels'])) {
         $pa_options['returnHierarchyLevels'] = false;
     }
     if (isset($pa_options['directChildrenOnly']) && $pa_options['directChildrenOnly']) {
         $pa_options['returnHierarchyLevels'] = false;
     }
     $vb_enabled_only = caGetOption('enabledOnly', $pa_options, false);
     $vb_labels_only = false;
     if (isset($pa_options['labelsOnly']) && $pa_options['labelsOnly']) {
         $pa_options['extractValuesByUserLocale'] = true;
         $pa_options['returnHierarchyLevels'] = false;
         $vb_labels_only = true;
     }
     $vs_cache_key = caMakeCacheKeyFromOptions(array_merge($pa_options, array('list_id' => $vn_list_id)));
     if (is_array(ca_lists::$s_list_item_cache[$vs_cache_key])) {
         return ca_lists::$s_list_item_cache[$vs_cache_key];
     }
     $t_list = new ca_lists($vn_list_id);
     $pn_type_id = isset($pa_options['type_id']) ? (int) $pa_options['type_id'] : null;
     $pn_sort = isset($pa_options['sort']) ? (int) $pa_options['sort'] : $t_list->get('default_sort');
     if (!($pn_item_id = isset($pa_options['item_id']) ? (int) $pa_options['item_id'] : null)) {
         $pn_item_id = $t_list->getRootListItemID($vn_list_id);
     }
     $t_list_item = new ca_list_items($pn_item_id);
     if (!$t_list_item->getPrimaryKey() || $t_list_item->get('list_id') != $vn_list_id) {
         return null;
     }
     $vs_hier_sql = '';
     if ($t_list_item->getPrimaryKey()) {
         $vs_hier_sql = " AND ((cli.hier_left >= " . floatval($t_list_item->get('hier_left')) . ") AND (cli.hier_right <= " . floatval($t_list_item->get('hier_right')) . "))";
     }
     if (!isset($pa_options['returnHierarchyLevels']) || !$pa_options['returnHierarchyLevels']) {
         $vs_type_sql = '';
         if ($pn_type_id) {
             $vs_type_sql = ' AND (cli.type_id = ' . intval($pn_type_id) . ')';
         }
         $vs_order_by = '';
         switch ($pn_sort) {
             case __CA_LISTS_SORT_BY_LABEL__:
                 // by label
                 $vs_order_by = 'clil.name_plural';
                 break;
             case __CA_LISTS_SORT_BY_RANK__:
                 // by rank
                 $vs_order_by = 'cli.rank';
                 break;
             case __CA_LISTS_SORT_BY_VALUE__:
                 // by value
                 $vs_order_by = 'cli.item_value';
                 break;
             case __CA_LISTS_SORT_BY_IDENTIFIER__:
                 // by identifier
                 $vs_order_by = 'cli.idno_sort';
                 break;
         }
         if ($vs_order_by) {
             $vs_order_by = "ORDER BY {$vs_order_by}";
         }
         $vs_enabled_sql = '';
         if ($vb_enabled_only) {
             $vs_enabled_sql = ' AND (cli.is_enabled = 1)';
         }
         $vs_direct_children_sql = '';
         if (isset($pa_options['directChildrenOnly']) && $pa_options['directChildrenOnly']) {
             $vs_direct_children_sql = " AND cli.parent_id = " . (int) $pn_item_id;
         }
         $o_db = $this->getDb();
         $vs_sql = "\n\t\t\t\tSELECT clil.*, cli.*\n\t\t\t\tFROM ca_list_items cli\n\t\t\t\tINNER JOIN ca_list_item_labels AS clil ON clil.item_id = cli.item_id\n\t\t\t\tWHERE\n\t\t\t\t\t(cli.deleted = 0) AND (clil.is_preferred = 1) AND (cli.list_id = ?) {$vs_type_sql} {$vs_direct_children_sql} {$vs_hier_sql} {$vs_enabled_sql}\n\t\t\t\t{$vs_order_by}\n\t\t\t";
         //print $vs_sql;
         $qr_res = $o_db->query($vs_sql, (int) $vn_list_id);
         $va_seen_locales = array();
         $va_items = array();
         while ($qr_res->nextRow()) {
             $vn_item_id = $qr_res->get('item_id');
             if (isset($pa_options['idsOnly']) && $pa_options['idsOnly']) {
                 $va_items[] = $vn_item_id;
                 continue;
             }
             if ((!isset($pa_options['includeSelf']) || !$pa_options['includeSelf']) && $vn_item_id == $pn_item_id) {
                 continue;
             }
             if (isset($pa_options['directChildrenOnly']) && $pa_options['directChildrenOnly'] && $qr_res->get('parent_id') != $pn_item_id) {
                 continue;
             }
             $va_items[$vn_item_id][$vn_locale_id = $qr_res->get('locale_id')] = $qr_res->getRow();
             $va_seen_locales[$vn_locale_id] = true;
         }
         if (isset($pa_options['idsOnly']) && $pa_options['idsOnly']) {
             return $va_items;
         }
         if (isset($pa_options['extractValuesByUserLocale']) && $pa_options['extractValuesByUserLocale']) {
             $va_items = caExtractValuesByUserLocale($va_items);
             if ($pn_sort == 0 && sizeof($va_seen_locales) > 1) {
                 // do we need to resort list based upon labels? (will already be in correct order if there's only one locale)
                 $va_labels = array();
                 foreach ($va_items as $vn_item_id => $va_row) {
                     $va_labels[$va_row['name_plural'] . $vn_item_id] = $va_row;
                 }
                 ksort($va_labels);
                 $va_items = array();
                 foreach ($va_labels as $vs_key => $va_row) {
                     $va_items[$va_row['item_id']] = $va_row;
                 }
             }
         }
         if ($vb_labels_only) {
             $va_labels = array();
             foreach ($va_items as $vn_item_id => $va_row) {
                 $va_labels[$vn_item_id] = $va_row['name_plural'];
             }
             return $va_labels;
         }
     } else {
         // hierarchical output
         $va_list_items = $t_list_item->getHierarchyAsList($pn_item_id, array('additionalTableToJoin' => 'ca_list_item_labels', 'additionalTableSelectFields' => array('name_singular', 'name_plural', 'locale_id'), 'additionalTableWheres' => array('ca_list_item_labels.is_preferred = 1')));
         foreach ($va_list_items as $vn_i => $va_item) {
             if ($pn_type_id && $va_item['NODE']['type_id'] != $pn_type_id) {
                 continue;
             }
             if ($vb_enabled_only && !$va_item['NODE']['is_enabled']) {
                 continue;
             }
             $vn_item_id = $va_item['NODE']['item_id'];
             $vn_parent_id = $va_item['NODE']['parent_id'];
             if ((!isset($pa_options['includeSelf']) || !$pa_options['includeSelf']) && $vn_item_id == $pn_item_id) {
                 continue;
             }
             if (isset($pa_options['directChildrenOnly']) && $pa_options['directChildrenOnly'] && $vn_parent_id != $pn_item_id) {
                 continue;
             }
             switch ($pn_sort) {
                 case __CA_LISTS_SORT_BY_LABEL__:
                     // label
                 // label
                 default:
                     $vs_key = $va_item['NODE']['name_singular'];
                     break;
                 case __CA_LISTS_SORT_BY_RANK__:
                     // rank
                     $vs_key = sprintf("%08d", (int) $va_item['NODE']['rank']);
                     break;
                 case __CA_LISTS_SORT_BY_VALUE__:
                     // value
                     $vs_key = $va_item['NODE']['item_value'];
                     break;
                 case __CA_LISTS_SORT_BY_IDENTIFIER__:
                     // identifier
                     $vs_key = $va_item['NODE']['idno_sort'];
                     break;
             }
             if (isset($pa_options['extractValuesByUserLocale']) && $pa_options['extractValuesByUserLocale']) {
                 $va_items[$vn_parent_id][$vn_item_id][$va_item['NODE']['locale_id']][$vs_key][$vn_item_id] = array_merge($va_item['NODE'], array('LEVEL' => $va_item['LEVEL']));
             } else {
                 $va_items[$vn_parent_id][$va_item['NODE']['locale_id']][$vs_key][$vn_item_id] = array_merge($va_item['NODE'], array('LEVEL' => $va_item['LEVEL']));
             }
         }
         $pa_sorted_items = array();
         if (is_array($va_items) && (isset($pa_options['extractValuesByUserLocale']) && $pa_options['extractValuesByUserLocale'])) {
             //$va_items = caExtractValuesByUserLocale($va_items);
             $va_proc_items = array();
             foreach ($va_items as $vn_parent_id => $va_item) {
                 $va_item = caExtractValuesByUserLocale($va_item);
                 foreach ($va_item as $vn_item_id => $va_items_by_key) {
                     foreach ($va_items_by_key as $vs_key => $va_val) {
                         foreach ($va_val as $vn_item_id => $va_item_info) {
                             $va_proc_items[$vn_parent_id][$vs_key][$vn_item_id] = $va_item_info;
                         }
                     }
                 }
             }
             $this->_getItemsForListProcListLevel($pn_item_id, $va_proc_items, $pa_sorted_items, $pa_options);
         } else {
             $this->_getItemsForListProcListLevel($pn_item_id, $va_items, $pa_sorted_items, $pa_options);
         }
         $va_items = $pa_sorted_items;
     }
     ca_lists::$s_list_item_cache[$vs_cache_key] = $va_items;
     return $va_items;
 }
/**
 * 
 *
 * @return string 
 */
function caLoadULAN($ps_path_to_ulan_data = null, $ps_path_to_ulan_config = null, $pa_options = null)
{
    require_once __CA_LIB_DIR__ . '/core/Db.php';
    require_once __CA_LIB_DIR__ . '/core/Configuration.php';
    require_once __CA_LIB_DIR__ . '/ca/Utils/DataMigrationUtils.php';
    require_once __CA_MODELS_DIR__ . '/ca_locales.php';
    require_once __CA_MODELS_DIR__ . '/ca_entities.php';
    require_once __CA_MODELS_DIR__ . '/ca_entities_x_entities.php';
    require_once __CA_MODELS_DIR__ . '/ca_lists.php';
    require_once __CA_MODELS_DIR__ . '/ca_list_items.php';
    require_once __CA_MODELS_DIR__ . '/ca_list_items_x_list_items.php';
    require_once __CA_MODELS_DIR__ . '/ca_relationship_types.php';
    $t = new Timer();
    $o_log = new KLogger(__CA_APP_DIR__ . '/log', KLogger::INFO);
    $va_parent_child_links = array();
    $va_item_item_links = array();
    $va_ulan_id_to_item_id = array();
    $o_log->logInfo("Starting import of Getty ULAN");
    define('__CA_DONT_DO_SEARCH_INDEXING__', true);
    $_ = new Zend_Translate('gettext', __CA_APP_DIR__ . '/locale/en_US/messages.mo', 'en_US');
    $t_locale = new ca_locales();
    $pn_en_locale_id = $t_locale->loadLocaleByCode('en_US');
    if (!($o_config = Configuration::load($ps_path_to_ulan_config))) {
        $o_log->logError("Could not load ULAN import configuration file");
        die("ERROR: Could not load ULAN import configuration\n");
    }
    $vs_ulan_import_mode = $o_config->get('ulan_import_target');
    $t_list = null;
    if ($vs_ulan_import_mode == 'ca_entities') {
        $va_ulan_types = $o_config->getAssoc('ulan_entity_types');
        $va_mapping = $o_config->getAssoc('ulan_entity_mapping');
    } elseif ($vs_ulan_import_mode == 'ca_list_items') {
        $va_ulan_types = $o_config->getAssoc('ulan_list_item_types');
        if (!($vs_ulan_list_code = $o_config->get('ulan_import_list'))) {
            $vs_ulan_list_code = 'ULAN';
        }
        // create vocabulary list record (if it doesn't exist already)
        $t_list = new ca_lists();
        if (!$t_list->load(array('list_code' => $vs_ulan_list_code))) {
            $t_list->setMode(ACCESS_WRITE);
            $t_list->set('list_code', $vs_ulan_list_code);
            $t_list->set('is_system_list', 0);
            $t_list->set('is_hierarchical', 1);
            $t_list->set('use_as_vocabulary', 1);
            $t_list->insert();
            if ($t_list->numErrors()) {
                $o_log->logError("Could not create list record for ULAN: " . join('; ', $t_list->getErrors()));
                die("ERROR: couldn't create ca_list row for ULAN: " . join('; ', $t_list->getErrors()) . "\n");
            }
            $t_list->addLabel(array('name' => 'Union List of Artist Names'), $pn_en_locale_id, null, true);
        }
        $vn_list_id = $t_list->getPrimaryKey();
        $va_mapping = $o_config->getAssoc('ulan_list_item_mapping');
    } else {
        $o_log->logError("Invalid ULAN import mode {$vs_ulan_import_mode}");
        die("ERROR: invalid ULAN import mode {$vs_ulan_import_mode}\n");
    }
    $vn_last_message_length = 0;
    $vn_term_count = 0;
    $va_subject = array();
    foreach (array('ULAN1.xml', 'ULAN2.xml', 'ULAN3.xml') as $vs_file) {
        if (!$ps_path_to_ulan_data) {
            $ps_path_to_ulan_data = ".";
        }
        if (!file_exists($ps_path_to_ulan_data . "/{$vs_file}")) {
            $o_log->logError("Could not find ULAN data file {$vs_file}");
            print "[ERROR] cannot find ULAN data.\n";
            continue;
        }
        $o_log->logInfo("Processing ULAN file {$vs_file}");
        print "[Notice] Processing ULAN file {$vs_file}\n";
        // load
        $o_xml = new XMLReader();
        $o_xml->open($ps_path_to_ulan_data . '/' . $vs_file);
        while ($o_xml->read()) {
            switch ($o_xml->name) {
                # ---------------------------
                case 'Subject':
                    if ($o_xml->nodeType == XMLReader::END_ELEMENT) {
                        if (in_array($va_subject['subject_id'], array('500000000', '500000001'))) {
                            break;
                        }
                        // skip top-level root
                        $vs_preferred_term = $va_subject['preferred_term'];
                        $pb_is_enabled = false;
                        switch ($va_subject['record_type']) {
                            case 'Person':
                            default:
                                $vn_type_id = $va_ulan_types['Person'];
                                $pb_is_enabled = true;
                                break;
                            case 'Corporate Body':
                                $vn_type_id = $va_ulan_types['Corporate Body'];
                                $pb_is_enabled = true;
                                break;
                        }
                        print str_repeat(chr(8), $vn_last_message_length);
                        $vs_message = "\tIMPORTING #" . ($vn_term_count + 1) . " [" . $va_subject['subject_id'] . "] " . $vs_preferred_term;
                        if (($vn_l = 100 - strlen($vs_message)) < 1) {
                            $vn_l = 1;
                        }
                        $vs_message .= str_repeat(' ', $vn_l);
                        $vn_last_message_length = strlen($vs_message);
                        print $vs_message;
                        if ($vs_ulan_import_mode == 'ca_entities') {
                            $va_np_labels = array();
                            if (is_array($va_subject['non_preferred_terms'])) {
                                for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) {
                                    $va_np_labels[] = DataMigrationUtils::splitEntityName(trim(htmlentities($va_subject['non_preferred_terms'][$vn_i])));
                                }
                            }
                            $t_item = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName(trim(htmlentities($vs_preferred_term, ENT_NOQUOTES))), $vn_type_id, $pn_en_locale_id, array('idno' => $va_subject['subject_id']), array('nonPreferredLabels' => $va_np_labels, 'returnInstance' => true));
                            if (!$t_item) {
                                $o_log->logError("Failed to create entity for ULAN artist {$vs_preferred_term}");
                                break;
                            }
                            $t_item->setMode(ACCESS_WRITE);
                            $va_ulan_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey();
                        } else {
                            if ($t_item = $t_list->addItem($va_subject['subject_id'], $pb_is_enabled, false, null, $vn_type_id, $va_subject['subject_id'], '', 4, 1)) {
                                $va_ulan_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey();
                                if ($va_subject['preferred_parent_subject_id'] != 500000000) {
                                    $va_parent_child_links[$va_subject['subject_id']] = $va_subject['preferred_parent_subject_id'];
                                }
                                // add preferred labels
                                if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'description' => $va_subject['description']), $pn_en_locale_id, null, true)) {
                                    $o_log->logError("Could not add preferred label to ULAN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_item->getErrors()));
                                }
                                // add alternate labels
                                if (is_array($va_subject['non_preferred_terms'])) {
                                    for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) {
                                        $vs_np_label = $va_subject['non_preferred_terms'][$vn_i];
                                        $vs_np_term_type = $va_subject['non_preferred_term_types'][$vn_i];
                                        switch ($vs_np_term_type) {
                                            case 'Used For Term':
                                                $vn_np_term_type_id = $vn_list_item_label_type_uf;
                                                break;
                                            case 'Alternate Descriptor':
                                                $vn_np_term_type_id = $vn_list_item_label_type_alt;
                                                break;
                                            default:
                                                $vn_np_term_type_id = null;
                                                break;
                                        }
                                        if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'description' => ''), $pn_en_locale_id, $vn_np_term_type_id, false)) {
                                            $o_log->logError("Could not add non-preferred label to ULAN term [" . $va_subject['subject_id'] . "] " . $vs_np_label);
                                        }
                                    }
                                }
                            } else {
                                $o_log->logError("Could not import ULAN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_list->getErrors()));
                                break;
                            }
                        }
                        // Map content fields
                        foreach ($va_mapping as $vs_dest => $vs_source) {
                            $va_values = array();
                            switch ($vs_source) {
                                case 'biography':
                                    if (!is_array($va_subject['biographies'])) {
                                        break;
                                    }
                                    foreach ($va_subject['biographies'] as $va_bio) {
                                        $va_values[] = $va_bio['text'];
                                    }
                                    break;
                                case 'biography_dates':
                                    if (!is_array($va_subject['biographies'])) {
                                        break;
                                    }
                                    foreach ($va_subject['biographies'] as $va_bio) {
                                        if ($va_bio['birth_date'] == 1000 || $va_bio['birth_date'] < -5000) {
                                            if ($va_bio['death_date'] >= 2050) {
                                                break 2;
                                            } else {
                                                $va_values[] = "before " . $va_bio['death_date'];
                                            }
                                        } elseif ($va_bio['death_date'] >= 2050) {
                                            $va_values[] = "after " . $va_bio['birth_date'];
                                        } else {
                                            $va_values[] = $va_bio['birth_date'] . " - " . $va_bio['death_date'];
                                        }
                                    }
                                    break;
                                case 'sex':
                                    if (!is_array($va_subject['biographies'])) {
                                        break;
                                    }
                                    foreach ($va_subject['biographies'] as $va_bio) {
                                        $va_values[] = $va_bio['sex'];
                                    }
                                    break;
                                case 'nationality_name':
                                    if (!is_array($va_subject['nationalities'])) {
                                        break;
                                    }
                                    foreach ($va_subject['nationalities'] as $va_nationality) {
                                        $va_values[] = $va_nationality['name'];
                                    }
                                    break;
                                case 'nationality_code':
                                    if (!is_array($va_subject['nationalities'])) {
                                        break;
                                    }
                                    foreach ($va_subject['nationalities'] as $va_nationality) {
                                        $va_values[] = $va_nationality['code'];
                                    }
                                    break;
                                case 'role_name':
                                    if (!is_array($va_subject['roles'])) {
                                        break;
                                    }
                                    foreach ($va_subject['roles'] as $va_role) {
                                        $va_values[] = $va_role['name'];
                                    }
                                    break;
                                case 'role_code':
                                    if (!is_array($va_subject['roles'])) {
                                        break;
                                    }
                                    foreach ($va_subject['roles'] as $va_role) {
                                        $va_values[] = $va_role['code'];
                                    }
                                    break;
                            }
                            if (sizeof($va_values)) {
                                $va_dest = explode('.', $vs_dest);
                                $vs_fld = array_pop($va_dest);
                                if ($t_item->hasField($vs_fld)) {
                                    $t_item->set($vs_fld, join("\n", $va_values));
                                } else {
                                    foreach ($va_values as $vs_value) {
                                        $t_item->addAttribute(array('locale_id' => $pn_en_locale_id, $vs_fld => $vs_value), $vs_fld);
                                    }
                                }
                                $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true));
                                if ($t_item->numErrors()) {
                                    $o_log->logError("Could not update ULAN list item with content values: " . join("; ", $t_item->getErrors()));
                                }
                            }
                        }
                        // record item-item relations
                        if (is_array($va_subject['related_subjects'])) {
                            foreach ($va_subject['related_subjects'] as $vs_rel_subject_id) {
                                $va_item_item_links[$va_subject['subject_id']] = $vs_rel_subject_id;
                            }
                        }
                        $vn_term_count++;
                    } else {
                        $va_subject = array('subject_id' => $o_xml->getAttribute('Subject_ID'));
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Biographies':
                    while ($o_xml->read()) {
                        switch ($o_xml->name) {
                            case 'Preferred_Biography':
                                $va_bio = array();
                                while ($o_xml->read()) {
                                    switch ($o_xml->name) {
                                        case 'Biography_Text':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $va_bio['text'] = $o_xml->value;
                                                    break;
                                            }
                                            break;
                                        case 'Birth_Date':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $va_bio['birth_date'] = $o_xml->value;
                                                    if ($va_bio['birth_date'] < 0) {
                                                        $va_bio['birth_date'] = abs($va_bio['birth_date']) . " BCE";
                                                    }
                                                    break;
                                            }
                                            break;
                                        case 'Death_Date':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $va_bio['death_date'] = $o_xml->value;
                                                    if ($va_bio['death_date'] < 0) {
                                                        $va_bio['death_date'] = abs($va_bio['death_date']) . " BCE";
                                                    }
                                                    break;
                                            }
                                            break;
                                        case 'Sex':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $va_bio['sex'] = $o_xml->value;
                                                    break;
                                            }
                                            break;
                                        case 'Preferred_Biography':
                                            break 2;
                                    }
                                }
                                $va_subject['biographies'][] = $va_bio;
                                break;
                            case 'Biographies':
                                break 2;
                        }
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Nationalities':
                    while ($o_xml->read()) {
                        switch ($o_xml->name) {
                            case 'Preferred_Nationality':
                                $va_nationality = array();
                                while ($o_xml->read()) {
                                    switch ($o_xml->name) {
                                        case 'Nationality_Code':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $va_nationality['code'] = $o_xml->value;
                                                    $va_nationality['name'] = array_pop(explode('/', $o_xml->value));
                                                    break;
                                            }
                                            break;
                                        case 'Preferred_Nationality':
                                            break 2;
                                    }
                                }
                                $va_subject['nationalities'][] = $va_nationality;
                                break;
                            case 'Nationalities':
                                break 2;
                        }
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Roles':
                    while ($o_xml->read()) {
                        switch ($o_xml->name) {
                            case 'Preferred_Role':
                                $va_role = array();
                                while ($o_xml->read()) {
                                    switch ($o_xml->name) {
                                        case 'Role_ID':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $va_role['code'] = $o_xml->value;
                                                    $va_role['name'] = array_pop(explode('/', $o_xml->value));
                                                    break;
                                            }
                                            break;
                                        case 'Preferred_Role':
                                            break 2;
                                    }
                                }
                                $va_subject['roles'][] = $va_role;
                                break;
                            case 'Roles':
                                break 2;
                        }
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Record_Type':
                    switch ($o_xml->nodeType) {
                        case XMLReader::ELEMENT:
                            $o_xml->read();
                            $va_subject['record_type'] = $o_xml->value;
                            break;
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Hierarchy':
                    switch ($o_xml->nodeType) {
                        case XMLReader::ELEMENT:
                            $o_xml->read();
                            $va_subject['hierarchy'] = $o_xml->value;
                            break;
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Parent_Relationships':
                    $vn_parent_id = $vs_historic_flag = null;
                    while ($o_xml->read()) {
                        switch ($o_xml->name) {
                            case 'Preferred_Parent':
                                while ($o_xml->read()) {
                                    switch ($o_xml->name) {
                                        case 'Parent_Subject_ID':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $vn_parent_id = $o_xml->value;
                                                    break;
                                            }
                                            break;
                                        case 'Historic_Flag':
                                            switch ($o_xml->nodeType) {
                                                case XMLReader::ELEMENT:
                                                    $o_xml->read();
                                                    $vs_historic_flag = $o_xml->value;
                                                    break;
                                            }
                                            break;
                                        case 'Preferred_Parent':
                                            $va_subject['preferred_parent_subject_id'] = $vn_parent_id;
                                            break 2;
                                    }
                                }
                                break;
                            case 'Parent_Relationships':
                                break 2;
                        }
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Preferred_Term':
                    while ($o_xml->read()) {
                        switch ($o_xml->name) {
                            case 'Term_Type':
                                switch ($o_xml->nodeType) {
                                    case XMLReader::ELEMENT:
                                        $o_xml->read();
                                        $va_subject['preferred_term_type'] = $o_xml->value;
                                        break;
                                }
                                break;
                            case 'Term_Text':
                                switch ($o_xml->nodeType) {
                                    case XMLReader::ELEMENT:
                                        $o_xml->read();
                                        $va_subject['preferred_term'] = $o_xml->value;
                                        break;
                                }
                                break;
                            case 'Term_ID':
                                switch ($o_xml->nodeType) {
                                    case XMLReader::ELEMENT:
                                        $o_xml->read();
                                        $va_subject['preferred_term_id'] = $o_xml->value;
                                        break;
                                }
                                break;
                                break;
                            case 'Preferred_Term':
                                break 2;
                        }
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'Non-Preferred_Term':
                    while ($o_xml->read()) {
                        switch ($o_xml->name) {
                            case 'Term_Type':
                                switch ($o_xml->nodeType) {
                                    case XMLReader::ELEMENT:
                                        $o_xml->read();
                                        $va_subject['non_preferred_term_types'][] = $o_xml->value;
                                        break;
                                }
                                break;
                            case 'Term_Text':
                                switch ($o_xml->nodeType) {
                                    case XMLReader::ELEMENT:
                                        $o_xml->read();
                                        $va_subject['non_preferred_terms'][] = $o_xml->value;
                                        break;
                                }
                                break;
                            case 'Term_ID':
                                switch ($o_xml->nodeType) {
                                    case XMLReader::ELEMENT:
                                        $o_xml->read();
                                        $va_subject['non_preferred_term_ids'][] = $o_xml->value;
                                        break;
                                }
                                break;
                            case 'Non-Preferred_Term':
                                break 2;
                        }
                    }
                    break;
                    # ---------------------------
                # ---------------------------
                case 'VP_Subject_ID':
                    switch ($o_xml->nodeType) {
                        case XMLReader::ELEMENT:
                            $o_xml->read();
                            $va_subject['related_subjects'][] = $o_xml->value;
                            break;
                    }
                    break;
                    # ---------------------------
            }
        }
        $o_xml->close();
    }
    $o_log->logInfo("Begin linking ULAN terms in hierarchy");
    print "\n\nLINKING TERMS IN HIERARCHY...\n";
    $vn_last_message_length = 0;
    $t_list = new ca_lists();
    $t_item = new ca_list_items();
    $t_item->setMode(ACCESS_WRITE);
    $vn_list_root_id = $t_list->getRootListItemID($vn_list_id);
    foreach ($va_parent_child_links as $vs_child_id => $vs_parent_id) {
        print str_repeat(chr(8), $vn_last_message_length);
        $vs_message = "\tLINKING {$vs_child_id} to parent {$vs_parent_id}";
        if (($vn_l = 100 - strlen($vs_message)) < 1) {
            $vn_l = 1;
        }
        $vs_message .= str_repeat(' ', $vn_l);
        $vn_last_message_length = strlen($vs_message);
        print $vs_message;
        if (in_array($vs_parent_id, array('500000000', '500000001'))) {
            if (!$t_item->load($vn_child_item_id)) {
                $o_log->logError("Could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})");
                continue;
            }
            $t_item->set('parent_id', $vn_list_root_id);
            $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true));
            if ($t_item->numErrors()) {
                $o_log->logError("Could not set parent_id for {$vs_child_id} to root): " . join('; ', $t_item->getErrors()));
                continue;
            }
            $va_ulan_id_to_item_id[$vs_parent_id] = $vn_list_root_id;
        }
        if (!($vn_child_item_id = $va_ulan_id_to_item_id[$vs_child_id])) {
            $o_log->logError("No list item id for child_id {$vs_child_id} (were there previous errors?)");
            continue;
        }
        if (!($vn_parent_item_id = $va_ulan_id_to_item_id[$vs_parent_id])) {
            $o_log->logError("No list item id for parent_id {$vs_parent_id} (were there previous errors?)");
            continue;
        }
        if (!$t_item->load($vn_child_item_id)) {
            $o_log->logError("Could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})");
            continue;
        }
        $t_item->set('parent_id', $vn_parent_item_id);
        $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true));
        if ($t_item->numErrors()) {
            $o_log->logError("Could not set parent_id for {$vs_child_id} (was translated to item_id={$vn_child_item_id}): " . join('; ', $t_item->getErrors()));
        }
    }
    if ($vn_list_item_relation_type_id_related > 0) {
        $o_log->logInfo("Begin adding ULAN related term links");
        $vn_last_message_length = 0;
        $t_item = new ca_list_items();
        $t_link = new ca_list_items_x_list_items();
        $t_link->setMode(ACCESS_WRITE);
        foreach ($va_item_item_links as $vs_left_id => $vs_right_id) {
            print str_repeat(chr(8), $vn_last_message_length);
            $vs_message = "\tLINKING {$vs_left_id} to {$vs_right_id}";
            if (($vn_l = 100 - strlen($vs_message)) < 1) {
                $vn_l = 1;
            }
            $vs_message .= str_repeat(' ', $vn_l);
            $vn_last_message_length = strlen($vs_message);
            print $vs_message;
            if (!($vn_left_item_id = $va_ulan_id_to_item_id[$vs_left_id])) {
                $o_log->logError("No list item id for left_id {$vs_left_id} (were there previous errors?)");
                continue;
            }
            if (!($vn_right_item_id = $va_ulan_id_to_item_id[$vs_right_id])) {
                $o_log->logError("No list item id for right_id {$vs_right_id} (were there previous errors?)");
                continue;
            }
            $t_link->set('term_left_id', $vn_left_item_id);
            $t_link->set('term_right_id', $vn_right_item_id);
            $t_link->set('type_id', $vn_list_item_relation_type_id_related);
            $t_link->insert();
            if ($t_link->numErrors()) {
                $o_log->logError("Could not set link between {$vs_left_id} (was translated to item_id={$vn_left_item_id}) and {$vs_right_id} (was translated to item_id={$vn_right_item_id}): " . join('; ', $t_link->getErrors()));
            }
        }
    } else {
        $o_log->logWarn("Skipped import of term-term relationships because the ca_list_items_x_list_items 'related' relationship type is not defined for your installation");
    }
    $vn_duration = $t->getTime(1);
    $vs_time = caFormatInterval($vn_duration);
    $o_log->logInfo("Rebuilding hierarchical indices...");
    $t_item->rebuildAllHierarchicalIndexes();
    $o_log->logInfo("ULAN import complete. Took {$vs_time} ({$vn_duration})");
    print "\n\nIMPORT COMPLETE. Took {$vs_time} ({$vn_duration})\n";
}