You are here

function _field_timer_jquery_countdown_led_formatter in Field Timer 7

Help function.

Build renderable array and load necessary files for jQuery Countdown LED formatter.

1 call to _field_timer_jquery_countdown_led_formatter()
field_timer_field_formatter_view in ./field_timer.module
Implements hook_field_formatter_view().

File

includes/field_timer.inc, line 172
Contains some help functions for formatters and settings forms.

Code

function _field_timer_jquery_countdown_led_formatter($items, $settings, $entity_type, $entity, $instance) {
  $info = entity_get_info($entity_type);
  $id_key = 'id_' . $entity->{$info['entity keys']['id']};
  $field_name = $instance['field_name'];
  $js_settings = array();
  $elements = array();

  // Add description, expiry text and url.
  $instance_settings = $instance['settings']['jquery-countdown'];
  $data = array(
    $entity_type => $entity,
  );
  $instance_settings['expiryUrl'] = token_replace($instance_settings['expiryUrl'], $data);
  $instance_settings['expiryText'] = token_replace($instance_settings['expiryText'], $data);
  $instance_settings['description'] = token_replace($instance_settings['description'], $data);
  foreach ($items as $delta => $item) {
    $layout = '<div class="jquery-countdown-led-display-wrapper">';
    if ($settings['display_days']) {
      for ($i = $settings['max_count_of_days']; $i > 0; $i--) {
        $layout .= '<span class="%t% image{d1' . substr('000', 0, $i - 1) . '}"></span>';
      }
      $layout .= '<span class="%t% imageDay"></span><span class="%t% imageSpace"></span>';
    }
    if ($settings['display_hours']) {
      $layout .= '<span class="%t% image{h10}"></span><span class="%t% image{h1}"></span>';
      if ($settings['display_minutes'] || $settings['display_seconds']) {
        $layout .= '<span class="%t% imageSep"></span>';
      }
    }
    if ($settings['display_minutes']) {
      $layout .= '<span class="%t% image{m10}"></span><span class="%t% image{m1}"></span>';
      if ($settings['display_seconds']) {
        $layout .= '<span class="%t% imageSep"></span>';
      }
    }
    if ($settings['display_seconds']) {
      $layout .= '<span class="%t% image{s10}"></span><span class="%t% image{s1}"></span>';
    }
    $layout .= '</div>';
    if (!empty($instance_settings['description'])) {
      $layout .= '<div class="jquery-countdown-led-description">{desc}</div>';
    }
    $elements[$delta] = array(
      '#type' => 'markup',
      '#markup' => '<span id="jquery-countdown-led-' . $entity_type . '-' . $id_key . '-' . $instance['field_name'] . '-' . $delta . '" class="jquery-countdown-led ' . $settings['countdown_theme'] . '">' . str_replace('%t%', $settings['countdown_theme'], $layout) . '</span>',
    );
    $js_settings[$entity_type][$id_key][$field_name][$delta]['timestamp'] = $item['timestamp'];
    $js_settings[$entity_type][$id_key][$field_name][$delta]['options'] = array_merge($settings, $instance_settings);
    $js_settings[$entity_type][$id_key][$field_name][$delta]['plugin'] = 'jquery.countdown.led';
    if ($settings['type'] == 'timer' || $settings['type'] == 'auto' && $item['timestamp'] <= REQUEST_TIME) {
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['until'] = FALSE;
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['since'] = TRUE;
    }
    elseif ($settings['type'] == 'countdown' || $settings['type'] == 'auto' && $item['timestamp'] > REQUEST_TIME) {
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['until'] = TRUE;
      $js_settings[$entity_type][$id_key][$field_name][$delta]['options']['since'] = FALSE;
    }
  }

  // Load library files.
  $library = libraries_load('jquery.countdown');
  $library_loaded = $library && !empty($library['loaded']);
  $js_name = 'jquery.countdown.js';
  $js_name_min = 'jquery.countdown.min.js';
  $base_path = 'sites/all/libraries/jquery.countdown';
  if (!$library_loaded) {
    drupal_set_message(t("Can't load jQuery Countdown library. Please download !url plugin and extract it to @path, so @js and @js_min can be found at @full_path and @full_path_min.", array(
      '!url' => l(t('jQuery Countdown'), 'http://keith-wood.name/countdown.html', array(
        'absolute' => TRUE,
      )),
      '@path' => $base_path,
      '@js' => $js_name,
      '@js_min' => $js_name_min,
      '@full_path' => $base_path . '/' . $js_name,
      '@full_path_min' => $base_path . '/' . $js_name_min,
    )), 'error');
    return array();
  }

  // Add necessary js and css files.
  drupal_add_js(array(
    'field_timer' => $js_settings,
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'field_timer') . '/js/field_timer.js', 'file');
  drupal_add_css(drupal_get_path('module', 'field_timer') . '/css/field_timer.css');

  // Since we can't get path to image from CSS file, we add this rule here.
  $path = libraries_get_path('jquery.countdown');
  drupal_add_css('.jquery-countdown-led.green span{background: url(/' . $path . '/img/countdownLED.png) no-repeat 0px 0px;} .jquery-countdown-led.blue span{background: url(/' . $path . '/img/countdownGlowing.gif) no-repeat 0px 0px;}', 'inline');
  return $elements;
}