From 10e920061bb8f55df529ef5a012444c6043f42be Mon Sep 17 00:00:00 2001 From: Brandon Bergren Date: Thu, 17 Nov 2011 16:00:42 -0600 Subject: [PATCH] Fix #1336080: Add solr integration. --- support_pm/support_pm.module | 39 +++++++ support_solr/support_solr.info | 7 ++ support_solr/support_solr.module | 218 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 264 insertions(+), 0 deletions(-) create mode 100644 support_solr/support_solr.info create mode 100644 support_solr/support_solr.install create mode 100644 support_solr/support_solr.module diff --git a/support_pm/support_pm.module b/support_pm/support_pm.module index 1557513..a057843 100644 --- a/support_pm/support_pm.module +++ b/support_pm/support_pm.module @@ -1023,3 +1023,42 @@ function support_pm_default_project($projects) { return db_result(db_query_range('SELECT projid FROM {support_project} WHERE projid IN (%s) AND disabled = 0 ORDER BY weight ASC', implode(',', $projids), 0, 1)); } } + +/** + * Implementation of hook_apachesolr_update_index(). + */ +function support_pm_apachesolr_update_index(&$document, $node) { + if (!isset($node->project->projid)) { + $document->is_support_project = 0; + } + else { + $document->is_support_project = (int)$node->project->projid; + } +} + +/** + * Implementation of hook_support_solr_info(). + */ +function support_pm_support_solr_info() { + return array( + 'support_pm_project' => array( + 'facet' => array( + 'info' => t('Support: Filter by project'), + 'facet_field' => 'is_support_project', + ), + 'filter_by' => t('Filter by Support Project'), + 'facet_callback' => '_support_pm_project_name', + 'block' => array( + 'info' => t('Support: Project'), + 'cache' => BLOCK_CACHE_PER_PAGE, + ), + ), + ); +} + +function _support_pm_project_name($projid) { + if (!$projid) { + return t('None'); + } + return check_plain(db_result(db_query('SELECT project FROM {support_project} WHERE projid = %d', $projid))); +} diff --git a/support_solr/support_solr.info b/support_solr/support_solr.info new file mode 100644 index 0000000..2573282 --- /dev/null +++ b/support_solr/support_solr.info @@ -0,0 +1,7 @@ +name = Support Solr +description = Provides integration with ApacheSolr for facet-based support ticket browsing +package = Support +dependencies[] = support +dependencies[] = apachesolr +core = 6.x + diff --git a/support_solr/support_solr.install b/support_solr/support_solr.install new file mode 100644 index 0000000..e69de29 diff --git a/support_solr/support_solr.module b/support_solr/support_solr.module new file mode 100644 index 0000000..288108d --- /dev/null +++ b/support_solr/support_solr.module @@ -0,0 +1,218 @@ +is_support_client = 0; + if ($node->type == 'support_ticket') { + $document->is_support_assigned = $node->assigned; + + //$document->ss_support_client = _support_client($node->client); // @@@ + $document->is_support_client = $node->client; + $document->ss_support_state = _support_state($node->state); + $document->ss_support_priority = _support_priorities($node->priority); + } +} + +/** + * Creates a Solr query for a given user + * + * @param $account an account to get grants for and build a solr query + * + * @throws Exception + */ +function _support_solr_build_subquery($account) { + if (!user_access('access content', $account)) { + throw new Exception('No access'); + } + $node_access_query = apachesolr_drupal_query(); + if (empty($node_access_query)) { + throw new Exception('No query object in apachesolr_nodeaccess'); + } + if (user_access('administer nodes', $account)) { + // Access all content from the current site, or public content. + //$node_access_query->add_filter('nodeaccess_all', 0); + //$node_access_query->add_filter('hash', apachesolr_site_hash()); + } + else { + $clients = support_search_available_clients(); + if (!empty($clients)) { + if (user_access('view other users tickets', $account) || user_access('administer support', $account) || user_access('edit any ticket', $account) || user_access('delete any ticket', $account)) { + // Allow non support tickets (client 0) + $node_access_query->add_filter('is_support_client', 0); + // OR check for each possible client... + foreach ($clients as $client) { + $node_access_query->add_filter('is_support_client', $client); + } + return $node_access_query; + } + else { + // Allow non support tickets (client 0) + $node_access_query->add_filter('is_support_client', 0); + // Set up a subquery to support the AND on the uid + $sq = apachesolr_drupal_query(); + // Set up a sub-subquery to support the OR of the clients. + $ssq = apachesolr_drupal_query(); + foreach ($clients as $client) { + $ssq->add_filter('is_support_client', $client); + } + $sq->add_subquery($ssq, 'OR'); + $sq->add_filter('uid', $account->uid); + $node_access_query->add_subquery($sq, 'AND'); // client is one of the available clients AND ticket is authored by the user... + return $node_access_query; + } + } + else { + // No access, filter out support tickets + return $node_access_query->add_filter('type', 'support_ticket', TRUE); + } + } + return $node_access_query; +} + +/** + * Implementation of hook_apachesolr_modify_query(). + */ +function support_solr_apachesolr_modify_query(&$query, &$params, $caller) { + global $user; + try { + $subquery = _support_solr_build_subquery($user); + } + catch (Exception $e) { + $query = NULL; + watchdog("support_solr", 'User %name (UID:!uid) cannot search: @message', array('%name' => $user->name, '!uid' => $user->uid, '@message' => $e->getMessage())); + return; + } + + if (!empty($subquery)) { + $query->add_subquery($subquery, 'OR'); + print_r($query->get_url_queryvalues()); // @@@ + } +} + +/** + * Implementation of hook_support_solr_info(). + */ +function support_solr_support_solr_info() { + return array( + 'support_client' => array( + 'facet' => array( + 'info' => t('Support: Filter by client'), + 'facet_field' => 'is_support_client', + ), + 'filter_by' => t('Filter by Support Client'), + 'facet_callback' => '_support_solr_client_name', + 'block' => array( + 'info' => t('Support: Client'), + 'cache' => BLOCK_CACHE_PER_PAGE, + ), + ), + 'support_assigned' => array( + 'facet' => array( + 'info' => t('Support: Filter by assigned'), + 'facet_field' => 'is_support_assigned', + ), + 'filter_by' => t('Filter by Assigned'), + 'facet_callback' => '_support_solr_assigned_name', + 'block' => array( + 'info' => t('Support: Assigned'), + 'cache' => BLOCK_CACHE_PER_PAGE, + ), + ), + ); +} + +/** + * Implementation of hook_apachesolr_facets(). + */ +function support_solr_apachesolr_facets() { + $facets = array(); + $info = module_invoke_all('support_solr_info'); + foreach ($info as $k => $v) { + $facets[$k] = $v['facet']; + } + return $facets; +} + +/** + * Implementation of hook_block(). + * Forwards to D7 style hooks. + */ +function support_solr_block($op = 'list', $delta = 0, $edit = array()) { + switch($op) { + case 'list': + return support_solr_block_list(); + case 'view': + return support_solr_block_view($delta); + } +} + +/** + * Implements hook_block_list(). + */ +function support_solr_block_list() { + $blocks = array(); + $enabled = apachesolr_get_enabled_facets('support_solr'); + $info = module_invoke_all('support_solr_info'); + foreach ($enabled as $name => $field) { + $blocks[$name] = $info[$name]['block']; + } + return $blocks; +} + +/** + * Implements hook_block_view(). + */ +function support_solr_block_view($delta = '') { + if (apachesolr_has_searched()) { + // Get the query and response. Without these no blocks make sense. + $response = apachesolr_static_response_cache(); + if (empty($response)) { + return; + } + $query = apachesolr_current_query(); + + $facets = apachesolr_get_enabled_facets('support_solr'); +/* + if (empty($facets[$delta]) && ($delta != 'currentsearch')) { + return; + }*/ + + $info = module_invoke_all('support_solr_info'); + + return apachesolr_facet_block($response, $query, 'support_solr', $delta, $facets[$delta], $info[$delta]['filter_by'], $info[$delta]['facet_callback']); + } +} + +/** + * Callback to get the client name given an is_support_client value. + */ +function _support_solr_client_name($facet) { + if (!$facet) { + return t('Unknown'); + } + $result = db_fetch_object(db_query('SELECT parent FROM {support_client} WHERE clid = %d', $facet)); + if ($result->parent) { + return check_plain(_support_client($result->parent) . '/' . _support_client($facet)); + } + else { + return check_plain(_support_client($facet)); + } +} + +/** + * Callback to get the assigned name given an is_support_assigned value. + */ +function _support_solr_assigned_name($facet) { + if (!$facet) { + return t('unassigned'); + } + $account = user_load($facet); + return check_plain($account->name); +} \ No newline at end of file -- 1.7.7.3