From 2c2b5142136437127f266bbfdafc079d56e0beca Mon Sep 17 00:00:00 2001
From: Brad Erickson <eosrei@gmail.com>
Date: Mon, 14 Nov 2011 16:24:16 -0800
Subject: [PATCH] Add Ad visibility by role based on Google Analytics
 functionality. #1330946

---
 google_admanager.admin.inc |   24 ++++++++++++++++++++++++
 google_admanager.install   |    2 ++
 google_admanager.module    |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/google_admanager.admin.inc b/google_admanager.admin.inc
index ef4873b..94b76f8 100644
--- a/google_admanager.admin.inc
+++ b/google_admanager.admin.inc
@@ -120,6 +120,30 @@ function google_admanager_admin_settings_form($form, &$form_state) {
       ) + $vocab_form_item;
     }
   }
+  // Render the role overview.
+  $form['role_vis_settings'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Visibility by role'),
+  );
+
+  $form['role_vis_settings']['google_admanager_visibility_roles'] = array(
+    '#type' => 'radios',
+    '#title' => t('Display ads for specific roles'),
+    '#options' => array(
+      t('Display to the selected roles only'),
+      t('Display to every role except the selected ones'),
+    ),
+    '#default_value' => variable_get('google_admanager_visibility_roles', 0),
+  );
+
+  $role_options = array_map('check_plain', user_roles());
+  $form['role_vis_settings']['google_admanager_roles'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Roles'),
+    '#default_value' => variable_get('google_admanager_roles', array()),
+    '#options' => $role_options,
+    '#description' => t('If none of the roles are selected, ads will be displayed to all users. If a user has any of the roles checked, ads will be displayed to that user (or hidden, depending on the setting above).'),
+  );
   return system_settings_form($form);
 }
 
diff --git a/google_admanager.install b/google_admanager.install
index 913bb9c..af15795 100644
--- a/google_admanager.install
+++ b/google_admanager.install
@@ -10,6 +10,8 @@
 function google_admanager_uninstall() {
   variable_del('google_admanager_account');
   variable_del('google_admanager_ad_slots');
+  variable_del('google_admanager_visibility_roles');
+  variable_del('google_admanager_roles');
 }
 
 /**
diff --git a/google_admanager.module b/google_admanager.module
index 978e378..c4190d9 100644
--- a/google_admanager.module
+++ b/google_admanager.module
@@ -60,6 +60,11 @@ function google_admanager_block_view($delta = '') {
     'subject' => '',
     'content' => '',
   );
+  global $user;
+  //If user shouldn't see ads, then return.
+  if(!_google_admanager_visibility_roles($user)) {
+    return $block;
+  }
   if ($id = variable_get('google_admanager_account', '')) {
     if (isset($ad_slots[$delta])) {
       // ad slot
@@ -500,3 +505,31 @@ function _google_admanager_clean_string($string, $length = 40) {
   return drupal_substr(preg_replace('/[^a-z0-9]+/', '-', drupal_strtolower($string)), 0, $length);
 }
 
+/**
+ * Based on visibility setting this function returns TRUE if AD code should
+ * be added for the current role and otherwise FALSE.
+ */
+function _google_admanager_visibility_roles($account) {
+
+  $visibility = variable_get('google_admanager_visibility_roles', 0);
+  $enabled = $visibility;
+  $roles = variable_get('google_admanager_roles', array());
+
+  if (array_sum($roles) > 0) {
+    // One or more roles are selected.
+    foreach (array_keys($account->roles) as $rid) {
+      // Is the current user a member of one of these roles?
+      if (isset($roles[$rid]) && $rid == $roles[$rid]) {
+        // Current user is a member of a role that should see ads, or have ads hidden.
+        $enabled = !$visibility;
+        break;
+      }
+    }
+  }
+  else {
+    // No role is selected, therefore all roles should see ads.
+    $enabled = TRUE;
+  }
+
+  return $enabled;
+}
-- 
1.7.5.4

