You are here

dynamic_background_blog.module in Dynamic Background 6

File

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

/**
 * @file
 *
 */
define('DYNAMIC_BACKGROUND_BLOG_PATH', 'blog');

/**
 * Implementation of hook_perm().
 */
function dynamic_background_blog_perm() {
  return array(
    'configure blog dynamic background',
    'user selection of blog background',
  );
}

/**
 * Implementation of hook_menu(). Hooks into the dynamic background modules menu
 * structure and adds the "blog" menu tab to the administration interface.
 *
 * @return array menu items
 */
function dynamic_background_blog_menu() {
  $items = array();
  $items['admin/build/backgrounds/blog'] = array(
    'title' => 'Blog',
    'description' => t('Configure blog dynamic background'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_blog_admin_form',
    ),
    'access arguments' => array(
      'configure blog dynamic background',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  $items['user/%user/blog/background'] = array(
    'title' => 'My blog background',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_blog_form',
      1,
    ),
    'access arguments' => array(
      'user selection of blog background',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  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_blog_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );

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

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

/**
 * Menu callback that generates the form used in the "My blog background" tab
 * on the user profile page.
 */
function dynamic_background_blog_form($form_id, $user) {
  $form = array();

  // Hidden fields that stores the used id of the user being edited.
  $form['uid'] = array(
    '#type' => 'hidden',
    '#value' => $user->uid,
  );

  // Add image selector.
  $form['dynamic_background'] = dynamic_background_image_selector_form(dynamic_background_blog_get_image_id($user->uid));

  // Add submit handler.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update background'),
  );
  return $form;
}

/**
 * Submit handler for user background selection and saves the selected image's
 * id into the database.
 */
function dynamic_background_blog_form_submit($form, &$form_state) {

  // Check if any image have been selected.
  $image_id = NULL;
  foreach ($form_state['values']['dynamic_background'] as $key => $value) {
    if ($value['selected']) {
      $image_id = $key;
      break;
    }
  }

  // If image was selected insert the image id into the database.
  if (!is_null($image_id)) {
    if (!is_null(dynamic_background_blog_get_image_id($form_state['values']['uid']))) {
      db_query('UPDATE {dynamic_background_blog} SET image_id = %d WHERE uid = %d', $image_id, $form_state['values']['uid']);
    }
    else {
      db_query('INSERT INTO {dynamic_background_blog} VALUES (%d, %d)', $form_state['values']['uid'], $image_id);
    }
  }
  else {
    db_query('DELETE FROM {dynamic_background_blog} WHERE uid=%d', $form_state['values']['uid']);
  }
}

/**
 * Utility function that loads image selection data from the database.
 *
 * @param int $nid
 * @param int $vid
 * @return mixed
 */
function dynamic_background_blog_get_image_id($uid) {
  $result = db_query('SELECT image_id FROM {dynamic_background_blog} WHERE uid=%d', $uid);
  $result = db_fetch_object($result)->image_id;
  if ($result === FALSE) {
    return NULL;
  }
  return $result;
}

/**
 * Implements hook_dynamic_background_css().
 */
function dynamic_background_blog_dynamic_background_css($vars) {
  $uid = NULL;

  // There are two ways to detecte the different parts of a blog. The path
  // /blog/%uid_optional of the node type blog. First we look at the node type,
  // then the path.
  if (isset($vars['node']) && $vars['node']->type == 'blog') {
    $uid = $vars['node']->uid;
  }
  else {

    // Try to find the uid by using path.
    $parts = split('/', $_GET['q']);
    if (count($parts) >= 2 && $parts[0] == DYNAMIC_BACKGROUND_BLOG_PATH && is_numeric($parts[1])) {

      // Test that the 2nd part is an interger (meight be a user id).
      $uid = (int) $parts[1];
    }
  }

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

  // If user id was found, try to load blog background image if one is definded.
  if (!is_null($uid)) {

    // Get selected image id for current blog.
    $image_id = dynamic_background_blog_get_image_id($uid);
    if (!is_null($image_id)) {

      // Get global backgrounds.
      $backgrounds = variable_get('dynamic_background_images', array());
      if (isset($backgrounds[$image_id]['default'])) {
        return array(
          'image' => $backgrounds[$image_id]['default'],
          'configuration' => variable_get('dynamic_background_blog_css', array()),
          'imagecache' => $imagecache ? $imagecache['present'] : FALSE,
          'weight' => 230,
        );
      }
    }
  }
}

Functions

Namesort descending Description
dynamic_background_blog_admin_form Build the administration interface for dynamic background nodes and enables administrators to select which content types have enable background selection.
dynamic_background_blog_dynamic_background_css Implements hook_dynamic_background_css().
dynamic_background_blog_form Menu callback that generates the form used in the "My blog background" tab on the user profile page.
dynamic_background_blog_form_submit Submit handler for user background selection and saves the selected image's id into the database.
dynamic_background_blog_get_image_id Utility function that loads image selection data from the database.
dynamic_background_blog_menu Implementation of hook_menu(). Hooks into the dynamic background modules menu structure and adds the "blog" menu tab to the administration interface.
dynamic_background_blog_perm Implementation of hook_perm().

Constants