diff --git a/og_context/includes/views/handlers/og_context_handler_filter_group_user_name.inc b/og_context/includes/views/handlers/og_context_handler_filter_group_user_name.inc new file mode 100644 index 0000000..0795a4d --- /dev/null +++ b/og_context/includes/views/handlers/og_context_handler_filter_group_user_name.inc @@ -0,0 +1,138 @@ +value = array(); + } + + function value_form(&$form, &$form_state) { + // We can't show a form element for the filter in the admin UI, so explain + // this to the user. + if (empty($form_state['exposed'])) { + $form['value'] = array( + '#markup' => t("This filter can only function when exposed, because it uses the current OG context for its possible values."), + ); + return; + } + + $this->og_context = og_context($this->options['group_type']); + if (empty($this->og_context)) { + // The filter cannot function without an OG context. + return; + } + + return parent::value_form($form, $form_state); + } + + function get_value_options() { + if (empty($this->og_context)) { + // The filter cannot function without an OG context. + return; + } + + // Get the group members for the group that's in context. + $selected_role_names = array_filter($this->options['group_roles']); + + if (empty($selected_role_names)) { + // No roles selected: all members. + $result = db_query("SELECT u.name, ogm.*, ogur.* FROM og_membership ogm + INNER JOIN users u + ON ogm.etid = u.uid + WHERE ogm.entity_type = 'user' AND ogm.group_type = :group_type AND ogm.gid = :group_gid", array( + ':group_type' => $this->options['group_type'], + ':group_gid' => $this->og_context['gid'], + )); + } + else { + // Get the role IDs from the names. + $rids = db_query("SELECT name, rid FROM {og_role} WHERE group_type = :group_type", array( + ':group_type' => $this->options['group_type'], + ))->fetchAllKeyed(); + + $selected_rids = array_intersect_key($rids, $selected_role_names); + + // The {og_users_roles} table only has records for members with a + // specific role other than OG_AUTHENTICATED_ROLE. + $result = db_query("SELECT DISTINCT u.uid, u.name FROM og_membership ogm + INNER JOIN users u + ON ogm.etid = u.uid + INNER JOIN og_users_roles ogur + ON ogm.etid = ogur.uid + AND ogur.group_type = :group_type + AND ogur.gid = :group_gid + AND ogur.rid IN (:selected_rids) + WHERE ogm.entity_type = 'user' AND ogm.group_type = :group_type AND ogm.gid = :group_gid", array( + ':group_type' => $this->options['group_type'], + ':group_gid' => $this->og_context['gid'], + ':selected_rids' => $selected_rids, + )); + } + + // Fetch the result as uid => username, ready for form options. + $this->value_options = $result->fetchAllKeyed(); + + return $this->value_options; + } + + function option_definition() { + $options = parent::option_definition(); + + $options['group_type'] = array('default' => array()); + $options['group_roles'] = array('default' => array()); + + return $options; + } + + function has_extra_options() { return TRUE; } + + function extra_options_form(&$form, &$form_state) { + $group_entity_types = og_get_all_group_entity(); + + // There's no API function to get all OG roles, so query for them ourselves. + $query = db_select('og_role', 'ogr') + ->fields('ogr', array('rid', 'name', 'group_type', 'group_bundle')) + ->orderBy('name', 'ASC'); + $result = $query + ->execute() + ->fetchAll(); + + $options_roles = array(); + foreach ($result as $og_role_data) { + $options_roles[$og_role_data->name] = t("@role-name (@group-type - @group-bundle)", array( + '@role-name' => $og_role_data->name, + '@group-type' => $og_role_data->group_type, + '@group-bundle' => $og_role_data->group_bundle, + )); + } + + $form['group_type'] = array( + '#type' => 'radios', + '#title' => t('Group type'), + '#required' => TRUE, + '#options' => $group_entity_types, + '#default_value' => $this->options['group_type'], + ); + + $form['group_roles'] = array( + '#type' => 'checkboxes', + '#title' => t('Group roles'), + '#description' => t("Group roles must match the selected group type."), + '#options' => $options_roles, + '#default_value' => $this->options['group_roles'], + ); + + return; + } + +} diff --git a/og_context/includes/views/og_context.views.inc b/og_context/includes/views/og_context.views.inc index 2c619fe..5e1cdc8 100644 --- a/og_context/includes/views/og_context.views.inc +++ b/og_context/includes/views/og_context.views.inc @@ -26,4 +26,22 @@ function og_context_views_plugins() { ), ), ); -} \ No newline at end of file +} + +/** + * Implements hook_views_data_alter(). + */ +function og_context_views_data_alter(&$data) { + // Filter handler for exposed filters that show the members of the current + // group. + // This can be used, for example, to limit a view of group content by author. + $data['users']['og_context_uid'] = array( + 'title' => t('OG Group member user name'), + 'help' => t('Filter on users who are members of the group currently in context. Only works as an exposed filter.'), + // This is a dummy field, so point it to the real field it acts on. + 'real field' => 'uid', + 'filter' => array( + 'handler' => 'og_context_handler_filter_group_user_name', + ), + ); +} diff --git a/og_context/og_context.info b/og_context/og_context.info index 141597b..35535f0 100644 --- a/og_context/og_context.info +++ b/og_context/og_context.info @@ -11,3 +11,6 @@ files[] = includes/views/handlers/og_context_plugin_argument_default_group_conte ; Views access plugin files[] = includes/views/handlers/og_context_plugin_access_og_perm.inc + +; Views filters +files[] = includes/views/handlers/og_context_handler_filter_group_user_name.inc