Blame view

php/classes/StateMgr.php 1.62 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
<?php

  class StateMgr  
  {
  	protected $stateXmlName, $stateXml;
  	protected $rootElement;
  	
  	function __construct() 
  	{	 		 
		$this->stateXmlName = USERWSDIR."State.xml";
        $this->stateXml = new DomDocument("1.0");
        if (file_exists($this->stateXmlName))
        {
			$this->stateXml->load($this->stateXmlName); 
			$this->rootElement = $this->stateXml->documentElement;
		}
		else 
		{
			$this->rootElement = $this->stateXml->createElement('state');
			$this->stateXml->appendChild($this->rootElement);
			$this->stateXml->save($this->stateXmlName);
		}
    }
    
    function saveState($datas)
    {
    	if (!isset($this->rootElement))
    		return array('error' => true, 'message' => 'Root node not defined.');
    		
    	while ($this->rootElement->hasChildNodes())
   			$this->rootElement->removeChild($this->rootElement->childNodes->item(0));
    	
    	foreach ($datas as $key => $value)
    	{
    		$node = $this->stateXml->createElement("ui",$value);
    		$node->setAttribute("name",$key);
    		$this->rootElement->appendChild($node);
    	}
    	
    	$this->stateXml->save($this->stateXmlName);
    	    	
    	return array('error' => false);
    }
    
    function loadState()
    {
   		if (!isset($this->rootElement))
    		return array('error' => true, 'message' => 'Root node not defined.');

    	$state = array();
    	
    	$uis = $this->rootElement->getElementsByTagName('ui');
    	foreach($uis as $ui)
    	{
    		$name  = $ui->getAttribute("name");
    		$value = $ui->nodeValue;	
    		$state[$name] = $value;
    	}
    	
    	return array('error' => false, 'state' => $state);
    }
  	
  }

?>