/** * * */ protected function GetHierarchyLevelData($pa_ids) { $vo_dm = Datamodel::load(); $o_config = Configuration::load(); $t_object = new ca_objects(); $va_level_data = array(); foreach ($pa_ids as $pn_id) { $va_params = $this->getItemIDComponents($pn_id); $vs_table = $va_params['table']; $vn_id = $va_params['id']; $vn_start = $va_params['start']; $vn_item_count = 0; $t_item = $vo_dm->getInstanceByTableName($vs_table, true); $vs_label_table_name = $t_item->getLabelTableName(); $vs_label_display_field_name = $t_item->getLabelDisplayField(); $vs_pk = $t_item->primaryKey(); if ($vn_start < 0) { $vn_start = 0; } $va_items_for_locale = array(); if (!$vn_id && method_exists($t_item, "getHierarchyList")) { $vn_id = $this->request->getParameter('root_item_id', pString); $va_params = $this->getItemIDComponents($vn_id); $vs_table = $va_params['table']; $vn_id = $va_params['id']; $vn_start = $va_params['start']; $t_item = $vo_dm->getInstanceByTableName($vs_table, true); $vs_label_table_name = $t_item->getLabelTableName(); $vs_label_display_field_name = $t_item->getLabelDisplayField(); $vs_pk = $t_item->primaryKey(); $t_item->load($vn_id); $va_tmp = array($vs_pk => $vn_id = $t_item->get($vs_table . '.' . $vs_pk), 'item_id' => $vs_table . '-' . $vn_id, 'parent_id' => $t_item->get($vs_table . '.parent_id'), 'idno' => $t_item->get($vs_table . '.idno'), $vs_label_display_field_name => $t_item->get($vs_table . '.preferred_labels.' . $vs_label_display_field_name), 'locale_id' => $t_item->get($vs_table . '.' . 'locale_id')); if (!$va_tmp[$vs_label_display_field_name]) { $va_tmp[$vs_label_display_field_name] = $va_tmp['idno']; } if (!$va_tmp[$vs_label_display_field_name]) { $va_tmp[$vs_label_display_field_name] = '???'; } if (!($vs_item_template = trim($o_config->get("{$vs_table}_hierarchy_browser_display_settings")))) { $vs_item_template = "^{$vs_table}.preferred_labels.{$vs_label_display_field_name}"; } $va_tmp['name'] = caProcessTemplateForIDs($vs_item_template, $vs_table, array($va_tmp[$vs_pk])); // Child count is only valid if has_children is not null $va_tmp['children'] = $t_item->get('has_children') ? (int) $t_item->get('child_count') : 1; // TODO: fix $va_items[$va_tmp[$vs_pk]][$va_tmp['locale_id']] = $va_tmp; $va_items_for_locale = caExtractValuesByUserLocale($va_items); } else { if ($t_item->load($vn_id)) { // id is the id of the parent for the level we're going to return $va_additional_wheres = array(); $t_label_instance = $t_item->getLabelTableInstance(); if ($t_label_instance && $t_label_instance->hasField('is_preferred')) { $va_additional_wheres[] = "(({$vs_label_table_name}.is_preferred = 1) OR ({$vs_label_table_name}.is_preferred IS NULL))"; } if (!is_array($va_sorts = $o_config->getList($vs_table . '_hierarchy_browser_sort_values')) || !sizeof($va_sorts)) { $va_sorts = null; } foreach ($va_sorts as $vn_i => $vs_sort_fld) { $va_tmp = explode(".", $vs_sort_fld); if ($va_tmp[1] == 'preferred_labels') { $va_tmp[0] = $vs_label_table_name; if (!($va_tmp[1] = $va_tmp[2])) { $va_tmp[1] = $vs_label_display_field_name; } unset($va_tmp[2]); $va_sorts[$vn_i] = join(".", $va_tmp); } } if (!in_array($vs_sort_dir = strtolower($o_config->get($vs_table . '_hierarchy_browser_sort_direction')), array('asc', 'desc'))) { $vs_sort_dir = 'asc'; } $qr_children = $t_item->getHierarchyChildrenAsQuery($t_item->getPrimaryKey(), array('additionalTableToJoin' => $vs_label_table_name, 'additionalTableJoinType' => 'LEFT', 'additionalTableSelectFields' => array($vs_label_display_field_name, 'locale_id'), 'additionalTableWheres' => $va_additional_wheres, 'returnChildCounts' => true, 'sort' => $va_sorts, 'sortDirection' => $vs_sort_dir)); $va_items = array(); if (!($vs_item_template = trim($o_config->get("{$vs_table}_hierarchy_browser_display_settings")))) { $vs_item_template = "^{$vs_table}.preferred_labels.{$vs_label_display_field_name}"; } $va_child_counts = array(); if (($vn_max_items_per_page = $this->request->getParameter('max', pInteger)) < 1 || $vn_max_items_per_page > 1000) { $vn_max_items_per_page = null; } $vn_c = 0; $vn_item_count = $qr_children->numRows(); $qr_children->seek($vn_start); while ($qr_children->nextRow()) { $va_tmp = array($vs_pk => $vn_id = $qr_children->get($vs_table . '.' . $vs_pk), 'item_id' => $vs_table . '-' . $vn_id, 'parent_id' => $qr_children->get($vs_table . '.parent_id'), 'idno' => $qr_children->get($vs_table . '.idno'), 'locale_id' => $qr_children->get($vs_table . '.' . 'locale_id')); if (!$va_tmp[$vs_label_display_field_name]) { $va_tmp[$vs_label_display_field_name] = $va_tmp['idno']; } if (!$va_tmp[$vs_label_display_field_name]) { $va_tmp[$vs_label_display_field_name] = '???'; } $va_tmp['name'] = caProcessTemplateForIDs($vs_item_template, $vs_table, array($va_tmp[$vs_pk])); // Child count is only valid if has_children is not null $va_tmp['children'] = $qr_children->get('has_children') ? (int) $qr_children->get('child_count') : 0; if (is_array($va_sorts)) { $vs_sort_acc = array(); foreach ($va_sorts as $vs_sort) { $vs_sort_acc[] = $qr_children->get($vs_sort); } $va_tmp['sort'] = join(";", $vs_sort_acc); } $va_items[$va_tmp['item_id']][$va_tmp['locale_id']] = $va_tmp; $vn_c++; if (!is_null($vn_max_items_per_page) && $vn_c >= $vn_max_items_per_page) { break; } } if ($t_item->tableName() == 'ca_collections') { $vs_object_collection_rel_type = $o_config->get('ca_objects_x_collections_hierarchy_relationship_type'); $va_cross_table_items = $t_item->getRelatedItems('ca_objects', array('restrictToRelationshipTypes' => array($vs_object_collection_rel_type))); $vn_item_count += sizeof($va_cross_table_items); $va_ids = array(); foreach ($va_cross_table_items as $vn_x_item_id => $va_x_item) { $va_items['ca_objects-' . $vn_x_item_id][$va_x_item['locale_id']] = $va_x_item; $va_items['ca_objects-' . $va_x_item['object_id']][$va_x_item['locale_id']]['item_id'] = 'ca_objects-' . $va_x_item['object_id']; $va_items['ca_objects-' . $va_x_item['object_id']][$va_x_item['locale_id']]['parent_id'] = $vn_id; unset($va_items['ca_objects-' . $vn_x_item_id][$va_x_item['locale_id']]['labels']); $va_items['ca_objects-' . $va_x_item['object_id']][$va_x_item['locale_id']]['children'] = 0; $va_ids[] = $va_x_item['object_id']; } if (!($vs_item_template = trim($o_config->get("ca_objects_hierarchy_browser_display_settings")))) { $vs_item_template = "^ca_objects.preferred_labels.name"; } if (sizeof($va_ids)) { $va_child_counts = $t_object->getHierarchyChildCountsForIDs($va_ids); $va_templates = caProcessTemplateForIDs($vs_item_template, 'ca_objects', $va_ids, array('returnAsArray' => true)); foreach ($va_child_counts as $vn_id => $vn_c) { $va_items['ca_objects-' . $vn_id][$va_x_item['locale_id']]['children'] = $vn_c; } foreach ($va_ids as $vn_i => $vn_id) { $va_items['ca_objects-' . $vn_id][$va_x_item['locale_id']]['name'] = $va_templates[$vn_i]; } } } $va_items_for_locale = caExtractValuesByUserLocale($va_items); $vs_rank_fld = $t_item->getProperty('RANK'); $va_sorted_items = array(); foreach ($va_items_for_locale as $vn_id => $va_node) { $vs_key = caSortableValue(mb_strtolower(preg_replace('![^A-Za-z0-9]!', '_', caRemoveAccents($va_node['name'])))) . "_" . $vn_id; if (isset($va_node['sort']) && $va_node['sort']) { $va_sorted_items[$va_node['sort']][$vs_key] = $va_node; } else { if ($vs_rank_fld && ($vs_rank = (int) sprintf("%08d", $va_node[$vs_rank_fld]))) { $va_sorted_items[$vs_rank][$vs_key] = $va_node; } else { $va_sorted_items[$vs_key][$vs_key] = $va_node; } } } ksort($va_sorted_items); if ($vs_sort_dir == 'desc') { $va_sorted_items = array_reverse($va_sorted_items); } $va_items_for_locale = array(); foreach ($va_sorted_items as $vs_k => $va_v) { ksort($va_v); if ($vs_sort_dir == 'desc') { $va_v = array_reverse($va_v); } $va_items_for_locale = array_merge($va_items_for_locale, $va_v); } } } $va_items_for_locale['_sortOrder'] = array_keys($va_items_for_locale); $va_items_for_locale['_primaryKey'] = $t_item->primaryKey(); // pass the name of the primary key so the hierbrowser knows where to look for item_id's $va_items_for_locale['_itemCount'] = $vn_item_count; //$qr_children ? $qr_children->numRows() : 0; $va_level_data[$pn_id] = $va_items_for_locale; } return $va_level_data; }
/** * 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'); $t_locale = new ca_locales(); $vs_display_value = caSortableValue($this->get($vs_display_field), array('locale' => $t_locale->localeIDToCode($this->get('locale_id')))); $this->set($vs_sort_field, $vs_display_value); } }
/** * Given a item_id (request parameter 'id') returns a list of direct children for use in the hierarchy browser * Returned data is JSON format */ public function GetHierarchyLevel() { header("Content-type: application/json"); $ps_bundle = (string) $this->request->getParameter('bundle', pString); $pa_ids = explode(";", $ps_ids = $this->request->getParameter('id', pString)); if (!sizeof($pa_ids)) { $pa_ids = array(null); } $t_item = $this->opo_item_instance; if (!$t_item->isHierarchical()) { return; } $va_level_data = array(); foreach ($pa_ids as $pn_id) { $va_tmp = explode(":", $pn_id); $vn_id = $va_tmp[0]; $vn_start = (int) $va_tmp[1]; if ($vn_start < 0) { $vn_start = 0; } if (sizeof($va_tmp) < 2) { $pn_id = '0:0'; } $va_items_for_locale = array(); $vb_gen = true; if (!$vn_id && method_exists($t_item, "getHierarchyList")) { $vn_id = (int) $this->request->getParameter('root_item_id', pInteger); $t_item->load($vn_id); // no id so by default return list of available hierarchies $va_items_for_locale = $t_item->getHierarchyList(); if (sizeof($va_items_for_locale) == 1 && $this->request->getAppConfig()->get($t_item->tableName() . '_hierarchy_browser_hide_root')) { $va_item = array_shift($va_items_for_locale); $vn_id = $va_item['item_id']; } else { $vb_gen = false; } } if ($vb_gen && $t_item->load($vn_id)) { // id is the id of the parent for the level we're going to return $vs_table_name = $t_item->tableName(); $vs_label_table_name = $this->opo_item_instance->getLabelTableName(); $vs_label_display_field_name = $this->opo_item_instance->getLabelDisplayField(); $vs_pk = $this->opo_item_instance->primaryKey(); $va_additional_wheres = array(); $t_label_instance = $this->opo_item_instance->getLabelTableInstance(); if ($t_label_instance && $t_label_instance->hasField('is_preferred')) { $va_additional_wheres[] = "(({$vs_label_table_name}.is_preferred = 1) OR ({$vs_label_table_name}.is_preferred IS NULL))"; } $o_config = Configuration::load(); if (!is_array($va_sorts = $o_config->getList($this->ops_table_name . '_hierarchy_browser_sort_values')) || !sizeof($va_sorts)) { $va_sorts = array(); } foreach ($va_sorts as $vn_i => $vs_sort_fld) { $va_tmp = explode(".", $vs_sort_fld); if ($va_tmp[1] == 'preferred_labels') { $va_tmp[0] = $vs_label_table_name; if (!($va_tmp[1] = $va_tmp[2])) { $va_tmp[1] = $vs_label_display_field_name; } unset($va_tmp[2]); $va_sorts[$vn_i] = join(".", $va_tmp); } } if (!in_array($vs_sort_dir = strtolower($o_config->get($this->ops_table_name . '_hierarchy_browser_sort_direction')), array('asc', 'desc'))) { $vs_sort_dir = 'asc'; } $va_items = array(); if (is_array($va_item_ids = $t_item->getHierarchyChildren($t_item->getPrimaryKey(), array('idsOnly' => true))) && sizeof($va_item_ids)) { $qr_children = $t_item->makeSearchResult($t_item->tableName(), $va_item_ids); $va_child_counts = $t_item->getHierarchyChildCountsForIDs($va_item_ids); if (!($vs_item_template = trim($o_config->get("{$vs_table_name}_hierarchy_browser_display_settings")))) { $vs_item_template = "^{$vs_table_name}.preferred_labels.{$vs_label_display_field_name}"; } if (($vn_max_items_per_page = $this->request->getParameter('max', pInteger)) < 1 || $vn_max_items_per_page > 1000) { $vn_max_items_per_page = 100; } $vn_c = 0; while ($qr_children->nextHit()) { $va_tmp = array($vs_pk => $vn_id = $qr_children->get($this->ops_table_name . '.' . $vs_pk), 'item_id' => $vn_id, 'parent_id' => $qr_children->get($this->ops_table_name . '.parent_id'), 'idno' => $qr_children->get($this->ops_table_name . '.idno'), $vs_label_display_field_name => $qr_children->get($this->ops_table_name . '.preferred_labels.' . $vs_label_display_field_name), 'locale_id' => $qr_children->get($this->ops_table_name . '.' . 'locale_id')); if (!$va_tmp[$vs_label_display_field_name]) { $va_tmp[$vs_label_display_field_name] = $va_tmp['idno']; } if (!$va_tmp[$vs_label_display_field_name]) { $va_tmp[$vs_label_display_field_name] = '???'; } $va_tmp['name'] = caProcessTemplateForIDs($vs_item_template, $vs_table_name, array($va_tmp[$vs_pk]), array('requireLinkTags' => true)); if (!$va_tmp['name']) { $va_tmp['name'] = '??? ' . $va_tmp[$vs_pk]; } // Child count is only valid if has_children is not null $va_tmp['children'] = isset($va_child_counts[$vn_id]) ? (int) $va_child_counts[$vn_id] : 0; if (strlen($vs_enabled = $qr_children->get('is_enabled')) > 0) { $va_tmp['is_enabled'] = $vs_enabled; } if (is_array($va_sorts)) { $vs_sort_acc = array(); foreach ($va_sorts as $vs_sort) { $vs_sort_acc[] = $qr_children->get($vs_sort, array('sortable' => true)); } $va_tmp['sort'] = join(";", $vs_sort_acc); } $va_items[$va_tmp[$vs_pk]][$va_tmp['locale_id']] = $va_tmp; $vn_c++; } $va_items_for_locale = caExtractValuesByUserLocale($va_items); } $vs_rank_fld = $t_item->getProperty('RANK'); $va_sorted_items = array(); foreach ($va_items_for_locale as $vn_id => $va_node) { $vs_key = caSortableValue(mb_strtolower(preg_replace('![^A-Za-z0-9]!', '_', caRemoveAccents($va_node['name'])))) . "_" . $vn_id; if (isset($va_node['sort']) && $va_node['sort']) { $va_sorted_items[$va_node['sort']][$vs_key] = $va_node; } else { if ($vs_rank_fld && ($vs_rank = (int) sprintf("%08d", $va_node[$vs_rank_fld]))) { $va_sorted_items[$vs_rank][$vs_key] = $va_node; } else { $va_sorted_items[$vs_key][$vs_key] = $va_node; } } } ksort($va_sorted_items); if ($vs_sort_dir == 'desc') { $va_sorted_items = array_reverse($va_sorted_items); } $va_items_for_locale = array(); $va_sorted_items = array_slice($va_sorted_items, $vn_start, $vn_max_items_per_page); foreach ($va_sorted_items as $vs_k => $va_v) { ksort($va_v); if ($vs_sort_dir == 'desc') { $va_v = array_reverse($va_v); } $va_items_for_locale = array_merge($va_items_for_locale, $va_v); } } $va_items_for_locale['_sortOrder'] = array_keys($va_items_for_locale); $va_items_for_locale['_primaryKey'] = $t_item->primaryKey(); // pass the name of the primary key so the hierbrowser knows where to look for item_id's $va_items_for_locale['_itemCount'] = $qr_children ? $qr_children->numHits() : 0; $va_level_data[$pn_id] = $va_items_for_locale; } if (!$this->request->getParameter('init', pInteger)) { // only set remember "last viewed" if the load is done interactively // if the GetHierarchyLevel() call is part of the initialization of the hierarchy browser // then all levels are loaded, sometimes out-of-order; if we record these initialization loads // as the 'last viewed' we can end up losing the true 'last viewed' value // // ... so the hierbrowser passes an extra 'init' parameters set to 1 if the GetHierarchyLevel() call // is part of a browser initialization $this->request->session->setVar($this->ops_table_name . '_' . $ps_bundle . '_browse_last_id', array_pop($pa_ids)); } $this->view->setVar(str_replace(' ', '_', $this->ops_name_singular) . '_list', $va_level_data); return $this->render(str_replace(' ', '_', $this->ops_name_singular) . '_hierarchy_level_json.php'); }
/** * Given a item_id (request parameter 'id') returns a list of direct children for use in the hierarchy browser * Returned data is JSON format */ public function GetHierarchyLevel() { $ps_bundle = (string) $this->request->getParameter('bundle', pString); $pa_ids = explode(";", $ps_ids = $this->request->getParameter('id', pString)); if (!sizeof($pa_ids)) { $pa_ids = array(null); } $t_item = $this->opo_item_instance; $vs_template = $t_item->getAppConfig()->get('ca_list_items_hierarchy_browser_display_settings'); $va_lists = array(); if ($ps_lists = $this->request->getParameter('lists', pString)) { $va_lists = explode(";", $ps_lists); } if (($vn_max_items_per_page = $this->request->getParameter('max', pInteger)) < 1 || $vn_max_items_per_page > 1000) { $vn_max_items_per_page = 100; } $va_level_data = array(); foreach ($pa_ids as $pn_id) { $va_tmp = explode(":", $pn_id); $vn_id = $va_tmp[0]; $vn_start = (int) $va_tmp[1]; if ($vn_start < 0) { $vn_start = 0; } if (!$vn_id && method_exists($t_item, "getHierarchyList")) { if (!($pn_list_id = $this->request->getParameter('list_id', pInteger))) { // no id so by default return list of available hierarchies $va_list_items = $t_item->getHierarchyList(); if (sizeof($va_lists)) { // filter out lists that weren't specified foreach ($va_list_items as $vn_list_id => $va_list) { if (!in_array($vn_list_id, $va_lists) && !in_array($va_list['list_code'], $va_lists)) { unset($va_list_items[$vn_list_id]); } } } else { if ($this->request->getParameter('voc', pInteger)) { // Only show vocabularies foreach ($va_list_items as $vn_list_id => $va_list) { if (!$va_list['use_as_vocabulary']) { unset($va_list_items[$vn_list_id]); } } } } } } else { if ($t_item->load($vn_id)) { // id is the id of the parent for the level we're going to return $t_list = new ca_lists($vn_list_id = $t_item->get('list_id')); $vs_label_table_name = $this->opo_item_instance->getLabelTableName(); $vs_label_display_field_name = $this->opo_item_instance->getLabelDisplayField(); $va_list_items = $t_list->getItemsForList($vn_list_id, array('returnHierarchyLevels' => false, 'item_id' => $vn_id, 'extractValuesByUserLocale' => true, 'sort' => $t_list->get('sort_type'), 'directChildrenOnly' => true, 'limit' => $vn_max_items_per_page, 'start' => $vn_start)); // output $va_display_values = caProcessTemplateForIDs($vs_template, 'ca_list_items', array_keys($va_list_items), array('requireLinkTags' => true, 'returnAsArray' => true)); $vn_c = 0; foreach ($va_list_items as $vn_item_id => $va_item) { if ($vn_c >= $vn_start) { unset($va_item['description']); unset($va_item['icon']); if (!$va_item[$vs_label_display_field_name]) { $va_item[$vs_label_display_field_name] = $va_item['idno']; } if (!$va_item[$vs_label_display_field_name]) { $va_item[$vs_label_display_field_name] = '???'; } $va_item['name'] = $va_display_values[$vn_c]; if (!$va_item['name']) { $va_item['name'] = '??? ' . $vn_item_id; } $va_item['table'] = 'ca_list_items'; // Child count is only valid if has_children is not null $va_item['children'] = 0; $va_list_items[$vn_item_id] = $va_item; } $vn_c++; if ($vn_c > $vn_start + $vn_max_items_per_page) { break; } } if (sizeof($va_list_items)) { $o_db = new Db(); $qr_res = $o_db->query("\n\t\t\t\t\t\t\t\tSELECT count(*) c, parent_id\n\t\t\t\t\t\t\t\tFROM ca_list_items\n\t\t\t\t\t\t\t\tWHERE \n\t\t\t\t\t\t\t\t\tparent_id IN (" . join(",", array_keys($va_list_items)) . ") AND deleted = 0\n\t\t\t\t\t\t\t\tGROUP BY parent_id\n\t\t\t\t\t\t\t"); while ($qr_res->nextRow()) { $va_list_items[$qr_res->get('parent_id')]['children'] = $qr_res->get('c'); } } } } $va_list_items_sortable = []; foreach ($va_list_items as $vn_item_id => $va_item) { $va_list_items_sortable[caSortableValue(mb_strtolower(preg_replace('![^A-Za-z0-9]!', '_', caRemoveAccents($va_item['name_plural'])))) . '_' . $vn_item_id] = $va_item; } $va_list_items = $va_list_items_sortable; $va_list_items['_sortOrder'] = array_keys($va_list_items); $va_list_items['_primaryKey'] = $t_item->primaryKey(); // pass the name of the primary key so the hierbrowser knows where to look for item_id's $va_list_items['_itemCount'] = sizeof($va_list_items); //$t_list ? $t_list->numItemsInList() : ($qr_res ? $qr_res->numRows() : 0); $va_level_data[$pn_id] = $va_list_items; } if (!$this->request->getParameter('init', pInteger)) { // only set remember "last viewed" if the load is done interactively // if the GetHierarchyLevel() call is part of the initialization of the hierarchy browser // then all levels are loaded, sometimes out-of-order; if we record these initialization loads // as the 'last viewed' we can end up losing the true 'last viewed' value // // ... so the hierbrowser passes an extra 'init' parameters set to 1 if the GetHierarchyLevel() call // is part of a browser initialization $this->request->session->setVar($this->ops_table_name . '_' . $ps_bundle . '_browse_last_id', $pn_id); } $this->view->setVar('dontShowSymbols', (bool) $this->request->getParameter('noSymbols', pString)); $this->view->setVar('list_item_list', $va_level_data); return $this->render('list_item_hierarchy_level_json.php'); }