public function Get() { global $g_ui_locale_id; $ps_query = $this->request->getParameter('q', pString); $ps_type = $this->request->getParameter('type', pString); $vo_conf = Configuration::load(); $vs_user = trim($vo_conf->get("geonames_user")); $va_items = array(); if (unicode_strlen($ps_query) >= 3) { $vs_base = "http://api.geonames.org/search"; $t_locale = new ca_locales($g_ui_locale_id); $vs_lang = $t_locale->get("language"); $va_params = array("q" => $ps_query, "lang" => $vs_lang, 'style' => 'full', 'username' => $vs_user); foreach ($va_params as $vs_key => $vs_value) { $vs_query_string .= "{$vs_key}=" . urlencode($vs_value) . "&"; } try { $vo_xml = new SimpleXMLElement(@file_get_contents("{$vs_base}?{$vs_query_string}")); //var_dump($vo_result); foreach ($vo_xml->children() as $vo_child) { if ($vo_child->getName() != "totalResultsCount") { $va_items[$vo_child->geonameId . ""] = array('displayname' => $vo_child->name, 'country' => $vo_child->countryName ? $vo_child->countryName : null, 'continent' => $vo_child->continentCode ? $vo_child->continentCode : null, 'fcl' => $vo_child->fclName ? $vo_child->fclName : null, 'lat' => $vo_child->lat ? $vo_child->lat : null, 'lng' => $vo_child->lng ? $vo_child->lng : null, 'idno' => $vo_child->geonameId); } } } catch (Exception $e) { $va_items[0] = array('displayname' => _t('Could not connect to GeoNames'), 'country' => '', 'continent' => '', 'fcl' => '', 'lat' => '', 'lng' => '', 'idno' => ''); } } $this->view->setVar('geonames_list', $va_items); return $this->render('ajax_geonames_list_html.php'); }
public function loadLocales() { require_once __CA_MODELS_DIR__ . "/ca_locales.php"; $t_locale = new ca_locales(); $va_locales = $t_locale->getLocaleList(array('index_by_code' => true)); if (!$va_locales) { return $this->processLocales(); } foreach ($va_locales as $vs_code => $va_locale) { $this->opa_locales[$vs_code] = $va_locale['locale_id']; } return true; }
public function Get($pa_additional_query_params = null, $pa_options = null) { global $g_ui_locale_id; $vn_max = $this->request->getParameter('maxRows', pInteger) ? $this->request->getParameter('maxRows', pInteger) : 20; $ps_query = $this->request->getParameter('term', pString); $ps_gn_elements = urldecode($this->request->getParameter('gnElements', pString)); $ps_gn_delimiter = urldecode($this->request->getParameter('gnDelimiter', pString)); $pa_elements = explode(',', $ps_gn_elements); $vo_conf = Configuration::load(); $vs_user = trim($vo_conf->get("geonames_user")); $va_items = array(); if (unicode_strlen($ps_query) >= 3) { $vs_base = $vo_conf->get('geonames_api_base_url') . '/search'; $t_locale = new ca_locales($g_ui_locale_id); $vs_lang = $t_locale->get("language"); $va_params = array("q" => $ps_query, "lang" => $vs_lang, 'style' => 'full', 'username' => $vs_user, 'maxRows' => $vn_max); $vs_query_string = ''; foreach ($va_params as $vs_key => $vs_value) { $vs_query_string .= "{$vs_key}=" . urlencode($vs_value) . "&"; } try { $vs_xml = caQueryExternalWebservice("{$vs_base}?{$vs_query_string}"); $vo_xml = new SimpleXMLElement($vs_xml); $va_attr = $vo_xml->status ? $vo_xml->status->attributes() : null; if ($va_attr && isset($va_attr['value']) && (int) $va_attr['value'] > 0) { $va_items[0] = array('displayname' => _t('Connection to GeoNames with username "%1" was rejected with the message "%2". Check your configuration and make sure your GeoNames.org account is enabled for web services.', $vs_user, $va_attr['message']), 'lat' => '', 'lng' => ''); $va_items[0]['label'] = $va_items[0]['displayname']; } else { foreach ($vo_xml->children() as $vo_child) { if ($vo_child->getName() == "geoname") { $va_elements = array(); foreach ($pa_elements as $ps_element) { $vs_val = $vo_child->{trim($ps_element)}; if (strlen(trim($vs_val)) > 0) { $va_elements[] = trim($vs_val); } } $va_items[(string) $vo_child->geonameId] = array('displayname' => $vo_child->name, 'label' => join($ps_gn_delimiter, $va_elements) . ($vo_child->lat ? " [" . $vo_child->lat . "," : '') . ($vo_child->lng ? $vo_child->lng . "]" : ''), 'lat' => $vo_child->lat ? $vo_child->lat : null, 'lng' => $vo_child->lng ? $vo_child->lng : null, 'id' => (string) $vo_child->geonameId); } } } } catch (Exception $e) { $va_items[0] = array('displayname' => _t('Could not connect to GeoNames'), 'lat' => '', 'lng' => '', 'id' => 0); $va_items[0]['label'] = $va_items[0]['displayname']; } } $this->view->setVar('geonames_list', $va_items); return $this->render('ajax_geonames_list_html.php'); }
/** * Sets and saves form element settings, taking parameters off of the request as needed. Does an update() * on the ca_search_forms instance to save settings to the database */ public function setSettingsFromHTMLForm($po_request, $pa_options = null) { $va_locales = ca_locales::getLocaleList(array('sort_field' => '', 'sort_order' => 'asc', 'index_by_code' => true, 'available_for_cataloguing_only' => true)); $va_available_settings = $this->getAvailableSettings(); $this->o_instance->setMode(ACCESS_WRITE); $va_values = array(); $vs_id_prefix = caGetOption('id', $pa_options, 'setting'); $vs_placement_code = caGetOption('placement_code', $pa_options, ''); foreach (array_keys($va_available_settings) as $vs_setting) { $va_properties = $va_available_settings[$vs_setting]; if (isset($va_properties['takesLocale']) && $va_properties['takesLocale']) { foreach ($va_locales as $vs_locale => $va_locale_info) { $va_values[$vs_setting][$va_locale_info['locale_id']] = $po_request->getParameter("{$vs_placement_code}{$vs_id_prefix}{$vs_setting}_{$vs_locale}", pString); } } else { if (isset($va_properties['useRelationshipTypeList']) && $va_properties['useRelationshipTypeList'] && $va_properties['height'] > 1 || isset($va_properties['useList']) && $va_properties['useList'] && $va_properties['height'] > 1 || isset($va_properties['showLists']) && $va_properties['showLists'] && $va_properties['height'] > 1 || isset($va_properties['showVocabularies']) && $va_properties['showVocabularies'] && $va_properties['height'] > 1) { $va_values[$vs_setting] = $po_request->getParameter("{$vs_placement_code}{$vs_id_prefix}{$vs_setting}", pArray); } else { $va_values = array($vs_setting => $po_request->getParameter("{$vs_placement_code}{$vs_id_prefix}{$vs_setting}", pString)); } } foreach ($va_values as $vs_setting_key => $vs_value) { $this->setSetting($vs_setting, $vs_value); } } return true; }
public function __construct($po_request, $ps_table = "") { $this->opo_request = $po_request; $this->ops_table = $ps_table; $this->opo_dm = Datamodel::load(); $this->opa_errors = array(); $this->ops_method = $this->opo_request->getRequestMethod(); if (!in_array($this->ops_method, array("PUT", "DELETE", "GET", "POST", "OPTIONS"))) { $this->addError("Invalid HTTP request method"); } $this->opn_id = $this->opo_request->getParameter("id", pString); // we allow for a string to support fetching by idno; typically it's a numeric id if ($vs_locale = $this->opo_request->getParameter('lang', pString)) { global $g_ui_locale, $g_ui_locale_id, $_; $g_ui_locale = $vs_locale; $t_locale = new ca_locales(); if ($g_ui_locale_id = $t_locale->localeCodeToID($vs_locale)) { $g_ui_locale = $vs_locale; if (!initializeLocale($g_ui_locale)) { die("Error loading locale " . $g_ui_locale); } $this->opo_request->reloadAppConfig(); } } $vs_post_data = $this->opo_request->getRawPostData(); if (strlen(trim($vs_post_data)) > 0) { $this->opa_post = json_decode($vs_post_data, true); if (!is_array($this->opa_post)) { $this->addError(_t("Data sent via POST doesn't seem to be in JSON format")); } } else { if ($vs_post_data = $this->opo_request->getParameter('source', pString)) { $this->opa_post = json_decode($vs_post_data, true); if (!is_array($this->opa_post)) { $this->addError(_t("Data sent via 'source' parameter doesn't seem to be in JSON format")); } } else { $this->opa_post = array(); } } $this->opa_valid_tables = array("ca_objects", "ca_object_lots", "ca_entities", "ca_places", "ca_occurrences", "ca_collections", "ca_list_items", "ca_lists", "ca_object_representations", "ca_storage_locations", "ca_movements", "ca_loans", "ca_tours", "ca_tour_stops", "ca_sets"); if (strlen($ps_table) > 0) { if (!in_array($ps_table, $this->opa_valid_tables)) { $this->addError(_t("Table does not exist")); } } }
public function ListLocales() { AssetLoadManager::register('tableList'); $t_locale = $this->getLocaleObject(); $vs_sort_field = $this->request->getParameter('sort', pString); $this->view->setVar('locale_list', ca_locales::getLocaleList(array('sort_field' => $vs_sort_field, 'sort_order' => 'asc', 'index_by_code' => false))); $this->render('locale_list_html.php'); }
/** * @link http://clangers.collectiveaccess.org/jira/browse/PROV-1026 */ public function testDataExporterCanLoadFromFile() { $t_locale = new ca_locales(); $va_locales = $t_locale->getLocaleList(); $vn_locale_id = key($va_locales); $t_exporter = new ca_data_exporters(); $va_errors = array(); ca_data_exporters::loadExporterFromFile(__DIR__ . '/data/list_item_export_mapping.xlsx', $va_errors, array('locale_id' => $vn_locale_id)); $vo_exporter = ca_data_exporters::loadExporterByCode('testmappingforunittests'); $this->assertEmpty($va_errors, 'Should be no error messages'); $this->assertTrue(is_object($vo_exporter), 'Should have found an exporter by the correct name'); $this->assertInstanceOf('ca_data_exporters', $vo_exporter, 'Incorrect type loaded'); $vo_exporter->setMode(ACCESS_WRITE); $vo_exporter->delete(true, array('hard' => true)); $vo_exporter = $t_exporter->load(array('exporter_code' => 'testmappingforunittests')); $this->assertFalse($vo_exporter, 'Should no longer have an exporter loaded'); }
public function __construct($po_engine_result = null, $pa_tables = null) { parent::__construct($po_engine_result, $pa_tables); $this->opo_list = new ca_lists(); $this->opa_locales = ca_locales::getLocaleList(); $this->ops_label_table_name = method_exists($this->opo_subject_instance, "getLabelTableName") ? $this->opo_subject_instance->getLabelTableName() : null; $this->ops_label_display_field = method_exists($this->opo_subject_instance, "getLabelDisplayField") ? $this->opo_subject_instance->getLabelDisplayField() : null; }
/** * */ public function getPreferredDisplayLocaleIDs($pn_item_locale_id = null) { $vs_mode = $this->getPreference('cataloguing_display_label_mode'); $va_locale_ids = array(); switch ($vs_mode) { case 'cataloguing_locale': if ($vs_locale = $this->getPreference('cataloguing_locale')) { $t_locale = new ca_locales(); if ($t_locale->loadLocaleByCode($vs_locale)) { $va_locale_ids[$t_locale->getPrimaryKey()] = true; } } break; case 'item_locale': if ($pn_item_locale_id) { $va_locale_ids[$pn_item_locale_id] = true; } break; case 'cataloguing_and_item_locale': default: if ($vs_locale = $this->getPreference('cataloguing_locale')) { $t_locale = new ca_locales(); if ($t_locale->loadLocaleByCode($vs_locale)) { $va_locale_ids[$t_locale->getPrimaryKey()] = true; } } if ($pn_item_locale_id) { $va_locale_ids[$pn_item_locale_id] = true; } break; } return array_keys($va_locale_ids); }
/** * Creates a default label when none exists * * @param int $pn_locale_id Locale id to use for default label. If not set the user's current locale is used. * @return boolean True on success, false on error. Success occurs when a default label is successfully added or when a default label is not required. false is only returned when an actionable error state occurs (eg. a blank label is not allowed, or the addition of the default label fails for some reason) */ public function addDefaultLabel($pn_locale_id = null) { global $g_ui_locale_id; if (!$this->getPreferredLabelCount()) { $va_locale_list = ca_locales::getLocaleList(); if ($pn_locale_id && isset($va_locale_list[$pn_locale_id])) { $vn_locale_id = $pn_locale_id; } else { if ($g_ui_locale_id) { $vn_locale_id = $g_ui_locale_id; } else { $va_tmp = array_keys($va_locale_list); $vn_locale_id = array_shift($va_tmp); } } if (!(bool) $this->getAppConfig()->get('require_preferred_label_for_' . $this->tableName())) { // only try to add a default when a label is not mandatory return $this->addLabel(array($this->getLabelDisplayField() => '[' . _t('BLANK') . ']'), $vn_locale_id, null, true); } else { $this->postError(1130, _t('Label must not be blank'), 'LabelableBaseModelWithAttributes->addDefaultLabel()', $this->tableName() . '.preferred_labels'); return false; } } return true; }
/** * @param array $pa_options * @param $t_rel * @param bool $pb_update */ private function _processInterstitials($pa_options, $t_rel, $pb_update) { global $g_ui_locale_id; // Are there interstitials to add? if (isset($pa_options['interstitialValues']) && is_array($pa_options['interstitialValues'])) { $t_rel->setMode(ACCESS_WRITE); foreach ($pa_options['interstitialValues'] as $vs_element => $va_value) { if ($t_rel->hasField($vs_element)) { $t_rel->set($vs_element, $va_value); continue; } // Convert a scalar or key-value array to an indexed array with a single element if (!is_array($va_value) || array_keys($va_value) !== range(0, sizeof($va_value) - 1)) { $va_value = array($va_value); } // Iterate through indexed array foreach ($va_value as $va_value_instance) { // Convert scalar to key-value array if (!is_array($va_value_instance)) { $va_value_instance = array($vs_element => $va_value_instance); } // Ensure we have a locale if (!isset($va_value_instance['locale_id'])) { $va_value_instance['locale_id'] = $g_ui_locale_id ? $g_ui_locale_id : ca_locales::getDefaultCataloguingLocaleID(); } // Create or update the attribute if ($pb_update) { $t_rel->editAttribute($va_value_instance, $vs_element); } else { $t_rel->addAttribute($va_value_instance, $vs_element); } } } $t_rel->update(); } }
/** * @param $pa_hits Array of row_ids to sort. *MUST HAVE row_ids AS KEYS, NOT VALUES* */ public function sortHits($pa_hits, $ps_field, $ps_direction = 'asc', $pa_options = null) { if (!in_array(strtolower($ps_direction), array('asc', 'desc'))) { $ps_direction = 'asc'; } if (!is_array($pa_hits) || !sizeof($pa_hits)) { return $pa_hits; } $vs_search_tmp_table = $this->loadListIntoTemporaryResultTable($pa_hits, $pa_options['search']); $t_table = $this->opo_datamodel->getInstanceByTableNum($this->opn_tablenum, true); $vs_table_pk = $t_table->primaryKey(); $vs_table_name = $this->ops_tablename; $va_fields = explode(';', $ps_field); $va_sorted_hits = array(); $vn_num_locales = ca_locales::numberOfCataloguingLocales(); foreach ($va_fields as $vs_field) { $va_joins = $va_orderbys = array(); $vs_locale_where = $vs_is_preferred_sql = ''; $va_tmp = explode('.', $vs_field); // Rewrite for <table>.preferred_labels.* syntax if ($va_tmp[1] == 'preferred_labels') { if ($t_labeled_item_table = $this->opo_datamodel->getInstanceByTableName($va_tmp[0], true)) { if ($t_label_table = $t_labeled_item_table->getLabelTableInstance()) { $va_tmp2 = array($t_label_table->tableName()); if (isset($va_tmp[2]) && $t_label_table->hasField($va_tmp[2])) { $va_tmp2[] = $va_tmp[2]; } else { $va_tmp2[] = $t_labeled_item_table->getLabelDisplayField(); } $va_tmp = $va_tmp2; $vs_field = join(".", $va_tmp); } } } if ($va_tmp[0] == $vs_table_name) { // // sort field is in search table // if (!$t_table->hasField($va_tmp[1])) { // // is it an attribute? // $t_element = new ca_metadata_elements(); $vs_sort_element_code = array_pop($va_tmp); if ($t_element->load(array('element_code' => $vs_sort_element_code))) { $vn_element_id = $t_element->getPrimaryKey(); if (!($vs_sortable_value_fld = Attribute::getSortFieldForDatatype($t_element->get('datatype')))) { return $pa_hits; } if ((int) $t_element->get('datatype') == 3) { $vs_sortable_value_fld = 'lil.name_plural'; $vs_sort_field = array_pop(explode('.', $vs_sortable_value_fld)); $vs_locale_where = $vn_num_locales > 1 ? ', lil.locale_id' : ''; $vs_sql = "\n\t\t\t\t\t\t\t\tSELECT attr.row_id, lil.locale_id, lower({$vs_sortable_value_fld}) {$vs_sort_field}\n\t\t\t\t\t\t\t\tFROM ca_attributes attr\n\t\t\t\t\t\t\t\tINNER JOIN ca_attribute_values AS attr_vals ON attr_vals.attribute_id = attr.attribute_id\n\t\t\t\t\t\t\t\tINNER JOIN ca_list_item_labels AS lil ON lil.item_id = attr_vals.item_id\n\t\t\t\t\t\t\t\tINNER JOIN {$vs_browse_tmp_table} ON {$vs_browse_tmp_table}.row_id = attr.row_id\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t(attr_vals.element_id = ?) AND (attr.table_num = ?) AND (lil.{$vs_sort_field} IS NOT NULL)\n\t\t\t\t\t\t\t\tORDER BY lil.{$vs_sort_field}\n\t\t\t\t\t\t\t"; } else { $vs_sortable_value_fld = 'attr_vals.' . $vs_sortable_value_fld; $vs_sort_field = array_pop(explode('.', $vs_sortable_value_fld)); $vs_locale_where = $vn_num_locales > 1 ? 'attr.locale_id' : ''; $vs_sql = "\n\t\t\t\t\t\t\t\tSELECT attr.row_id, attr.locale_id, lower({$vs_sortable_value_fld}) {$vs_sort_field}\n\t\t\t\t\t\t\t\tFROM ca_attributes attr\n\t\t\t\t\t\t\t\tINNER JOIN ca_attribute_values AS attr_vals ON attr_vals.attribute_id = attr.attribute_id\n\t\t\t\t\t\t\t\tINNER JOIN {$vs_search_tmp_table} ON {$vs_search_tmp_table}.row_id = attr.row_id\n\t\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\t\t(attr_vals.element_id = ?) AND (attr.table_num = ?) AND (attr_vals.{$vs_sort_field} IS NOT NULL)\n\t\t\t\t\t\t\t\tORDER BY attr_vals.{$vs_sort_field}\n\t\t\t\t\t\t\t"; //print $vs_sql." ; $vn_element_id/; ".$this->opn_tablenum."<br>"; } $qr_sort = $this->opo_db->query($vs_sql, (int) $vn_element_id, (int) $this->opn_tablenum); while ($qr_sort->nextRow()) { $va_row = $qr_sort->getRow(); if (!$va_row['row_id']) { continue; } if ($vn_num_locales > 1) { $va_sorted_hits[$va_row['row_id']][$va_row['locale_id']] .= trim(str_replace(array("'", '"'), array('', ''), $va_row[$vs_sort_field])); } else { $va_sorted_hits[$va_row['row_id']] .= trim(str_replace(array("'", '"'), array('', ''), $va_row[$vs_sort_field])); } unset($pa_hits[$va_row['row_id']]); } // Add on hits that aren't sorted because they don't have an attribute associated foreach ($pa_hits as $vn_id => $va_row) { if (!is_array($va_row)) { $va_row = array(); } if ($vn_num_locales > 1) { $va_sorted_hits[$vn_id][1] = $va_row; } else { $va_sorted_hits[$vn_id] = $va_row; } } } continue; } else { $va_field_info = $t_table->getFieldInfo($va_tmp[1]); if ($va_field_info['START'] && $va_field_info['END']) { $va_orderbys[] = $va_field_info['START'] . ' ' . $ps_direction; $va_orderbys[] = $va_field_info['END'] . ' ' . $ps_direction; } else { $va_orderbys[] = $vs_field . ' ' . $ps_direction; } if ($t_table->hasField('locale_id')) { $vs_locale_where = ", " . $vs_table_name . ".locale_id"; } $vs_sortable_value_fld = $vs_field; } } else { // sort field is in related table $va_path = $this->opo_datamodel->getPath($vs_table_name, $va_tmp[0]); if (sizeof($va_path) > 2) { // many-many $vs_last_table = null; // generate related joins foreach ($va_path as $vs_table => $va_info) { $t_table = $this->opo_datamodel->getInstanceByTableName($vs_table, true); if (!$vs_last_table) { //$va_joins[$vs_table] = "INNER JOIN ".$vs_table." ON ".$vs_table.".".$t_table->primaryKey()." = ca_sql_search_search_final.row_id"; } else { $va_rels = $this->opo_datamodel->getOneToManyRelations($vs_last_table, $vs_table); if (!sizeof($va_rels)) { $va_rels = $this->opo_datamodel->getOneToManyRelations($vs_table, $vs_last_table); } if ($vs_table == $va_rels['one_table']) { $va_joins[$vs_table] = "INNER JOIN " . $va_rels['one_table'] . " ON " . $va_rels['one_table'] . "." . $va_rels['one_table_field'] . " = " . $va_rels['many_table'] . "." . $va_rels['many_table_field']; } else { $va_joins[$vs_table] = "INNER JOIN " . $va_rels['many_table'] . " ON " . $va_rels['many_table'] . "." . $va_rels['many_table_field'] . " = " . $va_rels['one_table'] . "." . $va_rels['one_table_field']; } } $t_last_table = $t_table; $vs_last_table = $vs_table; } $va_orderbys[] = $vs_field . ' ' . $ps_direction; $vs_sortable_value_fld = $vs_field; } else { $va_rels = $this->opo_datamodel->getRelationships($vs_table_name, $va_tmp[0]); if (!$va_rels) { return $pa_hits; } // return hits unsorted if field is not valid $t_rel = $this->opo_datamodel->getInstanceByTableName($va_tmp[0], true); if (!$t_rel->hasField($va_tmp[1])) { return $pa_hits; } $va_joins[$va_tmp[0]] = 'LEFT JOIN ' . $va_tmp[0] . ' ON ' . $vs_table_name . '.' . $va_rels[$vs_table_name][$va_tmp[0]][0][0] . ' = ' . $va_tmp[0] . '.' . $va_rels[$vs_table_name][$va_tmp[0]][0][1] . "\n"; $va_orderbys[] = $vs_field . ' ' . $ps_direction; // if the related supports preferred values (eg. *_labels tables) then only consider those in the sort if ($t_rel->hasField('is_preferred')) { $vs_is_preferred_sql = " " . $va_tmp[0] . ".is_preferred = 1"; } if ($t_rel->hasField('locale_id')) { $vs_locale_where = ", " . $va_tmp[0] . ".locale_id"; } $vs_sortable_value_fld = $vs_field; } } // // Grab values and index for sorting later // $va_tmp = explode('.', $vs_sortable_value_fld); $vs_sort_field = array_pop($va_tmp); $vs_join_sql = join("\n", $va_joins); $vs_sql = "\n\t\t\t\tSELECT {$vs_table_name}.{$vs_table_pk}{$vs_locale_where}, lower({$vs_sortable_value_fld}) {$vs_sort_field}\n\t\t\t\tFROM {$vs_table_name}\n\t\t\t\t{$vs_join_sql}\n\t\t\t\tINNER JOIN {$vs_search_tmp_table} ON {$vs_search_tmp_table}.row_id = {$vs_table_name}.{$vs_table_pk}\n\t\t\t\t" . ($vs_is_preferred_sql ? 'WHERE' : '') . "\n\t\t\t\t\t{$vs_is_preferred_sql}\n\t\t\t"; //print $vs_sql; $qr_sort = $this->opo_db->query($vs_sql); while ($qr_sort->nextRow()) { $va_row = $qr_sort->getRow(); if (!($vs_sortable_value = str_replace(array("'", '"'), array('', ''), $va_row[$vs_sort_field]))) { $vs_sortable_value = ''; } if ($vn_num_locales > 1 && $vs_locale_where) { $va_sorted_hits[$va_row[$vs_table_pk]][$va_row['locale_id']] .= $vs_sortable_value; } else { $va_sorted_hits[$va_row[$vs_table_pk]] .= $vs_sortable_value; } } } // // Actually sort the hits here... // if ($vn_num_locales > 1 && $vs_locale_where) { $va_sorted_hits = caExtractValuesByUserLocale($va_sorted_hits); } asort($va_sorted_hits, SORT_STRING); if ($ps_direction == 'desc') { $va_sorted_hits = array_reverse($va_sorted_hits, true); } $this->cleanupTemporaryResultTable(); return $va_sorted_hits; }
/** * Returns HTML form element for editing of setting * * Options: * * 'name' => sets the name of the HTML form element explicitly, otherwise 'setting_<name_of_setting>' is used * 'value' => sets the value of the HTML form element explicitly, otherwise the current value for the setting in the loaded row is used */ public function settingHTMLFormElement($ps_widget_id, $ps_setting, $pa_options = null) { if (!$this->isValidSetting($ps_setting)) { return false; } $va_available_settings = $this->getAvailableSettings(); $va_properties = $va_available_settings[$ps_setting]; if (isset($pa_options['name'])) { $vs_input_name = $pa_options['name']; } else { $vs_input_name = "setting_{$ps_setting}"; } if (isset($pa_options['value']) && !is_null($pa_options['value'])) { $vs_value = $pa_options['value']; } else { $vs_value = $this->getSetting(trim($ps_setting)); } $vs_element = ''; switch ($va_properties['displayType']) { # -------------------------------------------- case DT_FIELD: $vb_takes_locale = false; if (isset($va_properties['takesLocale']) && $va_properties['takesLocale']) { $vb_takes_locale = true; $va_locales = ca_locales::getLocaleList(array('sort_field' => '', 'sort_order' => 'asc', 'index_by_code' => true)); } else { $va_locales = array('_generic' => array()); } foreach ($va_locales as $vs_locale => $va_locale_info) { if ($vb_takes_locale) { $vs_locale_label = " (" . $va_locale_info['name'] . ")"; $vs_input_name_suffix = '_' . $vs_locale; } else { $vs_input_name_suffix = $vs_locale_label = ''; } $vs_element .= caHTMLTextInput($vs_input_name . $vs_input_name_suffix, array('size' => $va_properties["width"], 'height' => $va_properties["height"], 'value' => $vs_value, 'id' => $vs_input_name . $vs_input_name_suffix)) . "{$vs_locale_label}"; // focus code is needed by Firefox for some reason $vs_element .= "<script type='text/javascript'>jQuery('#" . $vs_input_name . $vs_input_name_suffix . "').click(function() { this.focus(); });</script>"; } break; # -------------------------------------------- # -------------------------------------------- case DT_CHECKBOXES: $va_attributes = array('value' => '1'); if ($vs_value) { $va_attributes['checked'] = '1'; } $vs_element .= caHTMLCheckboxInput($vs_input_name, $va_attributes); break; # -------------------------------------------- # -------------------------------------------- case DT_SELECT: if (!is_array($va_properties['options'])) { $va_properties['options'] = array(); } $vs_element .= caHTMLSelect($vs_input_name, $va_properties['options'], array(), array('value' => $vs_value)); break; # -------------------------------------------- # -------------------------------------------- default: break; # -------------------------------------------- } $vs_label = $va_properties['label']; $vb_element_is_part_of_label = false; if (strpos($vs_label, '^ELEMENT') !== false) { $vs_label = str_replace('^ELEMENT', $vs_element, $vs_label); $vb_element_is_part_of_label = true; } $vs_return = "\n" . '<div class="formLabel" id="_widget_setting_' . $ps_setting . '_' . $ps_widget_id . '"><span>' . $vs_label . '</span>'; if (!$vb_element_is_part_of_label) { $vs_return .= '<br />' . $vs_element; } $vs_return .= '</div>' . "\n"; TooltipManager::add('#_widget_setting_' . $ps_setting . '_' . $ps_widget_id, "<h3>" . str_replace('^ELEMENT', 'X', $va_properties["label"]) . "</h3>" . $va_properties["description"]); return $vs_return; }
/** * Get a list of available languages * * @param array $pa_options List of options. Possible keys: * - sort_field: field to sort on * - sort_direction: direction to sort on * - index_by_code: use code as array keys * - return_display_values: add language display value to result * - available_for_cataloguing_only: only return languages that are used for cataloguing * @return array List of available language */ public function getLocaleList($pa_options = null) { return ca_locales::getLocaleList($pa_options); }
/** * */ public function getDefaultLocaleList() { global $g_ui_locale_id; $va_locale_dedup = array(); if ($g_ui_locale_id) { $va_locale_dedup[$g_ui_locale_id] = true; } $va_locales = ca_locales::getLocaleList(); if (is_array($va_locale_defaults = $this->getAppConfig()->getList('locale_defaults'))) { foreach ($va_locale_defaults as $vs_locale_default) { $va_locale_dedup[$va_locales[$vs_locale_default]] = true; } } foreach ($va_locales as $vn_locale_id => $vs_locale_code) { $va_locale_dedup[$vn_locale_id] = true; } return array_keys($va_locale_dedup); }
/** * Returns version of label 'display' field value suitable for sorting * The sortable value is the same as the display value except when the display value * starts with a definite article ('the' in English) or indefinite article ('a' or 'an' in English) * in the locale of the label, in which case the article is moved to the end of the sortable value. * * What constitutes an article is defined in the TimeExpressionParser localization files. So if the * locale of the label doesn't correspond to an existing TimeExpressionParser localization, then * the users' current locale setting is used. */ private function _generateSortableValue() { if ($vs_sort_field = $this->getProperty('LABEL_SORT_FIELD')) { $vs_display_field = $this->getProperty('LABEL_DISPLAY_FIELD'); $o_tep = new TimeExpressionParser(); $t_locale = new ca_locales(); $o_tep->setLanguage($t_locale->localeIDToCode($this->get('locale_id'))); $o_lang_settings = $o_tep->getLanguageSettings(); $vs_display_value = trim(preg_replace('![^\\p{L}0-9 ]+!u', ' ', $this->get($vs_display_field))); $va_definite_articles = $o_lang_settings->get('definiteArticles'); $va_indefinite_articles = $o_lang_settings->get('indefiniteArticles'); foreach (array($o_lang_settings->get('definiteArticles'), $o_lang_settings->get('indefiniteArticles')) as $va_articles) { if (is_array($va_articles)) { foreach ($va_articles as $vs_article) { if (preg_match('!^(' . $vs_article . ')[ ]+!i', $vs_display_value, $va_matches)) { $vs_display_value = trim(str_replace($va_matches[1], '', $vs_display_value) . ', ' . $va_matches[1]); break 2; } } } } $this->set($vs_sort_field, $vs_display_value); } }
$resp = $app->getResponse(); // TODO: move this into a library so $_, $g_ui_locale_id and $g_ui_locale gets set up automatically require_once __CA_APP_DIR__ . "/helpers/initializeLocale.php"; $va_ui_locales = $g_request->config->getList('ui_locales'); if ($vs_lang = $g_request->getParameter('lang', pString)) { if (in_array($vs_lang, $va_ui_locales)) { $g_request->session->setVar('lang', $vs_lang); } } if (!($g_ui_locale = $g_request->session->getVar('lang'))) { $g_ui_locale = $va_ui_locales[0]; } if (!in_array($g_ui_locale, $va_ui_locales)) { $g_ui_locale = $va_ui_locales[0]; } $t_locale = new ca_locales(); $g_ui_locale_id = $t_locale->localeCodeToID($g_ui_locale); // get current UI locale as locale_id (available as global) $_ = array(); if (file_exists($vs_theme_specific_locale_path = $g_request->getThemeDirectoryPath() . '/locale/' . $g_ui_locale . '/messages.mo')) { $_[] = new Zend_Translate('gettext', $vs_theme_specific_locale_path, $g_ui_locale); } $_[] = new Zend_Translate('gettext', __CA_APP_DIR__ . '/locale/' . $g_ui_locale . '/messages.mo', $g_ui_locale); if (!initializeLocale($g_ui_locale)) { die("Error loading locale " . $g_ui_locale); } $g_request->reloadAppConfig(); // need to reload app config to reflect current locale // // PageFormat plug-in generates header/footer shell around page content //
public function insert($pa_options = null) { if (!$this->inTransaction()) { $this->setTransaction(new Transaction()); } if ($this->get('is_default')) { $this->getDb()->query("\n\t\t\t\tUPDATE ca_list_items \n\t\t\t\tSET is_default = 0 \n\t\t\t\tWHERE list_id = ?\n\t\t\t", (int) $this->get('list_id')); } $vn_rc = parent::insert($pa_options); if ($this->getPrimaryKey()) { $t_list = new ca_lists(); $o_trans = $this->getTransaction(); $t_list->setTransaction($o_trans); if ($t_list->load($this->get('list_id')) && $t_list->get('list_code') == 'place_hierarchies' && $this->get('parent_id')) { // insert root or place hierarchy when creating non-root items in 'place_hierarchies' list $t_locale = new ca_locales(); $va_locales = $this->getAppConfig()->getList('locale_defaults'); $vn_locale_id = $t_locale->localeCodeToID($va_locales[0]); // create root in ca_places $t_place = new ca_places(); $t_place->setTransaction($o_trans); $t_place->setMode(ACCESS_WRITE); $t_place->set('hierarchy_id', $this->getPrimaryKey()); $t_place->set('locale_id', $vn_locale_id); $t_place->set('type_id', null); $t_place->set('parent_id', null); $t_place->set('idno', 'Root node for ' . $this->get('idno')); $t_place->insert(); if ($t_place->numErrors()) { $this->delete(); $this->errors = array_merge($this->errors, $t_place->errors); return false; } $t_place->addLabel(array('name' => 'Root node for ' . $this->get('idno')), $vn_locale_id, null, true); } } if ($this->numErrors()) { $this->getTransaction()->rollback(); } else { $this->getTransaction()->commit(); $this->_setSettingsForList(); } return $vn_rc; }
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 processLocales() { require_once __CA_MODELS_DIR__ . "/ca_locales.php"; $t_locale = new ca_locales(); $t_locale->setMode(ACCESS_WRITE); // Find any existing locales $va_locales = $t_locale->getLocaleList(array('index_by_code' => true)); foreach ($va_locales as $vs_code => $va_locale) { $this->opa_locales[$vs_code] = $va_locale['locale_id']; } if ($this->ops_base_name) { $va_locales = array(); foreach ($this->opo_profile->locales->children() as $vo_locale) { $va_locales[] = $vo_locale; } foreach ($this->opo_base->locales->children() as $vo_locale) { $va_locales[] = $vo_locale; } } else { $va_locales = $this->opo_profile->locales->children(); } foreach ($va_locales as $vo_locale) { $vs_language = self::getAttribute($vo_locale, "lang"); $vs_dialect = self::getAttribute($vo_locale, "dialect"); $vs_country = self::getAttribute($vo_locale, "country"); $vb_dont_use_for_cataloguing = self::getAttribute($vo_locale, "dontUseForCataloguing"); if (isset($this->opa_locales[$vs_language . "_" . $vs_country])) { // don't insert duplicate locales continue; } $t_locale->set('name', (string) $vo_locale); $t_locale->set('country', $vs_country); $t_locale->set('language', $vs_language); if ($vs_dialect) { $t_locale->set('dialect', $vs_dialect); } $t_locale->set('dont_use_for_cataloguing', (bool) $vb_dont_use_for_cataloguing); $t_locale->insert(); if ($t_locale->numErrors()) { $this->addError("There was an error while inserting locale {$vs_language}_{$vs_country}: " . join(" ", $t_locale->getErrors())); } $this->opa_locales[$vs_language . "_" . $vs_country] = $t_locale->getPrimaryKey(); } $va_locales = $t_locale->getAppConfig()->getList('locale_defaults'); $vn_locale_id = $t_locale->localeCodeToID($va_locales[0]); if (!$vn_locale_id) { throw new Exception("The locale default is set to a non-existing locale. Try adding '" . $va_locales[0] . "' to your profile."); } return true; }
/** * */ public function DownloadCaptionFile() { list($pn_representation_id, $t_rep) = $this->_initView(); $pn_caption_id = $this->request->getParameter('caption_id', pString); $this->view->setVar('representation_id', $pn_representation_id); $this->view->setVar('caption_id', $pn_caption_id); $this->view->setVar('t_object_representation', $t_rep); $t_caption = new ca_object_representation_captions($pn_caption_id); if (!$t_caption->getPrimaryKey() || (int) $t_caption->get('representation_id') !== (int) $pn_representation_id) { die(_t("Invalid caption file")); } $t_locale = new ca_locales(); $vn_locale_id = $t_caption->get('locale_id'); $vs_locale = $t_locale->localeIDToCode($vn_locale_id); $this->view->setVar('file_path', $t_caption->getFilePath('caption_file')); $va_info = $t_caption->getFileInfo("caption_file"); switch ($this->request->user->getPreference('downloaded_file_naming')) { case 'idno': $this->view->setVar('download_name', str_replace(' ', '_', $t_rep->get('idno')) . "_captions_{$vs_locale}.vtt"); break; case 'idno_and_version': $this->view->setVar('download_name', str_replace(' ', '_', $t_rep->get('idno')) . "_captions_{$vs_locale}.vtt"); break; case 'idno_and_rep_id_and_version': $this->view->setVar('download_name', str_replace(' ', '_', $t_rep->get('idno')) . "_representation_{$pn_representation_id}_captions_{$vs_locale}.vtt"); break; case 'original_name': default: if ($va_info['ORIGINAL_FILENAME']) { $this->view->setVar('download_name', $va_info['ORIGINAL_FILENAME'] . "_captions_{$vs_locale}.vtt"); } else { $this->view->setVar('download_name', str_replace(' ', '_', $t_rep->get('idno')) . "_representation_{$pn_representation_id}_captions_{$vs_locale}.vtt"); } break; } return $this->render('caption_download_binary.php'); }
private function editItem() { if (!($t_instance = $this->_getTableInstance($this->ops_table, $this->opn_id))) { return false; } $t_locales = new ca_locales(); $va_post = $this->getRequestBodyArray(); // intrinsic fields if (is_array($va_post["intrinsic_fields"]) && sizeof($va_post["intrinsic_fields"])) { foreach ($va_post["intrinsic_fields"] as $vs_field_name => $vs_value) { $t_instance->set($vs_field_name, $vs_value); } } // attributes if (is_array($va_post["remove_attributes"])) { foreach ($va_post["remove_attributes"] as $vs_code_to_delete) { $t_instance->removeAttributes($vs_code_to_delete); } } else { if ($va_post["remove_all_attributes"]) { $t_instance->removeAttributes(); } } if (is_array($va_post["attributes"]) && sizeof($va_post["attributes"])) { foreach ($va_post["attributes"] as $vs_attribute_name => $va_values) { foreach ($va_values as $va_value) { if ($va_value["locale"]) { $va_value["locale_id"] = $t_locales->localeCodeToID($va_value["locale"]); unset($va_value["locale"]); } $t_instance->addAttribute($va_value, $vs_attribute_name); } } } $t_instance->setMode(ACCESS_WRITE); $t_instance->update(); // AFTER UPDATE STUFF // yank all labels? if ($va_post["remove_all_labels"]) { $t_instance->removeAllLabels(); } // preferred labels if (is_array($va_post["preferred_labels"]) && sizeof($va_post["preferred_labels"])) { foreach ($va_post["preferred_labels"] as $va_label) { if ($va_label["locale"]) { $vn_locale_id = $t_locales->localeCodeToID($va_label["locale"]); unset($va_label["locale"]); } $t_instance->addLabel($va_label, $vn_locale_id, null, true); } } // nonpreferred labels if (is_array($va_post["nonpreferred_labels"]) && sizeof($va_post["nonpreferred_labels"])) { foreach ($va_post["nonpreferred_labels"] as $va_label) { if ($va_label["locale"]) { $vn_locale_id = $t_locales->localeCodeToID($va_label["locale"]); unset($va_label["locale"]); } if ($va_label["type_id"]) { $vn_type_id = $va_label["type_id"]; unset($va_label["type_id"]); } else { $vn_type_id = null; } $t_instance->addLabel($va_label, $vn_locale_id, $vn_type_id, false); } } // relationships if (is_array($va_post["remove_relationships"])) { foreach ($va_post["remove_relationships"] as $vs_table) { $t_instance->removeRelationships($vs_table); } } if ($va_post["remove_all_relationships"]) { foreach ($this->opa_valid_tables as $vs_table) { $t_instance->removeRelationships($vs_table); } } if (is_array($va_post["related"]) && sizeof($va_post["related"]) > 0) { foreach ($va_post["related"] as $vs_table => $va_relationships) { foreach ($va_relationships as $va_relationship) { $vs_source_info = isset($va_relationship["source_info"]) ? $va_relationship["source_info"] : null; $vs_effective_date = isset($va_relationship["effective_date"]) ? $va_relationship["effective_date"] : null; $vs_direction = isset($va_relationship["direction"]) ? $va_relationship["direction"] : null; $t_rel_instance = $this->_getTableInstance($vs_table); $vs_pk = isset($va_relationship[$t_rel_instance->primaryKey()]) ? $va_relationship[$t_rel_instance->primaryKey()] : null; $vs_type_id = isset($va_relationship["type_id"]) ? $va_relationship["type_id"] : null; $t_instance->addRelationship($vs_table, $vs_pk, $vs_type_id, $vs_effective_date, $vs_source_info, $vs_direction); } } } if ($t_instance->numErrors() > 0) { foreach ($t_instance->getErrors() as $vs_error) { $this->addError($vs_error); } return false; } else { return array($t_instance->primaryKey() => $t_instance->getPrimaryKey()); } }
/** Returns a list of locale_ids to use for UI presentation in priority order This will include the user's selected locale if one in logged in, as well as the default locale_id(s) as configured in app.conf or global.conf using the 'locale_defaults' directive. If no locales are set then a full list of locale_ids is returned. */ public function getUILocales() { $va_locale_codes = array(); $va_locale_ids = array(); if ($this->isLoggedIn()) { $va_locale_codes[] = $this->user->getPreference('ui_locale'); } if ($va_tmp = $this->config->getList('locale_defaults')) { $va_locale_codes = array_merge($va_locale_codes, $va_tmp); } $t_locale = new ca_locales(); if (sizeof($va_locale_codes) == 0) { foreach (ca_locales::getLocaleList() as $vn_locale_id => $va_locale_info) { $va_locale_ids[] = $vn_locale_id; } } else { foreach ($va_locale_codes as $vs_locale_code) { if ($vn_locale_id = $t_locale->loadLocaleByCode($vs_locale_code)) { $va_locale_ids[] = $vn_locale_id; } } } if (!sizeof($va_locale_ids)) { die("No locales configured?"); } return $va_locale_ids; }
public function savePlacementsFromHTMLForm($po_request, $ps_form_prefix, $ps_placement_code) { if ($vs_bundles = $po_request->getParameter("{$ps_placement_code}{$ps_form_prefix}displayBundleList", pString)) { $va_bundles = explode(';', $vs_bundles); $t_display = new ca_bundle_displays($this->getPrimaryKey()); if ($this->inTransaction()) { $t_display->setTransaction($this->getTransaction()); } $va_placements = $t_display->getPlacements(array('user_id' => $po_request->getUserID())); // remove deleted bundles foreach ($va_placements as $vn_placement_id => $va_bundle_info) { if (!in_array($va_bundle_info['bundle_name'] . '_' . $va_bundle_info['placement_id'], $va_bundles)) { $t_display->removePlacement($va_bundle_info['placement_id'], array('user_id' => $po_request->getUserID())); if ($t_display->numErrors()) { $this->errors = $t_display->errors; return false; } } } $va_locale_list = ca_locales::getLocaleList(array('index_by_code' => true)); $va_available_bundles = $t_display->getAvailableBundles(); foreach ($va_bundles as $vn_i => $vs_bundle) { // get settings if (preg_match('!^(.*)_([\\d]+)$!', $vs_bundle, $va_matches)) { $vn_placement_id = (int) $va_matches[2]; $vs_bundle = $va_matches[1]; } else { $vn_placement_id = null; } $vs_bundle_proc = str_replace(".", "_", $vs_bundle); $va_settings = array(); foreach ($_REQUEST as $vs_key => $vs_val) { if (preg_match("!^{$vs_bundle_proc}_([\\d]+)_([^\\d]+.*)\$!", $vs_key, $va_matches)) { // is this locale-specific? if (preg_match('!(.*)_([a-z]{2}_[A-Z]{2})$!', $va_matches[2], $va_locale_matches)) { $vn_locale_id = isset($va_locale_list[$va_locale_matches[2]]) ? (int) $va_locale_list[$va_locale_matches[2]]['locale_id'] : 0; $va_settings[(int) $va_matches[1]][$va_locale_matches[1]][$vn_locale_id] = $vs_val; } else { $va_settings[(int) $va_matches[1]][$va_matches[2]] = $vs_val; } } } if ($vn_placement_id === 0) { $t_display->addPlacement($vs_bundle, $va_settings[$vn_placement_id], $vn_i + 1, array('user_id' => $po_request->getUserID(), 'additional_settings' => $va_available_bundles[$vs_bundle]['settings'])); if ($t_display->numErrors()) { $this->errors = $t_display->errors; return false; } } else { $t_placement = new ca_bundle_display_placements($vn_placement_id, $va_available_bundles[$vs_bundle]['settings']); if ($this->inTransaction()) { $t_placement->setTransaction($this->getTransaction()); } $t_placement->setMode(ACCESS_WRITE); $t_placement->set('rank', $vn_i + 1); if (is_array($va_settings[$vn_placement_id])) { //foreach($va_settings[$vn_placement_id] as $vs_setting => $vs_val) { foreach ($t_placement->getAvailableSettings() as $vs_setting => $va_setting_info) { $vs_val = isset($va_settings[$vn_placement_id][$vs_setting]) ? $va_settings[$vn_placement_id][$vs_setting] : null; $t_placement->setSetting($vs_setting, $vs_val); } } $t_placement->update(); if ($t_placement->numErrors()) { $this->errors = $t_placement->errors; return false; } } } } }
/** * Import oral histories from specified directory into CollectiveAccess database */ public function commandImportOralHistories() { $o_conf = $this->getToolConfig(); // Get locale from config and translate to numeric code $t_locale = new ca_locales(); $pn_locale_id = $t_locale->localeCodeToID($o_conf->get('locale')); $o_log = $this->getLogger(); $o_progress = $this->getProgressBar(0); $vs_transcript_dir = $this->getSetting("transcript_directory"); if (!is_readable($vs_transcript_dir)) { if ($o_log) { $o_log->logError($vs_err_msg = _t("Transcript directory %1 is not readable", $vs_transcript_dir)); } if ($o_progress) { $o_progress->setError($vs_err_msg); $o_progress->finish(); } return false; } $vs_audio_dir = $this->getSetting("audio_directory"); if (!is_readable($vs_audio_dir)) { if ($o_log) { $o_log->logError($vs_err_msg = _t("Audio directory %1 is not readable", $vs_audio_dir)); } if ($o_progress) { $o_progress->setError($vs_err_msg); $o_progress->finish(); } return false; } if ($o_progress) { $o_progress->start("Starting oral history import"); } // ---------------------------------------------------------------------- // process main data $r_dir = opendir($vs_transcript_dir); while (($vs_file = readdir($r_dir)) !== false) { if ($vs_file[0] == '.') { continue; } // Get markup and fix it up to be valid XML $vs_markup = file_get_contents($vs_transcript_dir . $vs_file); $vs_markup = preg_replace('!&!', '&', $vs_markup); $vs_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><transcript>{$vs_markup}</transcript>"; try { $o_xml = new SimpleXMLElement($vs_xml); } catch (Exception $e) { $o_log->logError("Could not parse XML transcript for {$vs_transcript_dir}{$vs_file}: " . $e->getMessage()); continue; } $vs_idno = (string) $o_xml->identifier; if (!file_exists($vs_media_path = "{$vs_audio_dir}{$vs_idno}.mp3")) { $o_log->logError("No audio file found for {$vs_idno}. File path was {$vs_media_path}"); continue; } $vs_title = (string) $o_xml->title; $vs_date_created = (string) $o_xml->datecreated; $vs_format = (string) $o_xml->format; $vs_medium = (string) $o_xml->medium; $vs_place_recorded = (string) $o_xml->placeRecorded; $vs_rights = (string) $o_xml->rights; $vs_extent = (string) $o_xml->extent; $vs_country = (string) $o_xml->countryOfOrigin; $va_interviewers = array(); foreach ($o_xml->interviewer as $o_interviewer) { $va_interviewers[(string) $o_interviewer->attributes()->abbreviation] = (string) $o_interviewer; } $va_participants = array(); foreach ($o_xml->participant as $o_participant) { $va_participants[(string) $o_participant->attributes()->abbreviation] = (string) $o_participant; } $va_observers = array(); if ($o_xml->observer) { foreach ($o_xml->observer as $o_observer) { $va_observers[] = (string) $o_observer; } } // Create object $t_object = new ca_objects(); $t_object->setMode(ACCESS_WRITE); if (!$t_object->load(array('idno' => $vs_idno, 'deleted' => 0))) { $t_object->set('type_id', 'oral_history'); $t_object->set('idno', $vs_idno); $t_object->set('status', 0); $t_object->set('access', 1); } $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'dc_format' => $vs_format), 'dc_format'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'dates_value' => $vs_date_created, 'dc_dates_types' => 'created'), 'date'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'medium' => $vs_medium), 'medium'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'interview_location' => $vs_place_recorded), 'interview_location'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'rights' => $vs_rights), 'rights'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'extent' => $vs_extent), 'extent'); $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'countryOfOrigin' => $vs_country), 'countryOfOrigin'); if (!$t_object->getPrimaryKey()) { $t_object->insert(); DataMigrationUtils::postError($t_object, 'While inserting object'); if ($t_object->numErrors()) { $o_log->logError("While adding object for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } $t_object->addLabel(array('name' => $vs_title), $pn_locale_id, null, true); DataMigrationUtils::postError($t_object, 'While adding object label'); if ($t_object->numErrors()) { $o_log->logError("While adding object label for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } else { $t_object->update(); DataMigrationUtils::postError($t_object, 'While updating object'); if ($t_object->numErrors()) { $o_log->logError("While updating object for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } // add entities foreach ($va_interviewers as $vs_abbr => $vs_name) { $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id); $t_object->addRelationship('ca_entities', $vn_entity_id, 'interviewer'); DataMigrationUtils::postError($t_object, "While adding interviewer {$vs_name} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding interview {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } foreach ($va_participants as $vs_abbr => $vs_name) { $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id); $t_object->addRelationship('ca_entities', $vn_entity_id, 'interviewee'); DataMigrationUtils::postError($t_object, "While adding interviewee {$vs_name} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding interviee {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } foreach ($va_observers as $vn_i => $vs_name) { $vn_entity_id = DataMigrationUtils::getEntityID(DataMigrationUtils::splitEntityName($vs_name), 'ind', $pn_locale_id); $t_object->addRelationship('ca_entities', $vn_entity_id, 'observer'); DataMigrationUtils::postError($t_object, "While adding observer {$vs_name} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding observer {$vs_name} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } } // Add media $t_rep = $t_object->addRepresentation($vs_media_path, "front", $pn_locale_id, 0, 1, true, array(), array('returnRepresentation' => true)); DataMigrationUtils::postError($t_object, "While adding representation {$vs_media_path} to object"); if ($t_object->numErrors()) { $o_log->logError("While adding representation {$vs_media_path} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_object->getErrors())); } if ($t_object->numErrors()) { continue; } $va_clips = array(); foreach ($o_xml->clip as $o_clip) { $vs_content = nl2br(preg_replace('!^[\\n\\r\\t]+!', '', trim((string) $o_clip->asXML()))); $vs_start = (string) $o_clip->attributes()->start; $va_themes = $va_places = array(); foreach ($o_clip->children() as $o_node) { $vs_tag = (string) $o_node->getName(); switch ($vs_tag) { case 'place': $va_places[] = (string) $o_node; break; default: $va_themes[] = $vs_tag; break; } } $va_clips[] = array('start' => $vs_start, 'content' => $vs_content, 'themes' => $va_themes, 'places' => $va_places); } foreach ($va_clips as $vn_i => $va_clip) { $vs_start = $va_clip['start']; if (!($vs_end = $va_clips[$vn_i + 1]['start'])) { $va_info = $t_rep->getMediaInfo('media', 'original'); $vs_end = $va_info['PROPERTIES']['duration']; } //print "[$vs_start/$vs_end] (".join('/', $va_clip['themes'])."); (".join('/', $va_clip['places']).") ".substr($va_clip['content'], 0, 30)."\n\n\n"; $t_annotation = $t_rep->addAnnotation("{$vs_start} ... {$vs_end}", $pn_locale_id, 1, array('startTimecode' => $vs_start, 'endTimecode' => $vs_end), 0, 1, array('transcription' => $va_clip['content']), array('returnAnnotation' => true)); DataMigrationUtils::postError($t_rep, "While adding annotation to representation"); if ($t_rep->numErrors()) { $o_log->logError("While adding annotation {$vs_start}/{$vs_end} to {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_rep->getErrors())); } if ($t_annotation) { foreach ($va_clip['themes'] as $vs_theme) { $t_annotation->addRelationship('ca_list_items', $vs_theme, 'describes'); DataMigrationUtils::postError($t_annotation, "While adding theme {$vs_theme} to annotation"); if ($t_annotation->numErrors()) { $o_log->logError("While adding theme {$vs_theme} to annotation {$vs_start}/{$vs_end} for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_annotation->getErrors())); } } foreach ($va_clip['places'] as $vs_place) { if ($vn_place_id = ca_places::find(array('preferred_labels' => array('name' => $vs_place)), array('returnAs' => 'firstId'))) { $t_annotation->addRelationship('ca_places', $vn_place_id, 'describes'); DataMigrationUtils::postError($t_annotation, "While adding place {$vs_place} to annotation"); if ($t_annotation->numErrors()) { $o_log->logError("While adding place {$vs_place} to annotation {$vs_start}/{$vs_end} for {$vs_transcript_dir}{$vs_file}: " . join("; ", $t_annotation->getErrors())); } } } } } $o_log->logInfo("Imported {$vs_file}"); } $o_progress->finish("Completed processing"); if ($o_log) { $o_log->logDebug(_t("Ended oral history import")); } return true; }
public function getDisplaysAsXML() { $t_display = new ca_bundle_displays(); /** @var Datamodel $o_dm */ $o_dm = Datamodel::load(); $this->opt_locale = new ca_locales(); $va_displays = $t_display->getBundleDisplays(); $vs_buf = "<displays>\n"; foreach ($va_displays as $vn_i => $va_display_by_locale) { $va_locales = array_keys($va_display_by_locale); $va_info = $va_display_by_locale[$va_locales[0]]; if (!$t_display->load($va_info['display_id'])) { continue; } $vs_buf .= "\t<display code='" . ($va_info['display_code'] && preg_match('!^[A-Za-z0-9_]+$!', $va_info['display_code']) ? $va_info['display_code'] : 'display_' . $va_info['display_id']) . "' type='" . $o_dm->getTableName($va_info['table_num']) . "' system='" . $t_display->get('is_system') . "'>\n"; $vs_buf .= "\t\t<labels>\n"; foreach ($va_display_by_locale as $vn_locale_id => $va_display_info) { if (strlen($this->opt_locale->localeIDToCode($vn_locale_id)) > 0) { $vs_buf .= "\t\t\t<label locale='" . $this->opt_locale->localeIDToCode($vn_locale_id) . "'><name>" . caEscapeForXML($va_display_info['name']) . "</name></label>\n"; } } $vs_buf .= "\t\t</labels>\n"; $va_settings = $t_display->getSettings(); if (sizeof($va_settings) > 0) { $vs_buf .= "\t\t<settings>\n"; foreach ($va_settings as $vs_setting => $vm_val) { if (is_array($vm_val)) { foreach ($vm_val as $vn_i => $vn_val) { $vs_buf .= "\t\t\t<setting name='{$vs_setting}'><![CDATA[" . $vn_val . "]]></setting>\n"; } } else { $vs_buf .= "\t\t\t<setting name='{$vs_setting}'><![CDATA[" . $vm_val . "]]></setting>\n"; } } $vs_buf .= "\t\t</settings>\n"; } // User and group access $va_users = $t_display->getUsers(); if (sizeof($va_users) > 0) { $vs_buf .= "\t\t<userAccess>\n"; foreach ($va_users as $va_user_info) { $vs_buf .= "\t\t\t<permission user='******' access='" . $this->_convertUserGroupAccessToString(intval($va_user_info['access'])) . "'/>\n"; } $vs_buf .= "\t\t</userAccess>\n"; } $va_groups = $t_display->getUserGroups(); if (sizeof($va_groups) > 0) { $vs_buf .= "\t\t<groupAccess>\n"; foreach ($va_groups as $va_group_info) { $vs_buf .= "\t\t\t<permission group='" . $va_group_info["code"] . "' access='" . $this->_convertUserGroupAccessToString(intval($va_group_info['access'])) . "'/>\n"; } $vs_buf .= "\t\t</groupAccess>\n"; } $va_placements = $t_display->getPlacements(); $vs_buf .= "<bundlePlacements>\n"; foreach ($va_placements as $vn_placement_id => $va_placement_info) { $vs_buf .= "\t\t<placement code='" . preg_replace("![^A-Za-z0-9_]+!", "_", $va_placement_info['bundle_name']) . "'><bundle>" . $va_placement_info['bundle_name'] . "</bundle>\n"; $va_settings = caUnserializeForDatabase($va_placement_info['settings']); if (is_array($va_settings)) { $vs_buf .= "<settings>\n"; foreach ($va_settings as $vs_setting => $vm_value) { switch ($vs_setting) { case 'label': if (is_array($vm_value)) { foreach ($vm_value as $vn_locale_id => $vm_locale_specific_value) { if (preg_match("/^[a-z]{2,3}\\_[A-Z]{2,3}\$/", $vn_locale_id)) { // locale code $vs_locale_code = $vn_locale_id; } else { if (!($vs_locale_code = $this->opt_locale->localeIDToCode($vn_locale_id))) { $vs_locale_code = 'en_US'; } } $vs_buf .= "<setting name='label' locale='" . $vs_locale_code . "'>" . caEscapeForXML($vm_locale_specific_value) . "</setting>\n"; } } break; case 'restrict_to_relationship_types': if (is_array($vm_value)) { foreach ($vm_value as $vn_val) { $t_rel_type = new ca_relationship_types($vn_val); if ($t_rel_type->getPrimaryKey()) { $vs_value = $t_rel_type->get('type_code'); $vs_buf .= "\t\t\t\t<setting name='{$vs_setting}'><![CDATA[" . $vs_value . "]]></setting>\n"; } } } break; case 'restrict_to_types': if (is_array($vm_value)) { foreach ($vm_value as $vn_val) { $t_item = new ca_list_items($vn_val); if ($t_item->getPrimaryKey()) { $vs_value = $t_item->get('idno'); $vs_buf .= "\t\t\t\t<setting name='{$vs_setting}'><![CDATA[" . $vs_value . "]]></setting>\n"; } } } break; default: if (is_array($vm_value)) { foreach ($vm_value as $vn_i => $vn_val) { $vs_buf .= "\t\t\t\t<setting name='{$vs_setting}'><![CDATA[" . $vn_val . "]]></setting>\n"; } } else { $vs_buf .= "\t\t\t\t<setting name='{$vs_setting}'><![CDATA[" . $vm_value . "]]></setting>\n"; } break; } } $vs_buf .= "</settings>\n"; } $vs_buf .= "\t\t</placement>\n"; } $vs_buf .= "</bundlePlacements>\n"; $vs_buf .= "\t</display>\n"; } $vs_buf .= "</displays>\n"; return $vs_buf; }
/** * * @param $pa_locale_rules - Associative array defining which locales to extract, and how to fall back to alternative locales should your preferred locales not exist in $pa_values * @param $pa_values - Associative array keyed by unique item_id and then locale code (eg. en_US) or locale_id; the values can be anything - string, numbers, objects, arrays, etc. * @param $pa_options [optional] - Associative array of options; available options are: * 'returnList' = return an indexed array of found values rather than an associative array keys on unique item_id [default is false] * 'debug' = print debugging information [default is false] * @return Array - an array of found values keyed by unique item_id; or an indexed list of found values if option 'returnList' is passed in $pa_options */ function caExtractValuesByLocale($pa_locale_rules, $pa_values, $pa_options = null) { if (!is_array($pa_values)) { return array(); } $va_locales = ca_locales::getLocaleList(); if (!is_array($pa_options)) { $pa_options = array(); } if (!isset($pa_options['returnList'])) { $pa_options['returnList'] = false; } if (!is_array($pa_values)) { return array(); } $va_values = array(); foreach ($pa_values as $vm_id => $va_value_list_by_locale) { if (sizeof($va_value_list_by_locale) == 1) { // Don't bother looking if there's just a single value $va_values[$vm_id] = array_pop($va_value_list_by_locale); continue; } foreach ($va_value_list_by_locale as $pm_locale => $vm_value) { // convert locale_id to locale string if (is_numeric($pm_locale)) { if (!$va_locales[$pm_locale]) { continue; } // invalid locale_id? $vs_locale = $va_locales[$pm_locale]['language'] . '_' . $va_locales[$pm_locale]['country']; } else { $vs_locale = $pm_locale; } // try to find values for preferred locale if (isset($pa_locale_rules['preferred'][$vs_locale]) && $pa_locale_rules['preferred'][$vs_locale]) { $va_values[$vm_id] = $vm_value; break; } // try fallback locales if (isset($pa_locale_rules['fallback'][$vs_locale]) && $pa_locale_rules['fallback'][$vs_locale]) { $va_values[$vm_id] = $vm_value; } } if (!isset($va_values[$vm_id])) { // desperation mode: pick an available locale $va_values[$vm_id] = array_pop($va_value_list_by_locale); } } return $pa_options['returnList'] ? array_values($va_values) : $va_values; }
/** * Returns list of caption/subtitle files attached to a representation * The return value is an array key'ed on the caption_id; array values are arrays * with keys set to values for each file returned. They keys are: * path = The absolute file path to the file * url = The URL for the file * caption_id = a unique identifier for each attached caption file * * @param int $pn_representation_id The representation_id of the representation to return files for. If omitted the currently loaded representation is used. If no representation_id is specified and no row is loaded null will be returned. * @param array $pa_locale_ids * @param array $pa_options * @return array A list of caption files attached to the representations. If no files are associated an empty array is returned. */ public function getCaptionFileList($pn_representation_id = null, $pa_locale_ids = null, $pa_options = null) { if (!($vn_representation_id = $pn_representation_id)) { if (!($vn_representation_id = $this->getPrimaryKey())) { return null; } } $t_locale = new ca_locales(); $va_locale_ids = array(); if ($pa_locale_ids) { if (!is_array($pa_locale_ids)) { $pa_locale_ids = array($pa_locale_ids); } foreach ($pa_locale_ids as $vn_i => $vm_locale) { if (is_numeric($vm_locale) && (int) $vm_locale) { $va_locale_ids[] = (int) $vm_locale; } else { if ($vn_locale_id = $t_locale->localeCodeToID($vm_locale)) { $va_locale_ids[] = $vn_locale_id; } } } } $vs_locale_sql = ''; $va_params = array((int) $vn_representation_id); if (sizeof($va_locale_ids) > 0) { $vs_locale_sql = " AND locale_id IN (?)"; $va_params[] = $va_locale_ids; } $o_db = $this->getDb(); $qr_res = $o_db->query("\n \t\t\tSELECT *\n \t\t\tFROM ca_object_representation_captions\n \t\t\tWHERE\n \t\t\t\trepresentation_id = ?\n \t\t\t{$vs_locale_sql}\n \t\t", $va_params); $va_files = array(); while ($qr_res->nextRow()) { $vn_caption_id = $qr_res->get('caption_id'); $vn_locale_id = $qr_res->get('locale_id'); $va_files[$vn_caption_id] = $qr_res->getRow(); unset($va_files[$vn_caption_id]['caption_file']); $va_files[$vn_caption_id]['path'] = $qr_res->getFilePath('caption_file'); $va_files[$vn_caption_id]['url'] = $qr_res->getFileUrl('caption_file'); $va_files[$vn_caption_id]['filesize'] = caFormatFileSize(filesize($va_files[$vn_caption_id]['path'])); $va_files[$vn_caption_id]['caption_id'] = $vn_caption_id; $va_files[$vn_caption_id]['locale_id'] = $vn_locale_id; $va_files[$vn_caption_id]['locale'] = $t_locale->localeIDToName($vn_locale_id); $va_files[$vn_caption_id]['locale_code'] = $t_locale->localeIDToCode($vn_locale_id); } return $va_files; }
itemID: '<?php print $vs_id_prefix; ?> Item_', templateClassName: 'caItemTemplate', itemListClassName: 'caItemList', minRepeats: <?php print ($vn_n = $this->getVar('min_num_repeats')) ? $vn_n : 0; ?> , maxRepeats: <?php print ($vn_n = $this->getVar('max_num_repeats')) ? $vn_n : 65535; ?> , defaultValues: <?php print json_encode($va_element_value_defaults); ?> , readonly: <?php print $vb_read_only ? "1" : "0"; ?> , defaultLocaleID: <?php print ca_locales::getDefaultCataloguingLocaleID(); ?> }); <?php } ?> }); </script>
/** * */ private function _convertCodeToDisplayText($ps_prop, $pa_path_components, $pt_instance, $pa_options = null) { $vs_prop = $ps_prop; $vs_field_name = $pa_path_components['subfield_name'] ? $pa_path_components['subfield_name'] : $pa_path_components['field_name']; $vs_table_name = $pa_path_components['table_name']; if (method_exists($pt_instance, 'setLabelTypeList')) { $pt_instance->setLabelTypeList($this->opo_subject_instance->getAppConfig()->get($pa_path_components['field_name'] == 'nonpreferred_labels' ? "{$vs_table_name}_nonpreferred_label_type_list" : "{$vs_table_name}_preferred_label_type_list")); } if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && ($vs_list_code = $pt_instance->getFieldInfo($vs_field_name, "LIST_CODE"))) { $vs_prop = $this->opt_list->getItemFromListForDisplayByItemID($vs_list_code, $vs_prop); } else { if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && ($vs_list_code = $pt_instance->getFieldInfo($vs_field_name, "LIST"))) { $vs_prop = $this->opt_list->getItemFromListForDisplayByItemValue($vs_list_code, $vs_prop); } else { if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && $vs_field_name === 'locale_id' && (int) $vs_prop > 0) { $t_locale = new ca_locales($vs_prop); $vs_prop = $t_locale->getName(); } else { if (isset($pa_options['convertCodesToDisplayText']) && $pa_options['convertCodesToDisplayText'] && is_array($va_list = $pt_instance->getFieldInfo($vs_field_name, "BOUNDS_CHOICE_LIST"))) { foreach ($va_list as $vs_option => $vs_value) { if ($vs_value == $vs_prop) { $vs_prop = $vs_option; break; } } } } } } return $vs_prop; }