You are here

dynamic_background_blog.module in Dynamic Background 7

File

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

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

/**
 * Implementation of hook_permission().
 */
function dynamic_background_blog_permission() {
  return array(
    'configure blog dynamic background' => array(
      'title' => t('Configure blog dynamic background'),
    ),
    'user selection of blog background' => array(
      'title' => t('Allow selection of background on user blog'),
    ),
  );
}

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

  // 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, $form_state, $user) {

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

/**
 * 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) {
  $query = 'SELECT image_id FROM {dynamic_background_blog} WHERE uid=:uid';
  $values = array(
    ':uid' => $uid,
  );
  $image_id = db_query($query, $values)
    ->fetchField();
  if ($image_id === FALSE) {
    return NULL;
  }
  return $image_id;
}

/**
 * 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 = explode('/', drupal_get_path_alias());
    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];
    }
  }

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

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

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

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

Constants