/**
 * 
 *
 * @param string $ps_refinery_name
 * @param string $ps_table
 * @param array $pa_parents 
 * @param array $pa_source_data
 * @param array $pa_item
 * @param int $pn_c
 * @param KLogger $o_log
 * 
 * @return int
 */
function caProcessRefineryParents($ps_refinery_name, $ps_table, $pa_parents, $pa_source_data, $pa_item, $pn_c, $pa_options = null)
{
    global $g_ui_locale_id;
    if (!is_array($pa_options)) {
        $pa_options = array();
    }
    $o_log = caGetOption('log', $pa_options, null);
    $o_reader = caGetOption('reader', $pa_options, null);
    $o_trans = caGetOption('transaction', $pa_options, null);
    $vn_list_id = caGetOption('list_id', $pa_options, null);
    $vb_hierarchy_mode = caGetOption('hierarchyMode', $pa_options, false);
    if (!is_array($pa_parents)) {
        $pa_parents = array($pa_parents);
    }
    $vn_id = null;
    $pa_parents = array_reverse($pa_parents);
    foreach ($pa_parents as $vn_i => $va_parent) {
        if (!is_array($va_parent)) {
            $o_log->logWarn(_t('[%2] Parents options invalid. Did you forget to pass a list? Parents list passed was: %1', print_r($pa_parents, true), $ps_refinery_name));
            break;
        }
        $vs_name = BaseRefinery::parsePlaceholder($va_parent['name'], $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
        $vs_idno = BaseRefinery::parsePlaceholder($va_parent['idno'], $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
        $vs_type = BaseRefinery::parsePlaceholder($va_parent['type'], $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
        if (!$vs_name && !$vs_idno) {
            continue;
        }
        if (!$vs_name) {
            continue;
        }
        //$vs_name = $vs_idno; }
        $va_attributes = isset($va_parent['attributes']) && is_array($va_parent['attributes']) ? $va_parent['attributes'] : array();
        foreach ($va_attributes as $vs_element_code => $va_attrs) {
            if (is_array($va_attrs)) {
                foreach ($va_attrs as $vs_k => $vs_v) {
                    // BaseRefinery::parsePlaceholder may return an array if the input format supports repeated values (as XML does)
                    // DataMigrationUtils::getCollectionID(), which ca_data_importers::importDataFromSource() uses to create related collections
                    // only supports non-repeating attribute values, so we join any values here and call it a day.
                    $va_attributes[$vs_element_code][$vs_k] = BaseRefinery::parsePlaceholder($vs_v, $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
                }
            } else {
                $va_attributes[$vs_element_code] = array($vs_element_code => BaseRefinery::parsePlaceholder($va_attrs, $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' ')));
            }
        }
        $va_attributes['idno'] = $vs_idno;
        $va_attributes['parent_id'] = $vn_id;
        if (isset($va_parent['rules']) && is_array($va_parent['rules'])) {
            foreach ($va_parent['rules'] as $va_rule) {
                $vm_ret = ExpressionParser::evaluate($va_rule['trigger'], $pa_source_data);
                if (!ExpressionParser::hadError() && (bool) $vm_ret) {
                    foreach ($va_rule['actions'] as $va_action) {
                        if (!is_array($va_action) && strtolower($va_action) == 'skip') {
                            $va_action = array('action' => 'skip');
                        }
                        switch ($vs_action_code = strtolower($va_action['action'])) {
                            case 'set':
                                switch ($va_action['target']) {
                                    case 'name':
                                        $vs_name = BaseRefinery::parsePlaceholder($va_action['value'], $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
                                        break;
                                    case 'type':
                                        $vs_type = BaseRefinery::parsePlaceholder($va_action['value'], $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
                                        break;
                                    default:
                                        $va_attributes[$va_action['target']] = BaseRefinery::parsePlaceholder($va_action['value'], $pa_source_data, $pa_item, $pn_c, array('reader' => $o_reader, 'returnAsString' => true, 'delimiter' => ' '));
                                        break;
                                }
                                break;
                            case 'skip':
                            default:
                                if ($o_log) {
                                    if ($vs_action_code != 'skip') {
                                        $o_log->logInfo(_t('[%3] Parent was skipped using rule "%1" with default action because an invalid action ("%2") was specified', $va_rule['trigger'], $vs_action_code, $ps_refinery_name));
                                    } else {
                                        $o_log->logDebug(_t('[%3] Parent was skipped using rule "%1" with action "%2"', $va_rule['trigger'], $vs_action_code, $ps_refinery_name));
                                    }
                                }
                                continue 4;
                                break;
                        }
                    }
                } elseif (ExpressionParser::hadError() && $o_log) {
                    $o_log->logError(_t('[%3] Error processing rule "%1" as an error occurred. Error number was "%2"', $va_rule['trigger'], ExpressionParser::$s_last_error, $ps_refinery_name));
                }
            }
        }
        $va_match_on = caGetOption("{$ps_refinery_name}_dontMatchOnLabel", $pa_item['settings'], false) ? array('idno') : array('idno', 'label');
        $pa_options = array_merge(array('matchOn' => $va_match_on), $pa_options);
        switch ($ps_table) {
            case 'ca_objects':
                $vn_id = DataMigrationUtils::getObjectID($vs_name, $vn_id, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_entities':
                $vn_id = DataMigrationUtils::getEntityID($va_entity_label = DataMigrationUtils::splitEntityName($vs_name, $pa_options), $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels'] = $va_entity_label;
                $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_places':
                if (!$vn_id) {
                    // get place hierarchy root
                    require_once __CA_MODELS_DIR__ . "/ca_places.php";
                    $t_place = new ca_places();
                    if ($o_trans) {
                        $t_place->setTransaction($o_trans);
                    }
                    $vn_id = $pa_options['defaultParentID'];
                    if (!$vn_id) {
                        $vn_id = $t_place->getHierarchyRootID($pa_options['hierarchyID']);
                    }
                    $va_attributes['parent_id'] = $vn_id;
                }
                $vn_id = DataMigrationUtils::getPlaceID($vs_name, $vn_id, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_occurrences':
                $vn_id = DataMigrationUtils::getOccurrenceID($vs_name, $vn_id, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_collections':
                $vn_id = DataMigrationUtils::getCollectionID($vs_name, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_loans':
                $vn_id = DataMigrationUtils::getLoanID($vs_name, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_movements':
                $vn_id = DataMigrationUtils::getMovementID($vs_name, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            case 'ca_list_items':
                if (!$vn_list_id) {
                    if ($o_log) {
                        $o_log->logDebug(_t('[importHelpers:caProcessRefineryParents] List was not specified'));
                    }
                    return null;
                }
                if (!$vn_id) {
                    // get place hierarchy root
                    require_once __CA_MODELS_DIR__ . "/ca_lists.php";
                    $t_list = new ca_lists();
                    if ($o_trans) {
                        $t_list->setTransaction($o_trans);
                    }
                    $vn_id = $t_list->getRootItemIDForList($vn_list_id);
                    $va_attributes['parent_id'] = $vn_id;
                }
                $vn_id = DataMigrationUtils::getListItemID($vn_list_id, $vs_name, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name_singular'] = $va_attributes['preferred_labels']['name_plural'] = $vs_name;
                break;
            case 'ca_storage_locations':
                if (!$vn_id) {
                    // get storage location hierarchy root
                    require_once __CA_MODELS_DIR__ . "/ca_storage_locations.php";
                    $t_loc = new ca_storage_locations();
                    if ($o_trans) {
                        $t_loc->setTransaction($o_trans);
                    }
                    $vn_id = $t_loc->getHierarchyRootID();
                    $va_attributes['parent_id'] = $vn_id;
                }
                $vn_id = DataMigrationUtils::getStorageLocationID($vs_name, $vn_id, $vs_type, $g_ui_locale_id, $va_attributes, $pa_options);
                $va_attributes['preferred_labels']['name'] = $va_attributes['_preferred_labels'] = $vs_name;
                break;
            default:
                if ($o_log) {
                    $o_log->logDebug(_t('[importHelpers:caProcessRefineryParents] Invalid table %1', $ps_table));
                }
                return null;
                break;
        }
        $va_attributes['locale_id'] = $g_ui_locale_id;
        if ($o_log) {
            $o_log->logDebug(_t('[%6] Got parent %1 (%2) with id %3 and type %4 for %5', $vs_name, $vs_idno, $vn_id, $vs_type, $vs_name, $ps_refinery_name));
        }
    }
    if ($vb_hierarchy_mode) {
        return $va_attributes;
    }
    return $vn_id;
}
 /**
  * Returns list of placements for the currently loaded display.
  *
  * @param array $pa_options Optional array of options. Supports the following options:
  * 		noCache = if set to true then the returned list if always generated directly from the database, otherwise it is returned from the cache if possible. Set this to true if you expect the cache may be stale. Default is false.
  *		returnAllAvailableIfEmpty = if set to true then the list of all available bundles will be returned if the currently loaded display has no placements, or if there is no display loaded
  *		table = if using the returnAllAvailableIfEmpty option and you expect a list of available bundles to be returned if no display is loaded, you must specify the table the bundles are intended for use with with this option. Either the table name or number may be used.
  *		user_id = if specified then placements are only returned if the user has at least read access to the display
  *		settingsOnly = if true the settings forms are omitted and only setting values are returned; default is false
  * @return array List of placements in display order. Array is keyed on bundle name. Values are arrays with the following keys:
  *		placement_id = primary key of ca_bundle_display_placements row - a unique id for the placement
  *		bundle_name = bundle name (a code - not for display)
  *		settings = array of placement settings. Keys are setting names.
  *		display = display string for bundle
  */
 public function getPlacements($pa_options = null)
 {
     $pb_no_cache = isset($pa_options['noCache']) ? (bool) $pa_options['noCache'] : false;
     $pb_settings_only = isset($pa_options['settingsOnly']) ? (bool) $pa_options['settingsOnly'] : false;
     $pb_return_all_available_if_empty = isset($pa_options['returnAllAvailableIfEmpty']) && !$pb_settings_only ? (bool) $pa_options['returnAllAvailableIfEmpty'] : false;
     $ps_table = isset($pa_options['table']) ? $pa_options['table'] : null;
     $pn_user_id = isset($pa_options['user_id']) ? $pa_options['user_id'] : null;
     if ($pn_user_id && !$this->haveAccessToDisplay($pn_user_id, __CA_BUNDLE_DISPLAY_READ_ACCESS__)) {
         return array();
     }
     if (!($vn_display_id = $this->getPrimaryKey())) {
         if ($pb_return_all_available_if_empty && $ps_table) {
             return ca_bundle_displays::$s_placement_list_cache[$vn_display_id] = $this->getAvailableBundles($ps_table);
         }
         return array();
     }
     $vs_cache_key = $vn_display_id . '/' . ($pb_settings_only ? 1 : 0);
     if (!$pb_no_cache && isset(ca_bundle_displays::$s_placement_list_cache[$vs_cache_key]) && ca_bundle_displays::$s_placement_list_cache[$vs_cache_key]) {
         return ca_bundle_displays::$s_placement_list_cache[$vs_cache_key];
     }
     $o_dm = $this->getAppDatamodel();
     $o_db = $this->getDb();
     $t_list = new ca_lists();
     if ($this->inTransaction()) {
         $t_list->setTransaction($this->getTransaction());
     }
     $qr_res = $o_db->query("\n\t\t\tSELECT placement_id, bundle_name, settings\n\t\t\tFROM ca_bundle_display_placements\n\t\t\tWHERE\n\t\t\t\tdisplay_id = ?\n\t\t\tORDER BY rank\n\t\t", (int) $vn_display_id);
     $va_available_bundles = $pb_settings_only ? array() : $this->getAvailableBundles(null, $pa_options);
     $va_placements = array();
     if ($qr_res->numRows() > 0) {
         $vs_subject_table = $o_dm->getTableName($this->get('table_num'));
         $t_subject = $o_dm->getInstanceByTableNum($this->get('table_num'), true);
         $t_placement = new ca_bundle_display_placements();
         if ($this->inTransaction()) {
             $t_placement->setTransaction($this->getTransaction());
         }
         while ($qr_res->nextRow()) {
             $vs_bundle_name = $qr_res->get('bundle_name');
             $va_bundle_name = explode(".", $vs_bundle_name);
             $va_placements[$vn_placement_id = (int) $qr_res->get('placement_id')] = $qr_res->getRow();
             $va_placements[$vn_placement_id]['settings'] = $va_settings = caUnserializeForDatabase($qr_res->get('settings'));
             if (!$pb_settings_only) {
                 $t_placement->setSettingDefinitionsForPlacement($va_available_bundles[$vs_bundle_name]['settings']);
                 $va_placements[$vn_placement_id]['display'] = $va_available_bundles[$vs_bundle_name]['display'];
                 $va_placements[$vn_placement_id]['settingsForm'] = $t_placement->getHTMLSettingForm(array('id' => $vs_bundle_name . '_' . $vn_placement_id, 'settings' => $va_settings));
             } else {
                 $va_tmp = explode('.', $vs_bundle_name);
                 $t_instance = $o_dm->getInstanceByTableName($va_tmp[0], true);
                 $va_placements[$vn_placement_id]['display'] = $t_instance ? $t_instance->getDisplayLabel($vs_bundle_name) : "???";
             }
             if ($va_bundle_name[0] == $vs_subject_table) {
                 // Only primary fields are inline-editable
                 // Check if it is one of the types of fields that is inline editable
                 if ($va_bundle_name[1] == 'preferred_labels') {
                     // Preferred labels are inline editable
                     $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                     $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                 } elseif ($t_subject->hasField($va_bundle_name[1])) {
                     //
                     // Intrinsics are editable, except for type_id
                     //
                     if ($va_bundle_name[1] == $t_subject->getTypeFieldName()) {
                         $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                         $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                     } else {
                         if (isset($va_bundle_name[1])) {
                             // check if identifier is editable
                             $id_editable = $t_subject->opo_idno_plugin_instance->isFormatEditable($vs_subject_table);
                             // Do not allow in-line editing if the intrinsic element is identifier and
                             // a). is not editable (editable = 0 in multipart_id_numbering.conf)
                             // b). consists of multiple elements
                             if ($va_bundle_name[1] == $t_subject->getProperty('ID_NUMBERING_ID_FIELD') && $id_editable == false) {
                                 $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                             } else {
                                 $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                             }
                         }
                         switch ($t_subject->getFieldInfo($va_bundle_name[1], 'DISPLAY_TYPE')) {
                             case 'DT_SELECT':
                                 if (($vs_list_code = $t_subject->getFieldInfo($va_bundle_name[1], 'LIST')) || ($vs_list_code = $t_subject->getFieldInfo($va_bundle_name[1], 'LIST_CODE'))) {
                                     $va_placements[$vn_placement_id]['inlineEditingType'] = DT_SELECT;
                                     if (!is_array($va_list_labels = $t_list->getItemsForList($vs_list_code, array('labelsOnly' => true)))) {
                                         $va_list_labels = array();
                                     }
                                     $va_placements[$vn_placement_id]['inlineEditingListValues'] = array_values($va_list_labels);
                                 } else {
                                     $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                                 }
                                 break;
                             default:
                                 $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                                 break;
                         }
                     }
                 } elseif ($t_subject->hasElement($va_bundle_name[1])) {
                     $vn_data_type = ca_metadata_elements::getElementDatatype($va_bundle_name[1]);
                     switch ($vn_data_type) {
                         case 1:
                         case 2:
                         case 5:
                         case 6:
                         case 8:
                         case 9:
                         case 10:
                         case 11:
                         case 12:
                             $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                             $va_placements[$vn_placement_id]['inlineEditingType'] = DT_FIELD;
                             break;
                         case 3:
                             // lists
                             if ($t_element = ca_metadata_elements::getInstance($va_bundle_name[1])) {
                                 switch ($t_element->getSetting('render')) {
                                     case 'select':
                                     case 'yes_no_checkboxes':
                                     case 'radio_buttons':
                                     case 'checklist':
                                         $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                                         $va_placements[$vn_placement_id]['inlineEditingType'] = DT_SELECT;
                                         $va_placements[$vn_placement_id]['inlineEditingListValues'] = array_values($t_list->getItemsForList($t_element->get("list_id"), array('labelsOnly' => true)));
                                         break;
                                     case 'lookup':
                                     case 'horiz_hierbrowser':
                                     case 'horiz_hierbrowser_with_search':
                                     case 'vert_hierbrowser':
                                         $va_placements[$vn_placement_id]['allowInlineEditing'] = true;
                                         $va_placements[$vn_placement_id]['inlineEditingType'] = DT_LOOKUP;
                                         $va_placements[$vn_placement_id]['inlineEditingList'] = $t_element->get('list_id');
                                         break;
                                     default:
                                         // if it's a render setting we don't know about it's not editable
                                         $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                                         $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                                         break;
                                 }
                             }
                             break;
                         default:
                             $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                             $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                             break;
                     }
                 } else {
                     $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                     $va_placements[$vn_placement_id]['inlineEditingType'] = null;
                 }
             } else {
                 // Related bundles are never inline-editable (for now)
                 $va_placements[$vn_placement_id]['allowInlineEditing'] = false;
                 $va_placements[$vn_placement_id]['inlineEditingType'] = null;
             }
         }
     } else {
         if ($pb_return_all_available_if_empty) {
             $va_placements = $this->getAvailableBundles($this->get('table_num'));
         }
     }
     return ca_bundle_displays::$s_placement_list_cache[$vs_cache_key] = $va_placements;
 }
 /**
  *  Returns or Creates a list item or list item id matching the parameters and options provided
  * @param string/int $pm_list_code_or_id
  * @param string $ps_item_idno
  * @param string/int $pn_type_id
  * @param int $pn_locale_id
  * @param null/array $pa_values
  * @param array $pa_options An optional array of options. See DataMigrationUtils::_getID() for a list.
  * @return bool|ca_list_items|mixed|null
  *
  * @see DataMigrationUtils::_getID()
  */
 static function getListItemID($pm_list_code_or_id, $ps_item_idno, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     $pb_output_errors = caGetOption('outputErrors', $pa_options, false);
     $pa_match_on = caGetOption('matchOn', $pa_options, array('label', 'idno'), array('castTo' => "array"));
     $vn_parent_id = caGetOption('parent_id', $pa_values, false);
     $vs_singular_label = isset($pa_values['preferred_labels']['name_singular']) && $pa_values['preferred_labels']['name_singular'] ? $pa_values['preferred_labels']['name_singular'] : '';
     if (!$vs_singular_label) {
         $vs_singular_label = isset($pa_values['name_singular']) && $pa_values['name_singular'] ? $pa_values['name_singular'] : str_replace("_", " ", $ps_item_idno);
     }
     $vs_plural_label = isset($pa_values['preferred_labels']['name_plural']) && $pa_values['preferred_labels']['name_plural'] ? $pa_values['preferred_labels']['name_plural'] : '';
     if (!$vs_plural_label) {
         $vs_plural_label = isset($pa_values['name_plural']) && $pa_values['name_plural'] ? $pa_values['name_plural'] : str_replace("_", " ", $ps_item_idno);
     }
     if (!$vs_singular_label) {
         $vs_singular_label = $vs_plural_label;
     }
     if (!$vs_plural_label) {
         $vs_plural_label = $vs_singular_label;
     }
     if (!$ps_item_idno) {
         $ps_item_idno = $vs_plural_label;
     }
     if (!isset($pa_options['cache'])) {
         $pa_options['cache'] = true;
     }
     // Create cache key
     $vs_cache_key = md5($pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id . '/' . $vs_singular_label . '/' . $vs_plural_label . '/' . json_encode($pa_match_on));
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $ps_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     /** @var KLogger $o_log */
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     if ($pa_options['cache'] && isset(DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key])) {
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_item = new ca_list_items(DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key]);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_item->setTransaction($pa_options['transaction']);
             }
             return $t_item;
         }
         if ($o_event) {
             $o_event->beginItem($ps_event_source, 'ca_list_items', 'U');
             $o_event->endItem(DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key], __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using idno", $ps_item_idno, $pm_list_code_or_id));
         }
         return DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key];
     }
     if (!($vn_list_id = ca_lists::getListID($pm_list_code_or_id))) {
         if ($pb_output_errors) {
             print "[Error] " . _t("Could not find list with list code %1", $pm_list_code_or_id) . "\n";
         }
         if ($o_log) {
             $o_log->logError(_t("Could not find list with list code %1", $pm_list_code_or_id));
         }
         return DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key] = null;
     }
     if (!$vn_parent_id && $vn_parent_id !== false) {
         $vn_parent_id = caGetListRootID($pm_list_code_or_id);
     }
     $t_list = new ca_lists();
     $t_item = new ca_list_items();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_list->setTransaction($pa_options['transaction']);
         $t_item->setTransaction($pa_options['transaction']);
         if ($o_event) {
             $o_event->setTransaction($pa_options['transaction']);
         }
     }
     $vn_item_id = null;
     foreach ($pa_match_on as $vs_match_on) {
         switch (strtolower($vs_match_on)) {
             case 'label':
             case 'labels':
                 if (trim($vs_singular_label) || trim($vs_plural_label)) {
                     $va_criteria = array('preferred_labels' => array('name_singular' => $vs_singular_label), 'list_id' => $vn_list_id);
                     if ($vn_parent_id !== false) {
                         $va_criteria['parent_id'] = $vn_parent_id;
                     }
                     if ($vn_item_id = ca_list_items::find($va_criteria, array('returnAs' => 'firstId', 'purifyWithFallback' => true, 'transaction' => $pa_options['transaction']))) {
                         if ($o_log) {
                             $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using singular label %3", $ps_item_idno, $pm_list_code_or_id, $vs_singular_label));
                         }
                         break 2;
                     } else {
                         $va_criteria['preferred_labels'] = array('name_plural' => $vs_plural_label);
                         if ($vn_item_id = ca_list_items::find($va_criteria, array('returnAs' => 'firstId', 'purifyWithFallback' => true, 'transaction' => $pa_options['transaction']))) {
                             if ($o_log) {
                                 $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using plural label %3", $ps_item_idno, $pm_list_code_or_id, $vs_plural_label));
                             }
                             break 2;
                         }
                     }
                     break;
                 }
             case 'idno':
                 if ($ps_item_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 $va_criteria = array('idno' => $ps_item_idno ? $ps_item_idno : $vs_plural_label, 'list_id' => $vn_list_id);
                 if ($vn_parent_id !== false) {
                     $va_criteria['parent_id'] = $vn_parent_id;
                 }
                 if ($vn_item_id = ca_list_items::find($va_criteria, array('returnAs' => 'firstId', 'purifyWithFallback' => true, 'transaction' => $pa_options['transaction']))) {
                     if ($o_log) {
                         $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using idno with %3", $ps_item_idno, $pm_list_code_or_id, $ps_item_idno));
                     }
                     break 2;
                 }
                 break;
         }
     }
     if ($vn_item_id) {
         DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key] = $vn_item_id;
         if ($o_event) {
             $o_event->beginItem($ps_event_source, 'ca_list_items', 'U');
             $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if (($vb_force_update = caGetOption('forceUpdate', $pa_options, false)) || ($vb_return_instance = caGetOption('returnInstance', $pa_options, false))) {
             $vb_has_attr = false;
             if ($vb_force_update) {
                 foreach ($pa_values as $vs_element => $va_values) {
                     if ($t_item->hasElement($vs_element)) {
                         $vb_has_attr = true;
                         break;
                     }
                 }
             }
             if ($vb_return_instance || $vb_force_update && $vb_has_attr) {
                 $t_item = new ca_list_items($vn_item_id);
                 if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                     $t_item->setTransaction($pa_options['transaction']);
                 }
             }
             $vb_attr_errors = false;
             if ($vb_force_update && $vb_has_attr) {
                 $vb_attr_errors = !DataMigrationUtils::_setAttributes($t_item, $pn_locale_id, $pa_values, $pa_options);
             }
             if ($o_event) {
                 if ($vb_attr_errors) {
                     $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_item->getErrors())));
                 } else {
                     $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
                 }
             }
             if ($vb_return_instance) {
                 return $t_item;
             }
         }
         return $vn_item_id;
     }
     if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
         return false;
     }
     //
     // Need to create list item
     //
     if (!$t_list->load($vn_list_id)) {
         if ($o_log) {
             $o_log->logError(_t("Could not find list with list id %1", $vn_list_id));
         }
         return null;
     }
     if ($o_event) {
         $o_event->beginItem($ps_event_source, 'ca_list_items', 'I');
     }
     if ($t_item = $t_list->addItem($ps_item_idno, $pa_values['is_enabled'], $pa_values['is_default'], $vn_parent_id, $pn_type_id, $ps_item_idno, '', (int) $pa_values['status'], (int) $pa_values['access'], $pa_values['rank'])) {
         $vb_label_errors = false;
         $t_item->addLabel(array('name_singular' => $vs_singular_label, 'name_plural' => $vs_plural_label), $pn_locale_id, null, true);
         if ($t_item->numErrors()) {
             if ($pb_output_errors) {
                 print "[Error] " . _t("Could not set preferred label for list item %1: %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", join('; ', $t_item->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for list item %1: %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", join('; ', $t_item->getErrors())));
             }
             $vb_label_errors = true;
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         $vb_attr_errors = !DataMigrationUtils::_setAttributes($t_item, $pn_locale_id, $pa_values, $pa_options);
         DataMigrationUtils::_setNonPreferredLabels($t_item, $pn_locale_id, $pa_options);
         DataMigrationUtils::_setIdno($t_item, $ps_item_idno, $pa_options);
         $vn_item_id = DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key] = $t_item->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_item->getErrors())));
             } else {
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new list item %1 in list %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", $pm_list_code_or_id));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_item;
         }
         return $vn_item_id;
     } else {
         if ($o_log) {
             $o_log->logError(_t("Could not find add item to list: %1", join("; ", $t_list->getErrors())));
         }
     }
     return null;
 }
