diff --git a/scheduler_handler_field_scheduler_countdown.inc b/scheduler_handler_field_scheduler_countdown.inc index 63c9106..754f8c2 100644 --- a/scheduler_handler_field_scheduler_countdown.inc +++ b/scheduler_handler_field_scheduler_countdown.inc @@ -2,11 +2,17 @@ /** * @file - * Implements scheduler_handler_scheduler_countdown field handler + * Display scheduled dates in 'countdown' format for use in Views. + * + * Defines class scheduler_handler_field_scheduler_countdown. + * For background information on the development of this handler see + * http://www.ericschaefer.org/blog/2011/01/09/custom-field-handlers-for-views-2-drupal + * + * @see scheduler.views.inc */ /** - * Field handler to display a countdown until a scheduled action + * Field handler to display a countdown until a scheduled action. */ class scheduler_handler_field_scheduler_countdown extends views_handler_field { CONST SECOND_SCALE = 1; @@ -46,13 +52,23 @@ class scheduler_handler_field_scheduler_countdown extends views_handler_field { 'plural' => 'weeks', 'abbreviated' => 'w', ), - ); function query() { + ); + + /** + * Add the new timestamp_field into the SQL query. + * It will be calculated as publish_on - unix_timestamp() so the result is + * the number of seconds from now until the event. + */ + function query() { $this->ensure_my_table(); - $this->node_table = $this->query->ensure_table('node', $this->relationship); - $time_field = $this->definition['timestamp_field']; + $this->node_table = $this->query->ensure_table('node', $this->relationship); + $time_field = $this->definition['timestamp_field']; $this->field_alias = $this->query->add_field(NULL, 'IF(' . $time_field . ' AND ' . $time_field . ' > UNIX_TIMESTAMP(), ' . $time_field . ' - UNIX_TIMESTAMP(), NULL)', $this->table_alias . '_' . $this->field); } + /** + * Define our display options and provide defaults. + */ function option_definition() { $options = parent::option_definition(); $options['countdown_display'] = array('default' => 'smart'); @@ -60,6 +76,9 @@ class scheduler_handler_field_scheduler_countdown extends views_handler_field { return $options; } + /** + * Define the form for the user to select the display options. + */ function options_form(&$form, &$form_state) { parent::options_form($form, $form_state); $form['countdown_display'] = array( @@ -79,14 +98,17 @@ class scheduler_handler_field_scheduler_countdown extends views_handler_field { '#title' => t('Display time units'), '#type' => 'radios', '#options' => array( - 'long' => t('Long (e.g. 3 days)'), - 'short' => t('Short (e.g. 3d)'), + 'long' => t('Long (for example 3 days)'), + 'short' => t('Short (for example 3d)'), 'none' => t('No units at all'), ), '#default_value' => $this->options['units_display'], ); } + /** + * Render the countdown value in the units required. + */ function render($values) { $countdown_display = $this->options['countdown_display']; $value = $values->{$this->field_alias}; @@ -127,4 +149,3 @@ class scheduler_handler_field_scheduler_countdown extends views_handler_field { return $rendered_value; } } -