View source
<?php
class countdown_box extends boxes_box {
public function options_defaults() {
$date_default = new dateObject(time(), new DateTimeZone('UTC'));
return array(
'title' => '',
'countdown_target' => $date_default
->format(DATE_FORMAT_DATETIME),
'countdown_plugin' => NULL,
'countdown_elements' => drupal_map_assoc(array(
'day',
'hour',
'minute',
'second',
)),
'event_name' => NULL,
'event_description' => t('Until @event_name'),
'event_url' => '',
);
}
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'],
);
$plugins = ctools_get_plugins('countdown', 'countdown');
$options = array();
foreach ($plugins as $plugin_info) {
$options[$plugin_info['name']] = $plugin_info['label'];
$form['countdown_plugin'][$plugin_info['name']]['#description'] = $plugin_info['description'];
}
$form['countdown_plugin']['#options'] = $options;
$target_date = new dateObject($this->options['countdown_target'], new DateTimeZone('UTC'));
$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),
'#date_timezone' => date_default_timezone(),
'#required' => TRUE,
);
$form['countdown_elements'] = array(
'#title' => t('Countdown elements'),
'#description' => t('The date elements to include in the countdown.'),
'#type' => 'checkboxes',
'#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,
);
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;
}
public function options_submit($form, $form_state) {
$date = new dateObject($this->options['countdown_target'], date_default_timezone_object());
$date
->setTimezone(new DateTimeZone('UTC'));
$this->options['countdown_target'] = $date
->format(DATE_FORMAT_DATETIME);
}
public function cache_setting() {
return DRUPAL_CACHE_GLOBAL;
}
public function render() {
$countdown_plugin = ctools_get_plugins('countdown', 'countdown', $this->options['countdown_plugin']);
include_once $countdown_plugin['path'] . '/' . $countdown_plugin['name'] . '.inc';
$plugin_callback = $countdown_plugin['callback'];
$countdown_options = $this->options;
$countdown_options['css_id'] = $this->delta;
$date = new dateObject($countdown_options['countdown_target'], new DateTimeZone('UTC'));
$date
->setTimezone(date_default_timezone_object());
$countdown_options['countdown_target_timestamp'] = $date
->getTimestamp();
$countdown_options['countdown_target_offset'] = $date
->getOffset() / (60 * 60);
$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,
'title' => $title,
'subject' => $title,
'content' => $content,
);
}
}