You are here

dynamic_background_panels.module in Dynamic Background 6

File

modules/dynamic_background_panels/dynamic_background_panels.module
View source
<?php

/*
 * @file
 * Alters the panels variant "General" form to enable users to select a dynamic
 * background image any given panel variant.
 */

/**
 * Implementation of hook_perm().
 */
function dynamic_background_panels_perm() {
  return array(
    'configure panels dynamic background',
  );
}

/**
 * Implementation of hook_menu(). Hooks into the dynamic background modules menu
 * structure and adds the "nodes" menu tab to the administration interface.
 *
 * @return array menu items
 */
function dynamic_background_panels_menu() {
  $items = array();
  $items['admin/build/backgrounds/panels'] = array(
    'title' => 'Panels',
    'description' => t('Configure dynamic background penels'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_panels_admin_form',
    ),
    'access arguments' => array(
      'configure panels dynamic background',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  return $items;
}

/**
 * Build the administration interface for dynamic background panels and enables
 * administrators enter css to used for insertion of backgrounds.
 *
 * @return array $form
 */
function dynamic_background_panels_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );

  // Add imagecache present to the form.
  $form += dynamic_background_image_presents_form('dynamic_background_panels_imagecache');

  // Add css behaviour form to the form.
  $form += dynamic_background_css_behaviour_form('dynamic_background_panels_css');
  return system_settings_form($form);
}

/**
 * Implements hook_form_alter().
 */
function dynamic_background_panels_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'panels_panel_context_edit_settings') {

    // Add some styling and javascript to ensure single image selection.
    drupal_add_css(drupal_get_path('module', 'dynamic_background_panels') . '/css/dynamic_background_panels.admin.css', 'module');

    // Add fieldset.
    $form['conf']['dynamic_background'] = array(
      '#type' => 'fieldset',
      '#title' => t('Dynamic background'),
      '#description' => t('To used this feature setup dynamic background and upload images.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );

    // Find the currently selected image
    $image_id = NULL;
    if (isset($form_state['handler']->conf['dynamic_background'])) {
      foreach ($form_state['handler']->conf['dynamic_background'] as $id => $value) {
        if (isset($value['selected']) && $value['selected']) {
          $image_id = $id;
          break;
        }
      }
    }

    // Add the image selection part of the form
    $form['conf']['dynamic_background'] += dynamic_background_image_selector_form($image_id);

    // Add extra submit handler.
    $form['#submit'][] = 'dynamic_background_panels_conf_submit_handler';
  }
}

/*
 * Ensures that the configuration form panels is saved.
 */
function dynamic_background_panels_conf_submit_handler(&$form, &$form_state) {
  $form_state['handler']->conf['dynamic_background'] = $form_state['values']['dynamic_background'];
}

/**
 * Implements hook_dynamic_background_css().
 */
function dynamic_background_panels_dynamic_background_css($vars) {

  // If the current page has a panel, try to use dynamic background.
  $image_id = NULL;
  if ($pm_page = page_manager_get_current_page()) {
    if (isset($pm_page['handler']->conf['dynamic_background'])) {
      foreach ($pm_page['handler']->conf['dynamic_background'] as $id => $value) {
        if ($value['selected']) {
          $image_id = $id;
          break;
        }
      }
    }
  }

  // Load imagecache settings.
  $imagecache = variable_get('dynamic_background_panels_imagecache', FALSE);

  // Generate the css based in the image id.
  if (!is_null($image_id)) {
    $backgrounds = variable_get('dynamic_background_images', array());
    if (isset($backgrounds[$image_id])) {
      return array(
        'image' => $backgrounds[$image_id]['default'],
        'configuration' => variable_get('dynamic_background_panels_css', array()),
        'imagecache' => $imagecache ? $imagecache['present'] : FALSE,
        'weight' => 220,
      );
    }
  }
}

Functions

Namesort descending Description
dynamic_background_panels_admin_form Build the administration interface for dynamic background panels and enables administrators enter css to used for insertion of backgrounds.
dynamic_background_panels_conf_submit_handler
dynamic_background_panels_dynamic_background_css Implements hook_dynamic_background_css().
dynamic_background_panels_form_alter Implements hook_form_alter().
dynamic_background_panels_menu Implementation of hook_menu(). Hooks into the dynamic background modules menu structure and adds the "nodes" menu tab to the administration interface.
dynamic_background_panels_perm Implementation of hook_perm().