You are here

countdown_box.inc in Countdown 7.2

File

plugins/countdown_box.inc
View source
<?php

/**
 * Countdown box.
 */
class countdown_box extends boxes_box {

  /**
   * Implementation of boxes_box::options_defaults().
   */
  public function options_defaults() {

    // Use the current time as a default.
    $date_default = new dateObject(time(), new DateTimeZone('UTC'));
    return array(
      'title' => '',
      'countdown_target' => $date_default
        ->format(DATE_FORMAT_DATETIME),
      'countdown_plugin' => NULL,
      // The following options do not need to be supported by each countdown
      // plugin, and should be declared in the plugin's 'supported_options'
      // property.
      // The elements to show in the countdown.
      'countdown_elements' => drupal_map_assoc(array(
        'day',
        'hour',
        'minute',
        'second',
      )),
      'event_name' => NULL,
      'event_description' => t('Until @event_name'),
      'event_url' => '',
    );
  }

  /**
   * Implementation of boxes_box::options_form().
   */
  public function options_form(&$form_state) {
    $form = array();
    $form['countdown_plugin'] = array(
      '#title' => t('Countdown plugin'),
      '#description' => t('The plugin to use for the countdown.'),
      '#type' => 'radios',
      '#default_value' => $this->options['countdown_plugin'],
    );

    // Get the list of countdown plugins.
    // Yes, this is a plugin within a plugin. The alternative would be to have
    // a different box type for each countdown, which seems messier.
    // This way allows the possibility of the countdowns being used
    // independently of boxes.
    $plugins = ctools_get_plugins('countdown', 'countdown');
    $options = array();
    foreach ($plugins as $plugin_info) {
      $options[$plugin_info['name']] = $plugin_info['label'];

      // Add the description to the radio element that doesn't exist yet:
      // FormAPI picks this up when it expands the radios.
      $form['countdown_plugin'][$plugin_info['name']]['#description'] = $plugin_info['description'];
    }
    $form['countdown_plugin']['#options'] = $options;

    // Convert the date and time from UTC.
    // Make a date object from the given time, with the site's default timezone.
    $target_date = new dateObject($this->options['countdown_target'], new DateTimeZone('UTC'));

    // Convert the date to the site timezone.
    $target_date
      ->setTimezone(date_default_timezone_object());
    $form['countdown_target'] = array(
      '#title' => t('Target date'),
      '#description' => t("The date and time the countdown will count towards, in the site's timezone."),
      '#type' => 'date_popup',
      '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
      '#default_value' => $target_date
        ->format(DATE_FORMAT_DATETIME),
      // Get the site timezone.
      '#date_timezone' => date_default_timezone(),
      '#required' => TRUE,
    );

    // Optional elements follow.
    $form['countdown_elements'] = array(
      '#title' => t('Countdown elements'),
      '#description' => t('The date elements to include in the countdown.'),
      '#type' => 'checkboxes',
      // This loses us weeks :( TODO
      '#options' => date_granularity_names(),
      '#default_value' => $this->options['countdown_elements'],
    );
    $form['event_name'] = array(
      '#title' => t('Event name'),
      '#description' => t('The name of the event'),
      '#type' => 'textfield',
      '#default_value' => $this->options['event_name'],
      '#max_length' => 250,
    );
    $form['event_description'] = array(
      '#title' => t('Countdown description'),
      '#description' => t('Enter the description to go below the countdown. You may use <strong>@event_name</strong> as a special token in this field that will be replaced with the dynamic value of the event name. If you do not wish to have a description, simply leave this field blank.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['event_description'],
      '#max_length' => 250,
      '#size' => 80,
      '#required' => FALSE,
    );
    $form['event_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Event URL'),
      '#default_value' => $this->options['event_url'],
      '#maxlength' => 250,
      '#description' => t("A URL for the event. If set, turns the event description into a link to more information about the event."),
      '#required' => FALSE,
    );

    // Add states for the form elements that are optional, so they are hidden
    // when a plugin that does not support them is selected.
    foreach ($plugins as $plugin_info) {
      foreach ($plugin_info['supported_options'] as $supported_key) {
        $form[$supported_key]['#states']['visible'][':input[name="countdown_plugin"]'][] = array(
          'value' => $plugin_info['name'],
        );
      }
    }
    return $form;
  }

  /**
   * Implementation of boxes_box::options_submit().
   */
  public function options_submit($form, $form_state) {

    // Convert the given date and time to UTC for storage.
    // Make a date object from the given time, with the site's default timezone.
    $date = new dateObject($this->options['countdown_target'], date_default_timezone_object());

    // Convert the date to UTC.
    $date
      ->setTimezone(new DateTimeZone('UTC'));

    // Set it back into the box options.
    $this->options['countdown_target'] = $date
      ->format(DATE_FORMAT_DATETIME);
  }

  /**
   * Returns the block cache settings for this box.
   */
  public function cache_setting() {
    return DRUPAL_CACHE_GLOBAL;
  }

  /**
   * Implementation of boxes_box::render().
   */
  public function render() {

    // Get the countdown plugin to add the countdown.
    $countdown_plugin = ctools_get_plugins('countdown', 'countdown', $this->options['countdown_plugin']);

    // Load the plugin file. It won't be loaded, as we're letting CTools cache
    // plugin definitions.
    // TODO: is there a CTools API function for this?
    // Note that this sets the $plugin variable from the top of the plugin .inc
    // file (hence the reason we're using $countdown_plugin, so it's not
    // clobbered).
    include_once $countdown_plugin['path'] . '/' . $countdown_plugin['name'] . '.inc';
    $plugin_callback = $countdown_plugin['callback'];

    // Build the options for the countdown plugin.
    $countdown_options = $this->options;

    // Build an ID from the block delta so that countdown plugins can have a
    // unique CSS ID.
    $countdown_options['css_id'] = $this->delta;

    // Add the target date timezone and unix timestamp.
    $date = new dateObject($countdown_options['countdown_target'], new DateTimeZone('UTC'));

    // Convert the date to the site timezone.
    $date
      ->setTimezone(date_default_timezone_object());
    $countdown_options['countdown_target_timestamp'] = $date
      ->getTimestamp();
    $countdown_options['countdown_target_offset'] = $date
      ->getOffset() / (60 * 60);

    // Process and sanitize the event description and name.
    $countdown_options['event_description'] = format_string(check_plain($countdown_options['event_description']), array(
      '@event_name' => $countdown_options['event_name'],
    ));
    $countdown_options['event_name'] = check_plain($countdown_options['event_name']);
    $title = isset($this->title) ? $this->title : NULL;
    $content = $plugin_callback($countdown_plugin, $countdown_options);
    return array(
      'delta' => $this->delta,
      // Crucial.
      'title' => $title,
      'subject' => $title,
      'content' => $content,
    );
  }

}

Classes

Namesort descending Description
countdown_box Countdown box.