You are here

dynamic_background_user.module in Dynamic Background 6

This module provides user with the option to select a dynamic background image for each user.

File

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

/**
 * @file
 * This module provides user with the option to select a dynamic background
 * image for each user.
 */

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

/**
 * Implementation of hook_menu(). Hooks into the profile with a "My background"
 * tab, where users can select one of the backgrounds.
 *
 * @return array menu items
 */
function dynamic_background_user_menu() {
  $items = array();
  $items['admin/build/backgrounds/user'] = array(
    'title' => 'User',
    'description' => t('Configure user dynamic background'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_user_admin_form',
    ),
    'access arguments' => array(
      'configure user dynamic background',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -10,
  );
  $items['user/%user/backgrounds'] = array(
    'title' => 'My background',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'dynamic_background_user_form',
      1,
    ),
    'access arguments' => array(
      'user selection of background',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

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

  // Add hidden field with uid.
  $form['uid'] = array(
    '#type' => 'hidden',
    '#value' => $user->uid,
  );

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

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

/**
 * Submit handler for user background selection and saves the selected image's
 * id into the database.
 */
function dynamic_background_user_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_user_get_image_id($form_state['values']['uid']))) {
      db_query('UPDATE {dynamic_background_user} SET  data = %d WHERE uid = %d', $image_id, $form_state['values']['uid']);
    }
    else {
      db_query('INSERT INTO {dynamic_background_user} VALUES (%d, %d)', $form_state['values']['uid'], $image_id);
    }
  }
  else {
    db_query('DELETE FROM {dynamic_background_user} WHERE uid=%d', $form_state['values']['uid']);
  }
}

/**
 * Helper function that checks if the current user has selected a image, if a
 * selection is found in the database the image id is returned, if not NULL is
 * returned.
 *
 * @param int $uid user id
 * @return mixed image id if one is found else NULL
 */
function dynamic_background_user_get_image_id($uid) {
  $result = db_query('SELECT data FROM {dynamic_background_user} WHERE uid=%d', $uid);
  $result = db_fetch_object($result)->data;
  if ($result === FALSE) {
    return NULL;
  }
  return $result;
}

/**
 * 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_user_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );

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

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

/**
 * Implements hook_dynamic_background_css().
 */
function dynamic_background_user_dynamic_background_css($vars) {
  global $user;

  // Find the selected image id.
  $image_id = dynamic_background_user_get_image_id($user->uid);

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

Functions

Namesort descending Description
dynamic_background_user_admin_form Build the administration interface for dynamic background nodes and enables administrators to select which content types have enable background selection.
dynamic_background_user_dynamic_background_css Implements hook_dynamic_background_css().
dynamic_background_user_form Menu callback that generates the form used in the "My background" tab on the profile page.
dynamic_background_user_form_submit Submit handler for user background selection and saves the selected image's id into the database.
dynamic_background_user_get_image_id Helper function that checks if the current user has selected a image, if a selection is found in the database the image id is returned, if not NULL is returned.
dynamic_background_user_menu Implementation of hook_menu(). Hooks into the profile with a "My background" tab, where users can select one of the backgrounds.
dynamic_background_user_perm Implementation of hook_perm().