View source
<?php
define('CONTEXT_BREAKPOINT_SETTINGS', 'context_breakpoint_settings');
function context_breakpoint_init() {
drupal_add_library('system', 'jquery.cookie');
context_breakpoint_execute();
if (_context_breakpoint_check_disable_cache()) {
$GLOBALS['conf']['cache'] = FALSE;
}
}
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;
}
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) {
$disable = true;
}
else {
if ($url === null) {
if ($cookie !== '') {
return true;
}
}
else {
if ($url !== null && $cookie !== null) {
$disable = $url !== $cookie;
}
}
}
}
return $disable;
}
function context_breakpoint_get_active_breakpoints() {
if (isset($_COOKIE['context_breakpoints'])) {
return explode(',', $_COOKIE['context_breakpoints']);
}
else {
return null;
}
}
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);
}
function context_breakpoint_context_plugins() {
$plugins = array();
$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',
),
),
);
}
function context_breakpoint_execute() {
$plugin = context_get_plugin('condition', 'breakpoint');
if ($plugin) {
$plugin
->execute();
drupal_add_js(array(
'context_breakpoint' => $plugin
->getJSConfig(),
), 'setting');
}
}
function context_breakpoint_process_html(&$vars) {
$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);
}
}