/** * 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 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'); }
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 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; }
/** * @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'); }
/** * 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 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; }
/** * Get a record summary that is easier to parse when importing to another system */ private function getItemInfoForImport() { if (!($t_instance = $this->_getTableInstance($this->ops_table, $this->opn_id))) { return false; } $o_dm = Datamodel::load(); $t_list = new ca_lists(); $t_locales = new ca_locales(); // // Options // if (!($vs_delimiter = $this->opo_request->getParameter('delimiter', pString))) { $vs_delimiter = "; "; } if (!($vs_flatten = $this->opo_request->getParameter('flatten', pString))) { $vs_flatten = null; } $va_flatten = preg_split("![ ]*[;]+[ ]*!", $vs_flatten); $va_flatten = array_flip($va_flatten); $va_locales = $t_locales->getLocaleList(array("available_for_cataloguing_only" => true)); $va_return = array(); // allow user-defined template to be passed; allows flexible formatting of returned "display" value if (!($vs_template = $this->opo_request->getParameter('template', pString))) { $vs_template = ''; } if ($vs_template) { $va_return['display'] = caProcessTemplateForIDs($vs_template, $this->ops_table, array($this->opn_id)); } // "intrinsic" fields foreach ($t_instance->getFieldsArray() as $vs_field_name => $va_field_info) { $vs_list = null; if (!is_null($vs_val = $t_instance->get($vs_field_name))) { if (preg_match("/^hier\\_/", $vs_field_name)) { continue; } if (preg_match("/\\_sort\$/", $vs_field_name)) { continue; } if ($vs_field_name == $t_instance->primaryKey()) { continue; } if (isset($va_field_info["LIST_CODE"])) { // typical example: type_id $va_item = $t_list->getItemFromListByItemID($va_field_info["LIST_CODE"], $vs_val); if ($t_item = new ca_list_items($va_item["item_id"])) { $vs_val = $t_item->get('idno'); } } $va_return['intrinsic'][$vs_field_name] = $vs_val; } } // preferred labels $va_labels = $t_instance->get($this->ops_table . ".preferred_labels", array("returnAllLocales" => true)); $va_labels = end($va_labels); $vs_display_field_name = $t_instance->getLabelDisplayField(); if (is_array($va_labels)) { foreach ($va_labels as $vn_locale_id => $va_labels_by_locale) { foreach ($va_labels_by_locale as $va_tmp) { $va_label = array(); $va_label['locale'] = $va_locales[$vn_locale_id]["code"]; // add only UI fields to return foreach (array_merge($t_instance->getLabelUIFields(), array('type_id')) as $vs_label_fld) { $va_label[$vs_label_fld] = $va_tmp[$vs_label_fld]; } $va_label[$vs_label_fld] = $va_tmp[$vs_label_fld]; $va_label['label'] = $va_tmp[$vs_display_field_name]; $va_return["preferred_labels"][$va_label['locale']] = $va_label; } } if (isset($va_flatten['locales'])) { $va_return["preferred_labels"] = array_pop(caExtractValuesByUserLocale(array($va_return["preferred_labels"]))); } } // nonpreferred labels $va_labels = $t_instance->get($this->ops_table . ".nonpreferred_labels", array("returnAllLocales" => true)); $va_labels = end($va_labels); if (is_array($va_labels)) { foreach ($va_labels as $vn_locale_id => $va_labels_by_locale) { foreach ($va_labels_by_locale as $va_tmp) { $va_label = array(); $va_label['locale'] = $va_locales[$vn_locale_id]["code"]; // add only UI fields to return foreach (array_merge($t_instance->getLabelUIFields(), array('type_id')) as $vs_label_fld) { $va_label[$vs_label_fld] = $va_tmp[$vs_label_fld]; } $va_return["nonpreferred_labels"][$va_label['locale']] = $va_label; } } if (isset($va_flatten['locales'])) { $va_return["nonpreferred_labels"] = array_pop(caExtractValuesByUserLocale(array($va_return["nonpreferred_labels"]))); } } // attributes $va_codes = $t_instance->getApplicableElementCodes(); foreach ($va_codes as $vs_code) { if ($va_vals = $t_instance->get($this->ops_table . "." . $vs_code, array("convertCodesToDisplayText" => false, "returnAllLocales" => true))) { $va_vals_as_text = end($t_instance->get($this->ops_table . "." . $vs_code, array("convertCodesToDisplayText" => true, "returnAllLocales" => true))); $va_vals_by_locale = end($va_vals); foreach ($va_vals_by_locale as $vn_locale_id => $va_locale_vals) { foreach ($va_locale_vals as $vs_val_id => $va_actual_data) { if (!is_array($va_actual_data)) { continue; } $vs_locale_code = isset($va_locales[$vn_locale_id]["code"]) ? $va_locales[$vn_locale_id]["code"] : "none"; foreach ($va_actual_data as $vs_f => $vs_v) { if (isset($va_vals_as_text[$vn_locale_id][$vs_val_id][$vs_f]) && $vs_v != $va_vals_as_text[$vn_locale_id][$vs_val_id][$vs_f]) { $va_actual_data[$vs_f . '_display'] = $va_vals_as_text[$vn_locale_id][$vs_val_id][$vs_f]; if ($vs_item_idno = caGetListItemIdno($va_actual_data[$vs_f])) { $va_actual_data[$vs_f] = $vs_item_idno; } } } $va_return['attributes'][$vs_code][$vs_locale_code][] = array_merge(array('locale' => $vs_locale_code), $va_actual_data); } } } } if (isset($va_flatten['locales'])) { $va_return['attributes'] = caExtractValuesByUserLocale($va_return['attributes']); } // relationships // yes, not all combinations between these tables have // relationships but it also doesn't hurt to query foreach ($this->opa_valid_tables as $vs_rel_table) { $t_rel = $o_dm->getInstanceByTableName($vs_rel_table, true); // // set-related hacks if ($this->ops_table == "ca_sets" && $vs_rel_table == "ca_tours") { // throw SQL error in getRelatedItems continue; } $va_related_items = $t_instance->get($vs_rel_table, array("returnAsArray" => true, 'returnLocaleCodes' => true, 'groupFields' => true)); if ($this->ops_table == "ca_objects" && $vs_rel_table == "ca_object_representations") { $va_versions = $t_instance->getMediaVersions('media'); if (isset($va_flatten['all'])) { $va_reps = $t_instance->getRepresentations(array('original')); $va_urls = array(); foreach ($va_reps as $vn_i => $va_rep) { $va_urls[] = $va_rep['urls']['original']; } $va_return['representations'] = join($vs_delimiter, $va_urls); } else { $va_return['representations'] = $t_instance->getRepresentations($va_versions); } foreach ($va_return['representations'] as $vn_i => $va_rep) { unset($va_return['representations'][$vn_i]['media']); unset($va_return['representations'][$vn_i]['media_metadata']); } } if (is_array($va_related_items) && sizeof($va_related_items) > 0) { foreach ($va_related_items as $va_rel_item) { $va_item_add = array(); foreach ($va_rel_item as $vs_fld => $vs_val) { if (!is_array($vs_val) && strlen(trim($vs_val)) > 0) { // rewrite and ignore certain field names switch ($vs_fld) { case 'item_type_id': $va_item_add[$vs_fld] = $vs_val; $va_item_add['type_id'] = $vs_val; break; default: $va_item_add[$vs_fld] = $vs_val; break; } } else { if (in_array($vs_fld, array('preferred_labels', 'intrinsic'))) { $va_item_add[$vs_fld] = $vs_val; } } } if ($vs_rel_table == "ca_object_representations") { $t_rep = new ca_object_representations($va_rel_item['representation_id']); $va_item_add['media'] = $t_rep->getMediaUrl('media', 'original'); } $va_return["related"][$vs_rel_table][] = $va_item_add; } } } return $va_return; }
/** * 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; }
$vs_setting_value = array($vs_setting_value); } $va_type_list = $t_mapping_instance->getTypeList(); $va_list = array(); foreach ($vs_setting_value as $vn_type_id) { if (!$va_type_list[$vn_type_id]['idno']) { continue; } $va_list[] = $va_type_list[$vn_type_id]['idno']; } if (is_array($va_list) && sizeof($va_list)) { print $va_setting_info['label'] . " = " . join("; ", $va_list) . "<br/>\n"; } break; case 'defaultLocale': $va_locale_list = $t_locale->getLocaleList(); if (isset($va_locale_list[$vs_setting_value])) { print $va_setting_info['label'] . " = " . $va_locale_list[$vs_setting_value]['name'] . "<br/>\n"; } break; case 'removeExistingRelationshipsOnUpdate': case 'removeExistingAttributesOnUpdate': print $va_setting_info['label'] . " = " . ((bool) $vs_setting_value ? _t('yes') : _t('no')) . "<br/>\n"; break; default: if (is_array($vs_setting_value) && sizeof($vs_setting_value)) { print $va_setting_info['label'] . " = " . join("; ", $vs_setting_value) . "<br/>\n"; } else { if (strlen($vs_setting_value)) { print $va_setting_info['label'] . " = " . $vs_setting_value . "<br/>\n"; }
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; } } } } }
/** * Get a record summary that looks reasonably close to what we expect to be passed to the * PUT portion of this very service. With this hack editing operations should be easier to handle. */ private function getItemInfoForEdit() { if (!($t_instance = $this->_getTableInstance($this->ops_table, $this->opn_id))) { return false; } $t_list = new ca_lists(); $t_locales = new ca_locales(); $va_locales = $t_locales->getLocaleList(array("available_for_cataloguing_only" => true)); $va_return = array(); // "intrinsic" fields foreach ($t_instance->getFieldsArray() as $vs_field_name => $va_field_info) { $vs_list = null; if (!is_null($vs_val = $t_instance->get($vs_field_name))) { if (preg_match("/^hier\\_/", $vs_field_name)) { continue; } if (preg_match("/\\_sort\$/", $vs_field_name)) { continue; } if ($vs_field_name == $t_instance->primaryKey()) { continue; } $va_return['intrinsic_fields'][$vs_field_name] = $vs_val; } } // preferred labels $va_labels = $t_instance->get($this->ops_table . ".preferred_labels", array("returnAllLocales" => true)); $va_labels = end($va_labels); if (is_array($va_labels)) { foreach ($va_labels as $vn_locale_id => $va_labels_by_locale) { foreach ($va_labels_by_locale as $va_tmp) { $va_label = array(); $va_label['locale'] = $va_locales[$vn_locale_id]["code"]; // add only UI fields to return foreach ($t_instance->getLabelUIFields() as $vs_label_fld) { $va_label[$vs_label_fld] = $va_tmp[$vs_label_fld]; } $va_return["preferred_labels"][] = $va_label; } } } // nonpreferred labels $va_labels = $t_instance->get($this->ops_table . ".nonpreferred_labels", array("returnAllLocales" => true)); $va_labels = end($va_labels); if (is_array($va_labels)) { foreach ($va_labels as $vn_locale_id => $va_labels_by_locale) { foreach ($va_labels_by_locale as $va_tmp) { $va_label = array(); $va_label['locale'] = $va_locales[$vn_locale_id]["code"]; // add only UI fields to return foreach ($t_instance->getLabelUIFields() as $vs_label_fld) { $va_label[$vs_label_fld] = $va_tmp[$vs_label_fld]; } $va_return["nonpreferred_labels"][] = $va_label; } } } // representations for objects if ($this->ops_table == "ca_objects") { $va_return['representations'] = $t_instance->getRepresentations(); } // attributes $va_codes = $t_instance->getApplicableElementCodes(); foreach ($va_codes as $vs_code) { if ($va_vals = $t_instance->get($this->ops_table . "." . $vs_code, array("convertCodesToDisplayText" => false, "returnAllLocales" => true))) { $va_vals_by_locale = end($va_vals); // I seriously have no idea what that additional level of nesting in the return format is for foreach ($va_vals_by_locale as $vn_locale_id => $va_locale_vals) { foreach ($va_locale_vals as $vs_val_id => $va_actual_data) { if (!is_array($va_actual_data)) { continue; } $vs_locale_code = isset($va_locales[$vn_locale_id]["code"]) ? $va_locales[$vn_locale_id]["code"] : "none"; $va_return['attributes'][$vs_code][] = array_merge(array('locale' => $vs_locale_code), $va_actual_data); } } } } // relationships // yes, not all combinations between these tables have // relationships but it also doesn't hurt to query foreach ($this->opa_valid_tables as $vs_rel_table) { // // set-related hacks if ($this->ops_table == "ca_sets" && $vs_rel_table == "ca_tours") { // throw SQL error in getRelatedItems continue; } $va_related_items = $t_instance->get($vs_rel_table, array("returnAsArray" => true)); if ($this->ops_table == "ca_objects" && $vs_rel_table == "ca_object_representations") { $va_return['representations'] = $t_instance->getRepresentations(array('preview170', 'medium')); } if (is_array($va_related_items) && sizeof($va_related_items) > 0) { // most of the fields are usually empty because they are not supported on UI level foreach ($va_related_items as $va_rel_item) { $va_item_add = array(); foreach ($va_rel_item as $vs_fld => $vs_val) { if (!is_array($vs_val) && strlen(trim($vs_val)) > 0) { // rewrite and ignore certain field names switch ($vs_fld) { case 'relationship_type_id': $va_item_add['type_id'] = $vs_val; break; default: $va_item_add[$vs_fld] = $vs_val; break; } } } $va_return["related"][$vs_rel_table][] = $va_item_add; } } } return $va_return; }
/** * * @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; }
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_screen = new ca_editor_ui_screens($this->getPrimaryKey()); $va_placements = $t_screen->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_screen->removePlacement($va_bundle_info['placement_id'], array('user_id' => $po_request->getUserID())); if ($t_screen->numErrors()) { $this->errors = $t_screen->errors; return false; } } } $va_locale_list = ca_locales::getLocaleList(array('index_by_code' => true)); $va_available_bundles = $t_screen->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]+)_(.*)\$!", $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; // NOTE: we set keys for both locale_id (which how other placement-using editor like ca_search_forms and // ca_bundle_displays do) *AND* the locale code (eg. "en_US"). This is because the settings created in profile and // in pre v1.1 systems are keyed by code, not locale_id. There's nothing wrong with using code - it's just as unique as the locale_id // and it's convenient to use both interchangeably in any event. // $va_settings[(int) $va_matches[1]][$va_locale_matches[1]][$vn_locale_id] = $va_settings[(int) $va_matches[1]][$va_locale_matches[1]][$va_locale_matches[2]] = $vs_val; } else { $va_settings[(int) $va_matches[1]][$va_matches[2]] = $vs_val; } } } if ($vn_placement_id === 0) { $t_screen->addPlacement($vs_bundle, $vs_bundle . ($vn_i + 1), $va_settings[$vn_placement_id], $vn_i + 1, array('user_id' => $po_request->getUserID(), 'additional_settings' => $va_available_bundles[$vs_bundle]['settings'])); if ($t_screen->numErrors()) { $this->errors = $t_screen->errors; return false; } } else { $t_placement = new ca_editor_ui_bundle_placements($vn_placement_id, $va_available_bundles[$vs_bundle]['settings']); $t_placement->setMode(ACCESS_WRITE); $t_placement->set('rank', $vn_i + 1); if (is_array($va_settings[$vn_placement_id])) { 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; } } } } return true; }
public function __construct($po_engine_result = null, $pa_tables = null) { if (!SearchResult::$opo_datamodel) { SearchResult::$opo_datamodel = Datamodel::load(); } $this->opo_db = SearchResult::$o_db ? SearchResult::$o_db : (SearchResult::$o_db = new Db()); $this->opo_subject_instance = SearchResult::$opo_datamodel->getInstanceByTableName($this->ops_table_name, true); $this->ops_subject_pk = $this->opo_subject_instance->primaryKey(); $this->ops_subject_idno = $this->opo_subject_instance->getProperty('ID_NUMBERING_ID_FIELD'); $this->opb_use_identifiers_in_urls = (bool) $this->opo_subject_instance->getAppConfig()->get('use_identifiers_in_urls'); $this->opa_row_ids_to_prefetch_cache = array(); if (!SearchResult::$opo_locales) { SearchResult::$opo_locales = new ca_locales(); } if (!SearchResult::$opa_locales) { SearchResult::$opa_locales = ca_locales::getLocaleList(); } if (!SearchResult::$opt_list) { SearchResult::$opt_list = SearchResult::$opo_datamodel->getInstanceByTableName('ca_lists', true); } if ($po_engine_result) { $this->init($po_engine_result, $pa_tables); } if (!$GLOBALS["_DbResult_time_expression_parser"]) { $GLOBALS["_DbResult_time_expression_parser"] = new TimeExpressionParser(); } if (!$GLOBALS["_DbResult_timecodeparser"]) { $GLOBALS["_DbResult_timecodeparser"] = new TimecodeParser(); } if (!$GLOBALS["_DbResult_mediainfocoder"]) { $GLOBALS["_DbResult_mediainfocoder"] = MediaInfoCoder::load(); } if (!$GLOBALS["_DbResult_fileinfocoder"]) { $GLOBALS["_DbResult_fileinfocoder"] = FileInfoCoder::load(); } // valid options and defaults $this->opa_options = array('prefetch' => 50, 'prefetchAttributes' => null); $this->opo_tep = $GLOBALS["_DbResult_time_expression_parser"]; if (!is_array(self::$s_template_prefetch_cache)) { self::$s_template_prefetch_cache = array(); } }
/** * 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 * * @param RequestHTTP $po_request * @param array|null $pa_options * @return mixed */ 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 { switch ($va_properties['formatType']) { case FT_BIT: // skip bits if they're not set in the form; otherwise they default to 'No' even // though the default might be set to 'Yes' the settings definition. $vs_val = $po_request->getParameter("{$vs_placement_code}{$vs_id_prefix}{$vs_setting}", pString); if ($vs_val == '') { continue; } $va_values[$vs_setting] = $vs_val; break; default: $va_values[$vs_setting] = $po_request->getParameter("{$vs_placement_code}{$vs_id_prefix}{$vs_setting}", pString); break; } } } foreach ($va_values as $vs_setting_key => $vs_value) { $this->setSetting($vs_setting, $vs_value); } } return $this->o_instance->update(); }
/** * Returns the locale name of the specified locale_id, or null if the id is invalid. Note that this does not * change the state of the model - it just returns the locale. * * @param int $pn_id The locale_id of the locale, or null if the code is invalid * @return string The name of the locale */ public function localeIDToName($pn_id) { if (!isset(ca_locales::$s_locale_id_to_name[$pn_id])) { ca_locales::getLocaleList(); } return ca_locales::$s_locale_id_to_name[$pn_id]; }
/** * Returns the locale name of the specified locale_id, or null if the id is invalid. Note that this does not * change the state of the model - it just returns the locale. * * @param int $pn_id The locale_id of the locale, or null if the code is invalid * @return string The name of the locale */ public function localeIDToName($pn_id) { if (!MemoryCache::contains($pn_id, 'LocaleIdToName')) { ca_locales::getLocaleList(); } return MemoryCache::fetch($pn_id, 'LocaleIdToName'); }
/** * 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; }
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; }