/** * 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; }
/** * Save edits from "spreadsheet" (editable results) mode * */ public function saveInlineEdit($pa_options = null) { global $g_ui_locale_id; $pa_changes = $this->request->getParameter("changes", pArray); $vs_resp = array(); $o_dm = Datamodel::load(); if (!is_array($pa_changes) || !sizeof($pa_changes)) { $va_resp['messages'][0] = _t("Nothing to save"); } else { foreach ($pa_changes as $vn_i => $pa_change) { $ps_table = $pa_change['table']; $pa_bundle = explode("-", $ps_bundle = $pa_change['bundle']); $pn_id = (int) $pa_change['id']; $ps_val = $pa_change['value']; if (!($t_instance = $o_dm->getInstanceByTableName($ps_table, true))) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Invalid table: %1', $ps_table)); } else { if (!$t_instance->load($pn_id)) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Invalid id: %1', $pn_id)); } else { if (!$t_instance->isSaveable($this->request)) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } elseif ($pa_bundle[0] == 'preferred_labels') { if ($this->request->user->getBundleAccessLevel($ps_table, $pa_bundle[0]) != __CA_BUNDLE_ACCESS_EDIT__) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } else { $vn_label_id = $t_instance->getPreferredLabelID($g_ui_locale_id); $va_label_values = array(); if (sizeof($pa_bundle) == 1) { // is generic "preferred_labels" $va_label_values[$t_instance->getLabelDisplayField()] = $ps_val; } else { $vs_preferred_label_element = $pa_bundle[1]; $va_label_values[$vs_preferred_label_element] = $ps_val; } if ($vn_label_id) { $t_instance->editLabel($vn_label_id, $va_label_values, $g_ui_locale_id, null, true); // TODO: what about type? } else { $t_instance->addLabel($va_label_values, $g_ui_locale_id, null, true); } if ($t_instance->numErrors()) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Could not set preferred label %1 to %2: %3', $ps_bundle, $ps_val, join("; ", $t_instance->getErrors()))); } else { $va_resp['messages'][$pn_id] = array('message' => _t('Set preferred label %1 to %2', $ps_bundle, $ps_val), 'value' => $ps_val); } } } elseif ($t_instance->hasField($ps_bundle)) { if ($this->request->user->getBundleAccessLevel($ps_table, $ps_bundle) != __CA_BUNDLE_ACCESS_EDIT__) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } else { // is it a list? $t_list = new ca_lists(); $t_instance->setMode(ACCESS_WRITE); if (($vs_list_code = $t_instance->getFieldInfo($ps_bundle, 'LIST')) && ($va_item = $t_list->getItemFromListByLabel($vs_list_code, $ps_val))) { $t_instance->set($ps_bundle, $va_item['item_value']); } elseif (($vs_list_code = $t_instance->getFieldInfo($ps_bundle, 'LIST_CODE')) && ($vn_item_id = $t_list->getItemIDFromListByLabel($vs_list_code, $ps_val))) { $t_instance->set($ps_bundle, $vn_item_id); } else { $t_instance->set($ps_bundle, $ps_val); } $t_instance->update(); if ($t_instance->numErrors()) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Could not set %1 to %2: %3', $ps_bundle, $ps_val, join("; ", $t_instance->getErrors()))); } else { $va_resp['messages'][$pn_id] = array('message' => _t('Set %1 to %2', $ps_bundle, $ps_val), 'value' => $ps_val); } } } elseif ($t_instance->hasElement($ps_bundle)) { $vn_datatype = ca_metadata_elements::getElementDatatype($ps_bundle); // Check if it repeats? if ($vn_count = $t_instance->getAttributeCountByElement($ps_bundle) > 1) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Cannot edit <em>%1</em> here because it has multiple values. Try editing it directly.', mb_strtolower($t_instance->getDisplayLabel("{$ps_table}.{$ps_bundle}")))); } elseif (!in_array($vn_datatype, array(1, 2, 3, 5, 6, 8, 9, 10, 11, 12))) { // Check if it's a supported type? $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Cannot edit <em>%1</em> here. Try editing it directly.', mb_strtolower($t_instance->getDisplayLabel("{$ps_table}.{$ps_bundle}")))); } elseif ($this->request->user->getBundleAccessLevel($ps_table, $ps_bundle) != __CA_BUNDLE_ACCESS_EDIT__) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('You are not allowed to edit this.')); } else { // Do edit $t_instance->setMode(ACCESS_WRITE); $vs_val_proc = null; if ($vn_datatype == 3) { if ($vn_id = ca_list_items::find(array('preferred_labels' => array('name_plural' => $ps_val)), array('returnAs' => 'firstId'))) { $t_instance->replaceAttribute(array('locale_id' => $g_ui_locale_id, $ps_bundle => $vn_id), $ps_bundle); // convert list codes to display text $t_list_item = new ca_list_items((int) $vn_id); if ($t_list_item->getPrimaryKey()) { $vs_val_proc = $t_list_item->get('ca_list_items.preferred_labels.name_plural'); } } } else { $t_instance->replaceAttribute(array('locale_id' => $g_ui_locale_id, $ps_bundle => $ps_val), $ps_bundle); } $t_instance->update(); if (!$vs_val_proc) { $vs_val_proc = $t_instance->get($ps_table . '.' . $ps_bundle); } if ($t_instance->numErrors()) { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Could not set %1 to %2: %3', $ps_bundle, $ps_val, join("; ", $t_instance->getErrors()))); } else { $va_resp['messages'][$pn_id] = array('message' => _t('Set %1 to %2', $ps_bundle, $ps_val), 'value' => $vs_val_proc); } } } else { $va_resp['errors'][$pn_id] = array('error' => 100, 'message' => _t('Invalid bundle: %1', $ps_bundle)); } } } } } $this->view->setVar('results', $va_resp); $this->render('Results/ajax_save_inline_edit_json.php'); }
/** * @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); }
/** * 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; }
/** * @param $t_list ca_lists * @param $po_items SimpleXMLElement * @param $pn_parent_id int * @return bool */ protected function processListItems($t_list, $po_items, $pn_parent_id) { foreach ($po_items->children() as $vo_item) { $vs_item_value = self::getAttribute($vo_item, "value"); $vs_item_idno = self::getAttribute($vo_item, "idno"); $vs_type = self::getAttribute($vo_item, "type"); $vs_status = self::getAttribute($vo_item, "status"); $vs_access = self::getAttribute($vo_item, "access"); $vs_rank = self::getAttribute($vo_item, "rank"); $vn_enabled = self::getAttribute($vo_item, "enabled"); $vn_default = self::getAttribute($vo_item, "default"); if (!isset($vs_item_value) || !strlen(trim($vs_item_value))) { $vs_item_value = $vs_item_idno; } $vn_type_id = null; if ($vs_type) { $vn_type_id = $t_list->getItemIDFromList('list_item_types', $vs_type); } if (!isset($vs_status)) { $vs_status = 0; } if (!isset($vs_access)) { $vs_access = 0; } if (!isset($vs_rank)) { $vs_rank = 0; } $va_find_values = array('list_id' => (int) $t_list->getPrimaryKey(), 'idno' => $vs_item_idno); if ($pn_parent_id) { $va_find_values['parent_id'] = (int) $pn_parent_id; } $t_item = ca_list_items::find($va_find_values, array('returnAs' => 'firstModelInstance')); if ($t_item) { $t_item->set('item_value', $vs_item_value); $t_item->set('is_enabled', $vn_enabled); $t_item->set('is_default', $vn_default); $t_item->set('type_id', $vn_type_id); $t_item->set('status', (int) $vs_status); $t_item->set('access', (int) $vs_access); if (!is_null($vs_rank)) { $t_item->set('rank', (int) $vs_rank); } } else { $t_item = $t_list->addItem($vs_item_value, $vn_enabled, $vn_default, $pn_parent_id, $vn_type_id, $vs_item_idno, '', (int) $vs_status, (int) $vs_access, (int) $vs_rank); } if ($t_list->numErrors()) { $this->addError("There was an error while inserting list item {$vs_item_idno}: " . join(" ", $t_list->getErrors())); return false; } else { $t_item->setMode(ACCESS_WRITE); self::addLabelsFromXMLElement($t_item, $vo_item->labels, $this->opa_locales); if ($t_item->numErrors()) { $this->addError("There was an error while inserting list item label for {$vs_item_idno}: " . join(" ", $t_item->getErrors())); } } if (isset($vo_item->items)) { if (!$this->processListItems($t_list, $vo_item->items, $t_item->getPrimaryKey())) { return false; } } } return true; }
/** * 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; }