Blame view

php/classes/AliasMgr.php 3.6 KB
16035364   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
 * @class AliasMgr
 * @version $Id: AliasMgr.php 899 2012-05-02 14:08:28Z benjamin $
 *
 *
 */

  class AliasMgr extends AmdaObjectMgr {
  	
  function __construct() {

                parent::__construct('Alias.xml');
                $this->contentRootId = 'alias-treeRootNode';
                $this->contentRootTag = 'aliasList';
                $this->attributes = array('name' => '', 'param' => '');
                $this->objTagName = 'alias';
                if (!file_exists($this->xmlName)) {
				    $this->createDom();
				    $this->xp = new domxpath($this->contentDom);
		}
	}

	protected function createDom() {
                $rootElement = $this->contentDom->createElement($this->contentRootTag);
		$rootElement->setAttribute('xml:id', $this->contentRootId);
                $this->contentDom->appendChild($rootElement);
		$this->contentDom->save($this->xmlName);
        }
	
/*
* Add Alias in Alias.xml
*/      
 	protected function addAlias($obj) {
              
                $objList = $this->contentDom->getElementById($this->contentRootId); 		
                $newObj = $this->contentDom->createElement($this->objTagName);
                $newObj->setAttribute('xml:id',$this->id);
                $obj_arr = (array)$obj;
                foreach ($this->attributes as $key => $value) {
                	if ($key == 'name') $newObj->setAttribute($key, $obj_arr[$key]);
                }
                $objList -> appendChild($newObj);       

                $this->saveContent() ;
      }
      
	/*
	 *   Create Alias
	 */	      
    protected function createParameter($obj)   {	
		$this->id = $obj->param;
		$this -> addAlias($obj);
		return "OK";
	}
	/*****************************************************************
	 *                           PUBLIC FUNCTIONS
	 *****************************************************************/

     public function getList() {

	  $AliasList = $this->contentDom->getElementsByTagName('alias');

          return $AliasList;
      }

      public function substrParamAlias($chain, $paramName, $aliasName) {

        $aliasName="#".$aliasName;
        $pos = strpos($chain, $paramName);
        while ( $pos !== FALSE ) {
            $pos = $pos+strlen($paramName);
            if (preg_match('/[-+*,^<>&|=\/\[\]\(\)\ ]/', $chain[$pos]) || $chain[$pos] === '') {
                $chain = substr_replace($chain, $aliasName,$pos-strlen($paramName),strlen($paramName));
            }
            $pos = strpos($chain, $paramName, $pos);
        }
        return $chain;
    }

        public function substrAliasParam($chain, $paramName, $aliasName) {

        $aliasName="#".$aliasName;
        $pos = strpos($chain, $aliasName);
        while ( $pos !== FALSE ) {
            $pos = $pos+strlen($aliasName);
            if (preg_match('/[-+*,^<>&|=\/\[\]\(\)\ ]/', $chain[$pos]) || $chain[$pos] === '') {
                $chain = substr_replace($chain, $paramName,$pos-strlen($aliasName),strlen($aliasName));
            }
            $pos = strpos($chain, $aliasName, $pos);
        }
        return $chain;
    }
    
  	protected function deleteParameter($id){
		
	}
	
	/*
	 *   Get Object into Edit
	 */		
	public function getObject($id) {
		if (!($obj = $this->objectExistsById($id))) return "NO_SUCH_ID";
		$attributesToReturn['id'] = $obj->getAttribute('xml:id');
		$attributesToReturn['name'] = $obj->getAttribute('name');
		return  $attributesToReturn;
	}

	/*
	 *   Get alias of parameter
	 */			
	public function getAlias($id) {
		$alias = "";
		if (($obj = $this->objectExistsById($id))) 
			if ($obj->hasAttribute('name'))
				$alias = $obj->getAttribute('name');
			
		return $alias;
	}
  }