[Chameleon] How to include (join) php code in custom widget

Ines mleonsoft at yahoo.com.ar
Tue Jan 16 19:19:34 EST 2007


Hi, I'm customizing my widget and have to include a php code in it to take
the value of differents variables. I need to know in which place of the
widget have I to write the code.

This is my objective:

1) I have my custom widget yet. The objective of the widget is take a
longitude and latitude (x, y) an draw a point in the map in that coords. I
have this code and it works very well and I can see the point in the map. I
solved it in the ParseURL() function but using x,y coords (variables) with a
fixed  value to prove. Ex: $this->x = -85;  $this->y = 13;

2) Now, I have to take that values from a XML file to make the values of x
and y variables (change the values of x and y) for draw the point layer in
another coords. I have the code in php to read my XML file and it works very
good, but not into the widget.

3) My question is: In wich place or where do I have to put my php code (to
read my xml file) in the widget? to use the value of the variables that read
the xml file of my php code. I need to join the widget with my php code. I
see the widget have many functions. I tried  to copy-paste my php code in
the ParseUrl() function but it show me errors.

I send you :
a) The code of my ParseUrl() function and,
b) My php code to read the xml file. This php code works with a class
(include another file: "XML.inc.php") I included this other file in the
folder of the wiget (UUW) and I put the line: require_once('XML.inc.php');
in the top of the widget
c) The line where I Included the file required in the widget
d) The code of XML.inc.php (required class) in my php code to read the xml
file.

a)---------------------------------- The code of my
ParseUrl() --------------------------

function  ParseURL()
    {
        // execute parent function
        parent::ParseURL();

		// work some magic
if ( $this->isVarSet( "NAV_CMD" ) &&
                            $this->getVar( "NAV_CMD" ) == 'UUW' )
{


	$this->x = -85;  //----------------------------------> I need to change
this value
	$this->y = 13;   //----------------------------------> I need to change
this value


	if ( $this->mszCoords == 'aa')
	{
		//$this->lb = $this->getVar('especie');

		if ($this->isVarSet('espe') == 0)
		{
			$this->lb = "no";
		}
		else
		{
			$this->lb = $this->getVar('espe');
		}

		$poLayer = $this->moMapObject->oMap->getLayerByName('pointlatlon');
		$poLayer->set("status", MS_ON);
		$pt = ms_newPointObj();
   		$ln = ms_newLineObj();
		$shp = ms_newShapeObj(MS_SHAPE_POINT);
		$shp->set("text", $this->lb);
   		$pt->setXY($this->x,$this->y);
    		$ln->add($pt);
    		$shp->add($ln);
    		$poLayer->addFeature($shp);

	}

}

b) --------------------------------My php code to read the xml
file:----------------------


<?php
require_once("XML.inc.php");

$xml_file="./archivo4.xml";
$xml_data=implode("",file($xml_file));

$namespace = substr($xml_data, strpos($xml_data, "<content xmlns:")+15,
strpos($xml_data, "=", strpos($xml_data, "<content xmlns:")) -
strpos($xml_data, "<content xmlns:") -15);


$xml_data=str_replace($namespace.":", "", $xml_data);

$xml=new XML();

$xml->parse($xml_data);

$tot = count($xml->response);
echo "number tags 'response': $tot <br>";



if($tot > 0)
{
	$tot_a = count($xml->response->content);
	echo "tags 'content': $tot_a <br>";

	if($tot_a > 0)
	{
		$tot_b = count($xml->response->content->record);
		echo "tags 'record':".$tot_b."<br>";

		if($tot_b > 0)
		{
			echo "<b>Searched name</b>: Inga oerstediana <br>";


			for($j = 0; $j < $tot_b; $j++)
			{
				$sc_name = $xml->response->content->record[$j]->ScientificName->_value;

				if($sc_name == "Inga oerstediana")
				{
					$sc_name2= $xml->response->content->record[$j]->ScientificName->_value;
					$latitud= $xml->response->content->record[$j]->Latitude->_value;
					$longitud= $xml->response->content->record[$j]->Longitude->_value;
					echo "<br><br>";
					echo "<b>Nombre: </b>".$sc_name2."<br>";
					echo "<b>Latitud: </b>".$latitud."<br>";
					echo "<b>Longitud: </b>".$longitud."<br>";
				}
			}
		}
		else
		{
			echo "No tags 'record'";
		}


	}
	else
	{
			echo "No tag 'content'";
	}
}
else
{
	echo "No tag response";
}

