You are here

abstract class field_timer_text_base in Field Timer 7.2

Base class for field_timer_text formatter.

Hierarchy

Expanded class hierarchy of field_timer_text_base

File

includes/field_timer_text.inc, line 11
Contains help classes to perform field_timer_text formatter related actions.

View source
abstract class field_timer_text_base extends field_timer_formatter_base {

  /**
   * @inheritdoc
   */
  public function enableFormatter() {
    return TRUE;
  }

  /**
   * @inheritdoc
   */
  public final function getFormatterDefinition() {
    return array(
      'label' => t('Text'),
      'description' => t('Simple text timer and countdown.'),
    );
  }

  /**
   * @inheritdoc
   */
  public function getDefaultSettings() {
    return array(
      'type' => 'auto',
      'granularity_type' => 'auto',
      'granularity_auto' => 2,
      'granularity_manual' => array(
        'day' => 'day',
      ),
    );
  }

  /**
   * @inheritdoc
   */
  public function getSettingsSummary($settings, $field) {
    $summary = array();
    $types = $this
      ->formatterTypes();
    $granularity_options = $this
      ->granularitySettingsSummary($settings['granularity_type'], $settings['granularity_auto'], $settings['granularity_manual']);
    $summary[] = t('Type: %type', array(
      '%type' => $types[$settings['type']],
    ));
    $summary[] = t('Granularity: %granularity_type (%granularity)', $granularity_options);
    return $summary;
  }

  /**
   * @inheritdoc
   */
  public function getSettingsForm($settings, $field) {
    $settings_form = array();
    $settings_form['type'] = array(
      '#type' => 'select',
      '#title' => t('Type'),
      '#options' => $this
        ->formatterTypes(),
      '#default_value' => $settings['type'],
    );
    $settings_form['granularity_type'] = array(
      '#type' => 'select',
      '#title' => t('Granularity'),
      '#options' => $this
        ->granularityTypes(),
      '#default_value' => $settings['granularity_type'],
      '#attributes' => array(
        'class' => array(
          'field-timer-text-granularity-type',
        ),
      ),
    );
    $settings_form['granularity_auto'] = array(
      '#type' => 'select',
      '#options' => $this
        ->formatterGranularityAuto(),
      '#default_value' => $settings['granularity_auto'],
      '#states' => array(
        'visible' => array(
          'select.field-timer-text-granularity-type' => array(
            'value' => 'auto',
          ),
        ),
      ),
    );
    $settings_form['granularity_manual'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#options' => $this
        ->formatterGranularityManual(),
      '#default_value' => $settings['granularity_manual'],
      '#states' => array(
        'visible' => array(
          'select.field-timer-text-granularity-type' => array(
            'value' => 'manual',
          ),
        ),
      ),
    );
    return $settings_form;
  }
  public function renderItems($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
    $elements = array();
    $settings = $display['settings'];
    $granularity_option = $settings['granularity_' . $settings['granularity_type']];
    foreach ($items as $delta => $item) {
      $elements[$delta] = array(
        '#type' => 'markup',
        '#markup' => $this
          ->renderItem($settings['type'], $settings['granularity_type'], $granularity_option, $this
          ->getTimestamp($item, $field)),
      );
    }
    return $elements;
  }

  /**
   * @inheritdoc
   */
  public function getFormatterName() {
    return 'field_timer_text';
  }

  /**
   * Render field item.
   */
  protected function renderItem($type, $granularity_type, $granularity_option, $timestamp) {
    $time = '';

    // Check what we should display - timer or countdown.
    switch ($type) {
      case 'auto':
        $interval = REQUEST_TIME - $timestamp;
        $sign = $interval > 0 ? '+' : '-';
        $time = $sign . $this
          ->formatInterval(abs($interval), $granularity_type, $granularity_option);
        break;
      case 'timer':
        $time = $this
          ->formatInterval(REQUEST_TIME - $timestamp, $granularity_type, $granularity_option);
        break;
      case 'countdown':
        $time = $this
          ->formatInterval($timestamp - REQUEST_TIME, $granularity_type, $granularity_option);
        break;
    }
    return $time;
  }