Example #4
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 #5
0
 public function insert($pa_options = null)
 {
     if (!$this->inTransaction()) {
         $this->setTransaction(new Transaction());
     }
     if ($this->get('is_default')) {
         $this->getDb()->query("\n\t\t\t\tUPDATE ca_list_items \n\t\t\t\tSET is_default = 0 \n\t\t\t\tWHERE list_id = ?\n\t\t\t", (int) $this->get('list_id'));
     }
     $vn_rc = parent::insert($pa_options);
     if ($this->getPrimaryKey()) {
         $t_list = new ca_lists();
         $o_trans = $this->getTransaction();
         $t_list->setTransaction($o_trans);
         if ($t_list->load($this->get('list_id')) && $t_list->get('list_code') == 'place_hierarchies' && $this->get('parent_id')) {
             // insert root or place hierarchy when creating non-root items in 'place_hierarchies' list
             $t_locale = new ca_locales();
             $va_locales = $this->getAppConfig()->getList('locale_defaults');
             $vn_locale_id = $t_locale->localeCodeToID($va_locales[0]);
             // create root in ca_places
             $t_place = new ca_places();
             $t_place->setTransaction($o_trans);
             $t_place->setMode(ACCESS_WRITE);
             $t_place->set('hierarchy_id', $this->getPrimaryKey());
             $t_place->set('locale_id', $vn_locale_id);
             $t_place->set('type_id', null);
             $t_place->set('parent_id', null);
             $t_place->set('idno', 'Root node for ' . $this->get('idno'));
             $t_place->insert();
             if ($t_place->numErrors()) {
                 $this->delete();
                 $this->errors = array_merge($this->errors, $t_place->errors);
                 return false;
             }
             $t_place->addLabel(array('name' => 'Root node for ' . $this->get('idno')), $vn_locale_id, null, true);
         }
     }
     if ($this->numErrors()) {
         $this->getTransaction()->rollback();
     } else {
         $this->getTransaction()->commit();
         $this->_setSettingsForList();
     }
     return $vn_rc;
 }
 /**
  * Will return plural value of list item unless useSingular option is set to true, in which case singular version of list item label will be used.
  *
  * @param array Optional array of options. Support options are:
  * 			list_id = if set then the numeric item_id value is translated into label text in the current locale. If not set then the numeric item_id is returned.
  *			useSingular = If list_id is set then by default the returned text is the plural label. Setting this option to true will force use of the singular label. [Default is false]
  *			showHierarchy = If true then hierarchical parents of list item will be returned and hierarchical options described below will be used to control the output [Default is false]
  *			returnIdno = If true list item idno is returned rather than preferred label [Default is false]
  *			idsOnly = Return numeric item_id only [Default is false]
  *			HIERARCHICAL OPTIONS: 
  *				direction - For hierarchy specifications (eg. ca_objects.hierarchy) this determines the order in which the hierarchy is returned. ASC will return the hierarchy root first while DESC will return it with the lowest node first. Default is ASC.
  *				top - For hierarchy specifications (eg. ca_objects.hierarchy) this option, if set, will limit the returned hierarchy to the first X nodes from the root down. Default is to not limit.
  *				bottom - For hierarchy specifications (eg. ca_objects.hierarchy) this option, if set, will limit the returned hierarchy to the first X nodes from the lowest node up. Default is to not limit.
  * 				hierarchicalDelimiter - Text to place between items in a hierarchy for a hierarchical specification (eg. ca_objects.hierarchy) when returning as a string
  *				removeFirstItems - If set to a non-zero value, the specified number of items at the top of the hierarchy will be omitted. For example, if set to 2, the root and first child of the hierarchy will be omitted. Default is zero (don't delete anything).
  *				transaction = the transaction to execute database actions within. [Default is null]
  * @return string The value
  */
 public function getDisplayValue($pa_options = null)
 {
     if ($vb_return_idno = isset($pa_options['returnIdno']) && (bool) $pa_options['returnIdno']) {
         return caGetListItemIdno($this->ops_text_value);
     }
     $vb_ids_only = (bool) caGetOption('idsOnly', $pa_options, false);
     if ($vb_ids_only) {
         return (int) $this->ops_text_value;
     }
     $vn_list_id = is_array($pa_options) && isset($pa_options['list_id']) ? (int) $pa_options['list_id'] : null;
     if ($vn_list_id > 0) {
         $t_list = new ca_lists();
         if ($o_trans = caGetOption('transaction', $pa_options, null)) {
             $t_list->setTransaction($o_trans);
         }
         $t_item = new ca_list_items();
         if ($pa_options['showHierarchy'] || $vb_return_idno) {
             if ($o_trans) {
                 $t_item->setTransaction($o_trans);
             }
         }
         $vs_get_spec = isset($pa_options['useSingular']) && $pa_options['useSingular'] ? 'name_singular' : 'name_plural';
         // do we need to get the hierarchy?
         if ($pa_options['showHierarchy']) {
             $t_item->load($this->ops_text_value);
             return $t_item->get('ca_list_items.hierarchy.' . $vs_get_spec, $pa_options);
         }
         return $t_list->getItemFromListForDisplayByItemID($vn_list_id, $this->ops_text_value, isset($pa_options['useSingular']) && $pa_options['useSingular'] ? false : true);
     }
     return $this->ops_text_value;
 }