?>

c) ------- The line where I Included the file required in the
widget----------------

<?php

// inherit from the NavTool
include_once(dirname(__FILE__)."/../NavTool.php");
//include( 'utils.inc.php' );
require_once('XML.inc.php');
//---------------------------------------------> HERE


d)---------The code of XML.inc.php (required class) in my php code to read
the xml file-------



<?php
/*
* XML.inc.php
*
* Class to convert an XML file into an object
*
* Copyright (C) 2006  Oliver Strecke <oliver.strecke at browsertec.de>
*
*   This library is free software; you can redistribute it and/or
*   modify it under the terms of the GNU Lesser General Public
*   License as published by the Free Software Foundation; either
*   version 2 of the License, or (at your option) any later version.
*
*   This library is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*   Lesser General Public License for more details.
*
*   You should have received a copy of the GNU Lesser General Public
*   License along with this library; if not, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

class XML{
	var $_parser;
	var $_xml_data;
	var $_actual_tag;

	//Constructor...
	function xml(){
        $this->_parser=xml_parser_create("ISO-8859-1");
        $this->_xml_data="";
        $this->_actual_tag=$this;

        xml_set_object($this->_parser,$this);
        xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,false);
        xml_set_element_handler($this->_parser,"tag_open","tag_close");
        xml_set_character_data_handler($this->_parser,"tag_data");
        xml_set_default_handler($this->_parser,"tag_data");
	}

	//get XML data from file...
	function file_read($xml_file){
		if(file_exists($xml_file)){
			$this->_xml_data=implode("",file($xml_file));
		}
		return 1;
	}

	//parse XML data...
	function parse($xml_data=0){
		if($xml_data)$this->_xml_data=$xml_data;
		xml_parse($this->_parser,$this->_xml_data);
	    xml_parser_free($this->_parser);
	    return 1;
	}

    function tag_open($parser,$name,$attrs){
    	//create new tag...
    	$tag=new XML_TAG(&$this->_actual_tag);
    	$tag->_name=$name;
    	$tag->_param=$attrs;

    	//add tag object to parent/actual tag object...
    	if(is_a($this->_actual_tag,"XML_TAG")){
    		if(is_object($this->_actual_tag->$name) ||
is_array($this->_actual_tag->$name)){
    			//same child objects -> Array...
    			$last_index=$this->_actual_tag->new_child_array($tag,$name);
    			$this->_actual_tag=&$this->_actual_tag->{$name}[$last_index];
    		}else{
    			//add new child object to actual tag...
    			$this->_actual_tag->new_child($tag,$name);
    		    $this->_actual_tag=&$this->_actual_tag->$name;
    		}
    	}else{
    		//copy first tag object in this object...
    		$this->$name=$tag;
    		$this->_actual_tag=&$this->{$name};
    	}
    	return 1;
    }

    function tag_data($parser,$string){
    	if(strlen(trim($string))>0)$this->_actual_tag->_value=$string;
        return 1;
    }

    function tag_close($parser,$name){
        $this->_actual_tag=&$this->_actual_tag->_parent;
        return 1;
    }

	//Debug...
	function debug($exit=0){
		echo "<pre>";
		print_r($this);
		echo "</pre>";
		if($exit)exit;
	}
}

class XML_TAG{
	var $_parent;
	var $_name;
	var $_value;
	var $_param;

	//Constructor...
	function xml_tag($parent){
        $this->_parent=&$parent;
		$this->_name="";
		$this->_value=false;
		$this->_param=Array();
		return 1;
	}

	//simply add ne child to this object...
	function new_child($child,$child_name){
  		$this->$child_name=&$child;
	}

	//add child array for more same childs to this object...
	function new_child_array($child,$child_name){
		//create array and set old child object to the first array element...
		if(is_object($this->$child_name)){
			$tmp_obj=$this->$child_name;
			$this->$child_name=Array();
			$this->new_child_array($tmp_obj,$child_name);
		}
		//push child reference into child array...
		$this->{$child_name}[]=&$child;
		$last_index=count($this->$child_name)-1;
		return $last_index;
	}

	//Debug...
	function debug(){
	  echo "<pre>";
	  print_r($this);
	  echo "</pre>";
	}
}
?>


Thank you very much for your help

Ines



More information about the Chameleon mailing list