You are here

dynamic_background_node.module in Dynamic Background 7

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_permission().
 */
function dynamic_background_node_permission() {
  return array(
    'configure node dynamic background' => array(
      'title' => t('Configure node 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_node_menu() {
  $items = array();
  $items['admin/config/user-interface/backgrounds/node'] = array(
    'title' => 'Node',
    'description' => t('Configure node dynamic background'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_node_admin_form',
    ),
    'access arguments' => array(
      'configure node dynamic background',
    ),
    '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_type_get_types();
  foreach ($types as $key => $type) {
    $options[$key] = $type->name . ' (' . t(filter_xss_admin($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 image style to the form.
  $form += dynamic_background_image_style_form('dynamic_background_node_image_style');

  // 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 (isset($form['type']['#value'])) {
    $settings = variable_get('dynamic_background_node', array());
    if (isset($settings['content_types'][$form['type']['#value']]) && $settings['content_types'][$form['type']['#value']]) {
      $form['additional_settings']['dynamic_background'] = array(
        '#type' => 'fieldset',
        '#title' => t('Node background'),
        '#collapsed' => FALSE,
        '#collapsible' => TRUE,
        '#tree' => TRUE,
      );

      // Find the currently selected image
      $image_id = NULL;
      if (isset($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['additional_settings']['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');
    }
  }
}

/**
 * Implements hook_node_load(), which is used to load the background selected
 * for the nodes passed to the function. The image is only added to the node if
 * one is defined.
 */
function dynamic_background_node_node_load($nodes, $types) {
  $settings = variable_get('dynamic_background_node', array());
  foreach ($nodes as $nid => $node) {
    if (isset($settings['content_types'][$node->type]) && $settings['content_types'][$node->type]) {
      $data = dynamic_background_node_get_data($node->nid, $node->vid);
      if ($data) {

        // Data was found, so unserialize().
        $node->dynamic_background = unserialize($data);
      }
    }
  }
}

/**
 * Implements hook_node_insert(), which saves information about the select
 * background image.
 */
function dynamic_background_node_node_insert($node) {
  $settings = variable_get('dynamic_background_node', array());
  if (isset($settings['content_types'][$node->type]) && $settings['content_types'][$node->type] && isset($node->dynamic_background)) {
    db_insert('dynamic_background_node')
      ->fields(array(
      'nid' => $node->nid,
      'vid' => $node->vid,
      'data' => serialize($node->dynamic_background),
    ))
      ->execute();
  }
}

/**
 * Implements hook_node_update(), which updates the selected background image on
 * node update events.
 */
function dynamic_background_node_node_update($node) {
  $settings = variable_get('dynamic_background_node', array());
  if (isset($settings['content_types'][$node->type]) && $settings['content_types'][$node->type]) {
    if (dynamic_background_node_get_data($node->nid, $node->vid)) {
      $res = db_update('dynamic_background_node')
        ->fields(array(
        'data' => serialize($node->dynamic_background),
      ))
        ->condition('nid', $node->nid)
        ->condition('vid', $node->vid)
        ->execute();
    }
    else {

      // If update was not possible (e.g. module installed after node creation).
      dynamic_background_node_node_insert($node);
    }
  }
}

/**
 * Implements hook_node_delete(), that ensures that dynamic background
 * information is delete during node deletion.
 */
function dynamic_background_node_node_delete($node) {
  $settings = variable_get('dynamic_background_node', array());
  if (isset($settings['content_types'][$node->type]) && $settings['content_types'][$node->type]) {
    db_delete('dynamic_background_node')
      ->condition('nid', $node->nid)
      ->condition('vid', $node->vid)
      ->execute();
  }
}

/**
 * Utility function that loads image selection data from the database.
 *
 * @param int $nid
 * @param int $vid
 * @return mixed
 */
function dynamic_background_node_get_data($nid, $vid) {
  $query = 'SELECT data FROM {dynamic_background_node} WHERE nid=:nid AND vid=:vid';
  $values = array(
    ':nid' => $nid,
    ':vid' => $vid,
  );
  $data = db_query($query, $values)
    ->fetchField();
  if ($data) {
    return $data;
  }
  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 (isset($value['selected']) && $value['selected']) {
        $image_id = $id;
        break;
      }
    }

    // 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]['picture'])) {

        // Load image style settings.
        $image_style = variable_get('dynamic_background_node_image_style', FALSE);
        return array(
          'image' => $backgrounds[$image_id]['picture'],
          'configuration' => variable_get('dynamic_background_node_css', array()),
          'image_style' => $image_style ? $image_style['style'] : FALSE,
        );
      }
    }
  }
}

/**
 * Implements hook_dynamic_background_weight for the main module.
 */
function dynamic_background_node_dynamic_background_weight() {
  return array(
    'weight' => -30,
  );
}

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_dynamic_background_weight Implements hook_dynamic_background_weight for the main module.
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 Utility function that loads image selection data from the database.
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_node_delete Implements hook_node_delete(), that ensures that dynamic background information is delete during node deletion.
dynamic_background_node_node_insert Implements hook_node_insert(), which saves information about the select background image.
dynamic_background_node_node_load Implements hook_node_load(), which is used to load the background selected for the nodes passed to the function. The image is only added to the node if one is defined.
dynamic_background_node_node_update Implements hook_node_update(), which updates the selected background image on node update events.
dynamic_background_node_permission Implementation of hook_permission().