We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4188
    • 7 Posts
    There are actually 2 problems that I am experiencing.

    I have a page that is set up to respond with either JSON data or KML data.

    The snippet fails to run when I call it non-cached, but when I call it cached it works fine.

    I have cleared my cache to make sure there wasn’t something goofy going on there.

    Template code:

    [[parking_display]]

    -----------------------

    That works "just fine" but doesn’t "listen" to the $_GET variables.

    When I change to the non-cached version

    [!parking_display!] returns the following:

    Firefox can’t find the file at http://www.sjdowntownparking.com.php5-13.websitetestlink.com/data.html&kml=1&zoom=16&attach=1.

    but the JSON version will return it’s data:

    http://www.sjdowntownparking.com.php5-13.websitetestlink.com/data.html&json=1&zoom=16

    I am so very confused right now.

    Snippet code:
    <?php
    $ds = $modx->db->select('interesting_points_lat AS lat, interesting_points_lng AS lng,interesting_points_id AS number, interesting_points_name AS name','interesting_points','','interesting_points_id');
    
    	while ( $point = $modx->db->getRow($ds) ) {
    		$points_of_interest[] = $point;
    	}
    $zoom=$_GET['zoom'];
    $modx->setPlaceholder('parking_points_of_interest', $points_of_interest);
    //if (!isset($json) && !isset($kml)) { 
    if ($_GET['q'] == 'parking_map.html') { 
    ob_start();
    ?>
    <script language="JavaScript">
    	<!--
    	var data = <?php print_data($modx,$zoom); ?>;
    	var poiLocations = [ null // 1-indexed.
    <?php 
    	foreach ($points_of_interest as $p) { 
    		echo ", new GLatLng(".$p['lat'].",".$p['lng'].")";
    	}
    ?>
    	];
    	//-->
    </script>
    <?php 
    $output=ob_get_contents();
    ob_end_clean();
    $modx->setPlaceholder('parking_data',$output);
    } 
    elseif(isset($_GET['json'])) 
    { 
    	print_data($modx,$zoom);
    }
    else
    { 
    
    if ( isset($_GET['kml']) ) {
    
    	header('Content-Type: application/vnd.google-earth.kml+xml');
            if (isset($_GET['attach']) && $_GET['attach'])
              header('Content-Disposition: attachment; filename=sjdowntownparking-garages.kml');
            else
              header('Content-Disposition: inline; filename=garages' . $zoom . '.kml');
    	
    	$programs = array ();
    	if ( isset($_GET['programs']) ) {
    		$programs = explode(",", $_GET['programs']);
    	}
    	$zoom=$_GET['zoom'];
    	kml_garages($modx, $zoom, $programs);
    }
    }
    
    function kml_polygon_style ( $name, $rrggbb ) {
    	$bbggrr = substr($rrggbb, 4, 2)
    		. substr($rrggbb, 2, 2)
    		. substr($rrggbb, 0, 2);
    	
    	print '<Style id="' . $name . '">'
    			. '<PolyStyle>'
    				. '<color>ff' . $bbggrr . '</color>'
    				. '<fill>1</fill>'
    				. '<outline>1</outline>'
    			. '</PolyStyle>'
    			. '<LineStyle>'
    				. '<color>ff' . $bbggrr . '</color>'
    				. '<width>1</width>'
    			. '</LineStyle>'
    		. '</Style>' . "\n";
    }
    
    function kml_garage_style ( $modx, $garage_id ) {
    	$results = $modx->db->query(
    		"SELECT programs_name AS name
    			FROM parking_lots_to_programs, programs
    			WHERE parking_lots_to_programs_parking_lots_id = ".$garage_id."
    				AND parking_lots_to_programs_programs_id = programs_id");
    	while ($program=$modx->db->getRow($results)) { 
    		$programs[]=$program['name'];
    	}
    	if ( in_array("dash", $programs) ) {
    		print "<styleUrl>#dash</styleUrl>";
    	} elseif ( in_array("freeto10", $programs) ) {
    		print "<styleUrl>#freeto10</styleUrl>";
    	} elseif ( in_array("free", $programs) ) {
    		print "<styleUrl>#free</styleUrl>";
    	} elseif ( in_array("validated", $programs) ) {
    		print "<styleUrl>#validated</styleUrl>";
    	} else {
    		print "<styleUrl>#public</styleUrl>";
    	}
    }
    
    function kml_arrows ( $modx, $zoom, $garage_id ) {
    	$results = $modx->db->query(
    		"SELECT *
    			FROM parking_lots_arrows
    			WHERE parking_lots_arrows_parking_lots_id = ".$garage_id."
    				AND parking_lots_arrows_zoom =".$zoom);
    
    	while ( $my_arrow = $modx->db->getRow($results) ) {
    		$arrow=new stdClass();
    		foreach($my_arrow as $key=>$value) { 
    			$arrow->$key=$value;
    		}
    		print "<Placemark>"
    			. "<styleUrl>#"
    				. $arrow->parking_lots_arrows_street . "_arrow</styleUrl>"
    			. "<Polygon><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode><coordinates>"
    			. $arrow->parking_lots_arrows_lng_a . ','
    				. $arrow->parking_lots_arrows_lat_a . ",1\n"
    			. $arrow->parking_lots_arrows_lng_b . ','
    				. $arrow->parking_lots_arrows_lat_b . ",1\n"
    			. $arrow->parking_lots_arrows_lng_c . ','
    				. $arrow->parking_lots_arrows_lat_c . ",1\n"
    			. $arrow->parking_lots_arrows_lng_a . ','
    				. $arrow->parking_lots_arrows_lat_a . ",1"
    			. "</coordinates></LinearRing></outerBoundaryIs></Polygon>"
    			. "</Placemark>";
    	}
    	
    }
    
    
    function kml_garages ( $modx, $zoom, $programs ) {
    	print '<?xml version="1.0"?>' . "\n"
    		. '<kml xmlns="http://earth.google.com/kml/2.1" xmlns:atom="http://www.w3.org/2005/Atom">'
    		. '<Document><name>San Jose Dowtown Parking Garages</name>' . "\n"
                    . '<atom:author><atom:name>City of San Jose</atom:name></atom:author>' . "\n"
                    . '<atom:link href="http://www.sjdowntownparking.com/parking_map.php"/>' . "\n";
    	
    	kml_polygon_style("public", "1162DB");
    	kml_polygon_style("dash", "DF2F23");
    	kml_polygon_style("validated", "1C4996");
    	kml_polygon_style("free", "8204E8");
    	kml_polygon_style("freeto10", "FF7930");
    	kml_polygon_style("moto", "DF2F23");
    	kml_polygon_style("minor_arrow", "ffffff");
    	kml_polygon_style("major_arrow", "fff782");
    	
    	if ( $programs && count($programs) ) {
    		$program_list = "";
    		$comma = "'";
    		foreach ( $programs as $program ) {
    			$program_list .= $comma . $db->escapeSimple($program) . "'";
    			$comma = ",'";
    		}
    		
    		$program_condition = "
    				AND ( EXISTS
    						( SELECT 1
    							FROM parking_lots_to_programs, programs
    							WHERE parking_lots_to_programs_parking_lots_id
    									= parking_lots_id
    								AND parking_lots_to_programs_programs_id
    									= programs_id
    								AND programs_name IN ("
    									. $program_list . ") )";
    		if ( in_array("public", $programs) ) {
    			$program_condition .= "
    					OR NOT EXISTS
    						( SELECT 1
    							FROM parking_lots_to_programs
    							WHERE parking_lots_to_programs_parking_lots_id
    									= parking_lots_id )";
    		}
    		
    		$program_condition .= " )";
    	} else {
    		$program_condition = "";
    	}
    	
    	$results = $modx->db->query(
    		"SELECT parking_lots_points_parking_lots_id AS id, parking_lots_name,
    				parking_lots_points_lat AS lat, parking_lots_points_lng AS lng,
                                    parking_lots_address, parking_lots_hours_of_operation,
                                    parking_lots_rates
    			FROM parking_lots, parking_lots_points
    			WHERE parking_lots_points_zoom = ".$zoom."
    				AND parking_lots_points_parking_lots_id = parking_lots_id
    				" . $program_condition . "
    			ORDER BY id, parking_lots_points_sequence");
    
    	$first_point = new stdClass();
    	$first_point->id = null;
    	
    	while ( $my_point = $modx->db->getRow($results) ) {
    		$point= new stdClass();
    		foreach($my_point as $key=>$value) { 
    			$point->$key=$value;
    		}
    		if ( $first_point->id !== $point->id ) {
    			if ( $first_point->id !== null ) {
    				// End of the old polygon.
    				print $first_point->lng . "," . $first_point->lat . ",0";
    				print "</coordinates></LinearRing></outerBoundaryIs></Polygon>"
    					. "</Placemark>\n";
    			}
    			
                            print "<Folder><name>" . htmlspecialchars($point->parking_lots_name)
                                    . " Entrance Markers</name>";
                            
    			kml_arrows($modx, $zoom, $point->id);
    
                            print "</Folder>";
    			
    			// Start of the new polygon.
    			print "<Placemark>"
    				. "<name>" . htmlspecialchars($point->parking_lots_name)
    					. "</name>"
                                    . "<description>" . htmlspecialchars($point->parking_lots_address)
                                    . "\nHours of Operation: ".htmlspecialchars($point->parking_lots_hours_of_operation)
                                    . "\nRates: ".htmlspecialchars($point->parking_lots_rates)
                                    . "</description>";
    			kml_garage_style($modx, $point->id);
    			print "<Polygon><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode><coordinates>";
    			$first_point = $point;
    		}
    		
    		print $point->lng . "," . $point->lat . ",0\n";
    	}
    	
    	if ( $first_point->id !== null ) {
    		// End of the old polygon.
    		print $first_point->lng . "," . $first_point->lat . ",0";
    		print "</coordinates></LinearRing></outerBoundaryIs></Polygon>"
    			. "</Placemark>\n";
    	}
    	
    	print '</Document></kml>';
    	
    }
    
    
    
    
    
    
    function print_data ($modx, $zoom) {
    	if (!isset($zoom)) { 
    		$zoom=15;
    	} elseif ($zoom > 16) { 
    		$zoom=16;
    	}	
    	print_objects($modx, $zoom);
    }
    
    function print_objects ($modx, $zoom) {
    	print '{"zoom":' . $zoom
    		. ',"garages":';
    	print_garages($modx,$zoom);
    	print ',"moto_lots":';
    	print_moto_lots($modx, $zoom);
    	print ',"lines":';
    	print_lines($modx);
    	print '}';
    }
    
    function print_garages ($modx, $zoom ) {
    	$comma = "";
    	print '[';
    	
    //	$results = $db->query("SELECT parking_lots.* FROM parking_lots");
    //	if ( PEAR::isError($results) ) {
    //		throw new DBEx($results);
    //	}
    
    $ds = $modx->db->select('*','parking_lots');
    //		$ds = $modx->db->query("SELECT parking_lots.* FROM parking_lots");
    
    	while ( $garage = $modx->db->getRow($ds) ) {
    		print $comma
    			. '{"id":"' . addcslashes($garage['parking_lots_id'], "\\\n\r\t\"") . '",'
    			. '"name":"' . addcslashes($garage['parking_lots_name'], "\\\n\r\t\"") . '",'
    			. '"address":"' . addcslashes($garage['parking_lots_address'], "\\\n\r\t\"") . '",'
    			. '"phone":"' . addcslashes($garage['parking_lots_phone'], "\\\n\r\t\"") . '",'
    			. '"hours":"' . addcslashes($garage['parking_lots_hours_of_operation'], "\\\n\r\t\"") . '",'
    			. '"rates":"' . addcslashes($garage['parking_lots_rates'], "\\\n\r\t\"") . '",'
    			. '"discounts":"' . addcslashes($garage['parking_lots_discount_notes'], "\\\n\r\t\"") . '",'
    			. '"directions_link":"' . addcslashes($garage['parking_lots_directions_link'], "\\\n\r\t\"") . '",'
    			. '"points":';
    		print_garage_points($modx, $garage['parking_lots_id'], $zoom);
    //		print ',"arrows":';
    //		print_garage_arrows($garage['parking_lots_id'], $zoom);
    //		print_garage_programs($garage['parking_lots_id']);
    //		print ',"has_thumbnail":'
    //			. ( $garage['parking_lots_thumbnail'] ? 'true' : 'false' );
    //		print ',"has_image":'
    //			. ( $garage['parking_lots_image'] ? 'true' : 'false' );
    		print '}';
    		
    		$comma = ",";
    	}
    	
    	print ']';
    }
    
    function print_garage_points ($modx, $garage_id, $zoom ) {
    	$comma = "";
    	print "[ ";
    	
    //	$results = $db->query(
    //		"SELECT parking_lots_points_lat AS lat, parking_lots_points_lng AS lng
    //			FROM parking_lots_points
    //			WHERE parking_lots_points_parking_lots_id = ?
    //				AND parking_lots_points_zoom = ?
    //			ORDER BY parking_lots_points_sequence",
    //			array ( $garage_id, $zoom ));
    //	if ( PEAR::isError($results) ) {
    //		throw new DBEx($results);
    //	}
    //$ds = $modx->db->select('interesting_points_lat AS lat, interesting_points_lng AS lng,interesting_points_id AS number, interesting_points_name AS name','interesting_points','','interesting_points_id');
    
    $ds = $modx->db->select('parking_lots_points_lat AS lat, parking_lots_points_lng AS lng','parking_lots_points',' parking_lots_points_parking_lots_id = '.$garage_id.' and parking_lots_points_zoom = '.$zoom,'parking_lots_points_sequence');
    	while ( $point = $modx->db->getRow($ds) ) {
    		print $comma . '[' . $point['lat'] . "," . $point['lng'] . ']';
    		$comma = " , ";
    	}
    	
    	print " ]";
    }
    
    function print_moto_lots ( $modx, $zoom ) {
    	$comma = "";
    	print '[';
    	
    //	$results = $db->query(
    //		"SELECT *
    //			FROM motorcycle_lots, motorcycle_lots_points
    //			WHERE motorcycle_lots_points_zoom = ?
    //				AND motorcycle_lots_points_motorcycle_lots_id
    //					= motorcycle_lots_id",
    //			array ( $zoom ));
    //	if ( PEAR::isError($results) ) {
    //		throw new DBEx($results);
    //	}
    $ds = $modx->db->select('*','motorcycle_lots,motorcycle_lots_points','motorcycle_lots_points_zoom='.$zoom.' and motorcycle_lots_points_motorcycle_lots_id = motorcycle_lots_id');
    	
    	while ( $log = $modx->db->getRow($ds) ) {
    		print $comma
    			. '{"id":"' . addcslashes($log['motorcycle_lots_id'], "\\\n\r\t\"") . '",'
    			. '"address":"' . addcslashes($log['motorcycle_lots_address'], "\\\n\r\t\"") . '",'
    			. '"spaces":"' . addcslashes($log['motorcycle_lots_spaces'], "\\\n\r\t\"") . '",'
    			. '"hours":"'
    				. addcslashes($log['motorcycle_lots_hours_of_operation'], "\\\n\r\t\"") . '",';
    		
    		if ( $log['motorcycle_lots_points_lat_a'] === null ) {
    			print '"point_a":null,"point_b":null';
    		} else {
    			print '"point_a":[' . $log['motorcycle_lots_points_lat_a'] . ","
    					. $log['motorcycle_lots_points_lng_a'] . "],"
    				. '"point_b":[' . $log['motorcycle_lots_points_lat_b'] . ","
    					. $log['motorcycle_lots_points_lng_b'] . "]";
    		}
    		
    		print "}";
    		
    		$comma = ",";
    	}
    	
    	print ']';
    }
    
    function print_lines ( $modx ) {
    	$comma = "";
    	print '[';
    	
    //	$results = $db->query("SELECT * FROM transportation_lines");
    //	if ( PEAR::isError($results) ) {
    //		throw new DBEx($results);
    //	}
    
    $ds = $modx->db->select('*','transportation_lines');
    
    	
    	while ( $line = $modx->db->getRow($ds) ) {
    		print $comma
    			. '{"id":"' . addcslashes($line['transportation_lines_id'], "\\\n\r\t\"") . '",'
    			. '"name":"' . addcslashes($line['transportation_lines_name'], "\\\n\r\t\"") . '",'
    			. '"points":';
    		print_line_points($modx, $line['transportation_lines_id']);
    		print ',"stops":';
    		print_line_stops($modx, $line['transportation_lines_id']);		
    		print '}';
    		
    		$comma = ",";
    	}
    	
    	print ']';
    }
    
    function print_line_points ( $modx, $line_id ) {
    	$comma = "";
    	print "[ ";
    	
    //	$results = $db->query(
    //		"SELECT transportation_lines_points_lat AS lat,
    //				transportation_lines_points_lng AS lng
    //			FROM transportation_lines_points
    //			WHERE transportation_lines_points_transportation_lines_id = "
    //				. (integer)$line_id . "
    //			ORDER BY transportation_lines_points_sequence");
    //	if ( PEAR::isError($results) ) {
    //		throw new DBEx($results);
    //	}
    
    $ds = $modx->db->select('transportation_lines_points_lat AS lat, transportation_lines_points_lng AS lng','transportation_lines_points',' transportation_lines_points_transportation_lines_id = '.$line_id,'transportation_lines_points_sequence');
    	
    	while ( $point = $modx->db->getRow($ds) ) {
    		print $comma . '[' . $point['lat'] . ',' . $point['lng'] . ']';
    		$comma = ',';
    	}
    	
    	print " ]";
    }
    
    function print_line_stops ( $modx, $line_id ) {
    	$comma = "";
    	print "[ ";
    	
    //	$results = $db->query(
    //		"SELECT transportation_lines_stops_name AS name,
    //				transportation_lines_stops_lat AS lat,
    //				transportation_lines_stops_lng AS lng
    //			FROM transportation_lines_stops
    //			WHERE transportation_lines_stops_transportation_lines_id = "
    //				. (integer)$line_id);
    //	if ( PEAR::isError($results) ) {
    //		throw new DBEx($results);
    //	}
    $ds = $modx->db->select('transportation_lines_stops_name as name, transportation_lines_stops_lat AS lat, transportation_lines_stops_lng AS lng','transportation_lines_stops',' transportation_lines_stops_transportation_lines_id = '.$line_id);
    	
    	while ( $point = $modx->db->getRow($ds) ) {
    		if ( $point->name ) {
    			$name = "'" . addcslashes($point['name'], "\\\n\r\t\"'") . "'";
    		} else {
    			$name = 'null';
    		}
    		
    		print $comma
    			. '[' . $point['lat'] . ',' . $point['lng'] . ',' . $name . ']';
    		$comma = ',';
    	}
    	
    	print " ]";
    }
    ?>
    


    help smiley

    UPDATE: Ok, this is a duh factor! The header call that is in the code appears to have been causing most of the problem. Still looking into it though smiley

    Bryan
      • 4188
      • 7 Posts
      Ok I feel stupid.... Yup the PHP header call will cause things to break!

      I removed it and everything is working correctly now.
        • 3749
        • 24,544 Posts
        The header could be causing trouble, but I think there are some other issues.

        I don’t really understand your code, but it looks like you’re placing php code in the document via your placeholder, which MODx will try to strip out since all PHP code in MODx needs to be inside a snippet.

        Mixing echo, print, and placeholders also confuses thhings. It would be better if you could put all the output (with the exception of what goes in placeholders and maybe the JS code) into a variable (traditionally called $output) and return it at the end of the snippet.

        So, instead of

        print "<styleUrl>#dash</styleUrl>";

        use
        $output .= "<styleUrl>#dash</styleUrl>";


        Hope this helps. smiley
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 4188
          • 7 Posts
          All of the PHP code is in the snippet. The header call was what was causing the problems.

          I read about trying to make sure to put the output in a variable to be displayed in the main template; I am under a time constraint with this project, so am being somewhat lazy. The other option I should consider using is doing an ob_start(), ob_get_contents() and ob_end_clean(). That will also meet the good coding practices.

          So after getting the code to work last night, I have continued plugging away and found the following:

          Since I want the KML file downloaded as an attachment, and then accessed by Google Earth, I had to create a separate page that calls the KML functions. I set it’s content type to Application/google earth+ kml (or some such thing) and content disposition to be attachment. *YAY* It all worked. I got it to set the extension on the file download (from HTML to KML) by naming the alias data2.kml and VIOLA it is now working exactly the way that I want it to! Now, when I want to AJAX the data, I go to data and when I want to KML the data I go to data2 (ok, the names need to be worked on, but hey I am testing right now)

          Thanks again for your help!

          Bryan
          A poor neophite modxer -- who loves this system!
          http://www.cbtcwebworks.com
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            It might be better if you have the entire page uncached and call the snippet cached.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org