Classes | Functions

includes/handlers.inc File Reference

Defines the various handler objects to help build and display views. More...

Go to the source code of this file.

Classes

class  views_handler
 Base handler, from which all the other handlers are derived. More...
class  views_many_to_one_helper
 This many to one helper object is used on both arguments and filters. More...
class  views_join
 A function class to represent a join and create the SQL necessary to implement the join. More...
class  views_join_subquery
 Join handler for relationships that join with a subquery as the left field. More...

Functions

 _views_create_handler ($definition, $type= 'handler', $handler_type=NULL)
 Instantiate and construct a new handler.
 _views_prepare_handler ($definition, $data, $field, $type)
 Prepare a handler's data by checking defaults and such.
 views_get_table_join ($table, $base_table)
 Fetch a handler to join one table to a primary table from the data cache.
 views_break_phrase_string ($str, &$handler=NULL)
 views_break_phrase ($str, &$handler=NULL)
 views_get_timezone ()
 Figure out what timezone we're in; needed for some date manipulations.
 views_date_sql_field ($field, $field_type= 'int', $set_offset=NULL)
 Helper function to create cross-database SQL dates.
 views_date_sql_format ($format, $field, $field_type= 'int', $set_offset=NULL)
 Helper function to create cross-database SQL date formatting.
 views_date_sql_extract ($extract_type, $field, $field_type= 'int', $set_offset=NULL)
 Helper function to create cross-database SQL date extraction.

Detailed Description

Defines the various handler objects to help build and display views.

Definition in file handlers.inc.


Function Documentation

_views_create_handler ( definition,
type = 'handler',
handler_type = NULL 
)

Instantiate and construct a new handler.

Definition at line 10 of file handlers.inc.

References vpr().

Referenced by _views_prepare_handler(), views_get_handler(), and views_get_plugin().

                                                                                     {
//  debug('Instantiating handler ' . $definition['handler']);
  if (empty($definition['handler'])) {
    vpr('_views_create_handler - type: @type - failed: handler has not been provided.',
      array('@type' => isset($handler_type) ? ( $type . '(handler type: ' . $handler_type . ')' ) : $type)
    );
    return;
  }

  // class_exists will automatically load the code file.
  if (!empty($definition['override handler']) &&
      !class_exists($definition['override handler'])) {
    vpr(
      '_views_create_handler - loading override handler @type failed: class @override_handler could not be loaded. ' .
      'Verify the class file has been registered in the corresponding .info-file (files[]).',
      array(
        '@type' => isset($handler_type) ? ( $type . '(handler type: ' . $handler_type . ')' ) : $type,
        '@override_handler' => $definition['override handler']
      )
    );
    return;
  }

  if (!class_exists($definition['handler'])) {
    vpr(
      '_views_create_handler - loading handler @type failed: class @handler could not be loaded. ' .
      'Verify the class file has been registered in the corresponding .info-file (files[]).',
      array(
        '@type' => isset($handler_type) ? ( $type . '(handler type: ' . $handler_type . ')' ) : $type,
        '@handler' => $definition['handler']
      )
    );
    return;
  }

   if (!empty($definition['override handler'])) {
     $handler = new $definition['override handler'];
   }
   else {
     $handler = new $definition['handler'];
   }

  $handler->set_definition($definition);
  if ($type == 'handler') {
    $handler->is_handler = TRUE;
    $handler->handler_type = $handler_type;
  }
  else {
    $handler->is_plugin = TRUE;
    $handler->plugin_type = $type;
  }

  // let the handler have something like a constructor.
  $handler->construct();

  return $handler;
}

_views_prepare_handler ( definition,
data,
field,
type 
)

Prepare a handler's data by checking defaults and such.

Definition at line 71 of file handlers.inc.

References _views_create_handler().

Referenced by views_get_handler().

                                                                   {
  foreach (array('group', 'title', 'title short', 'help', 'real field') as $key) {
    if (!isset($definition[$key])) {
      // First check the field level
      if (!empty($data[$field][$key])) {
        $definition[$key] = $data[$field][$key];
      }
      // Then if that doesn't work, check the table level
      elseif (!empty($data['table'][$key])) {
        $definition[$key] = $data['table'][$key];
      }
    }
  }

  return _views_create_handler($definition, 'handler', $type);
}

views_date_sql_extract ( extract_type,
field,
field_type = 'int',
set_offset = NULL 
)

Helper function to create cross-database SQL date extraction.

Parameters:
$extract_type The type of value to extract from the date, like 'MONTH'.
$field The real table and field name, like 'tablename.fieldname'.
$field_type The type of date field, 'int' or 'datetime'.
$set_offset The name of a field that holds the timezone offset or a fixed timezone offset value. If not provided, the normal Drupal timezone handling will be used, i.e. $set_offset = 0 will make no timezone adjustment.
Returns:
An appropriate SQL string for the db type and field type.

