Location: PHPKode > projects > Web Engine CMS > elem_XML.php
<?
//PEAR functions
include_once "elem_common.php";
include_once "libraries/XMLSchema.php";

function &xquery($dom, $pattern)
 {
   if (version_compare(PHP_VERSION,'5','>='))
   {
    $xp = new domxpath($dom);
    $result = $xp->query($pattern);
    $ret_array = array();
    foreach($result as $val)
     {
     	$ret_array[] = $val;
     }
     return $ret_array;
   }
   else{
    $xpath = xpath_new_context($dom);
    $result = xpath_eval($xpath, $pattern);
    return $result->nodeset;
   }
 }

class CElemXML extends CElem_common
{
 var $xml_node;

 function &xquery($pattern)
 {
    return xquery($this->_Engine->xmlSiteDom, $pattern);
 }

 function CElemXML(&$Engine, $type, $elem_id="-1", $pos=0, $attrs=array()){
  $this->CElem_common($Engine, $type, $elem_id, $pos, $attrs);
 }

 function NextId($type, $issave=false)
  {
   $value=0;
   $elems = &$this->GetElemsByType($this->_Engine, "type");
   if(!count($elems)){
     //Add new Type node
     $newtype = $dom->create_Element("type");
     $newtype->set_attribute("name",$type);
     $newtype->set_attribute("title",$type);
     $newtype->set_attribute("count",0);
     $parent = "Not released yet";
     $newtype = $parent->append_child($newtype);
    }
   else{
     $counter = $elems[0];
     if (version_compare(PHP_VERSION,'5','>=')){
       $value = $counter->getAttribute("count");
      }
     else
      $value = $counter->get_attribute("count");
    }
   $value++;
   if($issave){
     $date = date('Ymd');
     $counter->set_attribute("lastdate", $date);
     $counter->set_attribute("count", $value);
    }
   return $value;
}

 function GetFromRequest()
  {
    $schema = new XMLSchema(sys_engine_path.'elems/'.$this->type.'/'.$this->type.".xsd");
    $attrs = $schema->GetAttrsFor($this->type);

   foreach($attrs as $attr_name){
	   $field_found=false;
           switch($attr_name){
             case 'id':
                $attr_name = "elem_id";
           	break;
	     case 'file':
	        //Try find uploaded file
	        $attr_val = $this->_Engine->ProcUploadedFile($attr_name);
  	        if($attr_val!=""){
     	          $field_found=true;
	          //Delete old file if modify
	          if($this->elem_id >0){
     	            $this->Get(); //fill elem fields
	            $datapath = sys_srv_path."_data/";
 	            $this->DeleteFile($datapath, $attr_val);
	           }
     	         }
     	        break;
     	    }

 	   if($this->_Engine->GetParam($attr_name, $attr_val)){
	     $field_found=true;
	    }

	   if($field_found){
	     if($attr_name == "elem_id"){
	       $attr_name = "id";
	       if($attr_val < 0)
	        $attr_val = -$attr_val;
	      }

	     $this->xml_node->set_attribute($attr_name, iconv("CP1251", "UTF-8",  stripslashes($attr_val)));
	    }
    }
 }

 function Create(){
     $schema = new XMLSchema(sys_engine_path."elems/".$this->type.".xsd");
     $sxml = $schema->GetEmptyElem($this->type);

     //hack to remove namespace
     $sxml = str_replace('xmlns:xs="http://www.w3.org/2001/XMLSchema"','',$sxml);

     $nodedom = domxml_open_mem($sxml);
     $xml_elem = $nodedom->document_element();
	 if(!$xml_elem){
	    return false;
	  }

     $xml_elem = $xml_elem->clone_node(1);


     if($xml_elem->has_attribute("date"))
       $xml_elem->set_attribute("date", Date("Y-m-d H:m:s"));

     if (version_compare(PHP_VERSION,'5','>='))
	$xml_attrs = $xml_elem->attributes;
     else
	$xml_attrs = $xml_elem->attributes();

     foreach($xml_attrs as $attr)
      {
       $this->attrs[$attr->name] =  $attr->value;
      }

     $this->xml_node = $xml_elem;
     return true;
  }

  function GetPos(&$parent)
   {
    //$parent
    $xpath = "//$parent->type[@id='$parent->id']/$this->type[id='.$this->elem_id.']";
    print $xpath;
    return $this->xmldoc->xquery($xpath);
   }

  function SetPos($table_links, $page_id, $newpos)
   {
     return false;
   }

   function Get($attr="", $key="")
    {
     if($attr=="")
      $attr= "id";
     if($key=="")
      $key = $this->elem_id;

     $pat = "//".$this->type."[@".$attr."=\"".$key."\"]";
//print $pat;
     $xpresult = $this->xquery($pat);
//print_r($xpresult);
     if(count($xpresult)!=1)
      return false;

     $this->xml_node = $xpresult[0];

     if (version_compare(PHP_VERSION,'5','>='))
	$xml_attrs = $this->xml_node->attributes;
     else
	$xml_attrs = $this->xml_node->attributes();

     foreach($xml_attrs as $attr)
      {
       $this->attrs[$attr->name] =  $attr->value;
      }

      return true;
    }

