You are here

flush_page_cache.admin.inc in Flush page cache 6

Same filename and directory in other branches
  1. 7 flush_page_cache.admin.inc

Administration pages for the 'Flush page cache' module.

File

flush_page_cache.admin.inc
View source
<?php

/**
 * @file
 * Administration pages for the 'Flush page cache' module.
 */

/**
 * Page callback for admin menu cache clear button.
 */
function flush_page_cache_admin_menu_flush_cache() {
  drupal_goto($_GET['destination'], 'flush_page_cache=1');
}

/**
 * Form builder; Administration page for the 'Flush page cache' module.
 *
 * @ingroup forms
 */
function flush_page_cache_admin_settings($form_state = NULL) {

  // Link settings
  $form['link'] = array(
    '#type' => 'fieldset',
    '#title' => t('Link settings'),
  );
  $form['link']['flush_page_cache_footer_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Add flush page cache link to footer'),
    '#default_value' => variable_get('flush_page_cache_footer_link', module_exists('admin_menu') ? '0' : '1'),
  );

  // Custom settings
  $form['flush_page_cache_custom'] = array(
    '#tree' => TRUE,
    '#type' => 'fieldset',
    '#title' => t('Custom settings'),
    '#description' => t('Enter custom cache objects to be purged when a page\'s cache is flushed, using the <a href="@href">cache_clear_all()</a> function.', array(
      '@href' => 'http://api.drupal.org/api/drupal/includes!cache.inc/function/cache_clear_all/6',
    )) . '<ul>' . '<li>' . t("<b>Path</b>: The path on which this cache entry should be cleared. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )) . '</li>' . '<li>' . t('<b>Cache ID ($cid)</b>: The cache ID to delete. If left blank, all cache entries that can expire are deleted.') . '</li>' . '<li>' . t('<b>Table ($table)</b>: The table $table to delete from.') . '</li>' . '<li>' . t('<b>Wildcard ($wildcard)</b>: If $wildcard is TRUE, cache IDs starting with $cid are deleted in addition to the exact cache ID specified by $cid. If $wildcard is TRUE and $cid is \'*\' then the entire table $table is emptied.') . '</li>' . '</ul>',
  );

  // Build custom settings from post back or variables.
  if (!empty($form_state['post']['flush_page_cache_custom'])) {
    $custom = flush_page_cache_custom_build($form_state['post']);
  }
  else {
    $custom = variable_get('flush_page_cache_custom', array(
      array(
        '*',
        'variables',
        'cache',
        FALSE,
      ),
    ));
  }
  $form['flush_page_cache_custom']['settings_table'] = array(
    '#theme' => 'flush_page_cache_custom_settings_table',
    '#prefix' => '<div id="flush-page-cache-custom-settings-table">',
    '#suffix' => '</div>',
  );
  $delta = 0;
  foreach ($custom as $item) {
    $form['flush_page_cache_custom']['settings_table'][$delta] = flush_page_cache_custom_settings_row($delta, $item[0], $item[1], $item[2], $item[3]);
    $delta++;
  }

  // Add placeholder rows
  if (isset($form_state['post']['flush_page_cache_custom']['settings_table'])) {
    $number_of_empty_rows = count($form_state['post']['flush_page_cache_custom']['settings_table']) - count($custom);
    if (isset($form_state['post']['op']) && $form_state['post']['op'] == t('Add rows')) {
      $number_of_empty_rows += 3;

      // Add 3 new rows.
    }
    elseif ($number_of_empty_rows != 1) {
      $number_of_empty_rows -= 1;

      // Minus 1 from remove request.
    }
  }
  else {
    $number_of_empty_rows = 3;

    // Alway include 3 new rows.
  }
  for ($index = 0; $index < $number_of_empty_rows; $index++) {
    $form['flush_page_cache_custom']['settings_table'][$delta] = flush_page_cache_custom_settings_row($delta);
    $delta++;
  }
  $form['flush_page_cache_custom']['add'] = array(
    '#type' => 'button',
    '#value' => t('Add rows'),
    '#ahah' => array(
      'path' => 'admin/settings/flush_page_cache/js',
      'wrapper' => 'flush-page-cache-custom-settings-table',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  $form['buttons']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  return $form;
}

/**
 * Form submission handler; For flush page cache admin settings
 */
function flush_page_cache_admin_settings_submit($form, &$form_state) {

  // Footer link
  variable_set('flush_page_cache_footer_link', $form_state['values']['flush_page_cache_footer_link']);

  // Custom. Uses 'clicked button' post which contains all the submitted values
  $flush_page_cache_settings = flush_page_cache_custom_build($form_state['clicked_button']['#post']);
  variable_set('flush_page_cache_custom', $flush_page_cache_settings);

  // Display message
  drupal_set_message(t('The configuration options have been saved.'));

  // DEBUG:
  // dpm($flush_page_cache_settings);
}

/**
 * AHAH callback for custom settings table.
 *
 * Code copied from:
 * - Adding dynamic form elements using AHAH
 *   http://drupal.org/node/331941
 */
function flush_page_cache_custom_settings_table_js() {

  // Build empty form state and get form from cache.
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);

  // Setup form state
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form_state['post'] = $form['#post'] = $_POST;
  $form['#programmed'] = $form['#redirect'] = FALSE;

  // Process and rebuild
  drupal_process_form($form_id, $form, $form_state);
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  // Get the HTML fragment we need to return
  $settings_table = $form['flush_page_cache_custom']['settings_table'];
  unset($settings_table['#prefix'], $settings_table['#suffix']);
  $output = theme('status_messages') . drupal_render($settings_table);

  // Final rendering callback.
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}

////////////////////////////////////////////////////////////////////////////////

// Admin settings form functions.

////////////////////////////////////////////////////////////////////////////////

/**
 * Build flush page cache custom settings row form elements.
 */
function flush_page_cache_custom_settings_row($delta, $path = '', $cid = '', $table = '', $wildcard = FALSE) {
  static $table_options;
  if (empty($table_options)) {
    $cache_tables = array_merge(array(
      '',
      'cache',
      'cache_block',
      'cache_filter',
      'cache_page',
    ), module_invoke_all('flush_caches'));
    asort($cache_tables);
    $table_options = array_combine($cache_tables, $cache_tables);
  }
  $form['path'] = array(
    '#type' => 'textfield',
    '#default_value' => $path,
    '#size' => 20,
  );
  $form['cid'] = array(
    '#type' => 'textfield',
    '#default_value' => $cid,
    '#size' => 20,
  );
  $form['table'] = array(
    '#type' => 'select',
    '#default_value' => $table,
    '#options' => $table_options,
  );
  $form['wildcard'] = array(
    '#title' => t('TRUE'),
    '#type' => 'checkbox',
    '#default_value' => $wildcard,
  );
  $form['operations'] = array(
    '#name' => 'remove_' . $delta,
    '#type' => 'button',
    '#value' => t('Remove'),
    '#ahah' => array(
      'path' => 'admin/settings/flush_page_cache/js',
      'wrapper' => 'flush-page-cache-custom-settings-table',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
  return $form;
}

/**
 * Build flush page cache custom array from form values.
 */
function flush_page_cache_custom_build($values) {
  $custom = array();
  foreach ($values['flush_page_cache_custom']['settings_table'] as $delta => $item) {
    if ($values['remove_' . $delta] == t('Remove') || empty($item['table'])) {
      continue;
    }
    $custom[] = array(
      empty($item['path']) ? '*' : trim($item['path'], '/'),
      empty($item['cid']) ? NULL : $item['cid'],
      $item['table'],
      empty($item['wildcard']) ? FALSE : TRUE,
    );
  }

  // DEBUG:
  // dpm($values); dpm($custom);
  return $custom;
}

////////////////////////////////////////////////////////////////////////////////

// Theme functions

////////////////////////////////////////////////////////////////////////////////

/**
 * Theme flush page cache custom settings table.
 */
function theme_flush_page_cache_custom_settings_table($form) {
  $rows = array();
  foreach (element_children($form) as $key) {
    $rows[$key] = array(
      'data' => array(
        drupal_render($form[$key]['path']),
        drupal_render($form[$key]['cid']),
        drupal_render($form[$key]['table']),
        drupal_render($form[$key]['wildcard']),
        drupal_render($form[$key]['operations']),
      ),
    );
  }
  $headers = array(
    t('Path'),
    t('Cache ID'),
    t('Table'),
    t('Wildcard'),
    t('Operations'),
  );
  return theme('table', $headers, $rows);
}

Functions

Namesort descending Description
flush_page_cache_admin_menu_flush_cache Page callback for admin menu cache clear button.
flush_page_cache_admin_settings Form builder; Administration page for the 'Flush page cache' module.
flush_page_cache_admin_settings_submit Form submission handler; For flush page cache admin settings
flush_page_cache_custom_build Build flush page cache custom array from form values.
flush_page_cache_custom_settings_row Build flush page cache custom settings row form elements.
flush_page_cache_custom_settings_table_js AHAH callback for custom settings table.
theme_flush_page_cache_custom_settings_table Theme flush page cache custom settings table.