Index: location.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/location/location.module,v
retrieving revision 1.60
diff -u -r1.60 location.module
--- location.module	3 Apr 2006 23:07:54 -0000	1.60
+++ location.module	8 Apr 2006 17:16:59 -0000
@@ -920,4 +920,435 @@
   return (abs(max($x, $y) - min($x, $y)) < pow(10, -6));
 }
 
-?>
+
+/**
+ * For operation with the views.module.
+ */
+function location_views_tables() {
+
+  $countries = array();
+  $provinces = array();
+  $this = location_get_configured_countries();
+  $country_list = _location_get_iso3166_list();
+  foreach ($this as $country) {
+    $countries[$country] = $country_list[$country];
+    $list = _location_province_select_options('', FALSE, $country);
+    $provinces = $list['#options'];
+  }
+
+  $contains = array('' => t('<All>'), 'contains' => t('contains'), 'starts' => t('starts with'), 'ends' => t('ends with'), 'not' => t('does not contain'));
+  $equals   = array('' => t('<All>'), '=' => t('='), '<>' => t('not ='));
+  
+  $tables['location'] = array(
+    'name' => 'location', 
+    'join' => array(
+      'left' => array(
+        'table' => 'node',
+        'field' => 'nid'
+      ), 
+      'right' => array(
+        'field' => 'oid'
+      ), 
+    ),
+    'fields' => array(
+      'name' => array(
+        'name' => t('Location: Name'), 
+        'sortable' => true,
+      ),
+      'street' => array(
+        'name' => t('Location: Street'), 
+        'sortable' => true,
+      ),
+      'additional' => array(
+        'name' => t('Location: Additional'), 
+        'sortable' => true,
+      ),
+      'city' => array(
+        'name' => t('Location: City'),
+        'sortable' => true,
+      ),
+      'province' => array(
+        'name' => t('Location: Province'),
+        'sortable' => true,
+      ),
+      'postal_code' => array(
+        'name' => t('Location: Postal Code'),
+        'sortable' => true,
+      ),
+      'country' => array(
+        'name' => t('Location: Country'),
+        'sortable' => true,
+      ),
+      'latitude' => array(
+        'name' => t('Location: Latitude'),
+        'sortable' => true,
+      ),
+      'longitude' => array(
+        'name' => t('Location: Longitude'),
+        'sortable' => true,
+      ),
+    ),
+    'sorts' => array(
+      'name' => array('name' => t('Location: Name')),
+      'street' => array('name' => t('Location: Street')),
+      'additional' => array('name' => t('Location: Additional')),
+      'city' => array('name' => t('Location: City')),
+      'province' => array('name' => t('Location: Province')),
+      'country' => array('name' => t('Location: Country')),
+      'postal_code' => array('name' => t('Location: Postal Code')),
+    ),
+    'filters' => array(
+      'name' => array(
+        'field' => 'name',
+        'name' => t('Location: Name'),
+        'operator' => $contains,
+        'handler' => '_location_like_operator',
+      ),
+      'additional' => array(
+        'field' => 'additional',
+        'name' => t('Location: Additional'),
+        'operator' => $contains,
+        'handler' => '_location_like_operator',
+      ),
+      'street' => array(
+        'field' => 'street',
+        'name' => t('Location: Street'),
+        'operator' => $contains,
+        'handler' => '_location_like_operator',
+      ),
+      'city' => array(
+        'field' => 'city',
+        'name' => t('Location: City'),
+        'operator' => $contains,
+        'handler' => '_location_like_operator',
+      ),
+    )
+  );
+  
+  // use a select box for countries where there is more than a blank and NOT LISTED value
+  // use a text input box for all others
+  if (sizeof($provinces) > 2) {
+    $tables['location']['filters']['province'] = array(
+      'field' => 'province',
+      'name' => t('Location: Province'),
+      'operator' => $equals,
+      'handler' => '_location_eq_operator',
+      'list' => $provinces,
+      'list-type' => 'select',
+      );
+  } else {
+    $tables['location']['filters']['province'] = array(
+      'field' => 'province',
+      'name' => t('Location: Province'),
+      'operator' => $contains,
+      'handler' => '_location_like_operator',
+      'list' => $provinces,
+      'list-type' => 'select',
+      );
+  }
+
+  $tables['location']['filters']['postal_code'] = array(
+    'field' => 'postal_code',
+    'name' => t('Location: Postal Code'),
+    'operator' => $contains,
+    'handler' => '_location_like_operator',
+  );
+  $tables['location']['filters']['country'] = array(
+    'field' => 'country',
+    'name' => t('Location: Country'),
+    'operator' => $equals,
+    'handler' => '_location_eq_operator',
+    'list' => $countries,
+    'list-type' => 'select',
+  );
+  return $tables;
+}
+
+/*
+ * Custom filter for <>= operations
+ */
+function _location_eq_operator($op, $filter, $filterinfo, &$query) {
+  switch($filter[value]) {
+    case(''):
+      return;
+      break;
+  }
+  switch($filter['operator']) {
+    case (''):
+      return;
+      break;
+    case (check_plain('<>')):
+      $filter['operator'] = '<>';
+      break;
+   }
+  $query->ensure_table('location');
+  $query->add_where("$filterinfo[table].$filterinfo[field] $filter[operator] '$filter[value]'");
+}
+
+/**
+ * Custom filter for LIKE operations
+ */
+function _location_like_operator($op, $filter, $filterinfo, &$query) {
+  switch ($op) {
+    case 'handler':
+      $query->ensure_table('location');
+      switch ($filter['operator']) {
+        case 'contains':
+          $query->add_where("{$filter['field']} LIKE '%%{$filter['value']}%%'");
+          break;
+        case 'starts':
+          $query->add_where("{$filter['field']} LIKE '{$filter['value']}%%'");
+          break;
+        case 'ends':
+          $query->add_where("{$filter['field']} LIKE '%%{$filter['value']}'");
+          break;
+        case 'not':
+          $query->add_where("{$filter['field']} NOT LIKE '%%{$filter['value']}%%'");
+          break;
+        case 'all':
+          break;
+      }
+      break;
+  }
+}
+
+
+/**
+ *  Create default location views
+ */
+function location_views_default_views() {
+  
+  // find all location-enabled nodes
+  foreach (module_list(TRUE, FALSE) as $module) {
+    if (variable_get('location_'. $module, 0)) {
+      $location_node_types[] = $module;
+    }
+  }
+  
+  $view = new stdClass();
+  $view->name = 'location table';
+  $view->description = 'User-selectable table of locations.';
+  $view->access = array (
+    );
+  $view->view_args_php = '';
+  $view->page = TRUE;
+  $view->page_title = '';
+  $view->page_header = '';
+  $view->page_header_format = '1';
+  $view->page_footer = '';
+  $view->page_footer_format = '1';
+  $view->page_empty = '';
+  $view->page_empty_format = '1';
+  $view->page_type = 'table';
+  $view->url = 'location/views';
+  $view->use_pager = TRUE;
+  $view->nodes_per_page = '10';
+  $view->menu = TRUE;
+  $view->menu_title = 'location table';
+  $view->menu_tab = FALSE;
+  $view->menu_tab_default = FALSE;
+  $view->menu_weight = '';
+  $view->sort = array (
+  );
+  $view->argument = array (
+  );
+  $view->field = array (
+    array (
+      'tablename' => 'term_data',
+      'field' => 'name',
+      'label' => 'Category:',
+      'handler' => 'views_handler_field_tid',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'node',
+      'field' => 'title',
+      'label' => 'Title:',
+      'handler' => 'views_handler_field_nodelink',
+      'sortable' => '1',
+      'defaultsort' => 'ASC',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'name',
+      'label' => 'Name:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'street',
+      'label' => 'Street:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'additional',
+      'label' => 'Additional:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'city',
+      'label' => 'City:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'province',
+      'label' => 'Province:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'postal_code',
+      'label' => 'Postal Code:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'country',
+      'label' => 'Country:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'latitude',
+      'label' => 'Latitude:',
+      'sortable' => '1',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'longitude',
+      'label' => 'Longitude:',
+      'sortable' => '1',
+    ),
+  );
+  $view->filter = array (
+    array (
+      'tablename' => 'node',
+      'field' => 'status',
+      'operator' => '=',
+      'options' => '',
+      'value' => '1',
+    ),
+    array (
+      'tablename' => 'node',
+      'field' => 'type',
+      'operator' => 'OR',
+      'options' => '',
+      'value' => $location_node_types,
+    ),
+    array (
+      'tablename' => 'term_node',
+      'field' => 'tid',
+      'operator' => 'OR',
+      'options' => '',
+      'value' => array (),
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'name',
+      'operator' => '',
+      'options' => '',
+      'value' => '',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'additional',
+      'operator' => '',
+      'options' => '',
+      'value' => '',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'street',
+      'operator' => '',
+      'options' => '',
+      'value' => '',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'city',
+      'operator' => '',
+      'options' => '',
+      'value' => '',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'province',
+      'operator' => '',
+      'options' => '',
+      'value' => '',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'postal_code',
+      'operator' => '',
+      'options' => '',
+      'value' => '',
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'country',
+      'operator' => '=',
+      'options' => '',
+      'value' => 'us',
+    ),
+    
+  );
+
+  $view->exposed_filter = array (
+    array (
+      'tablename' => 'term_node',
+      'field' => 'tid',
+      'label' => t('Category:'),
+      'optional' => 1,
+      'is_default' => 0,
+      'single' => 1,
+      'position' => 0,
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'name',
+      'label' => t('Name:'),
+      'optional' => 1,
+      'is_default' => 0,
+      'single' => 1,
+      'position' => 0,
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'city',
+      'label' => t('City:'),
+      'optional' => 1,
+      'is_default' => 0,
+      'single' => 1,
+      'position' => 0,
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'province',
+      'label' => t('Province:'),
+      'optional' => 1,
+      'is_default' => 0,
+      'single' => 1,
+      'position' => 0,
+    ),
+    array (
+      'tablename' => 'location',
+      'field' => 'country',
+      'label' => t('Country:'),
+      'optional' => 1,
+      'is_default' => 0,
+      'single' => 1,
+      'position' => 0,
+    ),
+  );
+
+
+  $view->requires = array(term_data, node, location, term_node);
+  $views[$view->name] = $view;
+  return $views;
+}
+
+