  /**
   * Formats interval according to granularity settings.
   */
  protected function formatInterval($interval, $granularity_type, $granularity_option) {
    $result = '';
    switch ($granularity_type) {
      case 'auto':
        $result = format_interval($interval, $granularity_option);
        break;
      case 'manual':

        // @see function format_interval
        $units = array(
          '1 year|@count years' => 31536000,
          '1 month|@count months' => 2592000,
          '1 week|@count weeks' => 604800,
          '1 day|@count days' => 86400,
          '1 hour|@count hours' => 3600,
          '1 min|@count min' => 60,
          '1 sec|@count sec' => 1,
        );
        $granularity_options = array_keys($this
          ->formatterGranularityManual());
        $output = '';
        foreach ($units as $key => $value) {
          if (isset($granularity_option[array_pop($granularity_options)])) {
            $key = explode('|', $key);
            if ($interval >= $value) {
              $output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1]);
              $interval %= $value;
            }
          }
        }
        $result = $output ? $output : (isset($key) ? format_plural(0, $key[0], $key[1]) : format_interval($interval));
        break;
    }
    return $result;
  }
  protected function granularitySettingsSummary($granularity_type, $granularity_auto, $granularity_manual) {
    $granularity_types = $this
      ->granularityTypes();
    switch ($granularity_type) {
      case 'auto':
        $granularity = $granularity_auto;
        break;
      case 'manual':
        $granularity = array();
        $granularity_manual_options = $this
          ->formatterGranularityManual();
        foreach ($granularity_manual as $key => $value) {
          if ($value) {
            $granularity[] = $granularity_manual_options[$key];
          }
        }
        $granularity = implode(', ', $granularity);
        break;
    }
    return array(
      '%granularity_type' => $granularity_types[$granularity_type],
      '%granularity' => $granularity,
    );
  }
  protected function formatterTypes() {
    return array(
      'auto' => t('Auto'),
      'timer' => t('Timer'),
      'countdown' => t('Countdown'),
    );
  }
  protected function granularityTypes() {
    return array(
      'auto' => t('Auto'),
      'manual' => t('Manual'),
    );
  }
  protected function formatterGranularityManual() {
    return array(
      'second' => t('Second'),
      'minute' => t('Minute'),
      'hour' => t('Hour'),
      'day' => t('Day'),
      'week' => t('Week'),
      'month' => t('Month'),
      'year' => t('Year'),
    );
  }
  protected function formatterGranularityAuto() {
    $options = range(0, 7);
    unset($options[0]);
    return $options;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
field_timer_formatter_base::getTimestamp abstract protected function Convert field item value to timestamp. 16
field_timer_formatter_interface::getFieldType public function Get field type. 16
field_timer_text_base::enableFormatter public function @inheritdoc Overrides field_timer_formatter_interface::enableFormatter
field_timer_text_base::formatInterval protected function Formats interval according to granularity settings.
field_timer_text_base::formatterGranularityAuto protected function
field_timer_text_base::formatterGranularityManual protected function
field_timer_text_base::formatterTypes protected function
field_timer_text_base::getDefaultSettings public function @inheritdoc Overrides field_timer_formatter_interface::getDefaultSettings 1
field_timer_text_base::getFormatterDefinition final public function @inheritdoc Overrides field_timer_formatter_interface::getFormatterDefinition
field_timer_text_base::getFormatterName public function @inheritdoc Overrides field_timer_formatter_interface::getFormatterName
field_timer_text_base::getSettingsForm public function @inheritdoc Overrides field_timer_formatter_interface::getSettingsForm 1
field_timer_text_base::getSettingsSummary public function @inheritdoc Overrides field_timer_formatter_interface::getSettingsSummary 1
field_timer_text_base::granularitySettingsSummary protected function
field_timer_text_base::granularityTypes protected function
field_timer_text_base::renderItem protected function Render field item.
field_timer_text_base::renderItems public function Render field items. Overrides field_timer_formatter_interface::renderItems 1