summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEarl Miles2007-03-03 22:26:04 (GMT)
committer Earl Miles2007-03-03 22:26:04 (GMT)
commita2e70276f2065eafbd9f93497772dbd948717769 (patch)
tree8646f6fc754a2e56491edb0406a4a31f92eb0f93
parent49115a172c7a4aa66120e6a6c3494929aeba54d1 (diff)
#74541: Improved handling of role filtering.
-rw-r--r--CHANGELOG.txt1
-rw-r--r--modules/views_node.inc12
-rw-r--r--modules/views_user.inc70
3 files changed, 62 insertions, 21 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 49f5056..bd0a65e 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -108,6 +108,7 @@ Views 4.7.x-1.5
Views 4.7.x-dev
o #111881: change 'user' table to use INNER join because every node has an author
o #102716: Reduced the number of JOINs for taxonomy hierarchy queries
+ o #74541: Improved handling of role filtering.
New features:
o #105620: (Attempt 2) allow modules to alter views tables + arguments
diff --git a/modules/views_node.inc b/modules/views_node.inc
index 0d32139..7ddd75d 100644
--- a/modules/views_node.inc
+++ b/modules/views_node.inc
@@ -694,18 +694,6 @@ function views_handler_filter_isnew($op, $filter, $filterinfo, &$query) {
}
/*
- * Create a list of roles.
- */
-function views_handler_filter_role() {
- $rids = array();
- $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
- while ($obj = db_fetch_object($result)) {
- $rids[$obj->rid] = $obj->name;
- }
- return $rids;
-}
-
-/*
* Create a list of node types.
*/
function views_handler_filter_nodetype($op) {
diff --git a/modules/views_user.inc b/modules/views_user.inc
index d128b76..3bf96d9 100644
--- a/modules/views_user.inc
+++ b/modules/views_user.inc
@@ -69,6 +69,7 @@ function user_views_tables() {
'name' => t('Role: Author Role'),
'operator' => 'views_handler_operator_andor',
'list' => 'views_handler_filter_role',
+ 'handler' => 'views_handler_filter_role_custom',
'value-type' => 'array',
'help' => t('Include the node only if the author is a member of the selected role.'),
),
@@ -327,15 +328,6 @@ function views_handler_filter_usercurrent() {
}
/*
- * Custom filter for current user.
- */
-function views_handler_filter_usercurrent_custom($op, $filter, $filterinfo, &$query) {
- global $user;
- $query->ensure_table('users');
- $query->add_where("users.uid $filter[operator] '$user->uid'");
-}
-
-/*
* Add special info to see if a user either authored OR commented on a post
*/
function views_handler_filter_uid_touched($op, $filter, $filterinfo, &$query) {
@@ -353,3 +345,63 @@ function views_handler_filter_uid_touched($op, $filter, $filterinfo, &$query) {
$query->set_distinct(); // once you do this, there's just one node.
$query->add_where("node.uid = '%s' OR $table.uid = '%s'", $filter['value'], $filter['value']);
}
+
+/*
+ * Create a list of roles.
+ */
+function views_handler_filter_role() {
+ $rids = array();
+ $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name");
+ while ($obj = db_fetch_object($result)) {
+ $rids[$obj->rid] = $obj->name;
+ }
+ return $rids;
+}
+
+/*
+ * Custom filter for roles
+ */
+function views_handler_filter_role_custom($op, $filter, $filterinfo, &$query) {
+ $table = $filterinfo['table'];
+ $field = $filterinfo['field'];
+
+ //filter for role authenticated users
+ if (in_array(DRUPAL_AUTHENTICATED_RID, $filter['value'])) {
+ if ($filter['operator'] == 'OR') {
+ //filter for anonymous users too -> return all
+ if (in_array(DRUPAL_ANONYMOUS_RID, $filter['value'])) {
+ return;
+ }
+ $query->ensure_table($table);
+ $query->add_where("(%s.%s != %d OR %s.%s IS NULL)", $table, $field, DRUPAL_ANONYMOUS_RID, $table, $field);
+ return;
+ }
+ else if ($filter['operator'] == 'NOR') {
+ //filter for anonymous users too?
+ if (in_array(DRUPAL_ANONYMOUS_RID, $filter['value'])) {
+ $query->ensure_table($table);
+ $query->add_where("%s.%s = '0'", $table, $field); //this will get no results
+ return;
+ }
+ else {
+ $query->ensure_table($table);
+ $query->add_where("%s.%s = %d", $table, $field, DRUPAL_ANONYMOUS_RID);
+ return;
+ }
+ }
+ else if ($filter['operator'] == 'AND' && !in_array(DRUPAL_ANONYMOUS_RID, $filter['value'])) {
+ $key = array_search(DRUPAL_AUTHENTICATED_RID, $filter['value']);
+ unset($filter['value'][$key]);
+ //if there are other roles left the default filter handling is ok, if not
+ //we need to exclude anonymous users
+ if (count($filter['value']) == 0) {
+ $query->ensure_table($table);
+ $query->add_where("%s.%s != %d", $table, $field, DRUPAL_ANONYMOUS_RID);
+ return;
+ }
+ }
+ }
+ if ($filter['value']) {
+ views_handler_filter_default($op, $filter, $filterinfo, $query);
+ }
+} \ No newline at end of file