   function Write($parent_node)
    {
     $xml_elem = $this->xmldoc->create_Element($this->type);

     //Add attrs
     foreach($this->attrs as $key=>$val)
      {
       $val = stripslashes($val); //according to $val
       if($key == "text"){
         //$text_node = $this->xmldoc->create_text_node(iconv("CP1251","UTF-8",$val));
         $text_node = $this->xmldoc->create_cdata_section($val);//iconv("CP1251","UTF-8",$val));
         $xml_elem->append_child($text_node);
        }
       else{
         $xml_elem->set_attribute($key, $val);//iconv("CP1251","UTF-8",$val));
        }
    }

    $xml_elem->set_attribute("id", $this->elem_id);
    if(isset($this->pos))
      $xml_elem->set_attribute("pos", $this->pos);

    $parent_node->append_child($xml_elem);
    return true;
  }

  function RefCount($table_links)
   {
     /*$q = "//".$this->type."[@id=\"".$this->elem_id."\"]";
     $res = $this->xquery($q);
     return count($res);
     */
     return 0; //in xml still no multi links
   }

   function Delete($parent_id, $datapath, $table_link)
   {
    if($this->elem_id==-1)
      return false;

     //for xml we dont need to look for links
     if(!$this->RefCount($table_link))
     {
	     //Delete elem's file if exists
	     $this->Get(); //fill elem fields
	     if(isset($this->attrs['file']))
	      $this->DeleteFile($datapath);

	     //Delete element from storage
	     $q = "//".$this->type."[@id=\"".$this->elem_id."\"]";
             $res = $this->xquery($q);
 	     if(!count($res)){
	       return false;
	      }
if (version_compare(PHP_VERSION,'5','>='))
	     $res[0]->parentNode->removeChild($res[0]);
else{
	     $res[0]->parent_Node->remove_Child($res[0]);
}
      }
      return true;
    }

   function DeleteFile($datapath, $newfilename="")
    {
     $files_path = $datapath.$this->type."s/";
     if(!is_array($this->attrs))
      return false;
     $oldfilename = $this->attrs['file'];
     if($oldfilename!="" && $oldfilename!= $newfilename)
      return @unlink($files_path."/".$oldfilename);
     return false;
    }

 function GetInfo()
  {
 	$xpath = xpath_new_context($this->schema);
 	xpath_register_ns($xpath, 'xs', 'http://www.w3.org/2001/XMLSchema');
     	$xpresult = xpath_eval($xpath, "//xs:element[@name='".$this->type."']//xs:attribute");
     	if(!count($xpresult))
     	 return false;
     	$Fields =  array();
     	foreach($xpresult as $node)
     	{
         $field['Field'] = $node->get_attribute("name");
         $field['Default'] = $node->get_attribute("default");
         $Fields[] = $field;
     	}
  	return $Fields;
  }

 function LinkTableByType($type, $lang="", $parent_id=""){
     if(!$type)
 	 $type = $this->type;
    if($lang=="")
     $lang = $this->_Engine->lang;
    if($parent_id!="")
     $parent_id .="_";

    if($parent_id){
      if(isset($this->attrs['href']))
       return $this->attrs['href'];
      else
       return sys_xml.sys_dbprefix."page_".$parent_id.$lang.".xml";
    }
    else
    switch($type){
      case "page":
        return sys_xml.sys_dbprefix.$lang.".xml";
      default:
	return sys_xml.sys_dbprefix.$type."s.xml";
     }
   }

  function GetElemsByType($_Engine, $type, $filename="", $elem_id="", $XPointer="/page/"){
     if($filename==""){
  	$cur_elem = new CElemXML($_Engine, $type,$elem_id);
     	$filename = $cur_elem->LinkTableByType($type);
       }
     //read page's dom
     $page_dom = $this->read_dom($filename);
     if(!$page_dom)
      return 0;
     $Elem = new CElemXML($_Engine, $type);

     $Elems = array();

     $pattern = $XPointer;
     if($type !="")
      $pattern .= $type;
     else
      $pattern .= "*";
     if($elem_id!="")
      $pattern .=  "[@id =$elem_id]";
//print $pattern;
     $xml_elems = &xquery($page_dom, $pattern);
     return $xml_elems;
 }

 function GetChilds($type="", $elem_id="")
  {
   $tablename = $this->LinkTableByType($this->type, $this->_Engine->lang, $this->elem_id);
   return $this->GetElemsByType($this->_Engine, $type, $tablename);
  }

 function read_dom($FileName){
   if(!file_exists($FileName)){
     return false;
    }

   if (version_compare(PHP_VERSION,'5','>=')){
	$dom =  new domDocument();
	if(!$dom->load($FileName))
	 return false;
      }
     else{
        if(!$dom= domxml_open_file($FileName)) {
	 return false;
	}
      }
   	 return $dom;
  }
};


?>
Return current item: Web Engine CMS