/** * Fetch item_id for item with specified idno in list * * @param string $ps_list List code or list label * @return int list_id of list or null if no matching list was found */ function caGetListID($ps_list) { $t_list = new ca_lists(); if (is_numeric($ps_list)) { if ($t_list->load((int) $ps_list)) { return $t_list->getPrimaryKey(); } } if ($t_list->load(array('list_code' => $ps_list))) { return $t_list->getPrimaryKey(); } $t_label = new ca_list_labels(); if ($t_label->load(array('name' => $ps_list))) { return $t_label->get('list_id'); } return null; }
function caGetListID($ps_list) { global $g_list_id_cache; if (isset($g_list_id_cache[$ps_list])) { return $g_list_id_cache[$ps_list]; } $t_list = new ca_lists(); if (is_numeric($ps_list)) { if ($t_list->load((int) $ps_list)) { return $g_list_id_cache[$ps_list] = $t_list->getPrimaryKey(); } } if ($t_list->load(array('list_code' => $ps_list))) { return $g_list_id_cache[$ps_list] = $t_list->getPrimaryKey(); } $t_label = new ca_list_labels(); if ($t_label->load(array('name' => $ps_list))) { return $g_list_id_cache[$ps_list] = $t_label->get('list_id'); } return $g_list_id_cache[$ps_list] = null; }
/** * Returns string representing the name of the item the browse will return * * If $ps_mode is 'singular' [default] then the singular version of the name is returned, otherwise the plural is returned */ public function browseName($ps_mode = 'singular') { $vb_type_restriction_has_changed = false; $vn_type_id = $this->opo_result_context->getTypeRestriction($vb_type_restriction_has_changed); $t_list = new ca_lists(); $t_list->load(array('list_code' => 'occurrence_types')); $t_list_item = new ca_list_items(); $t_list_item->load(array('list_id' => $t_list->getPrimaryKey(), 'parent_id' => null)); $va_hier = caExtractValuesByUserLocale($t_list_item->getHierarchyWithLabels()); if (!($vs_name = $ps_mode == 'singular' ? $va_hier[$vn_type_id]['name_singular'] : $va_hier[$vn_type_id]['name_plural'])) { $vs_name = '???'; } return $vs_name; }
function caGetListID($ps_list, $pa_options = null) { global $g_list_id_cache; if (isset($g_list_id_cache[$ps_list])) { return $g_list_id_cache[$ps_list]; } $t_list = new ca_lists(); if ($o_trans = caGetOption('transaction', $pa_options, null)) { $t_list->setTransaction($o_trans); } if (is_numeric($ps_list)) { if ($t_list->load((int) $ps_list)) { return $g_list_id_cache[$ps_list] = $t_list->getPrimaryKey(); } } if ($t_list->load(array('list_code' => $ps_list))) { return $g_list_id_cache[$ps_list] = $t_list->getPrimaryKey(); } $t_label = new ca_list_labels(); if ($t_label->load(array('name' => $ps_list))) { return $g_list_id_cache[$ps_list] = $t_label->get('list_id'); } return $g_list_id_cache[$ps_list] = null; }
/** * Check if currently loaded row is deletable */ public function isDeletable($po_request) { // Is row loaded? if (!$this->getPrimaryKey()) { // this happens when a new list item is about to be created. in those cases we extract the list from the request. $vn_list_id = $this->_getListIDFromRequest($po_request); } else { $vn_list_id = $this->get('list_id'); } if (!$vn_list_id) { return false; } $t_list = new ca_lists($vn_list_id); if (!$t_list->getPrimaryKey()) { return false; } return $t_list->isDeletable($po_request); }
private function fetchAndImport($pa_item_queue, $po_client, $pa_config, $pa_tables, $ps_code) { if (!is_array($pa_tables)) { $pa_tables = array(); } $t_rel_type = new ca_relationship_types(); $vs_base_url = $pa_config['baseUrl']; $o_dm = Datamodel::load(); $t_locale = new ca_locales(); $t_list = new ca_lists(); $vn_source_id = $t_list->getItemIDFromList('object_sources', $pa_config['code']); $pn_rep_type_id = $t_list->getItemIDFromList('object_representation_types', 'front'); foreach ($pa_item_queue as $vn_i => $va_item) { $vs_table = $va_item['table']; $va_import_relationships_from = $pa_config['importRelatedFor'][$va_item['table']]; print "oo"; print_r($va_import_relationships_from); $vn_id = $va_item['id']; if (!$vn_id) { print "[Notice] SKIP CAUSE NO ID ({$ps_code})\n"; continue; } if (isset($this->opa_processed_records[$vs_table . '/' . $vn_id])) { continue; } $vs_idno = trim((string) $va_item['idno']); try { $o_xml = $po_client->getItem($vs_table, $vn_id)->get(); } catch (exception $e) { print "[ERROR] While trying to get item information: " . $e->getMessage() . "\n"; continue; } $o_item = $o_xml->getItem; $t_instance = $o_dm->getInstanceByTableName($vs_table, false); $t_instance_label = $t_instance->getLabelTableInstance(); // Look for existing record $vb_skip = false; $vb_update = false; $vs_label_fld = $t_instance->getLabelDisplayField(); $vs_label = (string) $o_item->preferred_labels->en_US->{$vs_label_fld}; print "[Notice] Processing [{$vs_table}] {$vs_label} [{$vs_idno}] ({$ps_code})\n"; $t_instance_label->clear(); if ($vs_idno && ($vs_table == 'ca_objects' && $t_instance->load(array('idno' => $vs_idno)) || $vs_table != 'ca_objects' && $t_instance->load(array('idno' => $vs_idno)))) { if ($t_instance->hasField('deleted') && $t_instance->get('deleted') == 1) { $t_instance->set('deleted', 0); } //print "[Notice] Update [{$vs_idno}] for {$vs_table} 'cause it already exists ({$ps_code})\n"; if (!$t_instance->getPrimaryKey()) { $vb_skip = true; print "[ERROR] Could not load instance for [{$vs_idno}]\n"; } $vb_update = true; $t_instance->setMode(ACCESS_WRITE); // Clear labels $t_instance->removeAllLabels(); if ($t_instance->numErrors()) { print "[ERROR] Could not remove labels for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } // Clear attributes $t_instance->removeAttributes(null, array('dontCheckMinMax' => true)); if ($t_instance->numErrors()) { print "[ERROR] Could not remove attributes for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } // Clear relationships if (is_array($va_import_relationships_from)) { foreach ($va_import_relationships_from as $vs_rel_table => $va_table_info) { $t_instance->removeRelationships($vs_rel_table); if ($t_instance->numErrors()) { print "[ERROR] Could not remove {$vs_rel_table} relationships for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } } } if ($t_instance->tableName() == 'ca_objects') { //$t_instance->set('source_id', $vn_source_id); } $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not clear record for updating: " . join("; ", $t_instance->getErrors()) . "\n"; } } // create new one if (!$vb_update) { $t_instance->clear(); if ($t_instance->tableName() == 'ca_objects') { //$t_instance->set('source_id', $vn_source_id); } } $t_instance->setMode(ACCESS_WRITE); // add intrinsics switch ($vs_table) { case 'ca_collections': $va_intrinsics = array('status', 'access', 'idno'); break; case 'ca_occurrences': $va_intrinsics = array('status', 'access', 'idno'); break; case 'ca_objects': $va_intrinsics = array('status', 'access', 'idno'); break; case 'ca_entities': $va_intrinsics = array('status', 'access', 'lifespan', 'source_id', 'idno'); break; case 'ca_object_lots': $va_intrinsics = array('status', 'access', 'idno_stub'); break; default: $va_intrinsics = array('status', 'access', 'idno'); break; } // TODO: Need to properly handle foreign-key intrinsics when the item they point to doesn't exist // eg. source_id fields, various ca_objects and ca_object_lots intrinsics, etc. if ($vs_table == 'ca_list_items') { // does list exist? $vs_list_code = (string) $o_item->{'list_code'}; $t_list = new ca_lists(); if (!$t_list->load(array('list_code' => $vs_list_code))) { // create list $t_list->setMode(ACCESS_WRITE); // TODO: should we bother to replicate the is_hierarchical, use_as_vocabulary and default_sort settings via a service? // For now just set reasonable values $t_list->set('list_code', $vs_list_code); $t_list->set('is_hierarchical', 1); $t_list->set('use_as_vocabulary', 1); $t_list->set('default_sort', 0); $t_list->insert(); if ($t_list->numErrors()) { print "[ERROR] Could not insert new list '{$vs_list_code}': " . join('; ', $t_list->getErrors()) . "\n"; } else { $t_list->addLabel(array('name' => $vs_list_code), $pn_locale_id, null, true); if ($t_list->numErrors()) { print "[ERROR] Could not add label to new list '{$vs_list_code}': " . join('; ', $t_list->getErrors()) . "\n"; } } } $t_instance->set('list_id', $t_list->getPrimaryKey()); } foreach ($va_intrinsics as $vs_f) { $t_instance->set($vs_f, $o_item->{$vs_f}); } if (!$vb_update) { $vn_type_id = $t_instance->getTypeIDForCode((string) $o_item->type_id); if (!$vn_type_id) { print "NO TYPE FOR {$vs_table}/" . $o_item->type_id . "\n"; } $t_instance->set('type_id', $vn_type_id); if ($t_instance->tableName() == 'ca_objects') { //$t_instance->set('source_id', $vn_source_id); } // TODO: add hook onBeforeInsert() $t_instance->insert(); // TODO: add hook onInsert() if ($t_instance->numErrors()) { print "[ERROR] Could not insert record: " . join('; ', $t_instance->getErrors()) . "\n"; } } // add attributes // TODO: make this configurable $va_codes = $t_instance->getApplicableElementCodes(); // $va_codes = array( // 'description', // 'georeference', 'geonames', 'internal_notes', // 'oclc_number', 'file_name', // 'digitized_by', 'digitized_date', 'call_number', // 'other_call_number', 'collection_title', 'collection_number', // 'box_number', 'folder_number', 'volume_number', 'page_number', 'shelf', // 'pulled_digitization', 'pulled_name', 'pulled_date', 'returned_digitization', // 'returned_name', 'returned_date', 'needs_redigitization', 'donor', 'copyright_holder', // 'reproduction_restrictions', 'administrative_notes', 'date_view', 'date_item', // 'view_format', 'item_format', 'dimensions', 'map_scale', 'image_description', 'address', // 'lcsh_terms', 'inscription' // // ); foreach ($va_codes as $vs_code) { $t_element = $t_instance->_getElementInstance($vs_code); switch ($t_element->get('datatype')) { case 0: // container $va_elements = $t_element->getElementsInSet(); $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } $va_container_data = array('locale_id' => $vn_locale_id); foreach ($o_values as $o_value) { foreach ($va_elements as $vn_i => $va_element_info) { if ($va_element_info['datatype'] == 0) { continue; } if ($vs_value = trim((string) $o_value->{$va_element_info['element_code']})) { switch ($va_element_info['datatype']) { case 3: //list $va_tmp = explode(":", $vs_value); //<item_id>:<item_idno> //print "CONTAINER LIST CODE=".$va_tmp[1]."/$vs_value/".$va_element_info['list_id']."\n"; $va_container_data[$va_element_info['element_code']] = $t_list->getItemIDFromList($va_element_info['list_id'], $va_tmp[1]); break; default: $va_container_data[$va_element_info['element_code']] = $vs_value; break; } } } $t_instance->replaceAttribute($va_container_data, $vs_code); } } } break; case 3: // list $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } foreach ($o_values as $o_value) { if ($vs_value = trim((string) $o_value->{$vs_code})) { $va_tmp = explode(":", $vs_value); //<item_id>:<item_idno> // TODO: create lists and list items if they don't already exist if ($vn_item_id = $t_list->getItemIDFromList($t_element->get('list_id'), $va_tmp[1])) { $t_instance->replaceAttribute(array($vs_code => $vn_item_id, 'locale_id' => $vn_locale_id), $vs_code); } } } } } break; case 15: // File // File case 16: // Media $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not update record before media: " . join('; ', $t_instance->getErrors()) . "\n"; } // TODO: detect if media has changes and only pull if it has $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } foreach ($o_values as $o_value) { if ($vs_value = trim((string) $o_value->{$vs_code})) { $t_instance->replaceAttribute(array($vs_code => $vs_value, 'locale_id' => $vn_locale_id), $vs_code); } } } } $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not update record after media: " . join('; ', $t_instance->getErrors()) . "\n"; } break; default: $o_attr = $o_item->{'ca_attribute_' . $vs_code}; foreach ($o_attr as $va_tag => $o_tags) { foreach ($o_tags as $vs_locale => $o_values) { if (!($vn_locale_id = $t_locale->localeCodeToID($vs_locale))) { $vn_locale_id = null; } foreach ($o_values as $o_value) { if ($vs_value = trim((string) $o_value->{$vs_code})) { $t_instance->replaceAttribute(array($vs_code => $vs_value, 'locale_id' => $vn_locale_id), $vs_code); } } } } break; } } $t_instance->update(); if ($t_instance->numErrors()) { print "[ERROR] Could not update [1] record: " . join('; ', $t_instance->getErrors()) . "\n"; } // TODO: add hook onBeforeUpdate() $t_instance->update(); // TODO: add hook onUpdate() if ($t_instance->numErrors()) { print "[ERROR] Could not update [2] record: " . join('; ', $t_instance->getErrors()) . "\n"; } // get label fields $va_label_data = array(); foreach ($t_instance->getLabelUIFields() as $vs_field) { if (!($va_label_data[$vs_field] = $o_item->preferred_labels->en_US->{$vs_field})) { $va_label_data[$vs_field] = $o_item->preferred_labels->en_US->{$vs_field}; } } // TODO: add hook onBeforeAddLabel() $t_instance->addLabel($va_label_data, 1, null, true); // TODO: add hook onAddLabel() if ($t_instance->numErrors()) { print "ERROR adding label: " . join('; ', $t_instance->getErrors()) . "\n"; } $this->opa_processed_records[$va_item['table'] . '/' . (int) $va_item['id']] = $t_instance->getPrimaryKey(); if ($vb_skip) { continue; } if (!is_array($va_import_relationships_from)) { continue; } $pa_tables[$va_item['table']] = true; // Are there relationships? $pb_imported_self_relations = false; print_r($va_import_relationships_from); foreach ($va_import_relationships_from as $vs_rel_table => $va_table_info) { $vb_is_self_relation = $vs_rel_table == $t_instance->tableName() && !$pb_imported_self_relations ? true : false; if (!$pa_tables[$vs_rel_table] || $vb_is_self_relation) { // load related records recursively if ($vs_rel_table == $t_instance->tableName()) { $pb_imported_self_relations = true; } if ($o_item->{'related_' . $vs_rel_table}) { $t_rel = $o_dm->getInstanceByTableName($vs_rel_table, false); // TODO: add hook onBeforeAddRelationships() foreach ($o_item->{'related_' . $vs_rel_table} as $vs_tag => $o_related_items) { foreach ($o_related_items as $vs_i => $o_related_item) { if (is_array($pa_config['importRelatedFor'][$va_item['table']][$vs_rel_table])) { $va_rel_types = array_keys($pa_config['importRelatedFor'][$va_item['table']][$vs_rel_table]); if (is_array($va_rel_types) && sizeof($va_rel_types) && !in_array((string) $o_related_item->relationship_type_code, $va_rel_types)) { print "[INFO] Skipped relationship for {$vs_display_name} because type='" . (string) $o_related_item->relationship_type_code . "' is excluded\n"; continue; } } $vs_pk = $t_rel->primaryKey(); $vn_id = (int) $o_related_item->{$vs_pk}; $va_queue = array($vs_rel_table . "/" . $vn_id => array('table' => $vs_rel_table, 'id' => $vn_id, 'idno' => (string) $o_related_item->idno)); // TODO: Add from/until support $this->fetchAndImport($va_queue, $po_client, $pa_config, $pa_tables, $ps_code); $vn_rel_record_id = $this->opa_processed_records[$vs_rel_table . '/' . (int) $vn_id]; $vb_skip = false; if ($vb_is_self_relation) { if ($this->opa_processed_self_relations[$vs_rel_table][$vn_rel_record_id][$t_instance->getPrimaryKey()][(string) $o_related_item->relationship_type_code] || $this->opa_processed_self_relations[$vs_rel_table][$t_instance->getPrimaryKey()][$vn_rel_record_id][(string) $o_related_item->relationship_type_code]) { $vb_skip = true; } else { $this->opa_processed_self_relations[$vs_rel_table][$t_instance->getPrimaryKey()][$vn_rel_record_id][(string) $o_related_item->relationship_type_code] = $this->opa_processed_self_relations[$vs_rel_table][$vn_rel_record_id][$t_instance->getPrimaryKey()][(string) $o_related_item->relationship_type_code] = true; } } if (!$vb_skip) { $t_instance->addRelationship($vs_rel_table, $vn_rel_record_id, (string) $o_related_item->relationship_type_code); if ($t_instance->numErrors()) { print "[ERROR] Could not add relationship to {$vs_rel_table} for row_id={$vn_rel_record_id}: " . join('; ', $t_instance->getErrors()) . "\n"; } } } } // TODO: add hook onAddRelationships() } } } // Is there media? if ($t_instance->tableName() == 'ca_objects') { try { $o_rep_xml = $po_client->getObjectRepresentations((int) $va_item['id'], array('large', 'original'))->get(); } catch (exception $e) { print "[ERROR] While getting object representations: " . $e->getMessage() . "\n"; } $va_existing_reps = $t_instance->getRepresentations(array('large', 'original')); $va_existing_md5s = array(); $va_rep_ids = array(); $va_dupe_reps = array(); foreach ($va_existing_reps as $va_rep) { if ($va_existing_md5s[$va_rep['info']['original']['MD5']] && $va_existing_md5s[$va_rep['info']['large']['MD5']]) { // dupe $va_dupe_reps[] = $va_rep['representation_id']; continue; } $va_existing_md5s[$va_rep['info']['original']['MD5']] = $va_rep['representation_id']; $va_existing_md5s[$va_rep['info']['large']['MD5']] = $va_rep['representation_id']; $va_rep_ids[] = $va_rep['representation_id']; } if ($o_rep_xml->getObjectRepresentations) { foreach ($o_rep_xml->getObjectRepresentations as $vs_x => $o_reps) { foreach ($o_reps as $vs_key => $o_rep) { if ($vs_url = trim((string) $o_rep->urls->large)) { $vs_remote_original_md5 = (string) $o_rep->info->original->MD5; $vs_remote_large_md5 = (string) $o_rep->info->large->MD5; if (isset($va_existing_md5s[$vs_remote_original_md5]) && $va_existing_md5s[$vs_remote_original_md5] || isset($va_existing_md5s[$vs_remote_large_md5]) && $va_existing_md5s[$vs_remote_large_md5]) { print "[NOTICE] Skipping representation at {$vs_url} because it already exists (MD5={$vs_remote_original_md5}/{$vs_remote_large_md5}) ({$ps_code})\n"; if (!($vn_kill_rep_id = $va_existing_md5s[$vs_remote_large_md5])) { $vn_kill_rep_id = $va_existing_md5s[$vs_remote_original_md5]; } foreach ($va_existing_md5s as $vs_md5 => $vn_rep_id) { if ($vn_kill_rep_id == $vn_rep_id) { $t_existing_rep_link = new ca_objects_x_object_representations(); if ($t_existing_rep_link->load(array('object_id' => $t_instance->getPrimaryKey(), 'representation_id' => $vn_rep_id))) { $t_existing_rep_link->setMode(ACCESS_WRITE); // print "update object_id ".$t_instance->getPrimaryKey()."/rep=$vn_rep_id to rank=".$o_rep->rank."/primary=".$o_rep->is_primary."\n"; $t_existing_rep_link->set('is_primary', (int) $o_rep->is_primary); $t_existing_rep_link->set('rank', (int) $o_rep->rank); $t_existing_rep_link->update(); if ($t_existing_rep_link->numErrors()) { print_r($t_existing_rep_link->getErrors()); } } unset($va_existing_md5s[$vs_md5]); } } continue; } print "[Notice] Importing for [{$vs_idno}] media from {$vs_url}: primary=" . (string) $o_rep->is_primary . " ({$ps_code})\n"; print "instance has id=" . $t_instance->getPrimaryKey() . "\n"; // TODO: add hook onBeforeAddMedia() $vn_link_id = $t_instance->addRepresentation($vs_url, $pn_rep_type_id, 1, (int) $o_rep->status, (int) $o_rep->access, (int) $o_rep->is_primary); // TODO: add hook onAddMedia() if ($t_instance->numErrors()) { print "[ERROR] Could not load object representation: " . join("; ", $t_instance->getErrors()) . " ({$ps_code})\n"; } else { $t_link = new ca_objects_x_object_representations($vn_link_id); $t_new_rep = new ca_object_representations($t_link->get('representation_id')); //unlink($x=$t_new_rep->getMediaPath('media', 'original')); } } } } } $va_rep_ids = array(); foreach ($va_existing_md5s as $vs_md5 => $vn_rep_id) { if ($va_rep_ids[$vn_rep_id]) { continue; } $t_obj_x_rep = new ca_objects_x_object_representations(); while ($t_obj_x_rep->load(array('object_id' => $t_instance->getPrimaryKey(), 'representation_id' => $vn_rep_id))) { $t_obj_x_rep->setMode(ACCESS_WRITE); $t_obj_x_rep->delete(true); if ($t_obj_x_rep->numErrors()) { print "[ERROR] Could not load remove object-to-representation link: " . join("; ", $t_obj_x_rep->getErrors()) . " ({$ps_code})\n"; break; } if (!$t_obj_x_rep->load(array('representation_id' => $vn_rep_id))) { $t_rep = new ca_object_representations(); if ($t_rep->load($vn_rep_id)) { $t_rep->setMode(ACCESS_WRITE); $t_rep->delete(true, array('hard' => true)); if ($t_rep->numErrors()) { print "[ERROR] Could not remove representation: " . join("; ", $t_rep->getErrors()) . "\n"; break; } } } } $va_rep_ids[$vn_rep_id] = true; } foreach ($va_dupe_reps as $vn_dupe_rep_id) { $t_rep = new ca_object_representations(); if ($t_rep->load($vn_dupe_rep_id)) { print "[Notice] DELETE DUPE {$vn_dupe_rep_id}\n"; $t_rep->setMode(ACCESS_WRITE); $t_rep->delete(true, array('hard' => true)); if ($t_rep->numErrors()) { print "[ERROR] Could not remove dupe representation: " . join("; ", $t_rep->getErrors()) . "\n"; break; } } } } unset($pa_tables[$va_item['table']]); } }
public function processDisplays() { require_once __CA_MODELS_DIR__ . "/ca_bundle_displays.php"; require_once __CA_MODELS_DIR__ . "/ca_bundle_display_placements.php"; require_once __CA_MODELS_DIR__ . "/ca_bundle_display_type_restrictions.php"; $o_config = Configuration::load(); $vo_dm = Datamodel::load(); if ($this->ops_base_name) { // "merge" profile and its base $va_displays = array(); if ($this->opo_base->displays) { foreach ($this->opo_base->displays->children() as $vo_display) { $va_displays[self::getAttribute($vo_display, "code")] = $vo_display; } } if ($this->opo_profile->displays) { foreach ($this->opo_profile->displays->children() as $vo_display) { $va_displays[self::getAttribute($vo_display, "code")] = $vo_display; } } } else { if ($this->opo_profile->displays) { foreach ($this->opo_profile->displays->children() as $vo_display) { $va_displays[self::getAttribute($vo_display, "code")] = $vo_display; } } } if (!is_array($va_displays) || sizeof($va_displays) == 0) { return true; } foreach ($va_displays as $vo_display) { $vs_display_code = self::getAttribute($vo_display, "code"); $vb_system = self::getAttribute($vo_display, "system"); $vs_table = self::getAttribute($vo_display, "type"); if ($o_config->get($vs_table . '_disable')) { continue; } $t_display = $this->opb_updating ? ca_bundle_displays::find(array('code' => $vs_display_code, 'type' => $vs_table), array('returnAs' => 'firstModelInstance')) : false; $t_display = $t_display ? $t_display : new ca_bundle_displays(); $t_display->setMode(ACCESS_WRITE); $t_display->set("display_code", $vs_display_code); $t_display->set("is_system", $vb_system); $t_display->set("table_num", $vo_dm->getTableNum($vs_table)); $t_display->set("user_id", 1); // let administrative user own these $this->_processSettings($t_display, $vo_display->settings); if ($t_display->getPrimaryKey()) { $t_display->update(); } else { $t_display->insert(); } if ($t_display->numErrors()) { $this->addError("There was an error while inserting display {$vs_display_code}: " . join(" ", $t_display->getErrors())); } else { self::addLabelsFromXMLElement($t_display, $vo_display->labels, $this->opa_locales); if ($t_display->numErrors()) { $this->addError("There was an error while inserting display label for {$vs_display_code}: " . join(" ", $t_display->getErrors())); } if (!$this->processDisplayPlacements($t_display, $vo_display->bundlePlacements, null)) { return false; } } if ($vo_display->typeRestrictions) { foreach ($vo_display->typeRestrictions->children() as $vo_restriction) { $t_list = new ca_lists(); $t_list_item = new ca_list_items(); $vs_restriction_code = trim((string) self::getAttribute($vo_restriction, "code")); $vs_type = trim((string) self::getAttribute($vo_restriction, "type")); $t_instance = $vo_dm->getInstanceByTableNum($vn_table_num = $vo_dm->getTableNum($vs_table)); $vs_type_list_name = $t_instance->getFieldListCode($t_instance->getTypeFieldName()); if ($vs_type) { $t_list->load(array('list_code' => $vs_type_list_name)); $t_list_item->load(array('list_id' => $t_list->getPrimaryKey(), 'idno' => $vs_type)); } $vn_type_id = $vs_type ? $t_list_item->getPrimaryKey() : null; $t_restriction = $this->opb_updating ? ca_bundle_display_type_restrictions::find(array('table_num' => $vn_table_num, 'type_id' => $vn_type_id), array('returnAs' => 'firstModelInstance')) : false; $t_restriction = $t_restriction ? $t_restriction : new ca_bundle_display_type_restrictions(); $t_restriction->setMode(ACCESS_WRITE); $t_restriction->set('table_num', $vn_table_num); $t_restriction->set('include_subtypes', (bool) $vo_restriction->includeSubtypes ? 1 : 0); $t_restriction->set('type_id', $vn_type_id); $t_restriction->set('display_id', $t_display->getPrimaryKey()); $this->_processSettings($t_restriction, $vo_restriction->settings); if ($t_restriction->getPrimaryKey()) { $t_restriction->update(); } else { $t_restriction->insert(); } if ($t_restriction->numErrors()) { $this->addError("There was an error while inserting type restriction {$vs_restriction_code} in display {$vs_display_code}: " . join("; ", $t_restriction->getErrors())); } } } if ($vo_display->userAccess) { $t_user = new ca_users(); $va_display_users = array(); foreach ($vo_display->userAccess->children() as $vo_permission) { $vs_user = trim((string) self::getAttribute($vo_permission, "user")); $vn_access = $this->_convertUserGroupAccessStringToInt(self::getAttribute($vo_permission, 'access')); if ($vn_access && $t_user->load(array('user_name' => $vs_user))) { $va_display_users[$t_user->getUserID()] = $vn_access; } else { $this->addError("User name or access value invalid for display {$vs_display_code} (permission item with user name '{$vs_user}')"); } } if (sizeof($va_display_users) > 0) { $t_display->addUsers($va_display_users); } } if ($vo_display->groupAccess) { $t_group = new ca_user_groups(); $va_display_groups = array(); foreach ($vo_display->groupAccess->children() as $vo_permission) { $vs_group = trim((string) self::getAttribute($vo_permission, "group")); $vn_access = $this->_convertUserGroupAccessStringToInt(self::getAttribute($vo_permission, 'access')); if ($vn_access && $t_group->load(array('code' => $vs_group))) { $va_display_groups[$t_group->getPrimaryKey()] = $vn_access; } else { $this->addError("Group code or access value invalid for display {$vs_display_code} (permission item with group code '{$vs_group}')"); } } if (sizeof($va_display_groups) > 0) { $t_display->addUserGroups($va_display_groups); } } } return true; }
/** * Set setting value * (you must call insert() or update() to write the settings to the database) */ public function setSetting($ps_setting, $pm_value) { if (!$this->isValidSetting($ps_setting)) { return null; } $va_setting_info = $this->getSettingInfo($ps_setting); if ($va_setting_info['displayType'] == DT_CHECKBOXES) { $pm_value = (int) $pm_value; } if (isset($va_setting_info['useRelationshipTypeList']) && $va_setting_info['useRelationshipTypeList'] || isset($va_setting_info['useList']) && $va_setting_info['useList'] || isset($va_setting_info['showLists']) && $va_setting_info['showLists'] || isset($va_setting_info['showVocabularies']) && $va_setting_info['showVocabularies']) { if (!is_array($pm_value)) { $pm_value = array($pm_value); } foreach ($pm_value as $vn_i => $vm_value) { if (trim($vm_value) && !is_numeric($vm_value)) { // need to convert codes to ids if ($vs_t = $va_setting_info['useRelationshipTypeList']) { $t_rel = new ca_relationship_types(); $pm_value[$vn_i] = $t_rel->getRelationshipTypeID($vs_t, $vm_value); } else { if ($vs_l = $va_setting_info['useList']) { // is a list $t_list = new ca_lists(); $pm_value[$vn_i] = $t_list->getItemIDFromList($vs_l, $vm_value); } else { if ($va_setting_info['showLists'] || $va_setting_info['showVocabularies']) { // is a list $t_list = new ca_lists(); $vn_list_id = null; if ($t_list->load(array('list_code' => $vm_value))) { $vn_list_id = $t_list->getPrimaryKey(); } else { if ($t_list->load((int) $vm_value)) { $vn_list_id = $t_list->getPrimaryKey(); } } if ($vn_list_id) { $pm_value[$vn_i] = $vn_list_id; } } else { if ($va_setting_info['showSortableBundlesFor']) { } } } } } } } $va_settings = $this->getSettings(); if ($va_setting_info['formatType'] == FT_NUMBER) { $pm_value = (double) $pm_value; } $va_settings[$ps_setting] = $pm_value; $this->o_instance->set($this->ops_settings_field, $va_settings); return true; }
/** * * * @return string */ function caLoadULAN($ps_path_to_ulan_data = null, $ps_path_to_ulan_config = null, $pa_options = null) { require_once __CA_LIB_DIR__ . '/core/Db.php'; require_once __CA_LIB_DIR__ . '/core/Configuration.php'; require_once __CA_LIB_DIR__ . '/ca/Utils/DataMigrationUtils.php'; require_once __CA_MODELS_DIR__ . '/ca_locales.php'; require_once __CA_MODELS_DIR__ . '/ca_entities.php'; require_once __CA_MODELS_DIR__ . '/ca_entities_x_entities.php'; require_once __CA_MODELS_DIR__ . '/ca_lists.php'; require_once __CA_MODELS_DIR__ . '/ca_list_items.php'; require_once __CA_MODELS_DIR__ . '/ca_list_items_x_list_items.php'; require_once __CA_MODELS_DIR__ . '/ca_relationship_types.php'; $t = new Timer(); $o_log = new KLogger(__CA_APP_DIR__ . '/log', KLogger::INFO); $va_parent_child_links = array(); $va_item_item_links = array(); $va_ulan_id_to_item_id = array(); $o_log->logInfo("Starting import of Getty ULAN"); define('__CA_DONT_DO_SEARCH_INDEXING__', true); $_ = new Zend_Translate('gettext', __CA_APP_DIR__ . '/locale/en_US/messages.mo', 'en_US'); $t_locale = new ca_locales(); $pn_en_locale_id = $t_locale->loadLocaleByCode('en_US'); if (!($o_config = Configuration::load($ps_path_to_ulan_config))) { $o_log->logError("Could not load ULAN import configuration file"); die("ERROR: Could not load ULAN import configuration\n"); } $vs_ulan_import_mode = $o_config->get('ulan_import_target'); $t_list = null; if ($vs_ulan_import_mode == 'ca_entities') { $va_ulan_types = $o_config->getAssoc('ulan_entity_types'); $va_mapping = $o_config->getAssoc('ulan_entity_mapping'); } elseif ($vs_ulan_import_mode == 'ca_list_items') { $va_ulan_types = $o_config->getAssoc('ulan_list_item_types'); if (!($vs_ulan_list_code = $o_config->get('ulan_import_list'))) { $vs_ulan_list_code = 'ULAN'; } // create vocabulary list record (if it doesn't exist already) $t_list = new ca_lists(); if (!$t_list->load(array('list_code' => $vs_ulan_list_code))) { $t_list->setMode(ACCESS_WRITE); $t_list->set('list_code', $vs_ulan_list_code); $t_list->set('is_system_list', 0); $t_list->set('is_hierarchical', 1); $t_list->set('use_as_vocabulary', 1); $t_list->insert(); if ($t_list->numErrors()) { $o_log->logError("Could not create list record for ULAN: " . join('; ', $t_list->getErrors())); die("ERROR: couldn't create ca_list row for ULAN: " . join('; ', $t_list->getErrors()) . "\n"); } $t_list->addLabel(array('name' => 'Union List of Artist Names'), $pn_en_locale_id, null, true); } $vn_list_id = $t_list->getPrimaryKey(); $va_mapping = $o_config->getAssoc('ulan_list_item_mapping'); } else { $o_log->logError("Invalid ULAN import mode {$vs_ulan_import_mode}"); die("ERROR: invalid ULAN import mode {$vs_ulan_import_mode}\n"); } $vn_last_message_length = 0; $vn_term_count = 0; $va_subject = array(); foreach (array('ULAN1.xml', 'ULAN2.xml', 'ULAN3.xml') as $vs_file) { if (!$ps_path_to_ulan_data) { $ps_path_to_ulan_data = "."; } if (!file_exists($ps_path_to_ulan_data . "/{$vs_file}")) { $o_log->logError("Could not find ULAN data file {$vs_file}"); print "[ERROR] cannot find ULAN data.\n"; continue; } $o_log->logInfo("Processing ULAN file {$vs_file}"); print "[Notice] Processing ULAN file {$vs_file}\n"; // load $o_xml = new XMLReader(); $o_xml->open($ps_path_to_ulan_data . '/' . $vs_file); while ($o_xml->read()) { switch ($o_xml->name) { # --------------------------- case 'Subject': if ($o_xml->nodeType == XMLReader::END_ELEMENT) { if (in_array($va_subject['subject_id'], array('500000000', '500000001'))) { break; } // skip top-level root $vs_preferred_term = $va_subject['preferred_term']; $pb_is_enabled = false; switch ($va_subject['record_type']) { case 'Person': default: $vn_type_id = $va_ulan_types['Person']; $pb_is_enabled = true; break; case 'Corporate Body': $vn_type_id = $va_ulan_types['Corporate Body']; $pb_is_enabled = true; break; } print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tIMPORTING #" . ($vn_term_count + 1) . " [" . $va_subject['subject_id'] . "] " . $vs_preferred_term; if (($vn_l = 100 - strlen($vs_message)) < 1) { $vn_l = 1; } $vs_message .= str_repeat(' ', $vn_l); $vn_last_message_length = strlen($vs_message); print $vs_message; if ($vs_ulan_import_mode == 'ca_entities') { $va_np_labels = array(); if (is_array($va_subject['non_preferred_terms'])) { for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) { $va_np_labels[] = DataMigrationUtils::splitEntityName(trim(htmlentities($va_subject['non_preferred_terms'][$vn_i]))); } } $t_item = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName(trim(htmlentities($vs_preferred_term, ENT_NOQUOTES))), $vn_type_id, $pn_en_locale_id, array('idno' => $va_subject['subject_id']), array('nonPreferredLabels' => $va_np_labels, 'returnInstance' => true)); if (!$t_item) { $o_log->logError("Failed to create entity for ULAN artist {$vs_preferred_term}"); break; } $t_item->setMode(ACCESS_WRITE); $va_ulan_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey(); } else { if ($t_item = $t_list->addItem($va_subject['subject_id'], $pb_is_enabled, false, null, $vn_type_id, $va_subject['subject_id'], '', 4, 1)) { $va_ulan_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey(); if ($va_subject['preferred_parent_subject_id'] != 500000000) { $va_parent_child_links[$va_subject['subject_id']] = $va_subject['preferred_parent_subject_id']; } // add preferred labels if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'description' => $va_subject['description']), $pn_en_locale_id, null, true)) { $o_log->logError("Could not add preferred label to ULAN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_item->getErrors())); } // add alternate labels if (is_array($va_subject['non_preferred_terms'])) { for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) { $vs_np_label = $va_subject['non_preferred_terms'][$vn_i]; $vs_np_term_type = $va_subject['non_preferred_term_types'][$vn_i]; switch ($vs_np_term_type) { case 'Used For Term': $vn_np_term_type_id = $vn_list_item_label_type_uf; break; case 'Alternate Descriptor': $vn_np_term_type_id = $vn_list_item_label_type_alt; break; default: $vn_np_term_type_id = null; break; } if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'description' => ''), $pn_en_locale_id, $vn_np_term_type_id, false)) { $o_log->logError("Could not add non-preferred label to ULAN term [" . $va_subject['subject_id'] . "] " . $vs_np_label); } } } } else { $o_log->logError("Could not import ULAN term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_list->getErrors())); break; } } // Map content fields foreach ($va_mapping as $vs_dest => $vs_source) { $va_values = array(); switch ($vs_source) { case 'biography': if (!is_array($va_subject['biographies'])) { break; } foreach ($va_subject['biographies'] as $va_bio) { $va_values[] = $va_bio['text']; } break; case 'biography_dates': if (!is_array($va_subject['biographies'])) { break; } foreach ($va_subject['biographies'] as $va_bio) { if ($va_bio['birth_date'] == 1000 || $va_bio['birth_date'] < -5000) { if ($va_bio['death_date'] >= 2050) { break 2; } else { $va_values[] = "before " . $va_bio['death_date']; } } elseif ($va_bio['death_date'] >= 2050) { $va_values[] = "after " . $va_bio['birth_date']; } else { $va_values[] = $va_bio['birth_date'] . " - " . $va_bio['death_date']; } } break; case 'sex': if (!is_array($va_subject['biographies'])) { break; } foreach ($va_subject['biographies'] as $va_bio) { $va_values[] = $va_bio['sex']; } break; case 'nationality_name': if (!is_array($va_subject['nationalities'])) { break; } foreach ($va_subject['nationalities'] as $va_nationality) { $va_values[] = $va_nationality['name']; } break; case 'nationality_code': if (!is_array($va_subject['nationalities'])) { break; } foreach ($va_subject['nationalities'] as $va_nationality) { $va_values[] = $va_nationality['code']; } break; case 'role_name': if (!is_array($va_subject['roles'])) { break; } foreach ($va_subject['roles'] as $va_role) { $va_values[] = $va_role['name']; } break; case 'role_code': if (!is_array($va_subject['roles'])) { break; } foreach ($va_subject['roles'] as $va_role) { $va_values[] = $va_role['code']; } break; } if (sizeof($va_values)) { $va_dest = explode('.', $vs_dest); $vs_fld = array_pop($va_dest); if ($t_item->hasField($vs_fld)) { $t_item->set($vs_fld, join("\n", $va_values)); } else { foreach ($va_values as $vs_value) { $t_item->addAttribute(array('locale_id' => $pn_en_locale_id, $vs_fld => $vs_value), $vs_fld); } } $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true)); if ($t_item->numErrors()) { $o_log->logError("Could not update ULAN list item with content values: " . join("; ", $t_item->getErrors())); } } } // record item-item relations if (is_array($va_subject['related_subjects'])) { foreach ($va_subject['related_subjects'] as $vs_rel_subject_id) { $va_item_item_links[$va_subject['subject_id']] = $vs_rel_subject_id; } } $vn_term_count++; } else { $va_subject = array('subject_id' => $o_xml->getAttribute('Subject_ID')); } break; # --------------------------- # --------------------------- case 'Biographies': while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Biography': $va_bio = array(); while ($o_xml->read()) { switch ($o_xml->name) { case 'Biography_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['text'] = $o_xml->value; break; } break; case 'Birth_Date': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['birth_date'] = $o_xml->value; if ($va_bio['birth_date'] < 0) { $va_bio['birth_date'] = abs($va_bio['birth_date']) . " BCE"; } break; } break; case 'Death_Date': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['death_date'] = $o_xml->value; if ($va_bio['death_date'] < 0) { $va_bio['death_date'] = abs($va_bio['death_date']) . " BCE"; } break; } break; case 'Sex': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_bio['sex'] = $o_xml->value; break; } break; case 'Preferred_Biography': break 2; } } $va_subject['biographies'][] = $va_bio; break; case 'Biographies': break 2; } } break; # --------------------------- # --------------------------- case 'Nationalities': while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Nationality': $va_nationality = array(); while ($o_xml->read()) { switch ($o_xml->name) { case 'Nationality_Code': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_nationality['code'] = $o_xml->value; $va_nationality['name'] = array_pop(explode('/', $o_xml->value)); break; } break; case 'Preferred_Nationality': break 2; } } $va_subject['nationalities'][] = $va_nationality; break; case 'Nationalities': break 2; } } break; # --------------------------- # --------------------------- case 'Roles': while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Role': $va_role = array(); while ($o_xml->read()) { switch ($o_xml->name) { case 'Role_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_role['code'] = $o_xml->value; $va_role['name'] = array_pop(explode('/', $o_xml->value)); break; } break; case 'Preferred_Role': break 2; } } $va_subject['roles'][] = $va_role; break; case 'Roles': break 2; } } break; # --------------------------- # --------------------------- case 'Record_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['record_type'] = $o_xml->value; break; } break; # --------------------------- # --------------------------- case 'Hierarchy': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['hierarchy'] = $o_xml->value; break; } break; # --------------------------- # --------------------------- case 'Parent_Relationships': $vn_parent_id = $vs_historic_flag = null; while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Parent': while ($o_xml->read()) { switch ($o_xml->name) { case 'Parent_Subject_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $vn_parent_id = $o_xml->value; break; } break; case 'Historic_Flag': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $vs_historic_flag = $o_xml->value; break; } break; case 'Preferred_Parent': $va_subject['preferred_parent_subject_id'] = $vn_parent_id; break 2; } } break; case 'Parent_Relationships': break 2; } } break; # --------------------------- # --------------------------- case 'Preferred_Term': while ($o_xml->read()) { switch ($o_xml->name) { case 'Term_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term_type'] = $o_xml->value; break; } break; case 'Term_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term'] = $o_xml->value; break; } break; case 'Term_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term_id'] = $o_xml->value; break; } break; break; case 'Preferred_Term': break 2; } } break; # --------------------------- # --------------------------- case 'Non-Preferred_Term': while ($o_xml->read()) { switch ($o_xml->name) { case 'Term_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_term_types'][] = $o_xml->value; break; } break; case 'Term_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_terms'][] = $o_xml->value; break; } break; case 'Term_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_term_ids'][] = $o_xml->value; break; } break; case 'Non-Preferred_Term': break 2; } } break; # --------------------------- # --------------------------- case 'VP_Subject_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['related_subjects'][] = $o_xml->value; break; } break; # --------------------------- } } $o_xml->close(); } $o_log->logInfo("Begin linking ULAN terms in hierarchy"); print "\n\nLINKING TERMS IN HIERARCHY...\n"; $vn_last_message_length = 0; $t_list = new ca_lists(); $t_item = new ca_list_items(); $t_item->setMode(ACCESS_WRITE); $vn_list_root_id = $t_list->getRootListItemID($vn_list_id); foreach ($va_parent_child_links as $vs_child_id => $vs_parent_id) { print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tLINKING {$vs_child_id} to parent {$vs_parent_id}"; if (($vn_l = 100 - strlen($vs_message)) < 1) { $vn_l = 1; } $vs_message .= str_repeat(' ', $vn_l); $vn_last_message_length = strlen($vs_message); print $vs_message; if (in_array($vs_parent_id, array('500000000', '500000001'))) { if (!$t_item->load($vn_child_item_id)) { $o_log->logError("Could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})"); continue; } $t_item->set('parent_id', $vn_list_root_id); $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true)); if ($t_item->numErrors()) { $o_log->logError("Could not set parent_id for {$vs_child_id} to root): " . join('; ', $t_item->getErrors())); continue; } $va_ulan_id_to_item_id[$vs_parent_id] = $vn_list_root_id; } if (!($vn_child_item_id = $va_ulan_id_to_item_id[$vs_child_id])) { $o_log->logError("No list item id for child_id {$vs_child_id} (were there previous errors?)"); continue; } if (!($vn_parent_item_id = $va_ulan_id_to_item_id[$vs_parent_id])) { $o_log->logError("No list item id for parent_id {$vs_parent_id} (were there previous errors?)"); continue; } if (!$t_item->load($vn_child_item_id)) { $o_log->logError("Could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})"); continue; } $t_item->set('parent_id', $vn_parent_item_id); $t_item->update(array('dontCheckCircularReferences' => true, 'dontSetHierarchicalIndexing' => true)); if ($t_item->numErrors()) { $o_log->logError("Could not set parent_id for {$vs_child_id} (was translated to item_id={$vn_child_item_id}): " . join('; ', $t_item->getErrors())); } } if ($vn_list_item_relation_type_id_related > 0) { $o_log->logInfo("Begin adding ULAN related term links"); $vn_last_message_length = 0; $t_item = new ca_list_items(); $t_link = new ca_list_items_x_list_items(); $t_link->setMode(ACCESS_WRITE); foreach ($va_item_item_links as $vs_left_id => $vs_right_id) { print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tLINKING {$vs_left_id} to {$vs_right_id}"; if (($vn_l = 100 - strlen($vs_message)) < 1) { $vn_l = 1; } $vs_message .= str_repeat(' ', $vn_l); $vn_last_message_length = strlen($vs_message); print $vs_message; if (!($vn_left_item_id = $va_ulan_id_to_item_id[$vs_left_id])) { $o_log->logError("No list item id for left_id {$vs_left_id} (were there previous errors?)"); continue; } if (!($vn_right_item_id = $va_ulan_id_to_item_id[$vs_right_id])) { $o_log->logError("No list item id for right_id {$vs_right_id} (were there previous errors?)"); continue; } $t_link->set('term_left_id', $vn_left_item_id); $t_link->set('term_right_id', $vn_right_item_id); $t_link->set('type_id', $vn_list_item_relation_type_id_related); $t_link->insert(); if ($t_link->numErrors()) { $o_log->logError("Could not set link between {$vs_left_id} (was translated to item_id={$vn_left_item_id}) and {$vs_right_id} (was translated to item_id={$vn_right_item_id}): " . join('; ', $t_link->getErrors())); } } } else { $o_log->logWarn("Skipped import of term-term relationships because the ca_list_items_x_list_items 'related' relationship type is not defined for your installation"); } $vn_duration = $t->getTime(1); $vs_time = caFormatInterval($vn_duration); $o_log->logInfo("Rebuilding hierarchical indices..."); $t_item->rebuildAllHierarchicalIndexes(); $o_log->logInfo("ULAN import complete. Took {$vs_time} ({$vn_duration})"); print "\n\nIMPORT COMPLETE. Took {$vs_time} ({$vn_duration})\n"; }
public function _genTypeNav($pa_params) { $t_subject = $this->opo_datamodel->getInstanceByTableName($this->ops_tablename, true); $t_list = new ca_lists(); $t_list->load(array('list_code' => $t_subject->getTypeListCode())); $t_list_item = new ca_list_items(); $t_list_item->load(array('list_id' => $t_list->getPrimaryKey(), 'parent_id' => null)); $va_hier = caExtractValuesByUserLocale($t_list_item->getHierarchyWithLabels()); $va_restrict_to_types = null; if ($t_subject->getAppConfig()->get('perform_type_access_checking')) { $va_restrict_to_types = caGetTypeRestrictionsForUser($this->ops_tablename, array('access' => __CA_BUNDLE_ACCESS_READONLY__)); } $va_types = array(); if (is_array($va_hier)) { $va_types_by_parent_id = array(); $vn_root_id = $t_list->getRootItemIDForList($t_subject->getTypeListCode()); // organize items by parent id, exclude root foreach ($va_hier as $vn_item_id => $va_item) { if ($vn_item_id == $vn_root_id) { continue; } // skip root if (is_array($va_restrict_to_types) && !in_array($vn_item_id, $va_restrict_to_types)) { continue; } $va_types_by_parent_id[$va_item['parent_id']][] = $va_item; } foreach ($va_hier as $vn_item_id => $va_item) { if (is_array($va_restrict_to_types) && !in_array($vn_item_id, $va_restrict_to_types)) { continue; } if ($va_item['parent_id'] != $vn_root_id) { continue; } // does this item have sub-items? if (isset($va_item['item_id']) && isset($va_types_by_parent_id[$va_item['item_id']]) && is_array($va_types_by_parent_id[$va_item['item_id']])) { $va_subtypes = $this->_getSubTypes($va_types_by_parent_id[$va_item['item_id']], $va_types_by_parent_id, $va_restrict_to_types); } else { $va_subtypes = array(); } $va_types[] = array('displayName' => $va_item['name_plural'], 'parameters' => array('type_id' => $va_item['item_id']), 'is_enabled' => $va_item['is_enabled'], 'navigation' => $va_subtypes); } } return $va_types; }
// Create list for place types (if it doesn't exist already) $t_place_types = new ca_lists(); if (!$t_place_types->load(array('list_code' => 'tgn_place_types'))) { $t_place_types->setMode(ACCESS_WRITE); $t_place_types->set('list_code', 'tgn_place_types'); $t_place_types->set('is_system_list', 1); $t_place_types->set('is_hierarchical', 1); $t_place_types->set('use_as_vocabulary', 1); $t_place_types->insert(); if ($t_place_types->numErrors()) { print "[Error] couldn't create ca_list row for place types: " . join('; ', $t_place_types->getErrors()) . "\n"; die; } $t_place_types->addLabel(array('name' => 'Getty TGN place types'), $pn_en_locale_id, null, true); } $vn_place_type_list_id = $t_place_types->getPrimaryKey(); // load places $o_xml = new XMLReader(); print "[Notice] READING TGN TERMS...\n"; $vn_last_message_length = 0; $vn_term_count = 0; $t_place = new ca_places(); $t_place->setMode(ACCESS_WRITE); $t_place->logChanges(false); // Don't log changes to records during import – takes time and we don't need the logs if (true) { for ($vn_file_index = 1; $vn_file_index <= 15; $vn_file_index++) { $o_xml->open("tgn_xml_12/TGN{$vn_file_index}.xml"); print "\n[Notice] READING TERMS FROM TGN{$vn_file_index}.xml...\n"; while ($o_xml->read()) { switch ($o_xml->name) {
/** * Converts list specifier (code or list_id) into a list_id * * @param mixed $pm_list_name_or_id List code or list_id * @return int list for the specified list, or null if the list does not exist */ static function getListCode($pm_list_name_or_id) { if (ca_lists::$s_list_code_cache[$pm_list_name_or_id]) { return ca_lists::$s_list_code_cache[$pm_list_name_or_id]; } if (!is_numeric($pm_list_name_or_id)) { return $pm_list_name_or_id; } else { $t_list = new ca_lists(); if (!$t_list->load((int) $pm_list_name_or_id)) { return null; } $vn_list_id = $t_list->getPrimaryKey(); $vs_list_code = $t_list->get('list_code'); } return ca_lists::$s_list_code_cache[$vn_list_id] = $vs_list_code; }
/** * Converts list specifier (code or list_id) into a list_id * * @param mixed $pm_list_name_or_id List code or list_id * @return int list for the specified list, or null if the list does not exist */ static function getListCode($pm_list_name_or_id, $pa_options = null) { if (ca_lists::$s_list_code_cache[$pm_list_name_or_id]) { return ca_lists::$s_list_code_cache[$pm_list_name_or_id]; } if (!is_numeric($pm_list_name_or_id)) { return $pm_list_name_or_id; } else { $t_list = new ca_lists(); $o_trans = caGetOption('transaction', $pa_options, null); if ($o_trans) { $t_list->setTransaction($o_trans); } if (!$t_list->load((int) $pm_list_name_or_id)) { return null; } $vn_list_id = $t_list->getPrimaryKey(); $vs_list_code = $t_list->get('list_code'); } return ca_lists::$s_list_code_cache[$vn_list_id] = $vs_list_code; }
/** * Returns navigation fragment for types and subtypes of a given primary item type (Eg. ca_objects). Used to generate dynamic type menus * from database by AppNavigation class. * * @param array $pa_params Array of parameters used to generate menu * @return array List of types with subtypes ready for inclusion in a menu spec */ public function _genTypeNav($pa_params) { $t_subject = $this->opo_datamodel->getInstanceByTableName($this->ops_table_name, true); $t_list = new ca_lists(); $t_list->load(array('list_code' => $t_subject->getTypeListCode())); $t_list_item = new ca_list_items(); $t_list_item->load(array('list_id' => $t_list->getPrimaryKey(), 'parent_id' => null)); $va_hier = caExtractValuesByUserLocale($t_list_item->getHierarchyWithLabels()); $vn_sort_type = $t_list->get('default_sort'); $va_restrict_to_types = null; if ($t_subject->getAppConfig()->get('perform_type_access_checking')) { $va_restrict_to_types = caGetTypeRestrictionsForUser($this->ops_table_name, array('access' => __CA_BUNDLE_ACCESS_EDIT__)); } $va_types = array(); if (is_array($va_hier)) { $va_types_by_parent_id = array(); $vn_root_id = $t_list->getRootItemIDForList($t_subject->getTypeListCode()); foreach ($va_hier as $vn_item_id => $va_item) { if ($vn_item_id == $vn_root_id) { continue; } // skip root $va_types_by_parent_id[$va_item['parent_id']][] = $va_item; } foreach ($va_hier as $vn_item_id => $va_item) { if (is_array($va_restrict_to_types) && !in_array($vn_item_id, $va_restrict_to_types)) { continue; } if ($va_item['parent_id'] != $vn_root_id) { continue; } // does this item have sub-items? $va_subtypes = array(); if (!(bool) $this->getRequest()->config->get($this->ops_table_name . '_navigation_new_menu_shows_top_level_types_only') && !(bool) $this->getRequest()->config->get($this->ops_table_name . '_enforce_strict_type_hierarchy')) { if (isset($va_item['item_id']) && isset($va_types_by_parent_id[$va_item['item_id']]) && is_array($va_types_by_parent_id[$va_item['item_id']])) { $va_subtypes = $this->_getSubTypes($va_types_by_parent_id[$va_item['item_id']], $va_types_by_parent_id, $vn_sort_type, $va_restrict_to_types); } } switch ($vn_sort_type) { case 0: // label // label default: $vs_key = $va_item['name_singular']; break; case 1: // rank $vs_key = sprintf("%08d", (int) $va_item['rank']); break; case 2: // value $vs_key = $va_item['item_value']; break; case 3: // identifier $vs_key = $va_item['idno_sort']; break; } $va_types[$vs_key][] = array('displayName' => $va_item['name_singular'], 'parameters' => array('type_id' => $va_item['item_id']), 'is_enabled' => $va_item['is_enabled'], 'navigation' => $va_subtypes); } ksort($va_types); } $va_types_proc = array(); foreach ($va_types as $vs_sort_key => $va_items) { foreach ($va_items as $vn_i => $va_item) { $va_types_proc[] = $va_item; } } return $va_types_proc; }
/** * */ private function _getChangeLogFromRawData($pa_data, $pn_table_num, $pa_options = null) { //print "<pre>".print_r($pa_data, true)."</pre>\n"; $va_log_output = array(); $vs_blank_placeholder = '<' . _t('BLANK') . '>'; if (!$pa_options) { $pa_options = array(); } if (sizeof($pa_data)) { // // Init // $o_datamodel = Datamodel::load(); $va_change_types = array('I' => _t('Added'), 'U' => _t('Edited'), 'D' => _t('Deleted')); $vs_label_table_name = $vs_label_display_name = ''; $t_item = $o_datamodel->getInstanceByTableNum($pn_table_num, true); $vs_label_table_name = $vn_label_table_num = $vs_label_display_name = null; if (method_exists($t_item, 'getLabelTableName')) { $t_item_label = $t_item->getLabelTableInstance(); $vs_label_table_name = $t_item->getLabelTableName(); $vn_label_table_num = $t_item_label->tableNum(); $vs_label_display_name = $t_item_label->getProperty('NAME_SINGULAR'); } // // Group data by unit // $va_grouped_data = array(); foreach ($pa_data as $va_log_entry) { $va_grouped_data[$va_log_entry['unit_id']]['ca_table_num_' . $va_log_entry['logged_table_num']][] = $va_log_entry; } // // Process units // $va_attributes = array(); foreach ($va_grouped_data as $vn_unit_id => $va_log_entries_by_table) { foreach ($va_log_entries_by_table as $vs_table_key => $va_log_entries) { foreach ($va_log_entries as $va_log_entry) { $va_changes = array(); if (!is_array($va_log_entry['snapshot'])) { $va_log_entry['snapshot'] = array(); } // // Get date/time stamp for display // $vs_datetime = date("n/d/Y@g:i:sa T", $va_log_entry['log_datetime']); // // Get user name // $vs_user = $va_log_entry['fname'] . ' ' . $va_log_entry['lname']; $vs_email = $va_log_entry['email']; // The "logged" table/row is the row to which the change was actually applied // The "subject" table/row is the row to which the change is considered to have been made for workflow purposes. // // For example: if an entity is related to an object, strictly speaking the logging occurs on the ca_objects_x_entities // row (ca_objects_x_entities is the "logged" table), but the subject is ca_objects since it's only in the context of the // object (and probably the ca_entities row as well) that you can about the change. // $t_obj = $o_datamodel->getInstanceByTableNum($va_log_entry['logged_table_num'], true); // get instance for logged table if (!$t_obj) { continue; } $vs_subject_display_name = '???'; $vn_subject_row_id = null; $vn_subject_table_num = null; if (isset($pa_options['return_item_names']) && $pa_options['return_item_names']) { if (!($vn_subject_table_num = $va_log_entry['subject_table_num'])) { $vn_subject_table_num = $va_log_entry['logged_table_num']; $vn_subject_row_id = $va_log_entry['logged_row_id']; } else { $vn_subject_row_id = $va_log_entry['subject_row_id']; } if ($t_subject = $o_datamodel->getInstanceByTableNum($vn_subject_table_num, true)) { if ($t_subject->load($vn_subject_row_id)) { if (method_exists($t_subject, 'getLabelForDisplay')) { $vs_subject_display_name = $t_subject->getLabelForDisplay(false); } else { if ($vs_idno_field = $t_subject->getProperty('ID_NUMBERING_ID_FIELD')) { $vs_subject_display_name = $t_subject->getProperty('NAME_SINGULAR') . ' [' . $t_subject->get($vs_idno_field) . ']'; } else { $vs_subject_display_name = $t_subject->getProperty('NAME_SINGULAR') . ' [' . $vn_subject_row_id . ']'; } } } } } // // Get item changes // // --------------------------------------------------------------- // is this an intrinsic field? if ($pn_table_num == $va_log_entry['logged_table_num']) { foreach ($va_log_entry['snapshot'] as $vs_field => $vs_value) { $va_field_info = $t_obj->getFieldInfo($vs_field); if (isset($va_field_info['IDENTITY']) && $va_field_info['IDENTITY']) { continue; } if (isset($va_field_info['DISPLAY_TYPE']) && $va_field_info['DISPLAY_TYPE'] == DT_OMIT) { continue; } if (isset($va_field_info['DISPLAY_FIELD']) && is_array($va_field_info['DISPLAY_FIELD']) && ($va_disp_fields = $va_field_info['DISPLAY_FIELD'])) { // // Lookup value in related table // if (!$vs_value) { continue; } if (sizeof($va_disp_fields)) { $va_rel = $o_datamodel->getManyToOneRelations($t_obj->tableName(), $vs_field); $va_rel_values = array(); if ($t_rel_obj = $o_datamodel->getTableInstance($va_rel['one_table'], true)) { $t_rel_obj->load($vs_value); foreach ($va_disp_fields as $vs_display_field) { $va_tmp = explode('.', $vs_display_field); if (($vs_tmp = $t_rel_obj->get($va_tmp[1])) !== '') { $va_rel_values[] = $vs_tmp; } } } $vs_proc_val = join(', ', $va_rel_values); } } else { // Is field a foreign key? $va_keys = $o_datamodel->getManyToOneRelations($t_obj->tableName(), $vs_field); if (sizeof($va_keys)) { // yep, it's a foreign key $va_rel_values = array(); if ($t_rel_obj = $o_datamodel->getTableInstance($va_keys['one_table'], true)) { if ($t_rel_obj->load($vs_value)) { if (method_exists($t_rel_obj, 'getLabelForDisplay')) { $vs_proc_val = $t_rel_obj->getLabelForDisplay(false); } else { $va_disp_fields = $t_rel_obj->getProperty('LIST_FIELDS'); foreach ($va_disp_fields as $vs_display_field) { if (($vs_tmp = $t_rel_obj->get($vs_display_field)) !== '') { $va_rel_values[] = $vs_tmp; } } $vs_proc_val = join(' ', $va_rel_values); } if (!$vs_proc_val) { $vs_proc_val = '???'; } } else { $vs_proc_val = _t("Not set"); } } else { $vs_proc_val = _t('Non-existent'); } } else { // Adjust display of value for different field types switch ($va_field_info['FIELD_TYPE']) { case FT_BIT: $vs_proc_val = $vs_value ? 'Yes' : 'No'; break; default: $vs_proc_val = $vs_value; break; } // Adjust display of value for lists if ($va_field_info['LIST']) { $t_list = new ca_lists(); if ($t_list->load(array('list_code' => $va_field_info['LIST']))) { $vn_list_id = $t_list->getPrimaryKey(); $t_list_item = new ca_list_items(); if ($t_list_item->load(array('list_id' => $vn_list_id, 'item_value' => $vs_value))) { $vs_proc_val = $t_list_item->getLabelForDisplay(); } } } else { if ($va_field_info['BOUNDS_CHOICE_LIST']) { // TODO } } } } $va_changes[] = array('label' => $va_field_info['LABEL'], 'description' => strlen((string) $vs_proc_val) ? $vs_proc_val : $vs_blank_placeholder, 'value' => $vs_value); } } // --------------------------------------------------------------- // is this a label row? if ($va_log_entry['logged_table_num'] == $vn_label_table_num) { foreach ($va_log_entry['snapshot'] as $vs_field => $vs_value) { $va_changes[] = array('label' => $t_item_label->getFieldInfo($vs_field, 'LABEL'), 'description' => $vs_value); } } // --------------------------------------------------------------- // is this an attribute? if ($va_log_entry['logged_table_num'] == 3) { // attribute_values if ($t_element = ca_attributes::getElementInstance($va_log_entry['snapshot']['element_id'])) { if ($o_attr_val = Attribute::getValueInstance($t_element->get('datatype'))) { $o_attr_val->loadValueFromRow($va_log_entry['snapshot']); $vs_attr_val = $o_attr_val->getDisplayValue(); } else { $vs_attr_val = '?'; } // Convert list-based attributes to text if ($vn_list_id = $t_element->get('list_id')) { $t_list = new ca_lists(); $vs_attr_val = $t_list->getItemFromListForDisplayByItemID($vn_list_id, $vs_attr_val, true); } if (!$vs_attr_val) { $vs_attr_val = $vs_blank_placeholder; } $vs_label = $t_element->getLabelForDisplay(); $va_attributes[$va_log_entry['snapshot']['attribute_id']]['values'][] = array('label' => $vs_label, 'value' => $vs_attr_val); $va_changes[] = array('label' => $vs_label, 'description' => $vs_attr_val); } } // --------------------------------------------------------------- // is this a related (many-many) row? $va_keys = $o_datamodel->getOneToManyRelations($t_item->tableName(), $t_obj->tableName()); if (sizeof($va_keys) > 0) { if (method_exists($t_obj, 'getLeftTableNum')) { if ($t_obj->getLeftTableNum() == $t_item->tableNum()) { // other side of rel is on right $t_related_table = $o_datamodel->getInstanceByTableNum($t_obj->getRightTableNum(), true); $t_related_table->load($va_log_entry['snapshot'][$t_obj->getRightTableFieldName()]); } else { // other side of rel is on left $t_related_table = $o_datamodel->getInstanceByTableNum($t_obj->getLeftTableNum(), true); $t_related_table->load($va_log_entry['snapshot'][$t_obj->getLeftTableFieldName()]); } $t_rel = $o_datamodel->getInstanceByTableNum($t_obj->tableNum(), true); $va_changes[] = array('label' => caUcFirstUTF8Safe($t_related_table->getProperty('NAME_SINGULAR')), 'idno' => ($vs_idno_field = $t_related_table->getProperty('ID_NUMBERING_ID_FIELD')) ? $t_related_table->get($vs_idno_field) : null, 'description' => $t_related_table->getLabelForDisplay(), 'table_name' => $t_related_table->tableName(), 'table_num' => $t_related_table->tableNum(), 'row_id' => $t_related_table->getPrimaryKey(), 'rel_type_id' => $va_log_entry['snapshot']['type_id'], 'rel_typename' => $t_rel->getRelationshipTypename('ltor', $va_log_entry['snapshot']['type_id'])); } } // --------------------------------------------------------------- // record log line if (sizeof($va_changes)) { $va_log_output[$vn_unit_id][] = array('datetime' => $vs_datetime, 'user_fullname' => $vs_user, 'user_email' => $vs_email, 'user' => $vs_user . ' (' . $vs_email . ')', 'changetype_display' => $va_change_types[$va_log_entry['changetype']], 'changetype' => $va_log_entry['changetype'], 'changes' => $va_changes, 'subject' => $vs_subject_display_name, 'subject_id' => $vn_subject_row_id, 'subject_table_num' => $vn_subject_table_num, 'logged_table_num' => $va_log_entry['logged_table_num'], 'logged_table' => $t_obj->tableName(), 'logged_row_id' => $va_log_entry['logged_row_id']); } } } } } return $va_log_output; }
/** * */ public function getTypeMenu() { $t_list = new ca_lists(); $t_list->load(array('list_code' => $this->getTypeListCode())); $t_list_item = new ca_list_items(); $t_list_item->load(array('list_id' => $t_list->getPrimaryKey(), 'parent_id' => null)); $va_hierarchy = caExtractValuesByUserLocale($t_list_item->getHierarchyWithLabels()); $va_types = array(); if (is_array($va_hierarchy)) { $va_types_by_parent_id = array(); $vn_root_id = null; foreach ($va_hierarchy as $vn_item_id => $va_item) { if (!$vn_root_id) { $vn_root_id = $va_item['parent_id']; continue; } $va_types_by_parent_id[$va_item['parent_id']][] = $va_item; } foreach ($va_hierarchy as $vn_item_id => $va_item) { if ($va_item['parent_id'] != $vn_root_id) { continue; } // does this item have sub-items? if (isset($va_types_by_parent_id[$va_item['item_id']]) && is_array($va_types_by_parent_id[$va_item['item_id']])) { $va_subtypes = $this->_getSubTypes($va_types_by_parent_id[$va_item['item_id']], $va_types_by_parent_id); } else { $va_subtypes = array(); } $va_types[] = array('displayName' => $va_item['name_singular'], 'parameters' => array('type_id' => $va_item['item_id']), 'navigation' => $va_subtypes); } } return $va_types; }
// create vocabulary list record (if it doesn't exist already) $t_list = new ca_lists(); if (!$t_list->load(array('list_code' => 'aat_nl'))) { $t_list->setMode(ACCESS_WRITE); $t_list->set('list_code', 'aat_nl'); $t_list->set('is_system_list', 0); $t_list->set('is_hierarchical', 1); $t_list->set('use_as_vocabulary', 1); $t_list->insert(); if ($t_list->numErrors()) { print "ERROR: couldn't create ca_list row for AAT: " . join('; ', $t_list->getErrors()) . "\n"; die; } $t_list->addLabel(array('name' => 'Art & Architecture Thesaurus [Nederlands]'), $pn_en_locale_id, null, true); } $vn_list_id = $t_list->getPrimaryKey(); // get list item types (should be defined by base installation profile [base.profile]) // if your installation didn't use a profile inheriting from base.profile then you should make sure // that a list with code='list_item_types' is defined and the following four item codes are defined. // If these are not defined then the AAT will still import, but without any distinction between // terms, facets and guide terms $vn_list_item_type_concept = $t_list->getItemIDFromList('list_item_types', 'concept'); $vn_list_item_type_facet = $t_list->getItemIDFromList('list_item_types', 'facet'); $vn_list_item_type_guide_term = $t_list->getItemIDFromList('list_item_types', 'guide_term'); $vn_list_item_type_hierarchy_name = $t_list->getItemIDFromList('list_item_types', 'hierarchy_name'); // get list item label types (should be defined by base installation profile [base.profile]) // if your installation didn't use a profile inheriting from base.profile then you should make sure // that a list with code='list_item_label_types' is defined and the following four item codes are defined. // If these are not defined then the AAT will still import, but without any distinction between // terms, facets and guide terms $vn_list_item_label_type_uf = $t_list->getItemIDFromList('list_item_label_types', 'uf');
public function getUIsAsDOM() { $t_list = new ca_lists(); $t_rel_type = new ca_relationship_types(); $vo_uis = $this->opo_dom->createElement("userInterfaces"); $qr_uis = $this->opo_db->query("SELECT * FROM ca_editor_uis ORDER BY ui_id"); while ($qr_uis->nextRow()) { $vo_ui = $this->opo_dom->createElement("userInterface"); $t_ui = new ca_editor_uis($qr_uis->get("ui_id")); $vs_type = $this->opo_dm->getTableName($qr_uis->get("editor_type")); if (strlen($vs_code = $qr_uis->get("editor_code")) > 0) { $vo_ui->setAttribute("code", $this->makeIDNO($vs_code)); } else { $vo_ui->setAttribute("code", "standard_{$vs_type}_ui"); } $vo_ui->setAttribute("type", $vs_type); // labels $vo_labels = $this->opo_dom->createElement("labels"); $qr_ui_labels = $this->opo_db->query("SELECT * FROM ca_editor_ui_labels WHERE ui_id=?", $qr_uis->get("ui_id")); if ($qr_ui_labels->numRows() > 0) { while ($qr_ui_labels->nextRow()) { if ($vs_locale = $this->opt_locale->localeIDToCode($qr_ui_labels->get("locale_id"))) { $vo_label = $this->opo_dom->createElement("label"); $vo_label->setAttribute("locale", $vs_locale); $vo_label->appendChild($this->opo_dom->createElement("name", caEscapeForXML($qr_ui_labels->get("name")))); $vo_labels->appendChild($vo_label); } } } else { $vo_label = $this->opo_dom->createElement("label"); $vo_label->setAttribute("locale", "en_US"); $vo_label->appendChild($this->opo_dom->createElement("name", caEscapeForXML($vs_code))); $vo_labels->appendChild($vo_label); } $vo_ui->appendChild($vo_labels); // type restrictions $va_ui_type_restrictions = $t_ui->getTypeRestrictions(); if (sizeof($va_ui_type_restrictions) > 0) { $vo_ui_type_restrictions = $this->opo_dom->createElement("typeRestrictions"); $vo_ui->appendChild($vo_ui_type_restrictions); foreach ($va_ui_type_restrictions as $va_restriction) { $vo_restriction = $this->opo_dom->createElement("restriction"); $vo_ui_type_restrictions->appendChild($vo_restriction); /** @var BaseModelWithAttributes $t_instance */ $t_instance = $this->opo_dm->getInstanceByTableNum($va_restriction["table_num"]); if ($t_instance instanceof BaseRelationshipModel) { $t_rel_type->load($va_restriction["type_id"]); $vo_restriction->setAttribute("type", $t_rel_type->get('type_code')); } else { $vs_type_code = $t_instance->getTypeListCode(); $va_item = $t_list->getItemFromListByItemID($vs_type_code, $va_restriction["type_id"]); $vo_restriction->setAttribute("type", $va_item["idno"]); } } } // User and group access $va_users = $t_ui->getUsers(); if (sizeof($va_users) > 0) { $vo_user_access = $this->opo_dom->createElement("userAccess"); $vo_ui->appendChild($vo_user_access); foreach ($va_users as $va_user_info) { $vo_permission = $this->opo_dom->createElement("permission"); $vo_user_access->appendChild($vo_permission); $vo_permission->setAttribute("user", $va_user_info["user_name"]); $vo_permission->setAttribute("access", $this->_convertUserGroupAccessToString(intval($va_user_info['access']))); } } $va_groups = $t_ui->getUserGroups(); if (sizeof($va_groups) > 0) { $vo_group_access = $this->opo_dom->createElement("groupAccess"); $vo_ui->appendChild($vo_group_access); foreach ($va_groups as $va_group_info) { $vo_permission = $this->opo_dom->createElement("permission"); $vo_group_access->appendChild($vo_permission); $vo_permission->setAttribute("group", $va_group_info["code"]); $vo_permission->setAttribute("access", $this->_convertUserGroupAccessToString(intval($va_group_info['access']))); } } // screens $vo_screens = $this->opo_dom->createElement("screens"); $qr_screens = $this->opo_db->query("SELECT * FROM ca_editor_ui_screens WHERE parent_id IS NOT NULL AND ui_id=? ORDER BY rank,screen_id", $qr_uis->get("ui_id")); while ($qr_screens->nextRow()) { $t_screen = new ca_editor_ui_screens($qr_screens->get("screen_id")); $vo_screen = $this->opo_dom->createElement("screen"); if ($vs_idno = $qr_screens->get("idno")) { $vo_screen->setAttribute("idno", $this->makeIDNO($vs_idno)); } $vo_screen->setAttribute("default", $qr_screens->get("is_default")); $vo_labels = $this->opo_dom->createElement("labels"); $qr_screen_labels = $this->opo_db->query("SELECT * FROM ca_editor_ui_screen_labels WHERE screen_id=?", $qr_screens->get("screen_id")); if ($qr_ui_labels->numRows() > 0) { while ($qr_screen_labels->nextRow()) { if ($vs_locale = $this->opt_locale->localeIDToCode($qr_screen_labels->get("locale_id"))) { $vo_label = $this->opo_dom->createElement("label"); $vo_label->setAttribute("locale", $vs_locale); $vo_label->appendChild($this->opo_dom->createElement("name", caEscapeForXML($qr_screen_labels->get("name")))); if (strlen(trim($qr_screen_labels->get("description"))) > 0) { $vo_label->appendChild($this->opo_dom->createElement("description", caEscapeForXML($qr_screen_labels->get("description")))); } $vo_labels->appendChild($vo_label); } } } else { $vo_label = $this->opo_dom->createElement("label"); $vo_label->setAttribute("locale", "en_US"); $vo_label->appendChild($this->opo_dom->createElement("name", caEscapeForXML($vs_code))); $vo_labels->appendChild($vo_label); } $vo_screen->appendChild($vo_labels); if (is_array($t_screen->getTypeRestrictions()) && sizeof($t_screen->getTypeRestrictions()) > 0) { $vo_type_restrictions = $this->opo_dom->createElement("typeRestrictions"); foreach ($t_screen->getTypeRestrictions() as $va_restriction) { $vo_type_restriction = $this->opo_dom->createElement("restriction"); $t_instance = $this->opo_dm->getInstanceByTableNum($va_restriction["table_num"]); if ($t_instance instanceof BaseRelationshipModel) { $t_rel_type->load($va_restriction["type_id"]); $vo_type_restriction->setAttribute("type", $t_rel_type->get('type_code')); } else { $vs_type_code = $t_instance->getTypeListCode(); $va_item = $t_list->getItemFromListByItemID($vs_type_code, $va_restriction["type_id"]); $vo_type_restriction->setAttribute("type", $va_item["idno"]); } $vo_type_restrictions->appendChild($vo_type_restriction); } $vo_screen->appendChild($vo_type_restrictions); } $vo_placements = $this->opo_dom->createElement("bundlePlacements"); $va_placements = $t_screen->getPlacementsInScreen(); if (is_array($va_placements)) { foreach ($va_placements as $va_placement) { $vo_placement = $this->opo_dom->createElement("placement"); $vo_placements->appendChild($vo_placement); $vo_placement->setAttribute("code", $this->makeIDNO($va_placement["placement_code"])); $vo_placement->appendChild($this->opo_dom->createElement("bundle", caEscapeForXML($va_placement["bundle"]))); if (is_array($va_placement["settings"])) { $vo_settings = $this->opo_dom->createElement("settings"); foreach ($va_placement["settings"] as $vs_setting => $va_values) { if (is_null($va_values)) { continue; } if (!is_array($va_values)) { $va_values = array($va_values); } // account for legacy settings if ($vs_setting == "restrict_to_type") { $vs_setting = "restrict_to_types"; } foreach ($va_values as $vs_key => $vs_value) { switch ($vs_setting) { case 'restrict_to_types': $t_item = new ca_list_items($vs_value); if ($t_item->getPrimaryKey()) { $vs_value = $t_item->get('idno'); } break; case 'restrict_to_lists': $t_list = new ca_lists($vs_value); if ($t_list->getPrimaryKey()) { $vs_value = $t_list->get('list_code'); } break; case 'restrict_to_relationship_types': $t_rel_type = new ca_relationship_types($vs_value); if ($t_rel_type->getPrimaryKey()) { $vs_value = $t_rel_type->get('type_code'); } break; } if (strlen($vs_value) > 0) { // caEscapeForXML mangles zero values for some reason -> catch them here. if ($vs_value === 0 || $vs_value === "0") { $vs_setting_val = $vs_value; } else { $vs_setting_val = caEscapeForXML($vs_value); } $vo_setting = @$this->opo_dom->createElement("setting", $vs_setting_val); $vo_setting->setAttribute("name", $vs_setting); if ($vs_setting == "label" || $vs_setting == "add_label" || $vs_setting == "description") { if (preg_match("/^[a-z]{2,3}\\_[A-Z]{2,3}\$/", $vs_key)) { $vo_setting->setAttribute("locale", $vs_key); } else { continue; } } $vo_settings->appendChild($vo_setting); } } } $vo_placement->appendChild($vo_settings); } } } $vo_screen->appendChild($vo_placements); $vo_screens->appendChild($vo_screen); } $vo_ui->appendChild($vo_screens); $vo_uis->appendChild($vo_ui); } return $vo_uis; }
/** * * * @return string */ function caLoadAAT($ps_path_to_aat_data = null, $pa_options = null) { if (!$ps_path_to_aat_data) { $ps_path_to_aat_data = "./AAT.xml"; } if (!file_exists($ps_path_to_aat_data)) { die("ERROR: cannot find AAT data.\n"); } require_once __CA_LIB_DIR__ . '/core/Db.php'; require_once __CA_MODELS_DIR__ . '/ca_locales.php'; require_once __CA_MODELS_DIR__ . '/ca_lists.php'; require_once __CA_MODELS_DIR__ . '/ca_list_items.php'; require_once __CA_MODELS_DIR__ . '/ca_list_items_x_list_items.php'; require_once __CA_MODELS_DIR__ . '/ca_relationship_types.php'; $_ = new Zend_Translate('gettext', __CA_APP_DIR__ . '/locale/en_US/messages.mo', 'en_US'); $t_locale = new ca_locales(); $pn_en_locale_id = $t_locale->loadLocaleByCode('en_US'); // create vocabulary list record (if it doesn't exist already) $t_list = new ca_lists(); if (!$t_list->load(array('list_code' => 'aat'))) { $t_list->setMode(ACCESS_WRITE); $t_list->set('list_code', 'aat'); $t_list->set('is_system_list', 0); $t_list->set('is_hierarchical', 1); $t_list->set('use_as_vocabulary', 1); $t_list->insert(); if ($t_list->numErrors()) { print "ERROR: couldn't create ca_list row for AAT: " . join('; ', $t_list->getErrors()) . "\n"; die; } $t_list->addLabel(array('name' => 'Art & Architecture Thesaurus'), $pn_en_locale_id, null, true); } $vn_list_id = $t_list->getPrimaryKey(); // get list item types (should be defined by base installation profile [base.profile]) // if your installation didn't use a profile inheriting from base.profile then you should make sure // that a list with code='list_item_types' is defined and the following four item codes are defined. // If these are not defined then the AAT will still import, but without any distinction between // terms, facets and guide terms $vn_list_item_type_concept = $t_list->getItemIDFromList('list_item_types', 'concept'); $vn_list_item_type_facet = $t_list->getItemIDFromList('list_item_types', 'facet'); $vn_list_item_type_guide_term = $t_list->getItemIDFromList('list_item_types', 'guide_term'); $vn_list_item_type_hierarchy_name = $t_list->getItemIDFromList('list_item_types', 'hierarchy_name'); // get list item label types (should be defined by base installation profile [base.profile]) // if your installation didn't use a profile inheriting from base.profile then you should make sure // that a list with code='list_item_label_types' is defined and the following four item codes are defined. // If these are not defined then the AAT will still import, but without any distinction between // terms, facets and guide terms $vn_list_item_label_type_uf = $t_list->getItemIDFromList('list_item_label_types', 'uf'); $vn_list_item_label_type_alt = $t_list->getItemIDFromList('list_item_label_types', 'alt'); // get list item-to-item relationship type (should be defined by base installation profile [base.profile]) // if your installation didn't use a profile inheriting from base.profile then you should make sure // that a ca_list_items_x_list_items relationship type with code='related' is defined. Otherwise import of term-to-term // relationships will fail. $t_rel_types = new ca_relationship_types(); $vn_list_item_relation_type_id_related = $t_rel_types->getRelationshipTypeID('ca_list_items_x_list_items', 'related'); // load voc_terms $o_xml = new XMLReader(); $o_xml->open($ps_path_to_aat_data); print "READING AAT TERMS...\n"; $va_parent_child_links = array(); $va_item_item_links = array(); $va_aat_id_to_item_id = array(); $vn_last_message_length = 0; $va_subject = array(); $vn_term_count = 0; while ($o_xml->read()) { switch ($o_xml->name) { # --------------------------- case 'Subject': if ($o_xml->nodeType == XMLReader::END_ELEMENT) { if ($va_subject['subject_id'] == '300000000') { break; } // skip top-level root $vs_preferred_term = $va_subject['preferred_term']; switch ($va_subject['record_type']) { case 'Concept': $vn_type_id = $vn_list_item_type_hierarchy_name; $pb_is_enabled = true; break; case 'Facet': $vn_type_id = $vn_list_item_type_facet; $vs_preferred_term = '<' . $vs_preferred_term . '>'; $pb_is_enabled = false; break; case 'Guide Term': $vn_type_id = $vn_list_item_type_guide_term; $vs_preferred_term = '<' . $vs_preferred_term . '>'; $pb_is_enabled = false; break; case 'Hierarchy Name': $vn_type_id = $vn_list_item_type_hierarchy_name; $pb_is_enabled = false; break; default: $vn_type_id = null; $pb_is_enabled = true; break; } print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tIMPORTING #" . ($vn_term_count + 1) . " [" . $va_subject['subject_id'] . "] " . $vs_preferred_term; if (($vn_l = 100 - strlen($vs_message)) < 1) { $vn_l = 1; } $vs_message .= str_repeat(' ', $vn_l); $vn_last_message_length = strlen($vs_message); print $vs_message; if ($t_item = $t_list->addItem($va_subject['subject_id'], $pb_is_enabled, false, null, $vn_type_id, $va_subject['subject_id'], '', 4, 1)) { $va_aat_id_to_item_id[$va_subject['subject_id']] = $t_item->getPrimaryKey(); if ($va_subject['preferred_parent_subject_id'] != 300000000) { $va_parent_child_links[$va_subject['subject_id']] = $va_subject['preferred_parent_subject_id']; } // add preferred labels if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_preferred_term, ENT_NOQUOTES)), 'description' => $va_subject['description']), $pn_en_locale_id, null, true)) { print "ERROR: Could not add preferred label to AAT term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_item->getErrors()) . "\n"; } // add alternate labels if (is_array($va_subject['non_preferred_terms'])) { for ($vn_i = 0; $vn_i < sizeof($va_subject['non_preferred_terms']); $vn_i++) { $vs_np_label = $va_subject['non_preferred_terms'][$vn_i]; $vs_np_term_type = $va_subject['non_preferred_term_types'][$vn_i]; switch ($vs_np_term_type) { case 'Used For Term': $vn_np_term_type_id = $vn_list_item_label_type_uf; break; case 'Alternate Descriptor': $vn_np_term_type_id = $vn_list_item_label_type_alt; break; default: $vn_np_term_type_id = null; break; } if (!$t_item->addLabel(array('name_singular' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'name_plural' => trim(htmlentities($vs_np_label, ENT_NOQUOTES)), 'description' => ''), $pn_en_locale_id, $vn_np_term_type_id, false)) { print "ERROR: Could not add non-preferred label to AAT term [" . $va_subject['subject_id'] . "] " . $vs_np_label . "\n"; //: ".join("; ", $t_item->getErrors())."\n"; } } } // record item-item relations if (is_array($va_subject['related_subjects'])) { foreach ($va_subject['related_subjects'] as $vs_rel_subject_id) { $va_item_item_links[$va_subject['subject_id']] = $vs_rel_subject_id; } } $vn_term_count++; } else { print "ERROR: Could not import AAT term [" . $va_subject['subject_id'] . "] " . $vs_preferred_term . ": " . join("; ", $t_list->getErrors()) . "\n"; } } else { $va_subject = array('subject_id' => $o_xml->getAttribute('Subject_ID')); } break; # --------------------------- # --------------------------- case 'Descriptive_Note': while ($o_xml->read()) { switch ($o_xml->name) { case 'Note_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['description'] = $o_xml->value; break; } break; case 'Descriptive_Note': break 2; } } break; # --------------------------- # --------------------------- case 'Record_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['record_type'] = $o_xml->value; break; } break; # --------------------------- # --------------------------- case 'Facet_Code': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['facet_code'] = $o_xml->value; break; } break; # --------------------------- # --------------------------- case 'Parent_Relationships': $vn_parent_id = $vs_historic_flag = null; while ($o_xml->read()) { switch ($o_xml->name) { case 'Preferred_Parent': while ($o_xml->read()) { switch ($o_xml->name) { case 'Parent_Subject_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $vn_parent_id = $o_xml->value; break; } break; case 'Historic_Flag': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $vs_historic_flag = $o_xml->value; break; } break; case 'Preferred_Parent': $va_subject['preferred_parent_subject_id'] = $vn_parent_id; break 2; } } break; case 'Parent_Relationships': break 2; } } break; # --------------------------- # --------------------------- case 'Preferred_Term': while ($o_xml->read()) { switch ($o_xml->name) { case 'Term_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term_type'] = $o_xml->value; break; } break; case 'Term_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term'] = $o_xml->value; break; } break; case 'Term_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['preferred_term_id'] = $o_xml->value; break; } break; break; case 'Preferred_Term': break 2; } } break; # --------------------------- # --------------------------- case 'Non-Preferred_Term': while ($o_xml->read()) { switch ($o_xml->name) { case 'Term_Type': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_term_types'][] = $o_xml->value; break; } break; case 'Term_Text': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_terms'][] = $o_xml->value; break; } break; case 'Term_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['non_preferred_term_ids'][] = $o_xml->value; break; } break; case 'Non-Preferred_Term': break 2; } } break; # --------------------------- # --------------------------- case 'VP_Subject_ID': switch ($o_xml->nodeType) { case XMLReader::ELEMENT: $o_xml->read(); $va_subject['related_subjects'][] = $o_xml->value; break; } break; # --------------------------- } } $o_xml->close(); print "\n\nLINKING TERMS IN HIERARCHY...\n"; $vn_last_message_length = 0; $t_item = new ca_list_items(); $t_item->setMode(ACCESS_WRITE); foreach ($va_parent_child_links as $vs_child_id => $vs_parent_id) { print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tLINKING {$vs_child_id} to parent {$vs_parent_id}"; if (($vn_l = 100 - strlen($vs_message)) < 1) { $vn_l = 1; } $vs_message .= str_repeat(' ', $vn_l); $vn_last_message_length = strlen($vs_message); print $vs_message; if (!($vn_child_item_id = $va_aat_id_to_item_id[$vs_child_id])) { print "ERROR: no list item id for child_id {$vs_child_id} (were there previous errors?)\n"; continue; } if (!($vn_parent_item_id = $va_aat_id_to_item_id[$vs_parent_id])) { print "ERROR: no list item id for parent_id {$vs_child_id} (were there previous errors?)\n"; continue; } if (!$t_item->load($vn_child_item_id)) { print "ERROR: could not load item for {$vs_child_id} (was translated to item_id={$vn_child_item_id})\n"; continue; } $t_item->set('parent_id', $vn_parent_item_id); $t_item->update(); if ($t_item->numErrors()) { print "ERROR: could not set parent_id for {$vs_child_id} (was translated to item_id={$vn_child_item_id}): " . join('; ', $t_item->getErrors()) . "\n"; } } if ($vn_list_item_relation_type_id_related > 0) { print "\n\nADDING RELATED TERM LINKS...\n"; $vn_last_message_length = 0; $t_item = new ca_list_items(); $t_link = new ca_list_items_x_list_items(); $t_link->setMode(ACCESS_WRITE); foreach ($va_item_item_links as $vs_left_id => $vs_right_id) { print str_repeat(chr(8), $vn_last_message_length); $vs_message = "\tLINKING {$vs_left_id} to {$vs_right_id}"; if (($vn_l = 100 - strlen($vs_message)) < 1) { $vn_l = 1; } $vs_message .= str_repeat(' ', $vn_l); $vn_last_message_length = strlen($vs_message); print $vs_message; if (!($vn_left_item_id = $va_aat_id_to_item_id[$vs_left_id])) { print "ERROR: no list item id for left_id {$vs_left_id} (were there previous errors?)\n"; continue; } if (!($vn_right_item_id = $va_aat_id_to_item_id[$vs_right_id])) { print "ERROR: no list item id for right_id {$vs_right_id} (were there previous errors?)\n"; continue; } $t_link->set('term_left_id', $vn_left_item_id); $t_link->set('term_right_id', $vn_right_item_id); $t_link->set('type_id', $vn_list_item_relation_type_id_related); $t_link->insert(); if ($t_link->numErrors()) { print "ERROR: could not set link between {$vs_left_id} (was translated to item_id={$vn_left_item_id}) and {$vs_right_id} (was translated to item_id={$vn_right_item_id}): " . join('; ', $t_link->getErrors()) . "\n"; } } } else { print "WARNING: Skipped import of term-term relationships because the ca_list_items_x_list_items 'related' relationship type is not defined for your installation\n"; } print "\n\nIMPORT COMPLETE.\n"; }