Definition at line 1336 of file handlers.inc.

References views_date_sql_field().

Referenced by views_handler_argument_node_created_week::construct(), views_handler_argument_node_created_day::construct(), views_handler_argument_node_created_month::construct(), and views_handler_argument_node_created_year::construct().

                                                                                                {
  $db_type = Database::getConnection()->databaseType();
  $field = views_date_sql_field($field, $field_type, $set_offset);

  // Note there is no space after FROM to avoid db_rewrite problems
  // see http://drupal.org/node/79904.
  switch ($extract_type) {
  case('DATE'):
    return $field;
  case('YEAR'):
    return "EXTRACT(YEAR FROM($field))";
  case('MONTH'):
    return "EXTRACT(MONTH FROM($field))";
  case('DAY'):
    return "EXTRACT(DAY FROM($field))";
  case('HOUR'):
    return "EXTRACT(HOUR FROM($field))";
  case('MINUTE'):
    return "EXTRACT(MINUTE FROM($field))";
  case('SECOND'):
    return "EXTRACT(SECOND FROM($field))";
  case('WEEK'):  // ISO week number for date
    switch ($db_type) {
      case('mysql'):
        // WEEK using arg 3 in mysql should return the same value as postgres EXTRACT
        return "WEEK($field, 3)";
      case('pgsql'):
        return "EXTRACT(WEEK FROM($field))";
    }
  case('DOW'):
    switch ($db_type) {
      case('mysql'):
        // mysql returns 1 for Sunday through 7 for Saturday
        // php date functions and postgres use 0 for Sunday and 6 for Saturday
        return "INTEGER(DAYOFWEEK($field) - 1)";
      case('pgsql'):
        return "EXTRACT(DOW FROM($field))";
    }
  case('DOY'):
    switch ($db_type) {
      case('mysql'):
        return "DAYOFYEAR($field)";
      case('pgsql'):
        return "EXTRACT(DOY FROM($field))";
    }
  }
}

views_date_sql_field ( field,
field_type = 'int',
set_offset = NULL 
)

Helper function to create cross-database SQL dates.

Parameters:
$field The real table and field name, like 'tablename.fieldname'.
$field_type The type of date field, 'int' or 'datetime'.
$set_offset The name of a field that holds the timezone offset or a fixed timezone offset value. If not provided, the normal Drupal timezone handling will be used, i.e. $set_offset = 0 will make no timezone adjustment.
Returns:
An appropriate SQL string for the db type and field type.

Definition at line 1192 of file handlers.inc.

References views_get_timezone().

Referenced by views_date_sql_extract(), and views_date_sql_format().

                                                                               {
  $db_type = Database::getConnection()->databaseType();
  $offset = $set_offset !== NULL ? $set_offset : views_get_timezone();
  if (isset($offset) && !is_numeric($offset)) {
    $dtz = new DateTimeZone($offset);
    $dt = new DateTime("now", $dtz);
    $offset_seconds = $dtz->getOffset($dt);
  }

  switch ($db_type) {
    case 'mysql':
      switch ($field_type) {
        case 'int':
          $field = "DATE_ADD('19700101', INTERVAL $field SECOND)";
          break;
        case 'datetime':
          break;
      }
      if (!empty($offset)) {
        $field = "($field + INTERVAL $offset_seconds SECOND)";
      }
      return $field;
    case 'pgsql':
      switch ($field_type) {
        case 'int':
          $field = "TO_TIMESTAMP($field)";
          break;
        case 'datetime':
          break;
      }
      if (!empty($offset)) {
        $field = "($field + INTERVAL '$offset_seconds SECONDS')";
      }
      return $field;
    case 'sqlite':
      if (!empty($offset)) {
        $field = "($field + '$offset_seconds')";
      }
      return $field;
  }
}

views_date_sql_format ( format,
field,
field_type = 'int',
set_offset = NULL 
)

Helper function to create cross-database SQL date formatting.

Parameters:
$format A format string for the result, like 'Y-m-d H:i:s'.
$field The real table and field name, like 'tablename.fieldname'.
$field_type The type of date field, 'int' or 'datetime'.
$set_offset The name of a field that holds the timezone offset or a fixed timezone offset value. If not provided, the normal Drupal timezone handling will be used, i.e. $set_offset = 0 will make no timezone adjustment.
Returns:
An appropriate SQL string for the db type and field type.

Definition at line 1250 of file handlers.inc.

References views_date_sql_field().

