You are here

closeblock.api.inc in Close Block 6

Same filename and directory in other branches
  1. 7 includes/closeblock.api.inc

File

includes/closeblock.api.inc
View source
<?php

/**
 * Default theme's settings
 */
function closeblock_default_settings() {
  $defaults = array(
    'closeblock_active' => CLOSEBLOCK_ACTIVE,
    'closeblock_custom' => CLOSEBLOCK_CUSTOM,
    'closeblock_type' => CLOSEBLOCK_TYPE,
    'closeblock_speed' => CLOSEBLOCK_SPEED,
    'closeblock_save' => CLOSEBLOCK_SAVE,
    'closeblock_count' => CLOSEBLOCK_COUNT,
    'closeblock_frequency' => CLOSEBLOCK_FREQUENCY,
  );
  return $defaults;
}

/**
 * Load settings
 */
function closeblock_initialization() {
  if (user_access('closeblock')) {
    global $theme, $user, $closeblock_blocks, $closeblock_theme_settings;
    $current_theme = $theme ? $theme : variable_get('theme_default', 'garland');
    $params = array(
      'uid' => $user->uid,
      'theme' => $current_theme,
    );
    $closeblock_blocks = closeblock_closing_info_get($params);

    //load the settings
    $current_theme = $theme ? $theme : variable_get('theme_default', 'garland');
    $closeblock_theme_settings = variable_get(str_replace('/', '_', 'theme_' . $current_theme . '_settings'), array());
    $closeblock_theme_settings = array_merge(closeblock_default_settings(), $closeblock_theme_settings, array());
  }
}

/**
 * Get blocks settings.
 */
function closeblock_blocks_process(&$vars) {
  global $theme;

  // Populate all block regions.
  $regions = system_region_list($theme);

  // Load all region content assigned via blocks.
  foreach (array_keys($regions) as $region) {

    // Prevent left and right regions from rendering blocks when 'show_blocks' == FALSE.
    if (!(!$vars['show_blocks'] && ($region == 'left' || $region == 'right'))) {
      if ($list = block_list($region)) {
        foreach ($list as $key => $block) {
          $output .= closeblock_ajax_settings($block);
        }
      }
    }
  }
  $vars['scripts'] = drupal_get_js();
}

/**
 * Write block settings to Drupal.settings.closeblock
 */
function closeblock_ajax_settings($block) {
  if (user_access('closeblock') && !empty($block->content)) {
    global $closeblock_blocks, $closeblock_theme_settings;
    $settings = variable_get('closeblock_settings', array());
    $params = (array) $block;
    $block_id = theme('closeblock_block_id', $params);
    if (empty($settings[$block_id]['closeblock_active'])) {
      return;
    }
    if (!empty($settings[$block_id]['closeblock_custom'])) {
      $theme_settings = array_merge($closeblock_theme_settings, $settings[$block_id], array());
    }
    $closed = !empty($closeblock_blocks[$block_id]) && !empty($theme_settings['closeblock_save']);
    if (!empty($theme_settings['closeblock_count']) && $theme_settings['closeblock_count'] > 1) {
      if (empty($closeblock_blocks[$block_id]['count']) || $closeblock_blocks[$block_id]['count'] < $theme_settings['closeblock_count']) {
        if (empty($theme_settings['closeblock_frequency'])) {
          $closed = 0;
        }
        else {
          $current_time = time();
          $time = strtotime($theme_settings['closeblock_frequency']) - $current_time;
          if (empty($closeblock_blocks[$block_id]['timestamp']) || $closeblock_blocks[$block_id]['timestamp'] + $time < $current_time) {
            $closed = 0;
          }
        }
      }
    }
    $types = array(
      1 => '',
      2 => 'slideUp',
      3 => 'fadeOut',
    );
    drupal_add_js(array(
      'closeblock' => array(
        $block_id => array(
          'closed' => $closed,
          'speed' => $theme_settings['closeblock_speed'],
          'save' => $theme_settings['closeblock_save'],
          'type' => $types[$theme_settings['closeblock_type']],
          'module' => $params['module'],
          'delta' => $params['delta'],
        ),
      ),
    ), 'setting');
    $path = drupal_get_path('module', 'closeblock');
    drupal_add_js($path . '/theme/js/closeblock.js');
    drupal_add_css($path . '/theme/css/closeblock.css');
  }
}

/**
 * Set info.
 */
function closeblock_closing_info_set($params) {
  if (isset($params['uid']) && !empty($params['uid'])) {
    $info = closeblock_closing_info_get($params);
    $params['count'] = 1;
    $params['timestamp'] = time();
    if ($info) {
      $block_id = theme('closeblock_block_id', $params);
      $params['count'] = $info[$block_id]['count'] + 1;
      drupal_write_record('closeblock', $params, array(
        'module',
        'delta',
        'theme',
        'uid',
      ));
    }
    else {
      drupal_write_record('closeblock', $params);
    }
  }
  else {
    closeblock_closing_info_anonymous_set($params);
  }
}

/**
 * Get info.
 */
function closeblock_closing_info_get($params) {
  if (isset($params['uid']) && empty($params['uid'])) {
    return closeblock_closing_info_anonymous_get($params);
  }
  if (empty($params) || !is_array($params)) {
    return;
  }
  $result = closeblock_closing_info_get_db($params);
  $blocks = array();
  if (!empty($result)) {
    foreach ($result as $block) {
      $params = (array) $block;
      $block_id = theme('closeblock_block_id', $params);
      $blocks[$block_id] = $params;
    }
  }
  return $blocks;
}

/**
 * Set info for anonymous users.
 */
function closeblock_closing_info_anonymous_set($params) {
  if (!empty($params['theme']) && !empty($params['module']) && !empty($params['delta'])) {
    $block_id = theme('closeblock_block_id', $params);
    $count = 1;
    if (!empty($_SESSION['closeblock'][$block_id]['count'])) {
      $count += $_SESSION['closeblock'][$block_id]['count'];
    }
    $_SESSION['closeblock'][$block_id] = array(
      'count' => $count,
      'timestamp' => time(),
      'module' => $params['module'],
      'delta' => $params['delta'],
    );
  }
}

/**
 * Get info for anonymous users.
 */
function closeblock_closing_info_anonymous_get($params) {
  if (!empty($params['theme']) && !empty($params['module']) && !empty($params['delta'])) {
    $block_id = theme('closeblock_block_id', $params);
    if (!empty($_SESSION['closeblock'][$block_id])) {
      return $_SESSION['closeblock'][$block_id];
    }
  }
  else {
    if (!empty($_SESSION['closeblock'])) {
      return $_SESSION['closeblock'];
    }
  }
  return array();
}

/**
 * Ajax callback function.
 */
function closeblock_ajax_callback($module, $delta) {
  global $theme, $user;
  $current_theme = $theme ? $theme : variable_get('theme_default', 'garland');
  $params = array(
    'uid' => $user->uid,
    'theme' => $current_theme,
    'module' => $module,
    'delta' => $delta,
  );
  closeblock_closing_info_set($params);
}

Functions

Namesort descending Description
closeblock_ajax_callback Ajax callback function.
closeblock_ajax_settings Write block settings to Drupal.settings.closeblock
closeblock_blocks_process Get blocks settings.
closeblock_closing_info_anonymous_get Get info for anonymous users.
closeblock_closing_info_anonymous_set Set info for anonymous users.
closeblock_closing_info_get Get info.
closeblock_closing_info_set Set info.
closeblock_default_settings Default theme's settings
closeblock_initialization Load settings