Beispiel #1
0
/**
 * Converts the given list of list idnos or item_ids into a list of numeric item_ids
 *
 * @param mixed $pm_list List code or list_id
 * @param array $pa_list_items List of item idnos and/or item_ids 
 * @param array $pa_options Options include:
 *		transaction = transaction to execute queries within. [Default=null]
 *
 * @return array List of numeric item_ids
 */
function caMakeListItemIDList($pm_list, $pa_list_items, $pa_options = null)
{
    if (!($vn_list_id = caGetListID($pm_list))) {
        return array();
    }
    $t_item = new ca_list_items();
    if ($o_trans = caGetOption('transaction', $pa_options, null)) {
        $t_item->setTransaction($o_trans);
    }
    $va_ids = array();
    foreach ($pa_list_items as $vm_item) {
        if (is_numeric($vm_item) && (int) $vm_item > 0) {
            $va_ids[(int) $vm_item] = true;
        } else {
            if ($vn_id = caGetListItemID($vn_list_id, $vm_item)) {
                $va_ids[(int) $vn_id] = true;
            }
        }
    }
    return array_keys($va_ids);
}
 /**
  *  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;
 }
 /**
  * @param mixed $ps_value
  * @param array $pa_element_info
  * @param array $pa_options Options are:
  *		alwaysTreatValueAsIdno = Always try to convert $ps_value to a list idno value, even if it is numeric
  *		matchOn = 
  *
  * @return array
  */
 public function parseValue($ps_value, $pa_element_info, $pa_options = null)
 {
     $vb_treat_value_as_idno = caGetOption('alwaysTreatValueAsIdno', $pa_options, false);
     $va_match_on = caGetOption('matchOn', $pa_options, null);
     if ($va_match_on && !is_array($va_match_on)) {
         $va_match_on = array($va_match_on);
     }
     if (!is_array($va_match_on) && $vb_treat_value_as_idno) {
         $va_match_on = array('idno', 'item_id');
     }
     if ((!is_array($va_match_on) || !sizeof($va_match_on)) && preg_match('![^\\d]+!', $ps_value)) {
         $va_match_on = array('idno', 'item_id');
     }
     if ($vb_treat_value_as_idno && !in_array('idno', $va_match_on)) {
         array_push($va_match_on, 'idno');
     }
     if (!is_array($va_match_on) || !sizeof($va_match_on)) {
         $va_match_on = array('item_id');
     }
     $o_trans = caGetOption('transaction', $pa_options, null);
     $vb_require_value = is_null($pa_element_info['settings']['requireValue']) ? false : (bool) $pa_element_info['settings']['requireValue'];
     $ps_orig_value = $ps_value;
     $vn_id = null;
     $t_item = new ca_list_items();
     if ($o_trans) {
         $t_item->setTransaction($o_trans);
     }
     foreach ($va_match_on as $vs_match_on) {
         switch ($vs_match_on) {
             case 'idno':
                 // try to convert idno to item_id
                 if ($vn_id = caGetListItemID($pa_element_info['list_id'], $ps_value, $pa_options)) {
                     break 2;
                 }
                 break;
             case 'label':
             case 'labels':
                 // try to convert label to item_id
                 if ($vn_id = caGetListItemIDForLabel($pa_element_info['list_id'], $ps_value, $pa_options)) {
                     break 2;
                 }
                 break;
             case 'item_id':
             default:
                 if ($vn_id = ca_list_items::find(array('item_id' => (int) $ps_value, 'list_id' => $pa_element_info['list_id']), array('returnAs' => 'firstId'))) {
                     break 2;
                     //} else {
                     //$this->postError(1970, _t('Value with item_id %1 does not exist in list %2', $ps_value, $pa_element_info['list_id']), 'ListAttributeValue->parseValue()');
                 }
                 break;
         }
     }
     if (!$vb_require_value && !$vn_id) {
         return array('value_longtext1' => null, 'item_id' => null);
     } elseif ($vb_require_value && !$vn_id && !strlen($ps_value)) {
         $this->postError(1970, _t('Value for %1 [%2] cannot be blank', $pa_element_info['displayLabel'], $pa_element_info['element_code']), 'ListAttributeValue->parseValue()');
         return false;
     } elseif ($vb_require_value && !$vn_id) {
         $this->postError(1970, _t('Value %3 for %1 [%2] is invalid', $pa_element_info['displayLabel'], $pa_element_info['element_code'], $ps_value), 'ListAttributeValue->parseValue()');
         return false;
     }
     return array('value_longtext1' => (int) $vn_id, 'item_id' => (int) $vn_id);
 }
Beispiel #4
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;
 }
 /**
  * @param mixed $ps_value
  * @param array $pa_element_info
  * @param array $pa_options Options are:
  *		alwaysTreatValueAsIdno = Always try to convert $ps_value to a list idno value, even if it is numeric
  *
  * @return array
  */
 public function parseValue($ps_value, $pa_element_info, $pa_options = null)
 {
     $vb_treat_value_as_idno = caGetOption('alwaysTreatValueAsIdno', $pa_options, false);
     $vb_require_value = is_null($pa_element_info['settings']['requireValue']) ? true : (bool) $pa_element_info['settings']['requireValue'];
     $ps_orig_value = $ps_value;
     if ($vb_treat_value_as_idno || preg_match('![^\\d]+!', $ps_value)) {
         // try to convert idno to item_id
         if ($vn_id = ca_lists::getItemID($pa_element_info['list_id'], $ps_value, $pa_options)) {
             $ps_value = $vn_id;
         }
     }
     if (!$vb_require_value && !(int) $ps_value) {
         return array('value_longtext1' => null, 'item_id' => null);
     } elseif ($vb_require_value && !(int) $ps_value) {
         $this->postError(1970, _t('Value %1 [%2] cannot be blank', $pa_element_info['displayLabel'], $pa_element_info['element_code']), 'ListAttributeValue->parseValue()');
         return false;
     }
     if (strlen($ps_value) && !is_numeric($ps_value)) {
         $this->postError(1970, _t('Item_id %2 is not valid for element %1', $pa_element_info["element_code"], $ps_value), 'ListAttributeValue->parseValue()');
         return false;
     }
     $t_item = new ca_list_items();
     if ($o_trans = caGetOption('transaction', $pa_options, null)) {
         $t_item->setTransaction($o_trans);
     }
     if (!$t_item->load((int) $ps_value)) {
         if ($ps_value) {
             $this->postError(1970, _t('%1 is not a valid list item_id for %2 [%3]', $ps_value, $pa_element_info['displayLabel'], $pa_element_info['element_code']), 'ListAttributeValue->parseValue()');
         } else {
             if ($vb_require_value) {
                 $this->postError(1970, _t('Value %1 [%2] cannot be blank', $pa_element_info['displayLabel'], $pa_element_info['element_code']), 'ListAttributeValue->parseValue()');
                 return false;
             }
             return null;
         }
         return false;
     }
     if ((int) $t_item->get('list_id') != (int) $pa_element_info['list_id']) {
         $this->postError(1970, _t('Item is not in the correct list for element %1. List id is %2 but should be %3. Value was %4', $pa_element_info["element_code"], $t_item->get('list_id'), $pa_element_info['list_id'], "{$ps_orig_value}/{$ps_value}"), 'ListAttributeValue->parseValue()');
         return false;
     }
     return array('value_longtext1' => $ps_value, 'item_id' => (int) $ps_value);
 }