/[drupal]/contributions/modules/topichubs/plugins/topichubs_calais_geo/topichubs_calais_geo.topichubs.inc
ViewVC logotype

Diff of /contributions/modules/topichubs/plugins/topichubs_calais_geo/topichubs_calais_geo.topichubs.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Tue Mar 3 23:51:06 2009 UTC revision 1.1.2.1, Wed Sep 2 12:00:21 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /**  /**
3   * @file topichubs_calais_geo.topichubs.inc   * @file topichubs_calais_geo.topichubs.inc
4   */   */
5    
6  /**  /**
7   * Requires Calais Geo Module   * Requires Calais Geo Module
8   */   */
9  class topichubs_plugin_calais_geo extends topichub_sql_plugin {  class topichubs_plugin_calais_geo extends topichub_sql_plugin {
10    
11    function options_form(&$form, &$form_state) {    function options_form(&$form, &$form_state) {
12    
13      $geo_vocabs = variable_get('calais_geo_vocabularies', array());      $geo_vocabs = variable_get('calais_geo_vocabularies', array());
14      $all_vocabs = calais_get_entity_vocabularies();      $all_vocabs = calais_get_entity_vocabularies();
15      foreach($geo_vocabs as $vid) {      foreach($geo_vocabs as $vid) {
16        $vocab_options[$vid] = array_search($vid, $all_vocabs);        $vocab_options[$vid] = array_search($vid, $all_vocabs);
17      }      }
18    
19      $this->add_types_field($form, $form_state);      $this->add_types_field($form, $form_state);
20      $form['vocab'] = array(      $form['vocab'] = array(
21        '#type' => 'select',        '#type' => 'select',
22        '#title' => t('Vocabulary to Map'),        '#title' => t('Vocabulary to Map'),
23        '#description' => t('Choose a vocabulary whose terms should be used to plot/represent each node        '#description' => t('Choose a vocabulary whose terms should be used to plot/represent each node
24  shown in the Locations Mentioned map.'),  shown in the Locations Mentioned map.'),
25        '#options' => $vocab_options,        '#options' => $vocab_options,
26        '#default_value' => $this->settings['vocab'] ? $this->settings['vocab'] : array(),        '#default_value' => $this->settings['vocab'] ? $this->settings['vocab'] : array(),
27      );      );
28      $form['width'] = array(      $form['width'] = array(
29        '#type' => 'textfield',        '#type' => 'textfield',
30        '#title' => t('Map width'),        '#title' => t('Map width'),
31        '#default_value' => $this->settings['width'],        '#default_value' => $this->settings['width'],
32        '#size' => 10,        '#size' => 10,
33        '#maxlength' => 10,        '#maxlength' => 10,
34        '#description' => t('The default width of a Google map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>. Leave blank to use the defaults.'),        '#description' => t('The default width of a Google map, as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>. Leave blank to use the defaults.'),
35      );      );
36      $form['height'] = array(      $form['height'] = array(
37        '#type' => 'textfield',        '#type' => 'textfield',
38        '#title' => t('Map height'),        '#title' => t('Map height'),
39        '#default_value' => $this->settings['height'],        '#default_value' => $this->settings['height'],
40        '#size' => 10,        '#size' => 10,
41        '#maxlength' => 10,        '#maxlength' => 10,
42        '#description' => t('The default height of the map, expressed as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>. Leave blank to use the defaults.'),        '#description' => t('The default height of the map, expressed as a CSS length or percentage. Examples: <em>50px</em>, <em>5em</em>, <em>2.5in</em>, <em>95%</em>. Leave blank to use the defaults.'),
43      );      );
44    }    }
45    
46    function execute() {    function execute() {
47      $items = $this->get_nodes_by_term();      $items = $this->get_nodes_by_term();
48    
49      $markers = array();      $markers = array();
50      if($items && count($items) > 0) {      if($items && count($items) > 0) {
51        foreach ($items as $tid => $item) {        foreach ($items as $tid => $item) {
52          $term = calais_get_term(NULL, $tid);          $term = calais_get_term(NULL, $tid);
53    
54          $nodes = array();          $nodes = array();
55          foreach ($item['#nodes'] as $node) {          foreach ($item['#nodes'] as $node) {
56            $nodes[] = l($node->title, "node/$node->nid");            $nodes[] = l($node->title, "node/$node->nid");
57          }          }
58    
59          $marker = array(          $marker = array(
60            'text' => theme('topichubs_calais_geo_marker', $term, $nodes),            'text' => theme('topichubs_calais_geo_marker', $term, $nodes),
61            'latitude' => floatval($term->extra['latitude']),            'latitude' => floatval($term->extra['latitude']),
62            'longitude' => floatval($term->extra['longitude']),            'longitude' => floatval($term->extra['longitude']),
63          );          );
64          $markers[] = $marker;          $markers[] = $marker;
65        }        }
66      }      }
67    
68      $content = $this->build_map($markers);      $content = $this->build_map($markers);
69      $hub_data = array(      $hub_data = array(
70        '#values' => $result,        '#values' => $result,
71        '#view' => $content,        '#view' => $content,
72      );      );
73      return $hub_data;      return $hub_data;
74    }    }
75    
76    /**    /**
77     * Run the query to find relevant nodes, grouped by the terms of the selected vocabulary.     * Run the query to find relevant nodes, grouped by the terms of the selected vocabulary.
78     */     */
79    function get_nodes_by_term() {    function get_nodes_by_term() {
80      $vid = $this->get_setting('vocab', 0);      $vid = $this->get_setting('vocab', 0);
81      $args = array($vid);      $args = array($vid);
82    
83      $node_alias = 'n';      $node_alias = 'n';
84      $term_joins = $this->get_term_where($node_alias);      $term_joins = $this->get_term_where($node_alias);
85      $args = array_merge($args, $term_joins['args']);      $args = array_merge($args, $term_joins['args']);
86    
87      $type_where = $this->get_content_type_where($this->get_types_setting(), $node_alias);      $type_where = $this->get_content_type_where($this->get_types_setting(), $node_alias);
88      $args = array_merge($args, $type_where['args']);      $args = array_merge($args, $type_where['args']);
89    
90      $sql .= "SELECT DISTINCT(n.nid), n.title, ct.tid";      $sql .= "SELECT DISTINCT(n.nid), n.title, ct.tid";
91      $sql .= " FROM {node} {$node_alias}";      $sql .= " FROM {node} {$node_alias}";
92      $sql .= implode(' ', $term_joins['joins']);      $sql .= implode(' ', $term_joins['joins']);
93      $sql .= " JOIN {term_node} tn2 ON {$node_alias}.nid = tn2.nid ";      $sql .= " JOIN {term_node} tn2 ON {$node_alias}.nid = tn2.nid ";
94      $sql .= " JOIN {calais_term} ct ON ct.tdid = tn2.tid AND ct.vid = %d";      $sql .= " JOIN {calais_term} ct ON ct.tdid = tn2.tid AND ct.vid = %d";
95      $sql .= " WHERE {$node_alias}.status = 1 AND ct.resolved_type = 'geo'";      $sql .= " WHERE {$node_alias}.status = 1 AND ct.resolved_type = 'geo'";
96      $sql .= " AND " . $term_joins['where'];      $sql .= " AND " . $term_joins['where'];
97      $sql .= " AND " . $type_where['where'];      $sql .= " AND " . $type_where['where'];
98      //$sql .= " AND n.created >= %d AND n.created <= %d";      //$sql .= " AND n.created >= %d AND n.created <= %d";
99      $sql .= " ORDER BY {$node_alias}.created DESC";      $sql .= " ORDER BY {$node_alias}.created DESC";
100      $results = db_query($sql, $args);      $results = db_query($sql, $args);
101    
102      while($node = db_fetch_object($results)) {      while($node = db_fetch_object($results)) {
103        $items[$node->tid]['#nodes'][] = $node;        $items[$node->tid]['#nodes'][] = $node;
104      }      }
105      return $items;      return $items;
106    }    }
107    
108    /**    /**
109     * Build the gmap array.     * Build the gmap array.
110     */     */
111    function build_map($markers) {    function build_map($markers) {
112      $settings = array(      $settings = array(
113        'markers' => $markers,        'markers' => $markers,
114        'behavior' => array('autozoom' => TRUE),        'behavior' => array('autozoom' => TRUE),
115      );      );
116    
117      list($lat, $lon) = calais_geo_calc_map_center($markers);      list($lat, $lon) = calais_geo_calc_map_center($markers);
118      $settings['latitude'] = $lat;      $settings['latitude'] = $lat;
119      $settings['longitude'] = $lon;      $settings['longitude'] = $lon;
120      if(!empty($this->settings['width'])) {      if(!empty($this->settings['width'])) {
121        $settings['width'] = $this->settings['width'];        $settings['width'] = $this->settings['width'];
122      }      }
123      if(!empty($this->settings['height'])) {      if(!empty($this->settings['height'])) {
124        $settings['height'] = $this->settings['height'];        $settings['height'] = $this->settings['height'];
125      }      }
126    
127      $map_data = array(      $map_data = array(
128        '#settings' => $settings,        '#settings' => $settings,
129      );      );
130      return theme('gmap', $map_data);      return theme('gmap', $map_data);
131    }    }
132  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2