Index: js/lastuser.js =================================================================== --- js/lastuser.js (revision 0) +++ js/lastuser.js (revision 0) @@ -0,0 +1,29 @@ +/* + * Reads the user cookie and populates the login block with the + * first name of the last logged in user. If a username is found, + * add the question 'Not you?' to prompt for a login. + * + * Note that the user first name in the cookie is its second element + */ + +Drupal.behaviors.myModuleBehavior = function(context){ + var userdetails = readCookie("known_user_role_details"); + if (userdetails != null) { + // Note that "%7C" corresponds to "|" + var firstname = userdetails.split("%7C")[1]; + $("strong.welcome").append(firstname); + $("strong.notyou").append("Not you?"); + } +} + +// Given the name of a cookie, return the associated value +function readCookie(name) { + var nameEQ = name + "="; + var ca = document.cookie.split(';'); + for(var i=0;i < ca.length;i++) { + var c = ca[i]; + while (c.charAt(0)==' ') c = c.substring(1,c.length); + if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); + } + return null; +} \ No newline at end of file Index: known_user_role.module =================================================================== --- known_user_role.module (revision 875) +++ known_user_role.module (working copy) @@ -45,7 +45,7 @@ */ function known_user_role_boot() { global $user; - if (!$user->uid && (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid'])) { + if (!$user->uid && (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details'])) { // load the known user role $known_user_role = variable_get('known_user_role', array('1' => 'anonymous user')); // if the user is known, set the role @@ -56,10 +56,10 @@ } /** - * Clears the cookie. + * Clear the cookies. */ function known_user_role_clear() { - setcookie('known_user_role_uid', '', time()-1000000, '/'); + setcookie('known_user_role_details', '', time()-1000000, '/'); cache_clear_all(); drupal_goto('user'); } @@ -77,8 +77,8 @@ // load profile data $known_user = user_load($user->uid); } - elseif (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid']) { - $known_user = user_load($_COOKIE['known_user_role_uid']); + elseif (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details']) { + $known_user = user_load(explode('|',$_COOKIE['known_user_role_details'],1)); } else { $known_user->name = t('Guest'); @@ -91,6 +91,7 @@ */ function known_user_role_block($op = 'list', $delta = 0, $edit = array()) { global $user; + if ($op == 'list') { $blocks[0]['info'] = t('Known user login'); $blocks[0]['cache'] = BLOCK_NO_CACHE; @@ -100,8 +101,10 @@ $block = array(); switch ($delta) { case 0: + $base = drupal_get_path('module', 'known_user_role'); + drupal_add_js($base . '/js/lastuser.js'); //if the cookie is there or we're logged in, we need to show the welcome message - if ($user->uid || (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid'])) { + if ($user->uid || (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details'])) { $block['subject'] = 'Welcome'; //see theme_known_user_welcome() //developer can override the welcome message to known users @@ -137,7 +140,7 @@ */ function theme_known_user_welcome($known_user) { global $user; - + $output = '
'.t('Hello %user.', array('%user' => $known_user->name)).'
'; if (!$user->uid || $user->uid == 0) { $output .= '
'.l('Not you? Click here to login.', 'known-user/reset').'
'; @@ -249,8 +252,8 @@ function known_user_role_user($op, &$edit, &$account, $category = NULL) { switch ($op) { case 'login': - //set cookie, expires after 30 days - setcookie('known_user_role_uid', $account->uid, time()+2592000, '/'); + //set cookie, expire after 30 days + setcookie('known_user_role_details', $account->uid.'|'.$account->profile_first_name, time()+2592000, '/'); cache_clear_all(); break; } @@ -265,13 +268,13 @@ function known_user_role_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'comment_form': - if (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid']) { - $form['uid']['#value'] = $_COOKIE['known_user_role_uid']; + if (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details']) { + $form['uid']['#value'] = explode('|',$_COOKIE['known_user_role_details'],1); } break; } - if ($form['#id'] == 'node-form' && (isset($_COOKIE['known_user_role_uid']) && $_COOKIE['known_user_role_uid'])) { - $form['uid']['#value'] = $_COOKIE['known_user_role_uid']; + if ($form['#id'] == 'node-form' && (isset($_COOKIE['known_user_role_details']) && $_COOKIE['known_user_role_details'])) { + $form['uid']['#value'] = explode('|',$_COOKIE['known_user_role_details'],1); } } Index: README.txt =================================================================== --- README.txt (revision 875) +++ README.txt (working copy) @@ -97,3 +97,40 @@ } } + +Note, The 'known_user_role_details' cookie is created by this module when +a user logs in, and it will persist for a month, storing the user's id and +firstname. With Caching turned on in Drupal, there can be problems with the +names being displayed in the login block, and the problem is fixed by using +a script to apply the last login name to the home page. For the script in +this module to work, certain elements need to be applied to the theme +template.php file. Namely '' and ''. + +For example... +/** + * override the welcome message provided by the known_user_role + * module here + */ +function phptemplate_known_user_welcome($known_user) { + global $user; + + $output =''; + + $output .= '
'; + $output .= ''; + $output .= t('Welcome '); + + if (!$user->uid || $user->uid == 0) { + $output .= '
'; + $output .= ' '; + $output .= l('Click here to login.', 'known-user/reset'); + $output .= '
'; + } else { + $output .= ''; + $output .= '
'; + $output .= l(''.t('Logout').'', 'logout',array('attributes' => array('class' => t('logout-btn'), 'title' => t('Logout')), 'html' => TRUE)); + $output .= '
'; + } + $output .= '
'; + return $output; +}