You are here

function _kml_format_feed in KML 5

Same name and namespace in other branches
  1. 6 kml.module \_kml_format_feed()

A generic function for generating KML (Keyhole Markup Language for Google Earth) feeds from a set of nodes.

Parameters

$nodes: An object as returned by db_query() which contains the nid field.

$channel: An associative array containing title, link, description and other keys. The link should be an absolute URL.

1 call to _kml_format_feed()
_kml_feed in ./kml.module
Displays a KML feed containing all location-enabled nodes.

File

./kml.module, line 687
KML Module

Code

function _kml_format_feed($nodes_array = array(), $channel = array()) {
  $items = '';

  // get altitude to display placemarks at, and whether to extrude
  $altitude = variable_get('kml_altitude', 0);
  $kml_extra['altitudeMode'] = 'relativeToGround';

  // TODO: make configurable?
  if ($kml_extrude = variable_get('kml_extrude', 0)) {
    $kml_extra['extrude'] = variable_get('kml_extrude', 0);
  }
  if ($nodes_array) {

    /*
     * If we're going to load nodes, might as well remember then for
     * later use.
     */
    foreach ($nodes_array as &$node) {

      // Load the specified node:
      if ($node = node_load($node->nid)) {

        // TODO: using db_rewrite_sql above may make node_load redundant
        // TODO: check to make sure the node has geo properties
        $link = url('node/' . $node->nid, NULL, NULL, 1);

        // Filter and prepare node teaser
        if (node_hook($node, 'view')) {
          node_invoke($node, 'view', TRUE, FALSE);
        }
        else {
          $node = node_prepare($node, TRUE);
        }

        // Allow modules to change $node->teaser before viewing.
        node_invoke_nodeapi($node, 'view', true, false);

        // Allow modules to add additional item fields
        $extra = node_invoke_nodeapi($node, 'kml item');
        $extra = array_merge($extra, $kml_extra);

        // TODO: if node has more than one location, add a folder containing the locations as placemarks
        $items .= kml_format_placemark($node, $link, $altitude, $extra);
      }
    }
  }
  $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" . "<kml xmlns=\"http://earth.google.com/kml/2.1\">\n" . " <Document>\n";
  $content_types = node_get_types('names');
  foreach ($content_types as $name => $title) {
    $output .= ' <Style id="highlightPlacemark_' . $name . '">' . "\n";
    $output .= '  <IconStyle>' . "\n";
    $output .= '   <Icon>' . "\n";
    $output .= '    <href>' . variable_get('kml_highlightplacemark_url_' . $name, 'http://maps.google.com/mapfiles/kml/paddle/red-stars.png') . '</href>' . "\n";
    $output .= '   </Icon>' . "\n";
    $output .= '  </IconStyle>' . "\n";
    $output .= ' </Style>' . "\n";
    $output .= ' <Style id="normalPlacemark_' . $name . '">' . "\n";
    $output .= '  <IconStyle>' . "\n";
    $output .= '   <Icon>' . "\n";
    $output .= '    <href>' . variable_get('kml_normalplacemark_url_' . $name, 'http://maps.google.com/mapfiles/kml/paddle/wht-blank.png') . '</href>' . "\n";
    $output .= '   </Icon>' . "\n";
    $output .= '  </IconStyle>' . "\n";
    $output .= ' </Style>' . "\n";
    $output .= ' <StyleMap id="myStyleMap_' . $name . '">' . "\n";
    $output .= '  <Pair>' . "\n";
    $output .= '   <key>normal</key>' . "\n";
    $output .= '   <styleUrl>#normalPlacemark_' . $name . '</styleUrl>' . "\n";
    $output .= '  </Pair>' . "\n";
    $output .= '  <Pair>' . "\n";
    $output .= '   <key>highlight</key>' . "\n";
    $output .= '   <styleUrl>#highlightPlacemark_' . $name . '</styleUrl>' . "\n";
    $output .= '  </Pair>' . "\n";
    $output .= ' </StyleMap>' . "\n";
  }

  // See if any modules want to add anything to the file.
  // Pass in the list of nodes we'll present for their reference.
  $feed_extras = module_invoke_all('kml_feed_extras', $nodes_array);
  foreach ($feed_extras as $feed_extra) {
    $output .= $feed_extra;
  }

  // Add site logo if one is specified
  if ($sitelogo = variable_get('kml_sitelogo_url', '')) {
    $output .= "  <ScreenOverlay>\n" . "   <name><![CDATA[" . t('%site logo', array(
      '%site' => $channel['title'],
    )) . "]]></name>\n" . "   <Icon>\n" . "    <href>" . $sitelogo . "</href>\n" . "   </Icon>\n" . "   <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>\n" . "   <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>\n" . "   <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>\n" . "  </ScreenOverlay>\n";
  }
  $output .= kml_format_folder($channel['title'], $channel['description'], $items, $extras) . " </Document>\n" . "</kml>\n";
  return $output;
}