diff --git a/README.txt b/README.txt index 9533bfe..ce4cfbc 100644 --- a/README.txt +++ b/README.txt @@ -76,7 +76,7 @@ Installation $user_name to display the logged-in user name $user_link to display the name linked to their profile (both work for cached and non-cached pages). - $is_page_authcache is set to TRUE in all template hooks if the page + $$_authcache_is_cacheable is set to TRUE in all template hooks if the page is to be cached. =================== diff --git a/modules/authcache_example/authcache_example.info b/modules/authcache_example/authcache_example.info index 81f5354..c4def74 100644 --- a/modules/authcache_example/authcache_example.info +++ b/modules/authcache_example/authcache_example.info @@ -1,5 +1,11 @@ name = Authcache Example description = Example module that displays a cachable Drupal block of customizable user content. -package = Caching -core = 6.x +package = Performance and scalability dependencies[] = authcache +files[] = +; Information added by drupal.org packaging script on 2012-09-21 +version = "7.x-1.x-dev" +core = 7.x +project = "authcache" +datestamp = "1348186054" + diff --git a/modules/authcache_example/authcache_example.install b/modules/authcache_example/authcache_example.install index 148e6aa..45e7324 100644 --- a/modules/authcache_example/authcache_example.install +++ b/modules/authcache_example/authcache_example.install @@ -1,45 +1,34 @@ 'Stores example person entries for demonstration purposes.', 'fields' => array( - 'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'disp-width' => '10'), - 'block_text' => array('type' => 'text', 'not null' => TRUE)), + 'uid' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => "Creator user's {users}.uid", + ), + 'block_text' => array( + 'type' => 'text', + 'not null' => TRUE, + 'description' => 'block_text', + ), + ), 'primary key' => array('uid'), ); return $schema; } -/** - * Implementation of hook_enable(). - */ -function authcache_example_enable() { - -} - -/** - * Implementation of hook_disable(). - */ -function authcache_example_disable() { -} - -/** - * Implementation of hook_install(). - */ -function authcache_example_install() { - // Create table to hold our user block text - drupal_install_schema('authcache_example'); -} - -/** - * Implementation of hook_uninstall(). - */ -function authcache_example_uninstall() { - drupal_uninstall_schema('authcache_example'); -} diff --git a/modules/authcache_example/authcache_example.module b/modules/authcache_example/authcache_example.module index 4339f3e..af9f9b6 100644 --- a/modules/authcache_example/authcache_example.module +++ b/modules/authcache_example/authcache_example.module @@ -8,66 +8,124 @@ /** - * Implentation of hook_menu() + * Implements hook_menu() */ function authcache_example_menu() { - $items['user/%user/authcache_example'] = array( + $items['user/%user/authcache-example'] = array( 'title' => 'Authcache Example', 'page callback' => 'drupal_get_form', 'page arguments' => array('authcache_example_block_form', 1), - 'access callback' => 'user_edit_access', - 'access arguments' => array(1), + 'access callback' => 'authcache_example_custom_access', + 'access arguments' => array(1, 'edit'), 'type' => MENU_LOCAL_TASK, ); return $items; } +/* + * Access callback for the menu item. + * + * Note Implementation of hook_access has been removed in Drupal 7 in favor of + * hook_node_access. Thus this is just an access callback. + */ +function authcache_example_custom_access($account, $op) { + global $user; + if ($op == 'edit') { + // Allow user with sufficient permission to edit their or any authcache example block. + return (($user->uid && $user->uid == $account->uid && user_access('edit own authcache example block')) || user_access('administer authcache example') ); + } +} + +/* + * Implements hook_permisssion() + */ +function authcache_example_permission() { + return array( + 'administer authcache example' => array( + 'title' => t('Administer Authcache Example Block'), + 'description' => t('Allow user to edit any authcache example block'), + ), + 'edit own authcache example block' => array( + 'title' => t('edit own authcache example block'), + 'description' => t('Allow users to edit authcache example block.'), + ), + ); +} + + /** * Form to modify user's block */ -function authcache_example_block_form($form_state, $account) { +function authcache_example_block_form($form, &$form_state, $account) { + + // Retrive the block if there is any + $block = db_query("SELECT block_text, uid FROM {authcache_example} WHERE uid = :uid", array(':uid' => $account->uid))->fetchObject(); - $text = db_result(db_query("SELECT block_text FROM {authcache_example} WHERE uid = %d", $account->uid)); + // Hold on to the block + $form_state['storage']['authcache_block'] = (empty($block)) ? FALSE : $block; $form['block_text'] = array( '#title' => 'Your Custom Block Text', '#type' => 'textarea', - '#default_value' => ($text) ? $text : '', + '#default_value' => ($block && $block->block_text) ? $block->block_text : '', ); $form['submit'] = array( '#type' => 'submit', - '#value' => 'Save', + '#value' => ($block) ? 'Update': 'Save', ); return $form; } +/* + * @TODO Provide basic validation + */ +function authcache_example_block_form_validate($form, &$form_state) { + +} + /** * User updated their block */ function authcache_example_block_form_submit($form, &$form_state) { - $account = $form['#parameters'][2]; + $account = $form_state['build_info']['args'][0]; - db_query("INSERT INTO {authcache_example} (uid,block_text) VALUES (%d, '%s') ON DUPLICATE KEY UPDATE block_text='%s'", $account->uid, $form_state['values']['block_text'], $form_state['values']['block_text']); + $record = array( + 'uid' => $account->uid, + 'block_text' => $form_state['values']['block_text'], + ); + + if ($form_state['storage']['authcache_block']) { + // Update + $res = drupal_write_record('authcache_example', $record, array('uid')); + } + else { + // Save new block + $res = drupal_write_record('authcache_example', $record); + } + + if (!$res) { + form_set_error('authcache_example', 'An error occurred. Please consult the logs.'); + } // Invalidate browser cache by modify our cache cookie time - setcookie('authcache_example', time(), 0, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1'); + setcookie('authcache_example', REQUEST_TIME, 0, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1'); drupal_set_message(t("Your block has been updated.")); } /** - * Implementation of hook_block() + * Implements hook_block() */ function authcache_example_block_info() { $blocks['example'] = array( 'info' => t('Authcache Example Block'), 'weight' => 10, 'status' => 1, - 'region' => 'left', - 'cache' => BLOCK_NO_CACHE, + 'region' => 'sidebar_first', + 'cache' => DRUPAL_NO_CACHE, ); return $blocks; @@ -87,19 +145,26 @@ function authcache_example_block_view($delta = '') { * Display user-customized block */ function authcache_example_display_block_example() { - global $user, $is_page_authcache; + global $user; + // $_authcache_is_cacheable seems to be the right variable. + global $_authcache_is_cacheable; if (!$user->uid) { return 'Please login to test this block.'; } + + // $_authcache_is_cacheable is the variable for D77 because there is no other + // mention of $is_page_authcache except in the example module. + // @TODO Remove references to $is_page_authcache and update documentation. + // Do not cache block if page is to be cached - else if($is_page_authcache) { + elseif ($_authcache_is_cacheable) { // Use JS to retrieve block content drupal_add_js(drupal_get_path('module', 'authcache_example') . '/js/authcache_example.js', 'module', 'header'); return ' '; } - $block_text = check_plain(db_result(db_query("SELECT block_text FROM {authcache_example} WHERE uid = %d", $user->uid))); + $block_text = check_plain(db_query("SELECT block_text FROM {authcache_example} WHERE uid = :uid", array(':uid' => $user->uid))->fetchField()); $output = t("Hello, !user, this is a customized block of content that can be cached by the browser. Update it here!", array('!user' => $user->name, '!url' => url("user/$user->uid/authcache_example"))); @@ -109,7 +174,7 @@ function authcache_example_display_block_example() { } /** - * Implementation of hook_user_login() + * Implements hook_user_login() * * Cookies need to be reset in case user logs in under a different account */ @@ -117,17 +182,17 @@ function authcache_example_user_login(&$edit, &$account) { // cookie expiration $expires = ini_get('session.cookie_lifetime'); - $expires = (!empty($expires) && is_numeric($expires) ? time() + (int)$expires : 0); + $expires = (!empty($expires) && is_numeric($expires) ? REQUEST_TIME + (int)$expires : 0); - setcookie('authcache_example', time(), $expires, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1'); + setcookie('authcache_example', REQUEST_TIME, $expires, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1'); } /** - * Implementation of hook_user_logout() + * Implements hook_user_logout() * * Cookies need to be reset in case user logs in under a different account */ function authcache_example_user_loout($account) { - setcookie('authcache_example', "", time() - 86400, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1'); + setcookie('authcache_example', "", REQUEST_TIME - 86400, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1'); }