summaryrefslogtreecommitdiff
path: root/framework/Web/UI/TTheme.php
blob: 1e3d8fab00e6a0e4c134074abf7d239eea85d4cc (plain)
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
<?php

class TTheme extends TComponent
{
	private $_themePath;
	private $_skins=array();

	public function __construct($name)
	{
		$this->_themePath=$name;
		$this->initialize();
	}

	private function initialize()
	{
		if(($theme=opendir($this->_themePath))===false)
			throw new Exception("Invalid theme ".$this->_themePath);
		while(($file=readdir($theme))!==false)
		{
			if(basename($file,'.skin')!==$file)
				$this->parseSkinFile($this->_themePath.'/'.$file);
		}
		closedir($theme);
	}

	private function parseSkinFile($fileName)
	{
		if(($skin=simplexml_load_file($fileName))===false)
			throw new Exception("Parsing $fileName failed.");
		foreach($skin->children() as $type=>$control)
		{
			$attributes=array();
			foreach($control->attributes() as $name=>$value)
			{
				$attributes[strtolower($name)]=(string)$value;
			}
			$skinID=isset($attributes['skinid'])?(string)$attributes['skinid']:0;
			unset($attributes['skinid']);
			if(isset($this->_skins[$type][$skinID]))
				throw new Exception("Duplicated skin $type.$skinID");
			else
				$this->_skins[$type][$skinID]=$attributes;
		}
	}

	public function applySkin($control)
	{
		$type=get_class($control);
		if(($id=$control->getSkinID())==='')
			$id=0;
		if(isset($this->_skins[$type][$id]))
		{
			foreach($this->_skins[$type][$id] as $name=>$value)
			{
				$control->setPropertyByPath($name,$value);
			}
		}
		else
			return;
	}
}


?>