You are here

jquery_countdown_block.module in jQuery Countdown 6

Provides configurable blocks of countdown timers.

File

jquery_countdown_block/jquery_countdown_block.module
View source
<?php

/**
 * @file
 * Provides configurable blocks of countdown timers.
 */

/**
 * Implements hook_menu().
 */
function jquery_countdown_block_menu() {
  $items['admin/build/block/add-jquery-countdown-block'] = array(
    'title' => 'Add countdown block',
    'description' => 'Add a new Countdown block.',
    'access arguments' => array(
      'administer blocks',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_countdown_block_add_block_form',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/build/block/delete-jquery-countdown-block'] = array(
    'title' => 'Delete menu block',
    'access arguments' => array(
      'administer blocks',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jquery_countdown_block_delete',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'jquery_countdown_block.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_help().
 * @TODO: we need a help for our users ;)
 */
function jquery_countdown_block_help($path, $arg) {
  switch ($path) {
    case 'admin/build/block/configure':
      if ($arg[4] != 'jquery_countdown_block') {
        break;
      }
    case 'admin/help#jquery-countdown-block':
    case 'admin/build/block':
    case 'admin/build/block/add-jquery-countdown-block':
  }
}

/**
 * Form builder for the countdown block addition form.
 */
function jquery_countdown_block_add_block_form(&$form_state) {

  // Load block.admin.inc from block module.
  module_load_include('inc', 'block', 'block.admin');

  // Mimic block_add_block_form(), which is also just a wrapper.
  return block_admin_configure($form_state, 'jquery_countdown_block', NULL);
}

/**
 * Form submission handler for jquery_countdown_block_add_block_form().
 */
function jquery_countdown_block_add_block_form_submit($form, &$form_state) {

  // Determine the delta of the new block.
  $block_ids = variable_get('jquery_countdown_block_ids', array());
  $delta = empty($block_ids) ? 1 : max($block_ids) + 1;

  // Save the new array of blocks IDs.
  $block_ids[] = $delta;
  variable_set('jquery_countdown_block_ids', $block_ids);

  // Save the block configuration.
  _jquery_countdown_block_block_save($delta, $form_state['values']);

  // Run the normal new block submission (borrowed from block_add_block_form_submit).
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
      db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, %d, %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta, BLOCK_CACHE_GLOBAL);
    }
  }
  foreach (array_filter($form_state['values']['roles']) as $rid) {
    db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
  }
  drupal_set_message(t('The block has been created.'));
  cache_clear_all();
  $form_state['redirect'] = 'admin/build/block';
  return;
}

/**
 * Implements hook_block().
 */
function jquery_countdown_block_block($op = 'list', $delta = NULL, $edit = NULL) {
  $function = '_jquery_countdown_block_block_' . $op;
  switch ($op) {
    case 'view':
      return $function($delta, $edit);
    default:

      // Ops besides "view" are seldom used, so are in a separate file.
      module_load_include('inc', 'jquery_countdown_block', 'jquery_countdown_block.admin');
      return $function($delta, $edit);
  }
}

/**
 * Returns the 'view' $op info for hook_block().
 *
 * @param $delta
 *   string The id of the block to render.
 */
function _jquery_countdown_block_block_view($delta) {
  $data = array();

  // Get block settings from variables or throw error if not exists...
  $block_settings = variable_get('jquery_countdown_block_' . $delta . '_settings', NULL);
  if (!$block_settings) {
    $data['subject'] = '';
    $data['content'] = t('this block is not configured. please go to the block settings and configure it');
    return $data;
  }

  // sanitize block settings to a format the our jquery_countdown script is understanding ;)
  $jquery_countdown_options = array(
    $block_settings['until'] => date_format_date(date_make_date($block_settings['date']), 'custom', 'F d, Y g:i a', 'en_US'),
    'format' => implode('', $block_settings['format']),
  );

  // merge with the rest so we get always all settings
  $jquery_countdown_options = array_merge($block_settings, $jquery_countdown_options);

  // call theme function and return the countdown
  $data['subject'] = '';
  $data['content'] = theme('jquery_countdown', $jquery_countdown_options, '', 'jquery-countdown-block-' . $delta);
  return $data;
}

/**
 * Implements hook_form_FORM_ID_alter() for block_admin_display_form.
 *
 * Alters the block admin form to add delete links next to countdown blocks.
 */
function jquery_countdown_block_form_block_admin_display_form_alter(&$form, $form_state) {
  foreach (variable_get('jquery_countdown_block_ids', array()) as $delta) {
    $form['jquery_countdown_block_' . $delta]['delete'] = array(
      '#value' => l(t('delete'), 'admin/build/block/delete-jquery-countdown-block/' . $delta),
    );
  }
}

Functions

Namesort descending Description
jquery_countdown_block_add_block_form Form builder for the countdown block addition form.
jquery_countdown_block_add_block_form_submit Form submission handler for jquery_countdown_block_add_block_form().
jquery_countdown_block_block Implements hook_block().
jquery_countdown_block_form_block_admin_display_form_alter Implements hook_form_FORM_ID_alter() for block_admin_display_form.
jquery_countdown_block_help Implements hook_help(). @TODO: we need a help for our users ;)
jquery_countdown_block_menu Implements hook_menu().
_jquery_countdown_block_block_view Returns the 'view' $op info for hook_block().