CountryFetcher.php

Go to the documentation of this file.
00001 <?PHP
00002 function getCountryArray()
00003 {
00004         $countryXMLData = '';
00005         //$handle = fopen("http://www.unece.org/etrades/unedocs/repository/codelists/xml/CountryCodeList.xml", "r");
00006         global $module_folder;
00007         $handle = fopen("$module_folder/convention/code/CountryCodeList.xml", "r");
00008         while(!feof($handle))
00009                 $countryXMLData = $countryXMLData . fgets($handle, 4096);
00010         fclose ($handle);
00011         
00012         $countryXMLDataArray = XML_unserialize($countryXMLData);
00013         $dataArray = returnCountryDataArray($countryXMLDataArray, "Normal", "ASC", false);
00014         return $dataArray;
00015 }
00016 
00017 // parse through the data which we've got as an array
00018 function returnCountryDataArray($countryXMLDataArray, $textTransform, $sortOrder,$debug = false)
00019   {
00020   $x = 0;
00021   $countryOutputArray = array();
00022   
00023   if ($debug == true){print_r($countryXMLDataArray);}
00024   
00025   while ($x <= count($countryXMLDataArray['CountryCodeList']['Country']))
00026     {
00027     $countryCode = $countryXMLDataArray['CountryCodeList']['Country'][$x]['CountryCoded'];
00028     $countryName = $countryXMLDataArray['CountryCodeList']['Country'][$x]['CountryName'];
00029     
00030     // transform the text as needed...
00031 
00032     if ($textTransform == 'UPPERCASE')
00033       {
00034       $countryCode = strtoupper($countryCode);
00035       $countryName = strtoupper($countryName);
00036       }
00037     else if ($textTransform == 'lowercase')
00038       {
00039       $countryCode = strtolower($countryCode);
00040       $countryName = strtolower($countryName);
00041       }
00042     else
00043       {
00044       $countryCode = strtoupper($countryCode);
00045       $countryName = ucwords(strtolower($countryName));
00046       }
00047       
00048     if ($debug == true){echo "countryCode: $countryCode | countryName: $countryName<br/>";}
00049     if ($countryCode != '')
00050       {
00051       $countryOutputArray[$countryCode] = $countryName;
00052       }
00053     
00054     $x++;
00055     } // end while
00056   
00057   if ($debug == true)
00058     {
00059     echo "<pre>";
00060     print_r ($countryOutputArray);
00061     echo "</pre>";
00062     }
00063   
00064   if ($sortOrder == 'ASC')
00065     {
00066     asort($countryOutputArray);
00067     }
00068   else
00069     {
00070     arsort($countryOutputArray);
00071     }
00072   
00073   
00074     
00075   return $countryOutputArray;
00076   } // end function
00077 
00078 // XML transformation lives below...
00079 // sure... we could've done this with php 5, but to keep it compatible with 
00080 // php 4, we're just going to use this handy dandy function
00081 
00082 
00083 ###################################################################################
00084 #
00085 # XML Library, by Keith Devens, version 1.2b
00086 # http://keithdevens.com/software/phpxml
00087 #
00088 # This code is Open Source, released under terms similar to the Artistic License.
00089 # Read the license at http://keithdevens.com/software/license
00090 #
00091 ###################################################################################
00092 
00093 ###################################################################################
00094 # XML_unserialize: takes raw XML as a parameter (a string)
00095 # and returns an equivalent PHP data structure
00096 ###################################################################################
00097 function & XML_unserialize(&$xml){
00098         $xml_parser = &new XML();
00099         $data = &$xml_parser->parse($xml);
00100         $xml_parser->destruct();
00101         return $data;
00102 }
00103 ###################################################################################
00104 # XML_serialize: serializes any PHP data structure into XML
00105 # Takes one parameter: the data to serialize. Must be an array.
00106 ###################################################################################
00107 function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
00108         if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
00109         while(list($key, $value) = each($data))
00110                 if(!strpos($key, ' attr')) #if it's not an attribute
00111                         #we don't treat attributes by themselves, so for an empty element
00112                         # that has attributes you still need to set the element to NULL
00113 
00114                         if(is_array($value) and array_key_exists(0, $value)){
00115                                 XML_serialize($value, $level, $key);
00116                         }else{
00117                                 $tag = $prior_key ? $prior_key : $key;
00118                                 echo str_repeat("\t", $level),'<',$tag;
00119                                 if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
00120                                         while(list($attr_name, $attr_value) = each($data["$key attr"]))
00121                                                 echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
00122                                         reset($data["$key attr"]);
00123                                 }
00124 
00125                                 if(is_null($value)) echo " />\n";
00126                                 elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
00127                                 else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
00128                         }
00129         reset($data);
00130         if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
00131 }
00132 ###################################################################################
00133 # XML class: utility class to be used with PHP's XML handling functions
00134 ###################################################################################
00135 class XML{
00136         var $parser;   #a reference to the XML parser
00137         var $document; #the entire XML structure built up so far
00138         var $parent;   #a pointer to the current parent - the parent will be an array
00139         var $stack;    #a stack of the most recent parent at each nesting level
00140         var $last_opened_tag; #keeps track of the last tag opened.
00141 
00142         function XML(){
00143                 $this->parser = &xml_parser_create();
00144                 xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
00145                 xml_set_object(&$this->parser, &$this);
00146                 xml_set_element_handler(&$this->parser, 'open','close');
00147                 xml_set_character_data_handler(&$this->parser, 'data');
00148         }
00149         function destruct(){ xml_parser_free(&$this->parser); }
00150         function & parse(&$data){
00151                 $this->document = array();
00152                 $this->stack    = array();
00153                 $this->parent   = &$this->document;
00154                 return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
00155         }
00156         function open(&$parser, $tag, $attributes){
00157                 $this->data = ''; #stores temporary cdata
00158                 $this->last_opened_tag = $tag;
00159                 if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
00160                         if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
00161                                 #this is the third or later instance of $tag we've come across
00162                                 $key = count_numeric_items($this->parent[$tag]);
00163                         }else{
00164                                 #this is the second instance of $tag that we've seen. shift around
00165                                 if(array_key_exists("$tag attr",$this->parent)){
00166                                         $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
00167                                         unset($this->parent["$tag attr"]);
00168                                 }else{
00169                                         $arr = array(&$this->parent[$tag]);
00170                                 }
00171                                 $this->parent[$tag] = &$arr;
00172                                 $key = 1;
00173                         }
00174                         $this->parent = &$this->parent[$tag];
00175                 }else{
00176                         $key = $tag;
00177                 }
00178                 if($attributes) $this->parent["$key attr"] = $attributes;
00179                 $this->parent  = &$this->parent[$key];
00180                 $this->stack[] = &$this->parent;
00181         }
00182         function data(&$parser, $data){
00183                 if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
00184                         $this->data .= $data;
00185         }
00186         function close(&$parser, $tag){
00187                 if($this->last_opened_tag == $tag){
00188                         $this->parent = $this->data;
00189                         $this->last_opened_tag = NULL;
00190                 }
00191                 array_pop($this->stack);
00192                 if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
00193         }
00194 }
00195 function count_numeric_items(&$array){
00196         return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
00197 }
00198 ?>

Generated on Sun Oct 26 20:33:12 2008 for The Travelsized Content Management System by  doxygen 1.5.5