Referenced by views_handler_argument_node_created_year_month::construct(), views_handler_argument_node_created_fulldate::construct(), and views_handler_sort_date::query().

                                                                                         {
  $db_type = Database::getConnection()->databaseType();
  $field = views_date_sql_field($field, $field_type, $set_offset);
  switch ($db_type) {
    case 'mysql':
      $replace = array(
        'Y' => '%Y',
        'y' => '%y',
        'M' => '%b',
        'm' => '%m',
        'n' => '%c',
        'F' => '%M',
        'D' => '%a',
        'd' => '%d',
        'l' => '%W',
        'j' => '%e',
        'W' => '%v',
        'H' => '%H',
        'h' => '%h',
        'i' => '%i',
        's' => '%s',
        'A' => '%p',
        );
      $format = strtr($format, $replace);
      return "DATE_FORMAT($field, '$format')";
    case 'pgsql':
      $replace = array(
        'Y' => 'YYYY',
        'y' => 'YY',
        'M' => 'Mon',
        'm' => 'MM',
        'n' => 'MM', // no format for Numeric representation of a month, without leading zeros
        'F' => 'Month',
        'D' => 'Dy',
        'd' => 'DD',
        'l' => 'Day',
        'j' => 'DD', // no format for Day of the month without leading zeros
        'W' => 'WW',
        'H' => 'HH24',
        'h' => 'HH12',
        'i' => 'MI',
        's' => 'SS',
        'A' => 'AM',
        );
      $format = strtr($format, $replace);
      return "TO_CHAR($field, '$format')";
    case 'sqlite':
      $replace = array(
        'Y' => '%Y', // 4 digit year number
        'y' => '%Y', // no format for 2 digit year number
        'M' => '%m', // no format for 3 letter month name
        'm' => '%m', // month number with leading zeros
        'n' => '%m', // no format for month number without leading zeros
        'F' => '%m', // no format for full month name
        'D' => '%d', // no format for 3 letter day name
        'd' => '%d', // day of month number with leading zeros
        'l' => '%d', // no format for full day name
        'j' => '%d', // no format for day of month number without leading zeros
        'W' => '%W', // ISO week number
        'H' => '%H', // 24 hour hour with leading zeros
        'h' => '%H', // no format for 12 hour hour with leading zeros
        'i' => '%M', // minutes with leading zeros
        's' => '%S', // seconds with leading zeros
        'A' => '', // no format for  AM/PM
      );
      $format = strtr($format, $replace);
      return "strftime('$format', $field, 'unixepoch')";
  }
}

views_get_table_join ( table,
base_table 
)

Fetch a handler to join one table to a primary table from the data cache.

Definition at line 91 of file handlers.inc.

References views_fetch_data(), and vpr().

Referenced by views_many_to_one_helper::add_table(), views_handler::get_join(), views_plugin_query_default::get_join_data(), and views_handler_argument::summary_name_field().

                                                   {
  $data = views_fetch_data($table);
  if (isset($data['table']['join'][$base_table])) {
    $h = $data['table']['join'][$base_table];
    if (!empty($h['handler']) && class_exists($h['handler'])) {
      $handler = new $h['handler'];
    }
    else {
      $handler = new views_join();
    }

    // Fill in some easy defaults
    $handler->definition = $h;
    if (empty($handler->definition['table'])) {
      $handler->definition['table'] = $table;
    }
    // If this is empty, it's a direct link.
    if (empty($handler->definition['left_table'])) {
      $handler->definition['left_table'] = $base_table;
    }

    if (isset($h['arguments'])) {
      call_user_func_array(array(&$handler, 'construct'), $h['arguments']);
    }
    else {
      $handler->construct();
    }

    return $handler;
  }

  // DEBUG -- identify missing handlers
  vpr("Missing join: @table @base_table", array('@table' => $table, '@base_table' => $base_table));
}

views_get_timezone (  ) 

Figure out what timezone we're in; needed for some date manipulations.

Definition at line 1149 of file handlers.inc.

Referenced by views_date_sql_field().

                              {
  global $user;
  if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
    $timezone = $user->timezone;
  }
  else {
    $timezone = variable_get('date_default_timezone', 0);
  }

  // set up the database timezone
  $db_type = Database::getConnection()->databaseType();
  if (in_array($db_type, array('mysql', 'pgsql'))) {
    $offset = '+00:00';
    static $already_set = FALSE;
    if (!$already_set) {
      if ($db_type == 'pgsql') {
        db_query("SET TIME ZONE INTERVAL '$offset' HOUR TO MINUTE");
      }
      elseif ($db_type == 'mysql') {
        db_query("SET @@session.time_zone = '$offset'");
      }

      $already_set = true;
    }
  }

  return $timezone;
}