From 9c72875d0b81e995a6cd971a9b5980d738f612a7 Mon Sep 17 00:00:00 2001
From: Joseph McGuire <joseph.p.mcguire@googlemail.com>
Date: Tue, 7 Jun 2011 15:11:35 +0100
Subject: [PATCH] Added new roles attributes

---
 google_admanager.admin.inc |   29 +++++++++++++++++++++++++++++
 google_admanager.module    |    9 +++++++++
 2 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/google_admanager.admin.inc b/google_admanager.admin.inc
index ef4873b..a57c5c4 100644
--- a/google_admanager.admin.inc
+++ b/google_admanager.admin.inc
@@ -88,6 +88,24 @@ function google_admanager_admin_settings_form($form, &$form_state) {
     '#default_value' => variable_get('google_admanager_nodetype_attributes', FALSE),
     '#description' => t('This option allows you to target whole content types as an attribute within Google Ad Manager'),
   );
+  $form['google_admanager_roles_attributes'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Expose current user role names as atttribute'),
+    '#default_value' => variable_get('google_admanager_roles_attributes', FALSE),
+    '#description' => t('This option allows you to target users by their role name within Google Ad Manager'),
+  );
+  $form['google_admanager_roles_attributes_list'] = array(
+    '#type' => 'item',
+    '#title' => t('Roles Values'),
+    '#markup' => '<pre>' . implode(PHP_EOL,  _google_admanager_roles()) . '</pre>',
+    '#states' => array(
+      'invisible' => array(
+        'input[name="google_admanager_roles_attributes"]' => array('checked' => FALSE),
+      ),
+    ),
+    '#description' => t("The site's roles with special characters replaced. You can add this list to the <em>Customised targeting</em> key <strong>roles</strong> to be able to target specific roles."),
+  );
+
   if (module_exists('taxonomy')) {
     // Get an array of vocabularies
     $vocabs = taxonomy_get_vocabularies();
@@ -224,3 +242,14 @@ function google_admanager_admin_superslot_form_submit($form, &$form_state) {
   $superslots[$form_state['values']['google_admanager_superslot_name']] = array();
   variable_set('google_admanager_superslots', $superslots);
 }
+
+/**
+ * Internal function to return "clean" roles to be used as attribute
+ */
+function _google_admanager_roles() {
+  $roles = user_roles();
+  foreach ($roles as &$role) {
+    $role = _google_admanager_clean_string($role);
+  }
+  return $roles;
+}
diff --git a/google_admanager.module b/google_admanager.module
index 7d0343d..01267a1 100644
--- a/google_admanager.module
+++ b/google_admanager.module
@@ -49,6 +49,15 @@ function google_admanager_init() {
   if (variable_get('google_admanager_lazy', FALSE)) {
     drupal_add_js(drupal_get_path('module', 'google_admanager') . '/google_admanager.js');
   }
+
+  // Option to pass role names as attributes on the settings page.
+  // If selected then loop through the current users roles.
+  if (variable_get('google_admanager_roles_attributes', FALSE)) {
+    global $user;
+    foreach ($user->roles as $role) {
+      google_admanager_add_attribute('roles', _google_admanager_clean_string($role));
+    }
+  }
 }
 
 /**
-- 
1.7.4.1

