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

Ines mleonsoft at yahoo.com.ar
Mon Jan 22 16:32:25 EST 2007


Julien, I tried puting the code just where you tell me. But I had errors
with the variables of my new code. The application don't recognize all the
new variables, for example: $xml_file (the first new variable) and the
others.

So,

1) I declared all the new variables in: class UUW extends NavTool:

var $xml_file;
var $xml_data;
var $namespace;
var $xml;
............etc.

2) I access at the new variables in this way:

Example: variable:      $xml_file
I access in this way:   $this->xml_file

Now, the application don't show me errors with the new variables. Is 1) and
2) right?

But the application show me one error in the required file
(require_once('XML.inc.php'), class to use the new code:

It could be a problem between the objects?


I send you:

A) The error
B) The new code into the widget with all the "$this->variablexx" access
C) The file 'XML.inc.php' with the line 66 marked

A)

This is the error, all the errors at the line 66. It marks all the xml tags
(header, version, etc)

Notice: Undefined property: header in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: header in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: version in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: version in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: sendTime in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: sendTime in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: source in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: source in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: destination in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: destination in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: type in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: type in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: content in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
Notice: Undefined property: content in
D:\ms4w\apps\wmsviewer\geointegrator\htdocs\XML.inc.php on line 66
................etc
----------------------------------------------------------------------------
------------------

B)

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

	if ( $this->mszCoords == 'aa')
	{

		$this->xml_file="archivo4.xml";
		$this->xml_data=implode("",file($this->xml_file));

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


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

		$this->xml=new XML();

		$this->xml->parse($this->xml_data);

		$this->tot = count($this->xml->response);


		if($this->tot > 0)
		{
			$this->tot_a = count($this->xml->response->content);

			if($this->tot_a > 0)
			{
				$this->tot_b = count($this->xml->response->content->record);

				if($this->tot_b > 0)
				{

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

						if($this->sc_name == "Inga oerstediana")
						{
							$this->sc_name2=
$this->xml->response->content->record[$this->j]->ScientificName->_value;
							$this->latitud=
$this->xml->response->content->record[$this->j]->Latitude->_value;
							$this->longitud=
$this->xml->response->content->record[$this->j]->Longitude->_value;

							exit;
						}
					}
				}
				else
				{
					return false;

				}

			}
			else
			{
				return false;

			}
		}
		else
		{
			return false;
		}


		$this->x = $this->longitud;
		$this->y = $this->latitud;

		//$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);


	}


}

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

C)


<?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")){

///////////////-----------LINE
66: --------------///////////////////////////////////////

    		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 again,
Ines

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

Hi,

Sorry for the late response. Put your PHP where your $this->x=... is.
This will give you a widget file like the following. Note that I will
skip some part of the code to make the message shorter.

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

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

/////////////////////////////
// Put your code here
/////////////////////////////
$xml_file="./archivo4.xml";
$xml_data=implode("",file($xml_file));

//[...snip...]

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

				if($sc_name == "Inga oerstediana")
				{

/////////////////////////////////////////////////////
/// Here//////////////
////////////////////////////////////////////////
					$sc_name2= $xml->response->content->record[$j]->ScientificName->_value;
					$this->y= $xml->response->content->record[$j]->Latitude->_value;
					$this->x= $xml->response->content->record[$j]->Longitude->_value;
				}
			}
		}
		else
		{
//////////////////// Note that! /////////
return false;
		}


	}
	else
	{
//////////////////// Note that! /////////
return false;
	}
}
else
{
//////////////////// Note that! /////////
return false;
}

		//$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');
//[...snip...]



Ines wrote:
> 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.
>
>
--
Julien-Samuel Lacroix
Mapgears
http://www.mapgears.com/



More information about the Chameleon mailing list