[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/data/ -> data.views.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   * Views hooks and utility functions.
   5   */
   6  
   7  /**
   8   * Implementation of hook_views_data().
   9   * 
  10   * Dynamically create views integration for any table Data manages.
  11   */
  12  function data_views_data() {
  13    $data = array();
  14  
  15    $tables = data_get_all_tables();
  16  
  17    foreach ($tables as $table) {
  18  
  19      // Get schema and check wether there are field definitions.
  20      $schema = $table->get('table_schema');
  21      $meta = $table->get('meta');
  22      if (!isset($schema['fields'])) {
  23        continue;
  24      }
  25  
  26      $table_data = array();
  27      $table_data['table'] = array(
  28        'group' => $table->get('title'),
  29        );
  30      foreach ($schema['fields'] as $field_name => $field) {
  31        // If there is no label, generate one from field name.
  32        $title = empty($meta['fields'][$field_name]['label']) ? data_natural_name($field_name) : $meta['fields'][$field_name]['label'];
  33  
  34        $table_data[$field_name] = array(
  35          'title' => $title,
  36          'help' => $title,
  37          'field' => array(
  38            'handler' => data_get_views_handler('field', $table, $field_name),
  39            'help' => $title,
  40            'click sortable' => TRUE,
  41          ),
  42          'filter' => array(
  43            'handler' => data_get_views_handler('filter', $table, $field_name),
  44            'allow empty' => TRUE,
  45            'help' => t('Filter on %field', array('%field' => $title)),
  46          ),
  47          'argument' => array(
  48            'handler' => data_get_views_handler('argument', $table, $field_name),
  49            'help' => $title,
  50          ),
  51          'sort' => array(
  52            'handler' => data_get_views_handler('sort', $table, $field_name),
  53            'help' => t('Sort by %field', array('%field' => $title)),
  54          ),
  55        );
  56      }
  57  
  58      // Tables with a primary key are base tables.
  59      if (isset($schema['primary key']) && count($schema['primary key']) >= 1) {
  60        $table_data['table']['base'] = array(
  61          'field' => current($schema['primary key']),
  62          'title' => $table->get('title'),
  63          'help' => t('Data table'),
  64          'weight' => 10, 
  65        );
  66      }
  67  
  68      // Add join information.
  69      if (isset($meta['join']) && is_array($meta['join'])) {
  70        $table_data['table']['join'] = array();
  71        foreach ($meta['join'] as $left_table => $join) {
  72          // @todo: See if left table has other tables it is linked to and link
  73          // all the way to the leftmost table.
  74          $table_data['table']['join'][$left_table] = array(
  75            'left_field' => $join['left_field'],
  76            'field' => $join['field'],
  77            'type' => $join['inner_join'] ? 'INNER' : 'LEFT',
  78          );
  79          // Add an explicit relationship for every join added.
  80          $table_data[$join['field']]['relationship'] = array(
  81            'label' => "{$table->get('name')}.{$join['field']} -> {$left_table}.{$join['left_field']}",
  82            'base' => $left_table,
  83            'base field' => $join['left_field'],
  84          );
  85        }
  86      }
  87  
  88      $data[$table->get('name')] = $table_data;
  89    }
  90  
  91    return $data;
  92  }
  93  
  94  /**
  95   * Implementation of hook_views_handlers().
  96   */
  97  function data_views_handlers() {
  98    return array(
  99      'info' => array(
 100        'path' => drupal_get_path('module', 'data') . '/views',
 101      ),
 102      'handlers' => array(
 103        'views_handler_field_data_markup' => array(
 104          'parent' => 'views_handler_field',
 105        ),
 106      ),
 107    );
 108  }
 109  
 110  /**
 111   * Return all available field handlers.
 112   *
 113   * @param $type
 114   *   Optional: the view handler type whose options should be provided
 115   *   ('field', 'filter', 'sort', 'argument'). If omitted, a full array
 116   *   keyed on type is returned.
 117   * @param $reset
 118   *   Boolean to reset the static cache.
 119   *
 120   * @return
 121   *   An array suitable for use as options in a FormAPI element.
 122   */
 123  function data_get_views_handler_options($type = NULL, $reset = FALSE) {
 124    static $handlers;
 125    if (!isset($handlers) || $reset) {
 126      $handlers = array();
 127      module_load_include('inc', 'views', 'includes/base');
 128      module_load_include('inc', 'views', 'includes/handlers');
 129      $available_handlers = views_discover_handlers();
 130      $handlers = array(
 131        'field',
 132        'filter',
 133        'argument',
 134        'sort',
 135      );
 136      foreach ($available_handlers as $available_handler => $def) {
 137        foreach ($handlers as $handler) {
 138          // Look for handler names of the form 'views_handler_TYPE.*'.
 139          $pattern = '/^views_handler_';
 140          $pattern .= $handler;
 141          $pattern .= '([_][^_.]+)*$/';
 142          if (preg_match($pattern, $available_handler)) {
 143            $handlers[$handler][$available_handler] = $available_handler;
 144          };
 145        };
 146      };
 147      // Allow other modules to alter the list of available handlers.
 148      drupal_alter('data_views_handlers', $handlers);
 149    }
 150    return isset($type) && isset($handlers[$type]) ? $handlers[$type] : $handlers;
 151  }
 152  
 153  /**
 154   * Get the handler (class name) for a specified data table field.
 155   *
 156   * @param $type
 157   *   The view handler type ('field', 'filter', 'sort', 'argument').
 158   * @param $table
 159   *   A data table object.
 160   * @param $field_name
 161   *   String: name of the field.
 162   * @param $default
 163   *   Boolean for whether to return the default handler for the given
 164   *   db column type.
 165   *
 166   * @return
 167   *   String: A views handler class name.
 168   */
 169  function data_get_views_handler($type, $table, $field_name, $default = FALSE) {
 170    // Return the handler's custom setting if available
 171    if (!$default) {
 172      $meta = $table->get('meta');
 173      if (isset($meta['fields'][$field_name]["views_{$type}_handler"])) {
 174        return $meta['fields'][$field_name]["views_{$type}_handler"];
 175      }
 176    }
 177  
 178    $schema = $table->get('table_schema');
 179    switch ($type) {
 180  
 181      case 'field':
 182        switch ($schema['fields'][$field_name]['type']) {
 183          case 'int':
 184          case 'float':
 185          case 'serial':
 186          case 'numeric':
 187            return 'views_handler_field_numeric';
 188          case 'datetime':
 189            return 'views_handler_field_date';
 190        }
 191        return 'views_handler_field';
 192  
 193      case 'filter':
 194        switch ($schema['fields'][$field_name]['type']) {
 195          case 'float':
 196          case 'numeric':
 197            return 'views_handler_filter_float';
 198          case 'int':
 199          case 'serial':
 200            return 'views_handler_filter_numeric';
 201          case 'datetime':
 202            // @TODO: make this conditional on whether the time is stored as a timestamp/datetime
 203            if (module_exists('date_api')) {
 204              return 'date_api_filter_handler';
 205            }
 206            return 'views_handler_filter_date';
 207        }
 208        return 'views_handler_filter_string';
 209  
 210      case 'argument':
 211        switch ($schema['fields'][$field_name]['type']) {
 212          case 'int':
 213          case 'float':
 214          case 'serial':
 215          case 'numeric':
 216            return 'views_handler_argument_numeric';
 217          case 'datetime':
 218            return 'views_handler_argument_date';
 219          case 'varchar':
 220          return 'views_handler_argument_string';
 221        }
 222        return 'views_handler_argument';
 223  
 224      case 'sort':
 225        switch ($schema['fields'][$field_name]['type']) {
 226          case 'datetime':
 227            return 'views_handler_sort_date';
 228        }
 229        return 'views_handler_sort';
 230  
 231    }
 232  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7