Skip to content

Instantly share code, notes, and snippets.

@and1truong
Created March 15, 2012 03:41
Show Gist options
  • Select an option

  • Save and1truong/2041749 to your computer and use it in GitHub Desktop.

Select an option

Save and1truong/2041749 to your computer and use it in GitHub Desktop.
DF momodule
name = Custom Date Filter
version = VERSION
description = Date filter for TW.
core = 6.x
<?php
/**
* @file
*/
/**
* Implementation of hook_views_api().
*/
function df_views_api() {
return array('api' => '2.0');
}
function df_views_data_alter(&$tables) {
if (isset($tables['wixi_domain_stats'])) {
foreach (array_keys($tables['wixi_domain_stats']) as $k) {
if (strpos($k, 'date') !== FALSE) {
if (isset($tables['wixi_domain_stats'][$k]['filter'])) {
$tables['wixi_domain_stats'][$k]['filter']['handler'] = 'df_handler_filter_date';
}
}
}
}
}
/**
* Implementation of hook_views_handlers() to register all of the basic handlers
* views uses.
*/
function df_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'df'),
),
'handlers' => array(
'df_handler_filter_date' => array(
'parent' => 'views_handler_filter_date',
),
)
);
}
<?php
/**
* @file
*/
class df_handler_filter_date extends views_handler_filter_date {
/**
* Add a type selector to the value form
*/
function value_form(&$form, &$form_state) {
if (empty($form_state['exposed'])) {
$form['value']['type'] = array(
'#type' => 'radios',
'#title' => t('Value type'),
'#options' => array(
'date' => t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
'offset' => t('An offset from the current time such as "!example1" or "!example2"', array('!example1' => '+1 day', '!example2' => '-2 hours -30 minutes')),
),
'#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
);
}
parent::value_form($form, $form_state);
unset($form['value']['type']['#options']['offset']);
}
function op_between($field) {
if ($this->operator == 'between') {
$a = $this->value['min'];
$b = $this->value['max'];
}
else {
$a = $this->value['max'];
$b = $this->value['min'];
}
// %s is safe here because strtotime scrubbed the input and we might
// have a string if using offset.
if ($this->operator == 'between') {
$this->query->add_where($this->options['group'], "$field >= '%s'", $a);
$this->query->add_where($this->options['group'], "$field <= '%s'", $b);
}
else {
$this->query->add_where($this->options['group'], "$field >= '%s' OR $field <= '%s'", array($a, $b));
}
}
function op_simple($field) {
$value = $this->value['value'];
$this->query->add_where($this->options['group'], "$field $this->operator '%s'", $value);
$this->query->add_where($this->options['group'], "$field <> '1970-01-01'");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment