/**
  * Options:
  *		appendToSearch = optional text to be AND'ed wuth current search expression
  *		output_format = determines format out search result output. "PDF" and "HTML" are currently supported; "HTML" is the default
  *		view = view with path relative to controller to use overriding default ("search/<table_name>_search_basic_html.php")
  *		vars = associative array with key value pairs to assign to the view
  *
  * Callbacks:
  * 		hookBeforeNewSearch() is called just before executing a new search. The first parameter is the BrowseEngine object containing the search.
  */
 public function Index($pa_options = null)
 {
     $po_search = isset($pa_options['search']) ? $pa_options['search'] : null;
     if (isset($pa_options['saved_search']) && $pa_options['saved_search']) {
         $this->opo_result_context->setSearchExpression($pa_options['saved_search']['search']);
         $this->opo_result_context->isNewSearch(true);
     }
     parent::Index($pa_options);
     AssetLoadManager::register('hierBrowser');
     AssetLoadManager::register('browsable');
     // need this to support browse panel when filtering/refining search results
     $t_model = $this->opo_datamodel->getInstanceByTableName($this->ops_tablename, true);
     $va_access_values = caGetUserAccessValues($this->request);
     // Get elements of result context
     $vn_page_num = $this->opo_result_context->getCurrentResultsPageNumber();
     $vs_search = $this->opo_result_context->getSearchExpression();
     $vb_is_new_search = $this->opo_result_context->isNewSearch();
     if ((bool) $this->request->getParameter('reset', pString) && $this->request->getParameter('reset', pString) != 'save') {
         $vs_search = '';
         $vb_is_new_search = true;
     }
     if (!($vn_items_per_page = $this->opo_result_context->getItemsPerPage())) {
         $vn_items_per_page = $this->opn_items_per_page_default;
         $this->opo_result_context->setItemsPerPage($vn_items_per_page);
     }
     if (!($vs_view = $this->opo_result_context->getCurrentView())) {
         $va_tmp = array_keys($this->opa_views);
         $vs_view = $this->ops_view_default ? $this->ops_view_default : array_shift($va_tmp);
         $this->opo_result_context->setCurrentView($vs_view);
     }
     if (!isset($this->opa_views[$vs_view])) {
         $va_tmp = array_keys($this->opa_views);
         $vs_view = array_shift($va_tmp);
     }
     if (!($vs_sort = $this->opo_result_context->getCurrentSort())) {
         $va_tmp = array_keys($this->opa_sorts);
         $vs_sort = array_shift($va_tmp);
     }
     $vs_sort_direction = $this->opo_result_context->getCurrentSortDirection();
     $vb_sort_has_changed = $this->opo_result_context->sortHasChanged();
     if (!$this->opn_type_restriction_id) {
         $this->opn_type_restriction_id = '';
     }
     $this->view->setVar('type_id', $this->opn_type_restriction_id);
     MetaTagManager::setWindowTitle(_t('%1 search', $this->searchName('plural')));
     // Get attribute sorts
     $va_sortable_elements = ca_metadata_elements::getSortableElements($this->ops_tablename, $this->opn_type_restriction_id);
     if (!is_array($this->opa_sorts)) {
         $this->opa_sorts = array();
     }
     foreach ($va_sortable_elements as $va_sortable_element) {
         $this->opa_sorts[$this->ops_tablename . '.' . $va_sortable_element['element_code']] = $va_sortable_element['display_label'];
     }
     if ($pa_options['appendToSearch']) {
         $vs_append_to_search .= " AND (" . $pa_options['appendToSearch'] . ")";
     }
     //
     // Execute the search
     //
     if ($vs_search && $vs_search != "") {
         /* any request? */
         $va_search_opts = array('sort' => $vs_sort, 'sort_direction' => $vs_sort_direction, 'appendToSearch' => $vs_append_to_search, 'checkAccess' => $va_access_values, 'no_cache' => $vb_is_new_search, 'dontCheckFacetAvailability' => true, 'filterNonPrimaryRepresentations' => true);
         if ($vb_is_new_search || isset($pa_options['saved_search']) || is_subclass_of($po_search, "BrowseEngine") && !$po_search->numCriteria()) {
             $vs_browse_classname = get_class($po_search);
             $po_search = new $vs_browse_classname();
             if (is_subclass_of($po_search, "BrowseEngine")) {
                 $po_search->addCriteria('_search', $vs_search);
                 if (method_exists($this, "hookBeforeNewSearch")) {
                     $this->hookBeforeNewSearch($po_search);
                 }
             }
             $this->opo_result_context->setParameter('show_type_id', null);
         }
         if ($this->opn_type_restriction_id) {
             $po_search->setTypeRestrictions(array($this->opn_type_restriction_id), array('includeSubtypes' => false));
         }
         $vb_criteria_have_changed = false;
         if (is_subclass_of($po_search, "BrowseEngine")) {
             //
             // Restrict facets to specific group for main browse landing page (if set in app.conf config)
             //
             if ($vs_facet_group = $this->request->config->get($this->ops_tablename . '_search_refine_facet_group')) {
                 $po_search->setFacetGroup($vs_facet_group);
             }
             $vb_criteria_have_changed = $po_search->criteriaHaveChanged();
             $po_search->execute($va_search_opts);
             $this->opo_result_context->setParameter('browse_id', $po_search->getBrowseID());
             if ($vs_group_name = $this->request->config->get('browse_facet_group_for_' . $this->ops_tablename)) {
                 $po_search->setFacetGroup($vs_group_name);
             }
             $vo_result = $po_search->getResults($va_search_opts);
         } else {
             $vo_result = $po_search->search($vs_search, $va_search_opts);
         }
         $vo_result = isset($pa_options['result']) ? $pa_options['result'] : $vo_result;
         $this->opo_result_context->validateCache();
         // Only prefetch what we need
         $vo_result->setOption('prefetch', $vn_items_per_page);
         //
         // Handle details of partitioning search results by type, if required
         //
         if ((bool) $this->request->config->get('search_results_partition_by_type')) {
             $va_type_counts = $vo_result->getResultCountForFieldValues(array('ca_objects.type_id'));
             $va_type_counts_obj_type = $va_type_counts['ca_objects.type_id'];
             ksort($va_type_counts_obj_type);
             $this->view->setVar('counts_by_type', $va_type_counts_obj_type);
             $vn_show_type_id = $this->opo_result_context->getParameter('show_type_id');
             if (!isset($va_type_counts_obj_type[$vn_show_type_id])) {
                 $va_tmp = array_keys($va_type_counts_obj_type);
                 $vn_show_type_id = array_shift($va_tmp);
             }
             $this->view->setVar('show_type_id', $vn_show_type_id);
             $vo_result->filterResult('ca_objects.type_id', $vn_show_type_id);
         }
         if ($vb_is_new_search || $vb_criteria_have_changed || $vb_sort_has_changed) {
             $this->opo_result_context->setResultList($vo_result->getPrimaryKeyValues());
             $this->opo_result_context->setParameter('availableVisualizationChecked', 0);
             if ($this->opo_result_context->searchExpressionHasChanged()) {
                 $vn_page_num = 1;
             }
         }
         $this->view->setVar('num_hits', $vo_result->numHits());
         $this->view->setVar('num_pages', $vn_num_pages = ceil($vo_result->numHits() / $vn_items_per_page));
         if ($vn_page_num > $vn_num_pages) {
             $vn_page_num = 1;
         }
         $vo_result->seek(($vn_page_num - 1) * $vn_items_per_page);
         $this->view->setVar('page', $vn_page_num);
         $this->view->setVar('search', $vs_search);
         $this->view->setVar('result', $vo_result);
     }
     //
     // Set up view for display of results
     //
     $t_model = $this->opo_datamodel->getInstanceByTableName($this->ops_tablename, true);
     $this->view->setVar('views', $this->opa_views);
     // pass view list to view for rendering
     $this->view->setVar('current_view', $vs_view);
     $this->view->setVar('sorts', $this->opa_sorts);
     // pass sort list to view for rendering
     $this->view->setVar('current_sort', $vs_sort);
     $this->view->setVar('current_sort_direction', $vs_sort_direction);
     $this->view->setVar('current_items_per_page', $vn_items_per_page);
     $this->view->setVar('items_per_page', $this->opa_items_per_page);
     $this->view->setVar('t_subject', $t_model);
     $this->view->setVar('mode_name', _t('search'));
     $this->view->setVar('mode', 'search');
     $this->view->setVar('mode_type_singular', $this->searchName('singular'));
     $this->view->setVar('mode_type_plural', $this->searchName('plural'));
     $this->view->setVar('search_history', $this->opo_result_context->getSearchHistory());
     $this->view->setVar('result_context', $this->opo_result_context);
     $this->view->setVar('uses_hierarchy_browser', $this->usesHierarchyBrowser());
     $this->view->setVar('access_values', $va_access_values);
     $this->view->setVar('browse', $po_search);
     $t_display = $this->view->getVar('t_display');
     if (!is_array($va_display_list = $this->view->getVar('display_list'))) {
         $va_display_list = array();
     }
     if ($vs_view == 'editable') {
         $va_initial_data = array();
         $va_row_headers = array();
         $vn_item_count = 0;
         if ($vo_result) {
             $vs_pk = $vo_result->primaryKey();
             while ($vn_item_count < 100 && $vo_result->nextHit()) {
                 $va_result = array('item_id' => $vn_id = $vo_result->get($vs_pk));
                 foreach ($va_display_list as $vn_placement_id => $va_bundle_info) {
                     $va_result[str_replace(".", "-", $va_bundle_info['bundle_name'])] = $t_display->getDisplayValue($vo_result, $vn_placement_id, array('request' => $this->request));
                 }
                 $va_initial_data[] = $va_result;
                 $vn_item_count++;
                 $va_row_headers[] = $vn_item_count . " " . caEditorLink($this->request, caNavIcon($this->request, __CA_NAV_BUTTON_EDIT__), 'caResultsEditorEditLink', $this->ops_tablename, $vn_id);
             }
         }
         $this->view->setVar('initialData', $va_initial_data);
         $this->view->setVar('rowHeaders', $va_row_headers);
     }
     //
     // Bottom line
     //
     $va_bottom_line = array();
     $vb_bottom_line_is_set = false;
     foreach ($va_display_list as $vn_placement_id => $va_placement) {
         if (isset($va_placement['settings']['bottom_line']) && $va_placement['settings']['bottom_line']) {
             $va_bottom_line[$vn_placement_id] = caProcessBottomLineTemplate($this->request, $va_placement, $vo_result, array('pageStart' => ($vn_page_num - 1) * $vn_items_per_page, 'pageEnd' => ($vn_page_num - 1) * $vn_items_per_page + $vn_items_per_page));
             $vb_bottom_line_is_set = true;
         } else {
             $va_bottom_line[$vn_placement_id] = '';
         }
     }
     $this->view->setVar('bottom_line', $vb_bottom_line_is_set ? $va_bottom_line : null);
     switch ($pa_options['output_format']) {
         # ------------------------------------
         case 'LABELS':
             $this->_genLabels($vo_result, $this->request->getParameter("label_form", pString), $vs_search, $vs_search);
             break;
             # ------------------------------------
         # ------------------------------------
         case 'EXPORT':
             $this->_genExport($vo_result, $this->request->getParameter("export_format", pString), $vs_search, $vs_search);
             break;
             # ------------------------------------
         # ------------------------------------
         case 'HTML':
         default:
             // generate type menu and type value list
             if (method_exists($t_model, "getTypeList")) {
                 $this->view->setVar('type_list', $t_model->getTypeList());
             }
             if ($this->opb_uses_hierarchy_browser) {
                 AssetLoadManager::register('hierBrowser');
                 // only for interfaces that use the hierarchy browser
                 $t_list = new ca_lists();
                 if ($vs_type_list_code = $t_model->getTypeListCode()) {
                     $this->view->setVar('num_types', $t_list->numItemsInList($vs_type_list_code));
                     $this->view->setVar('type_menu', $t_list->getListAsHTMLFormElement($vs_type_list_code, 'type_id', array('id' => 'hierTypeList')));
                 }
                 // set last browse id for hierarchy browser
                 $this->view->setVar('browse_last_id', intval($this->request->session->getVar($this->ops_tablename . '_browse_last_id')));
             }
             $this->opo_result_context->setAsLastFind();
             $this->opo_result_context->saveContext();
             if (isset($pa_options['vars']) && is_array($pa_options['vars'])) {
                 foreach ($pa_options['vars'] as $vs_key => $vs_val) {
                     $this->view->setVar($vs_key, $vs_val);
                 }
             }
             if (isset($pa_options['view']) && $pa_options['view']) {
                 $this->render($pa_options['view']);
             } else {
                 $this->render('Search/' . $this->ops_tablename . '_search_basic_html.php');
             }
             break;
             # ------------------------------------
     }
 }
 /**
  * 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) {
                     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 (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['_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'] = $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');
 }
 /**
  * Checks validity of setting value for attribute; used by ca_metadata_elements to
  * validate settings before they are saved.
  *
  * @param array $pa_element_info Associative array containing data from a ca_metadata_elements row
  * @param string $ps_setting_key Alphanumeric setting code
  * @param string $ps_value Value of setting
  * @param string $ps_error Variable to place error message in, if setting fails validation
  * @return boolean True if value is valid for setting, false if not. If validation fails an error message is returned in $ps_error
  */
 public function validateSetting($pa_element_info, $ps_setting_key, $ps_value, &$ps_error)
 {
     $ps_error = '';
     switch ($ps_setting_key) {
         case 'render':
             switch ($ps_value) {
                 case 'yes_no_checkboxes':
                     $t_list = new ca_lists((int) $pa_element_info['list_id']);
                     // Yes/no must be used with lists that have exactly two items
                     if ((int) $t_list->numItemsInList() != 2) {
                         $ps_error = _t('The list must have exactly two items to be used as a yes/no checkbox');
                         return false;
                     }
                     break;
                 case 'checklist':
                     // Check list is only valid for top-level elements
                     if ((int) $pa_element_info['parent_id'] > 0) {
                         $ps_error = _t('Sub-elements may not be used as checklists');
                         return false;
                     }
                     break;
             }
             break;
     }
     return true;
 }
 /**
  * Checks validity of setting value for attribute; used by ca_metadata_elements to
  * validate settings before they are saved.
  *
  * @param array $pa_element_info Associative array containing data from a ca_metadata_elements row
  * @param string $ps_setting_key Alphanumeric setting code
  * @param string $ps_value Value of setting
  * @param string $ps_error Variable to place error message in, if setting fails validation
  * @return boolean True if value is valid for setting, false if not. If validation fails an error message is returned in $ps_error
  */
 public function validateSetting($pa_element_info, $ps_setting_key, $ps_value, &$ps_error)
 {
     $ps_error = '';
     switch ($ps_setting_key) {
         case 'render':
             switch ($ps_value) {
                 case 'yes_no_checkboxes':
                     $t_list = new ca_lists((int) $pa_element_info['list_id']);
                     // Yes/no must be used with lists that have exactly two items
                     if ((int) $t_list->numItemsInList() != 2) {
                         $ps_error = _t('The list must have exactly two items to be used as a yes/no checkbox');
                         return false;
                     }
                     break;
                 case 'checklist':
                     // Check list is only valid for top-level elements
                     if ((int) $pa_element_info['parent_id'] > 0) {
                         $ps_error = _t('Sub-elements may not be used as checklists');
                         return false;
                     }
                     break;
             }
             break;
     }
     if (preg_match("/^hideIfSelected/", $ps_setting_key)) {
         if (isset($pa_element_info['settings']['render']) && !is_null($pa_element_info['settings']['render'])) {
             if (!in_array($pa_element_info['settings']['render'], array('radio_buttons', 'select', 'yes_no_checkboxes'))) {
                 $ps_error = _t("dependent field visibility is only supported for radio buttons and drop-down (select) menus");
                 return false;
             }
         }
     }
     return true;
 }