Example #7
0
 /**
  * Converts the given list of list item idnos or item_ids into an expanded list of numeric item_ids. Processing
  * includes expansion of items to include sub-items and conversion of any idnos to item_ids.
  *
  * @param mixed $pm_table_name_or_num Table name or number to which types apply
  * @param array $pa_types List of item idnos and/or item_ids that are the basis of the list
  * @param array $pa_options Array of options:
  * 		dont_include_sub_items = if set, returned list is not expanded to include sub-items
  *		dontIncludeSubItems = synonym for dont_include_sub_items
  * 		transaction = transaction to perform database operations within. [Default is null]
  *
  * @return array List of numeric item_ids
  */
 public static function getItemIDsFromList($pm_list_name_or_id, $pa_idnos, $pa_options = null)
 {
     if (isset($pa_options['dontIncludeSubItems']) && (!isset($pa_options['dont_include_sub_items']) || !$pa_options['dont_include_sub_items'])) {
         $pa_options['dont_include_sub_items'] = $pa_options['dontIncludeSubItems'];
     }
     if (isset($pa_options['dont_include_sub_items']) && $pa_options['dont_include_sub_items']) {
         $pa_options['noChildren'] = true;
     }
     $t_list = new ca_lists();
     $t_item = new ca_list_items();
     if ($o_trans = caGetOption('transaction', $pa_options, null)) {
         $t_list->setTransaction($o_trans);
         $t_item->setTransaction($o_trans);
     }
     $va_tmp = $va_item_ids = array();
     foreach ($pa_idnos as $vs_idno) {
         $vn_item_id = null;
         if (is_numeric($vs_idno)) {
             $va_tmp = array((int) $vs_idno);
         } else {
             $va_tmp = ca_list_items::find(array('idno' => $vs_idno, 'deleted' => 0), array('returnAs' => 'ids', 'transaction' => $o_trans));
         }
         if (sizeof($va_tmp) && !(isset($pa_options['noChildren']) || $pa_options['noChildren'])) {
             foreach ($va_tmp as $vn_item_id) {
                 if ($qr_children = $t_item->getHierarchy($vn_item_id, array())) {
                     while ($qr_children->nextRow()) {
                         $va_item_ids[$qr_children->get('item_id')] = true;
                     }
                 }
             }
         } else {
             foreach ($va_tmp as $vn_item_id) {
                 $va_item_ids[$vn_item_id] = true;
             }
         }
     }
     return array_keys($va_item_ids);
 }
 /** 
  *
  * @param array $pa_options An optional array of options, which include:
  *				outputErrors - if true, errors will be printed to console [default=false]
  *				dontCreate - if true then new items will not be created [default=false]
  *				matchOnLabel =  if true then list items are looked up exclusively using labels [default=false]
  *				matchOnIdno - try to match on idno if name match fails [default=false]
  *				cache = cache item_ids of previously created/loaded items [default=true]
  *				returnInstance = return ca_occurrences instance rather than occurrence_id. Default is false. 
  *				importEvent = if ca_data_import_events instance is passed then the insert/update of the list item will be logged as part of the import
  *				importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *				log = if KLogger instance is passed then actions will be logged
  */
 static function getListItemID($pm_list_code_or_id, $ps_item_idno, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pb_match_on_label = caGetOption('matchOnLabel', $pa_options, false);
     $pb_match_on_idno = caGetOption('matchOnIdno', $pa_options, false);
     $vn_parent_id = caGetOption('parent_id', $pa_values, null);
     if (!isset($pa_options['cache'])) {
         $pa_options['cache'] = true;
     }
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     if ($pa_options['cache'] && isset(DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id])) {
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return new ca_list_items(DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id]);
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_list_items', 'U');
             $o_event->endItem(DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id], __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using idno", $ps_item_idno, $pm_list_code_or_id));
         }
         return DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id];
     }
     if (!($vn_list_id = ca_lists::getListID($pm_list_code_or_id))) {
         if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
             print "[Error] " . _t("Could not find list with list code %1", $pm_list_code_or_id) . "\n";
         }
         if ($o_log) {
             $o_log->logError(_t("Could not find list with list code %1", $pm_list_code_or_id));
         }
         return DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id] = null;
     }
     $t_list = new ca_lists();
     $t_item = new ca_list_items();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_list->setTransaction($pa_options['transaction']);
         $t_item->setTransaction($pa_options['transaction']);
     }
     $va_find_arr = array('list_id' => $vn_list_id);
     if ($vn_parent_id) {
         $va_find_arr['parent_id'] = $vn_parent_id;
     }
     $vn_item_id = null;
     if ($pb_match_on_label) {
         if (!($vn_item_id = ca_list_items::find(array_merge(array('preferred_labels' => array('name_singular' => $ps_item_idno)), $va_find_arr), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction'])))) {
             $vn_item_id = ca_list_items::find(array_merge(array('preferred_labels' => array('name_plural' => $ps_item_idno)), $va_find_arr), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
         }
         if ($vn_item_id) {
             DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id] = $vn_item_id;
             if ($o_event) {
                 $o_event->beginItem($vs_event_source, 'ca_list_items', 'U');
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
             if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
                 return new ca_list_items($vn_item_id);
             }
             if ($o_log) {
                 $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using label %3 and %4", $ps_item_idno, $pm_list_code_or_id, $vs_label, print_R($va_find_arr, true)));
             }
             return DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id];
         }
     }
     if (!$pb_match_on_label || $pb_match_on_idno) {
         if ($vn_item_id = ca_list_items::find(array_merge(array('idno' => $vs_idno), $va_find_arr), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
             DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id] = $vn_item_id;
             if ($o_event) {
                 $o_event->beginItem($vs_event_source, 'ca_list_items', 'U');
                 $o_event->endItem(DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id], __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
             if ($o_log) {
                 $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using idno with %3", $ps_item_idno, $pm_list_code_or_id, print_R($va_find_arr, true)));
             }
             if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
                 return $t_item;
             }
             return DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id];
         }
     }
     if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
         return false;
     }
     //
     // Need to create list item
     //
     if (!$t_list->load($vn_list_id)) {
         if ($o_log) {
             $o_log->logError(_t("Could not find list with list id %1", $vn_list_id));
         }
         return null;
     }
     if ($o_event) {
         $o_event->beginItem($vs_event_source, 'ca_list_items', 'I');
     }
     if ($t_item = $t_list->addItem($ps_item_idno, $pa_values['is_enabled'], $pa_values['is_default'], $vn_parent_id, $pn_type_id, $ps_item_idno, '', (int) $pa_values['status'], (int) $pa_values['access'], $pa_values['rank'])) {
         $vb_label_errors = false;
         $t_item->addLabel(array('name_singular' => $pa_values['name_singular'] ? $pa_values['name_singular'] : $ps_item_idno, 'name_plural' => $pa_values['name_plural'] ? $pa_values['name_plural'] : $ps_item_idno), $pn_locale_id, null, true);
         if ($t_item->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for list item %1: %2", $pa_values['name_singular'] . "/" . $pa_values['name_plural'] . "/{$ps_item_idno}", join('; ', $t_item->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for list item %1: %2", $pa_values['name_singular'] . "/" . $pa_values['name_plural'] . "/{$ps_item_idno}", join('; ', $t_item->getErrors())));
             }
             $vb_label_errors = true;
         }
         $vn_item_id = DataMigrationUtils::$s_cached_list_item_ids[$pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id] = $t_item->getPrimaryKey();
         if ($o_event) {
             if ($vb_label_errors) {
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting preferred labels: %1", join('; ', $t_item->getErrors())));
             } else {
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new list item %1 in list %2", $pa_values['name_singular'] . "/" . $pa_values['name_plural'] . "/{$ps_item_idno}", $pm_list_code_or_id));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_item;
         }
         return $vn_item_id;
     } else {
         if ($o_log) {
             $o_log->logError(_t("Could not find add litem to list: %1", join("; ", $t_list->getErrors())));
         }
     }
     return null;
 }
 /**
  *  Returns or Creates a list item or list item id matching the parameters and options provided
  * @param string/int $pm_list_code_or_id
  * @param string $ps_item_idno
  * @param string/int $pn_type_id
  * @param int $pn_locale_id
  * @param null/array $pa_values
  * @param array $pa_options An optional array of options, which include:
  *                outputErrors - if true, errors will be printed to console [default=false]
  *                dontCreate - if true then new list items will not be created [default=false]
  *                matchOn = optional list indicating sequence of checks for an existing record; values of array can be "label" and "idno". Ex. array("idno", "label") will first try to match on idno and then label if the first match fails.
  *                cache = cache item_ids of previously created/loaded items [default=true]
  *                returnInstance = return ca_occurrences instance rather than occurrence_id. Default is false.
  *                importEvent = if ca_data_import_events instance is passed then the insert/update of the list item will be logged as part of the import
  *                importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *                nonPreferredLabels = an optional array of nonpreferred labels to add to any newly created list items. Each label in the array is an array with required list item label values.
  *                log = if KLogger instance is passed then actions will be logged
  * @return bool|\ca_list_items|mixed|null
  */
 static function getListItemID($pm_list_code_or_id, $ps_item_idno, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pa_match_on = caGetOption('matchOn', $pa_options, array('label', 'idno'), array('castTo' => "array"));
     $vn_parent_id = caGetOption('parent_id', $pa_values, null);
     $vs_singular_label = isset($pa_values['preferred_labels']['name_singular']) && $pa_values['preferred_labels']['name_singular'] ? $pa_values['preferred_labels']['name_singular'] : '';
     if (!$vs_singular_label) {
         $vs_singular_label = isset($pa_values['name_singular']) && $pa_values['name_singular'] ? $pa_values['name_singular'] : str_replace("_", " ", $ps_item_idno);
     }
     $vs_plural_label = isset($pa_values['preferred_labels']['name_plural']) && $pa_values['preferred_labels']['name_plural'] ? $pa_values['preferred_labels']['name_plural'] : '';
     if (!$vs_plural_label) {
         $vs_plural_label = isset($pa_values['name_plural']) && $pa_values['name_plural'] ? $pa_values['name_plural'] : str_replace("_", " ", $ps_item_idno);
     }
     if (!$vs_singular_label) {
         $vs_singular_label = $vs_plural_label;
     }
     if (!$vs_plural_label) {
         $vs_plural_label = $vs_singular_label;
     }
     if (!$ps_item_idno) {
         $ps_item_idno = $vs_plural_label;
     }
     if (!isset($pa_options['cache'])) {
         $pa_options['cache'] = true;
     }
     // Create a cache key and compress it to save memory
     $vs_cache_key = md5($pm_list_code_or_id . '/' . $ps_item_idno . '/' . $vn_parent_id . '/' . $vs_singular_label . '/' . $vs_plural_label . '/' . json_encode($pa_match_on));
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     /** @var KLogger $o_log */
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     if ($pa_options['cache'] && isset(DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key])) {
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_item = new ca_list_items(DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key]);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_item->setTransaction($pa_options['transaction']);
             }
             return $t_item;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_list_items', 'U');
             $o_event->endItem(DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key], __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using idno", $ps_item_idno, $pm_list_code_or_id));
         }
         return DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key];
     }
     if (!($vn_list_id = ca_lists::getListID($pm_list_code_or_id))) {
         if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
             print "[Error] " . _t("Could not find list with list code %1", $pm_list_code_or_id) . "\n";
         }
         if ($o_log) {
             $o_log->logError(_t("Could not find list with list code %1", $pm_list_code_or_id));
         }
         return DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key] = null;
     }
     if (!$vn_parent_id) {
         $vn_parent_id = caGetListRootID($pm_list_code_or_id);
     }
     $t_list = new ca_lists();
     $t_item = new ca_list_items();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_list->setTransaction($pa_options['transaction']);
         $t_item->setTransaction($pa_options['transaction']);
         if ($o_event) {
             $o_event->setTransaction($pa_options['transaction']);
         }
     }
     $vn_item_id = null;
     foreach ($pa_match_on as $vs_match_on) {
         switch (strtolower($vs_match_on)) {
             case 'label':
             case 'labels':
                 if (trim($vs_singular_label) || trim($vs_plural_label)) {
                     if ($vn_item_id = ca_list_items::find(array('preferred_labels' => array('name_singular' => $vs_singular_label), 'parent_id' => $vn_parent_id, 'list_id' => $vn_list_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                         if ($o_log) {
                             $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using singular label %3", $ps_item_idno, $pm_list_code_or_id, $vs_singular_label));
                         }
                         break 2;
                     } else {
                         if ($vn_item_id = ca_list_items::find(array('preferred_labels' => array('name_plural' => $vs_plural_label), 'parent_id' => $vn_parent_id, 'list_id' => $vn_list_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                             if ($o_log) {
                                 $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using plural label %3", $ps_item_idno, $pm_list_code_or_id, $vs_plural_label));
                             }
                             break 2;
                         }
                     }
                     break;
                 }
             case 'idno':
                 if ($ps_item_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 if ($vn_item_id = ca_list_items::find(array('idno' => $ps_item_idno ? $ps_item_idno : $vs_plural_label, 'list_id' => $vn_list_id, 'parent_id' => $vn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                     if ($o_log) {
                         $o_log->logDebug(_t("Found existing list item %1 (member of list %2) in DataMigrationUtils::getListItemID() using idno with %3", $ps_item_idno, $pm_list_code_or_id, $ps_item_idno));
                     }
                     break 2;
                 }
                 break;
         }
     }
     if ($vn_item_id) {
         DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key] = $vn_item_id;
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_list_items', 'U');
             $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_item = new ca_list_items($vn_item_id);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_item->setTransaction($pa_options['transaction']);
             }
             return $t_item;
         }
         return $vn_item_id;
     }
     if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
         return false;
     }
     //
     // Need to create list item
     //
     if (!$t_list->load($vn_list_id)) {
         if ($o_log) {
             $o_log->logError(_t("Could not find list with list id %1", $vn_list_id));
         }
         return null;
     }
     if ($o_event) {
         $o_event->beginItem($vs_event_source, 'ca_list_items', 'I');
     }
     if ($t_item = $t_list->addItem($ps_item_idno, $pa_values['is_enabled'], $pa_values['is_default'], $vn_parent_id, $pn_type_id, $ps_item_idno, '', (int) $pa_values['status'], (int) $pa_values['access'], $pa_values['rank'])) {
         $vb_label_errors = false;
         $t_item->addLabel(array('name_singular' => $vs_singular_label, 'name_plural' => $vs_plural_label), $pn_locale_id, null, true);
         if ($t_item->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for list item %1: %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", join('; ', $t_item->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for list item %1: %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", join('; ', $t_item->getErrors())));
             }
             $vb_label_errors = true;
         }
         if (is_array($va_nonpreferred_labels = caGetOption("nonPreferredLabels", $pa_options, null))) {
             if (caIsAssociativeArray($va_nonpreferred_labels)) {
                 // single non-preferred label
                 $va_labels = array($va_nonpreferred_labels);
             } else {
                 // list of non-preferred labels
                 $va_labels = $va_nonpreferred_labels;
             }
             foreach ($va_labels as $va_label) {
                 $t_item->addLabel($va_label, $pn_locale_id, null, false);
                 if ($t_item->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not set non-preferred label for list item %1: %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", join('; ', $t_item->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not set non-preferred label for list item %1: %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", join('; ', $t_item->getErrors())));
                     }
                 }
             }
         }
         /** @var IIDNumbering $o_idno */
         if ($o_idno = $t_item->getIDNoPlugInInstance()) {
             $va_values = $o_idno->htmlFormValuesAsArray('idno', $ps_item_idno);
             if (!is_array($va_values)) {
                 $va_values = array($va_values);
             }
             if (!($vs_sep = $o_idno->getSeparator())) {
                 $vs_sep = '';
             }
             if (($vs_proc_idno = join($vs_sep, $va_values)) && $vs_proc_idno != $ps_item_idno) {
                 $t_item->set('idno', $vs_proc_idno);
                 $t_item->update();
                 if ($t_item->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not update idno for %1: %2", $vs_plural_label, join('; ', $t_item->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not idno for %1: %2", $vs_plural_label, join('; ', $t_item->getErrors())));
                     }
                     return null;
                 }
             }
         }
         $vn_item_id = DataMigrationUtils::$s_cached_list_item_ids[$vs_cache_key] = $t_item->getPrimaryKey();
         if ($o_event) {
             if ($vb_label_errors) {
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting preferred labels: %1", join('; ', $t_item->getErrors())));
             } else {
                 $o_event->endItem($vn_item_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new list item %1 in list %2", "{$vs_singular_label}/{$vs_plural_label}/{$ps_item_idno}", $pm_list_code_or_id));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_item;
         }
         return $vn_item_id;
     } else {
         if ($o_log) {
             $o_log->logError(_t("Could not find add item to list: %1", join("; ", $t_list->getErrors())));
         }
     }
     return null;
 }
 /**
  * When returning text will return plural value of list item unless useSingular option is set to true, in which case singular version of list item label will be used.
  *
  * @param array Optional array of options. Support options are:
  * 			list_id = if set then the numeric item_id value is translated into label text in the current locale. If not set then the numeric item_id is returned.
  *			useSingular = If list_id is set then by default the returned text is the plural label. Setting this option to true will force use of the singular label. [Default is false]
  *			showHierarchy = If true then hierarchical parents of list item will be returned and hierarchical options described below will be used to control the output [Default is false]
  *			returnIdno = If true list item idno is returned rather than preferred label [Default is false]
  *			idsOnly = Return numeric item_id only [Default is false]
  *			alwaysReturnItemID = Synonym for idsOnly [Default is false]
  *			output = what value for the list to return. Valid values are text [display text], idno [identifier; same as returnIdno option], value [numeric item_id; same as idsOnly option]. [Default is value]
  *
  *			HIERARCHICAL OPTIONS:
  *				direction - For hierarchy specifications (eg. ca_objects.hierarchy) this determines the order in which the hierarchy is returned. ASC will return the hierarchy root first while DESC will return it with the lowest node first. Default is ASC.
  *				top - For hierarchy specifications (eg. ca_objects.hierarchy) this option, if set, will limit the returned hierarchy to the first X nodes from the root down. Default is to not limit.
  *				bottom - For hierarchy specifications (eg. ca_objects.hierarchy) this option, if set, will limit the returned hierarchy to the first X nodes from the lowest node up. Default is to not limit.
  * 				hierarchicalDelimiter - Text to place between items in a hierarchy for a hierarchical specification (eg. ca_objects.hierarchy) when returning as a string
  *				removeFirstItems - If set to a non-zero value, the specified number of items at the top of the hierarchy will be omitted. For example, if set to 2, the root and first child of the hierarchy will be omitted. Default is zero (don't delete anything).
  *				transaction = the transaction to execute database actions within. [Default is null]
  * @return string The value
  */
 public function getDisplayValue($pa_options = null)
 {
     if (isset($pa_options['output'])) {
         switch (strtolower($pa_options['output'])) {
             case 'idno':
                 $pa_options['returnIdno'] = true;
                 break;
             case 'text':
                 $pa_options['returnIdno'] = false;
                 $pa_options['idsOnly'] = false;
                 break;
             default:
                 $pa_options['idsOnly'] = true;
                 break;
         }
     }
     if ($vb_return_idno = isset($pa_options['returnIdno']) && (bool) $pa_options['returnIdno']) {
         return caGetListItemIdno($this->opn_item_id);
     }
     if (is_null($vb_ids_only = isset($pa_options['idsOnly']) ? (bool) $pa_options['idsOnly'] : null)) {
         $vb_ids_only = isset($pa_options['alwaysReturnItemID']) ? (bool) $pa_options['alwaysReturnItemID'] : false;
     }
     if ($vb_ids_only) {
         return (int) $this->opn_item_id;
     }
     $vn_list_id = is_array($pa_options) && isset($pa_options['list_id']) ? (int) $pa_options['list_id'] : null;
     if ($vn_list_id > 0) {
         $t_list = new ca_lists();
         if ($o_trans = isset($pa_options['transaction']) ? $pa_options['transaction'] : null) {
             $t_list->setTransaction($o_trans);
         }
         $t_item = new ca_list_items();
         if ($pa_options['showHierarchy'] || $vb_return_idno) {
             if ($o_trans) {
                 $t_item->setTransaction($o_trans);
             }
         }
         $vs_get_spec = isset($pa_options['useSingular']) && $pa_options['useSingular'] ? 'preferred_labels.name_singular' : 'preferred_labels.name_plural';
         // do we need to get the hierarchy?
         if ($pa_options['showHierarchy']) {
             $t_item->load((int) $this->opn_item_id);
             return $t_item->get('ca_list_items.hierarchy.' . $vs_get_spec, array_merge(array('removeFirstItems' => 1, 'delimiter' => ' ➔ ', $pa_options)));
         }
         return $t_list->getItemFromListForDisplayByItemID($vn_list_id, $this->opn_item_id, isset($pa_options['useSingular']) && $pa_options['useSingular'] ? false : true);
     }
     return $this->ops_text_value;
 }