You are here

context_breakpoint.module in Context Breakpoint 7

context_screenresolution.module Main module file.

File

context_breakpoint.module
View source
<?php

/**
 * @file context_screenresolution.module
 * Main module file.
 */
define('CONTEXT_BREAKPOINT_SETTINGS', 'context_breakpoint_settings');

/**
 * Implements hook_init().
 */
function context_breakpoint_init() {
  drupal_add_library('system', 'jquery.cookie');
  context_breakpoint_execute();
  if (_context_breakpoint_check_disable_cache()) {
    $GLOBALS['conf']['cache'] = FALSE;
  }
}

/**
 * Implements hook_menu().
 */
function context_breakpoint_menu() {
  $items = array();
  $items['admin/config/media/context-breakpoint'] = array(
    'title' => 'Context Breakpoint',
    'description' => 'Global settings for breakpoint-aware context.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'context_breakpoint_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * If the contexts are added to the url,
 * wee need to disable caching if the users
 * contexts in the cookie differ from the
 * ones in the url.
 *
 * Returns flag whether caching should be disabled.
 *
 * @return bool
 */
function _context_breakpoint_check_disable_cache() {
  $disable = false;
  $settings = context_breakpoint_get_settings();
  if ($settings['breakpoints_in_url']) {
    $url = isset($_GET['context-breakpoints']) ? $_GET['context-breakpoints'] : null;
    $cookie = isset($_COOKIE['context_breakpoints']) ? $_COOKIE['context_breakpoints'] : null;
    if ($cookie === null) {

      // When no cookie is set, always diable caching to be sure.
      $disable = true;
    }
    else {
      if ($url === null) {

        // If no url is set, but the cookie contains an
        // active breakpoint, we have a mismatch.
        if ($cookie !== '') {
          return true;
        }
      }
      else {
        if ($url !== null && $cookie !== null) {

          // If they differ, caching should be disabled.
          $disable = $url !== $cookie;
        }
      }
    }
  }
  return $disable;
}

/**
 * Extract active breakpoints from cookie.
 * @return array
 */
function context_breakpoint_get_active_breakpoints() {
  if (isset($_COOKIE['context_breakpoints'])) {
    return explode(',', $_COOKIE['context_breakpoints']);
  }
  else {
    return null;
  }
}

/**
 * Get global module settings.
 * @return array
 */
function context_breakpoint_get_settings() {
  $settings = variable_get(CONTEXT_BREAKPOINT_SETTINGS, array(
    'save_resolution' => false,
    'breakpoints_in_url' => false,
    'admin_disable_reload' => false,
  ));
  return $settings;
}
function context_breakpoint_admin_settings_form($form, &$form_state) {
  $settings = context_breakpoint_get_settings();
  $form['admin_disable_reload'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable reload on admin pages'),
    '#description' => t('Disable the reload feature on admin pages.'),
    '#default_value' => $settings['admin_disable_reload'],
  );
  $form['save_resolution'] = array(
    '#type' => 'checkbox',
    '#title' => t('Save resolution in cookie'),
    '#description' => t('SEE README.txt FOR CAHCHING IMPLICATIONS.<br /><br /> If checked, the users current browser resolution in pixels is saved in a cookie.<br />Normally, just the active context is saved in the context_breakpoints cookie.'),
    '#default_value' => $settings['save_resolution'],
  );
  $form['breakpoints_in_url'] = array(
    '#type' => 'checkbox',
    '#title' => t('EXPERIMENTAL: Add active contexts to url for CACHING'),
    '#description' => t('<b>EXPERIMENTAL!</b><br /> CHECK README.TXT UNDER CACHING BEFORE YOU ENABLE.'),
    '#default_value' => $settings['breakpoints_in_url'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}
function context_breakpoint_admin_settings_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $settings = array(
    'admin_disable_reload' => (bool) $values['admin_disable_reload'],
    'save_resolution' => (bool) $values['save_resolution'],
    'breakpoints_in_url' => (bool) $values['breakpoints_in_url'],
  );
  variable_set(CONTEXT_BREAKPOINT_SETTINGS, $settings);
}

/**
 * Implements hook_context_plugins().
 */
function context_breakpoint_context_plugins() {
  $plugins = array();

  // Screen resolution plugin.
  $plugins['context_condition_breakpoint'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'context_breakpoint') . '/plugins',
      'file' => 'context_condition_breakpoint.inc',
      'class' => 'ContextConditionBreakpoint',
      'parent' => 'context_condition',
    ),
  );
  return $plugins;
}
function context_breakpoint_context_registry() {
  return array(
    'conditions' => array(
      'breakpoint' => array(
        'title' => t('Breakpoint'),
        'plugin' => 'context_condition_breakpoint',
      ),
    ),
  );
}

/**
 * Execute the context checks().
 */
function context_breakpoint_execute() {
  $plugin = context_get_plugin('condition', 'breakpoint');
  if ($plugin) {
    $plugin
      ->execute();
    drupal_add_js(array(
      'context_breakpoint' => $plugin
        ->getJSConfig(),
    ), 'setting');
  }
}

/**
 * Implements hook_js_alter().
 */
function context_breakpoint_process_html(&$vars) {

  // Add initialization code to the end of head to prevent loading the whole page
  // when the reload will be triggered anyway.
  // This needs to be done here, because drupal_add_js does not allow adding
  // something after the settings.
  //
  // See #1866322.
  $output = array(
    '#type' => 'markup',
    '#markup' => '<script type="text/javascript">Drupal.behaviors.contextBreakpoint.preInit();</script>' . "\n",
  );
  $vars['scripts'] .= drupal_render($output);
}
function context_breakpoint_url_outbound_alter(&$path, &$options, $original_path) {
  $settings = context_breakpoint_get_settings();
  $add_url = $settings['breakpoints_in_url'];
  $active = context_breakpoint_get_active_breakpoints();
  if ($add_url && !$options['external'] && count($active)) {
    $options['query']['context-breakpoints'] = implode(',', $active);
  }
}

Functions

Namesort descending Description
context_breakpoint_admin_settings_form
context_breakpoint_admin_settings_form_submit
context_breakpoint_context_plugins Implements hook_context_plugins().
context_breakpoint_context_registry
context_breakpoint_execute Execute the context checks().
context_breakpoint_get_active_breakpoints Extract active breakpoints from cookie.
context_breakpoint_get_settings Get global module settings.
context_breakpoint_init Implements hook_init().
context_breakpoint_menu Implements hook_menu().
context_breakpoint_process_html Implements hook_js_alter().
context_breakpoint_url_outbound_alter
_context_breakpoint_check_disable_cache If the contexts are added to the url, wee need to disable caching if the users contexts in the cookie differ from the ones in the url.

Constants

Namesort descending Description
CONTEXT_BREAKPOINT_SETTINGS @file context_screenresolution.module Main module file.