You are here

dynamic_background_user.module in Dynamic Background 7

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_permission() {
  return array(
    'configure user dynamic background' => array(
      'title' => t('Configure user dynamic background'),
    ),
    'user selection of background' => array(
      'title' => t('Allow selection of backgrounds on nodes'),
    ),
  );
}

/**
 * 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/config/user-interface/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;
}

/**
 * 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 image style to the form.
  $form += dynamic_background_image_style_form('dynamic_background_user_image_style');

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

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

  // 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));
  $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_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']))) {
      $res = db_update('dynamic_background_user')
        ->fields(array(
        'data' => $image_id,
      ))
        ->condition('uid', $form_state['values']['uid'])
        ->execute();
    }
    else {
      db_insert('dynamic_background_user')
        ->fields(array(
        'uid' => $form_state['values']['uid'],
        'data' => $image_id,
      ))
        ->execute();
    }
  }
  else {
    db_delete('dynamic_background_user')
      ->condition('uid', $form_state['values']['uid'])
      ->execute();
  }
}

/**
 * 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) {
  $data = db_query('SELECT data FROM {dynamic_background_user} WHERE uid=:uid', array(
    ':uid' => $uid,
  ))
    ->fetchField();

  // First image id is zero, henc false.
  if ($data === FALSE) {
    return NULL;
  }
  return $data;
}

/**
 * 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);
  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_user_image_style', FALSE);
      return array(
        'image' => $backgrounds[$image_id]['picture'],
        'configuration' => variable_get('dynamic_background_user_css', array()),
        'image_style' => $image_style ? $image_style['style'] : FALSE,
      );
    }
  }
}

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

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_dynamic_background_weight Implements hook_dynamic_background_weight for the main module.
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_permission Implementation of hook_perm().