You are here

dynamic_background_node.module in Dynamic Background 6

This module provides the node adminitrators with the option to use different dynamic background images for each node.

File

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

/**
 * @file
 * This module provides the node adminitrators with the option to use different
 * dynamic background images for each node.
 */

/**
 * Implementation of hook_perm().
 */
function dynamic_background_node_perm() {
  return array(
    'configure dynamic background nodes',
    'select node background image',
  );
}

/**
 * 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_node_menu() {
  $items = array();
  $items['admin/build/backgrounds/nodes'] = array(
    'title' => 'Nodes',
    'description' => t('Configure dynamic background nodes'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_node_admin_form',
    ),
    'access arguments' => array(
      'configure dynamic background nodes',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  return $items;
}

/**
 * Build the administration interface for dynamic background nodes and enables
 * administrators to select which content types have enable background selection.
 *
 * @return array $form
 */
function dynamic_background_node_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );

  // Finde all the node types and make them radio options frindly.
  $options = array();
  $types = node_get_types();
  foreach ($types as $key => $type) {
    $options[$key] = $type->name . ' (' . t(check_plain($type->description)) . ')';
  }
  $defaults = variable_get('dynamic_background_node', array());
  $form['dynamic_background_node'] = array(
    '#type' => 'fieldset',
    '#title' => t('Content types'),
    '#description' => t('Enable background selection for these content types.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['dynamic_background_node']['content_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Content types'),
    '#required' => TRUE,
    '#options' => $options,
    '#default_value' => isset($defaults['content_types']) ? $defaults['content_types'] : array(),
  );

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

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

/**
 * Hooks into the selected content types an insert a form selection option of
 * background images uploaded by the site administrator.
 *
 * @param array $form
 * @param array $form_state
 * @param string $form_id
 */
function dynamic_background_node_form_alter(&$form, &$form_state, $form_id) {
  if (user_access('select node background image')) {
    $settings = variable_get('dynamic_background_node', array());
    if (isset($settings['content_types'][$form['type']['#value']]) && $settings['content_types'][$form['type']['#value']]) {
      $form['dynamic_background'] = array(
        '#type' => 'fieldset',
        '#title' => t('Node background'),
        '#collapsed' => TRUE,
        '#collapsible' => TRUE,
        '#tree' => TRUE,
      );

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

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

      // Add some style to the image selection.
      drupal_add_css(drupal_get_path('module', 'dynamic_background_node') . '/css/dynamic_background_node.admin.css', 'module');
    }
  }
}

/**
 * Implementation of hook_nodeapi(), which is used to save and load information
 * about the image selected for a given node.
 */
function dynamic_background_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  $settings = variable_get('dynamic_background_node', array());
  if (isset($settings['content_types'][$node->type]) && $settings['content_types'][$node->type]) {
    switch ($op) {
      case 'load':
        $data = dynamic_background_node_get_data($node->nid, $node->vid);
        if ($data) {
          $node->dynamic_background = $data;
        }
        break;
      case 'insert':
      case 'update':
        if (user_access('select node background image')) {
          $data = dynamic_background_node_get_data($node->nid, $node->vid);
          if ($data) {
            db_query('UPDATE {dynamic_background_node} SET data = "%s", vid = %d WHERE nid = %d', serialize($node->dynamic_background), $node->vid, $node->nid);
          }
          else {
            db_query('INSERT INTO {dynamic_background_node} VALUES (%d, %d, "%s")', $node->nid, $node->vid, serialize($node->dynamic_background));
          }
        }
        break;
      case 'delete':
        db_query('DELETE FROM {dynamic_background_node} WHERE nid=%d', $node->nid);
        break;
    }
  }
}
function dynamic_background_node_get_data($nid, $vid) {
  $result = db_query('SELECT data FROM {dynamic_background_node} WHERE nid=%d AND vid=%d', $nid, $vid);
  $result = db_fetch_object($result)->data;
  if ($result) {
    return unserialize($result);
  }
  return NULL;
}

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

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

    // Load imagecache settings.
    $imagecache = variable_get('dynamic_background_node_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_node_css', array()),
          'imagecache' => $imagecache ? $imagecache['present'] : FALSE,
          'weight' => 210,
        );
      }
    }
  }
}

Functions

Namesort descending Description
dynamic_background_node_admin_form Build the administration interface for dynamic background nodes and enables administrators to select which content types have enable background selection.
dynamic_background_node_dynamic_background_css Implements hook_dynamic_background_css().
dynamic_background_node_form_alter Hooks into the selected content types an insert a form selection option of background images uploaded by the site administrator.
dynamic_background_node_get_data
dynamic_background_node_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_node_nodeapi Implementation of hook_nodeapi(), which is used to save and load information about the image selected for a given node.
dynamic_background_node_perm Implementation of hook_perm().