AmdaObjectMgr.php 10.7 KB
<?php
/**
 * @class AmdaObjectMgr 
 * @version $Id: AmdaObjectMgr.php 2891 2015-04-30 11:31:51Z elena $
 * 
 */

 class AmdaObjectMgr {

      protected $xmlName, $xp;      
      protected $attributes, $optionalAttributes;       
      protected $contentRootId, $objTagName;
      protected $descFileName, $resFileName;
      protected $id_prefix;
      protected $contentRootTag;
      protected $obj, $id;
      protected $types;

      public $contentDom; 
      public $objectDom;

      protected function __construct($xmlFile) {
// content XML
		$this->xmlName = USERWSDIR."/".$xmlFile;
		$this->contentDom = new DomDocument("1.0");
		$this->contentDom->preserveWhiteSpace = false;
		$this->contentDom->formatOutput = true;

		if (file_exists($this->xmlName)) {
		    $this->contentDom -> load($this->xmlName);	 
		    $this->xp = new domxpath($this->contentDom); 
		}
// object desc XML
//TODO not used in RequestMgr
		$this->objectDom = new DomDocument("1.0"); 
		$this->objectDom->preserveWhiteSpace = false;
		$this->objectDom->formatOutput = true;
      }
   
     private function getNewId() {                   
      //  Get all ID attributes
	   $elements = $this->xp->query("//".$this->objTagName."/@xml:id");

      // Now find New Valid ID  
		if ($elements->length  > 0) {
		      if($elements->length > 0) for ($i = 0; $i < $elements->length; $i++) {
							     $id =  explode('_',$elements->item($i)->nodeValue);
						             $idList[] = $id[1]; 
			}

			sort($idList);

			for ($i = 0; $i < count($idList); $i++) {
			      if ($idList[$i] > $i) { 
				      $newID = $i;
				      break;
				}  
			      $newID = $i+1;  
			    }                 
		  } else
			  $newID = 0;
	    return $newID;
      }

	protected function setId() { 
                $id_ = $this->getNewId();
		if ($id_ === false) return false;
		$this->id = $this->id_prefix.$id_;
		return $this->id;         
	} 
   
	protected function objectExistsByName($name){
                $this->obj = $this->xp->query("//".$this->objTagName."[@name='".$name."']");		 
		if ($this->obj->length != 0) return true;
		return false;
	} 

	protected function getObjectIdByName($name) {
		$this->obj = $this->xp->query("//".$this->objTagName."[@name='".$name."']");
                if ($this->obj->length == 0) return false; 
		    $id = $this->obj->item(0)->getAttribute('xml:id');
                if ($id) return $id;
		return false;           
	}
	
	protected function folderExistsByName($name){
                $this->obj = $this->xp->query("//".$this->contentRootTag."/folder[@name='".$name."']");		 
		if ($this->obj->length != 0) return true;
		return false;
	}  

	protected function objectExistsById($id){      
		$this->obj = $this->contentDom->getElementById($id); 
		if ($this->obj != null) return $this->obj;
		return false;
        } 
      
         protected function createObjectResource(){}
         protected function renameInResource(){}

/*
*  Write Object into desc file
*/
	protected function createObjectDescription($obj){
	      $root = $this->objectDom->createElement($this->objTagName); 
	      $root->setAttribute('xml:id',$this->id);
          
	      foreach($obj as $key => $value) {
		if ($key != 'id' && $key != 'leaf' && $key != 'nodeType') {	
		    $node =  $this->objectDom->createElement($key,htmlspecialchars($value)); 
		    $root -> appendChild($node);
		}
	      }      
// add Optional Attributes if they are undefined	      
              foreach ($this->optionalAttributes as $key => $value)   
		 if ($root->getElementsByTagName($key)->length == 0) {
                        $node =  $this->objectDom->createElement($key,htmlspecialchars($value));  
			$root -> appendChild($node);
                  }
 
	      $this->objectDom->appendChild($root);
	      $this->objectDom->save($this->descFileName);  
      }
/*
*    Just Save  Content XML
*/
     protected function saveContent() {
	      $this->contentDom->save($this->xmlName);
      }
    
/*
* Add Object to Content XML
*/
      protected function addToContent($obj, $folder) {
              
                $folderToAdd = null;

		$objList = $this->contentDom->getElementById($this->contentRootId); 		
                $newObj = $this->contentDom->createElement($this->objTagName);
                $newObj->setAttribute('xml:id',$this->id);
// object to mapped array
                $obj_arr = (array)$obj;
                foreach ($this->attributes as $key => $value) {
                          $newObj->setAttribute($key, $obj_arr[$key]);	 
                }              
	        if ($folder != null)  
                          $folderToAdd = $this->contentDom->getElementById($folder);

		if ($folderToAdd) $folderToAdd -> appendChild($newObj);
                else $objList -> appendChild($newObj);
		  

		$this->saveContent();
      }
     
/*
* Delete Object From Content XML
*/
      protected function deleteFromContent($obj) {
		$objList = $obj -> parentNode;// $this->contentDom->getElementById($this->contentRootId);
                $objList -> removeChild($obj);
		$this->saveContent();
      }

/*
*         Create Folder
*/
      protected function createFolder($obj){
                 if ($this -> folderExistsByName($obj->name)) return array('error' => NAME_EXISTS);
                 $newFolder = $this->contentDom->createElement('folder');
		 $id = $obj->name."_".$this->objTagName;
		 $newFolder -> setAttribute('xml:id',$id); 
                 $newFolder -> setAttribute('name',$obj->name);
		 $objList = $this->contentDom->getElementById($obj->parent);
                 $objList -> appendChild($newFolder);
		 $this -> saveContent();
		return array('id' => $id);          
      }
/*
*         Get Folder of the object 
*/
      protected function getObjectFolder($id) {

                if (!($obj = $this->objectExistsById($id))) return "NO_SUCH_ID";
		if ($obj->parentNode->tagName == 'folder') return $obj->parentNode->getAttribute('xml:id');
		return null;

      }
         
	protected function setAlias($chain) {

		$aliasMgr = new AliasMgr();		 
		$listeAlias = $aliasMgr->getList();

		foreach($listeAlias as $alias) {
			$chain = $aliasMgr->substrParamAlias($chain, $alias->getAttribute("xml:id"),$alias->getAttribute("name"));
		}
        	
		return $chain;
	}	

	protected function resetAlias($chain) {

		$aliasMgr = new AliasMgr();		 
		$listeAlias = $aliasMgr->getList();

		foreach($listeAlias as $alias) {
			$chain = $aliasMgr->substrAliasParam($chain, $alias->getAttribute("xml:id"),$alias->getAttribute("name"));
		}
        	
		return $chain;
	}	
	
	protected function createDom() {
		 
		$rootElement = $this->contentDom->createElement('ws');
                foreach ($this->types as $type) {
		    $contentId = $type.'-treeRootNode';
		    $contentTag = $type.'List';
		    $typeElement = $this->contentDom->createElement($contentTag);
		    $typeElement->setAttribute('xml:id', $contentId);
		    $rootElement->appendChild($typeElement);
		}
                $this->contentDom->appendChild($rootElement);
		$this->contentDom->save($this->xmlName);
        }

  
/*****************************************************************
*                           PUBLIC FUNCTIONS
*****************************************************************/  
	
/*
*        Create Parameter[TT...]/Folder
*        create object itself, add it to content DOM
*/   
      function createObject($p, $folder){

	      if ($p -> leaf) return $this->createParameter($p, $folder);
	//      else return $this->createFolder($p);
//TODO check if this is possible?
	      else return array('error' => 'createFolder should be called from RENAME');

      }

/*
*          Rename Parameter[TT...]/Folder
*/
      function renameObject($p){

		if (!($objToRename = $this -> objectExistsById($p->id))) {
// NO SUCH ID:  leaf -> error;
			   if ($p->leaf) return array('error' => NO_SUCH_ID);
// NO SUCH ID:  folder  -> create  
			    return  $this -> createFolder($p); 			    
                 }
// object was just DD in the tree : move tag in xml
		if ($p -> name == $p -> old_name) {
		    if (!($parentNode = $this->contentDom->getElementById($p -> parent))) return array('error' => NO_SUCH_PARENT_ID);   
		    $parentNode -> appendChild($objToRename);  
		    $this->saveContent();
		    return array('id' => $p->id);
		 }

//		if ($this -> objectExistsByName($p->name, $this->objTagName)) return array('error' => NAME_EXISTS);
//TODO  CHECK: With some PHP versions setAttribute ADD attribute
		if ($objToRename->hasAttribute('name')) $objToRename->removeAttribute('name');	
                $objToRename->setAttribute('name',$p->name);
		if (!$p->leaf) {		 
		    $objToRename->removeAttribute('xml:id');
		    $objToRename->setAttribute('xml:id', $p->name.'_'.$this->objTagName);
		  }
		else  
                    $this -> renameInResource($p->name, $p->id); 

		 $this->saveContent();
		 return array('id' => $p->id);		
	}
   
/*
*          Delete Parameter[TT...]/Folder
*    	   Delete object itself, delete from contentDOM, mark as undefined in depending objects		
*/
       function deleteObject($p){

		  if ($p->leaf) {       
//   if Parameter[TT...] - delete resources first
                         $isDeleted = $this->deleteParameter($p->id);
	 		 if (!($objToDelete = $this -> objectExistsById($p->id))) return array('error' => NO_SUCH_ID);		                         
                  }
		  else {
			if (!($objToDelete = $this -> objectExistsById($p->id))) return array('error' => NO_SUCH_ID); 
			$folderToDelete = $objToDelete->getElementsByTagName($this->objTagName);
// delete all parameters[TT..] in folder(s)
			foreach ($folderToDelete as $obj) {
			    $id = $obj->getAttribute('xml:id');
			    $this->deleteParameter($id);
                        }
		  }
		  $this -> deleteFromContent($objToDelete);

		if ($isDeleted) return array('id' => $p->id, 'maskDeleted' => true); 
		else return array('id' => $p->id);
      }

/*
*         Modify Parameter[TT...]/Folder
*/ 
     
      function modifyObject($p){ 
            if ($this->renameOnly($p)) { 
			$p->leaf = 1;
                        return $this->renameObject($p);
	      } 
	    $folder = $this->getObjectFolder($p->id);
            $this->deleteObject($p);	    	     
	    return 
		    $this->createObject($p, $folder);
         } 

      
      function validNameObject($p){
      	if (!isset($p->name) || ($p->name == ""))
      		return array('valid' => false, 'error' => 'Name is required');
      	if ($p->leaf)
      		$alreadyExist = $this -> objectExistsByName($p->name, $this->objTagName);
      	else
      		$alreadyExist = $this -> folderExistsByName($p->name);
      	if ($alreadyExist)
      		return array('valid' => false, 'error' => 'This name already exists in this subtree!');
      	return array('valid' => true);
      }
         
      function getObject($id){}

      function execute($id){}
 
}
?>