summaryrefslogtreecommitdiff
path: root/framework/Web/UI/ActiveControls/TStyleDiff.php
blob: a7b707e04525ed7c6cec6a0689590028bc0a8f2a (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
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
<?php
/**
 * TActiveControlAdapter and TCallbackPageStateTracker class file.
 *
 * @author Wei Zhuo <weizhuo[at]gamil[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package Prado\Web\UI\ActiveControls
 */

namespace Prado\Web\UI\ActiveControls;
use Prado\Web\UI\WebControls\TStyle;

/**
 * TStyleDiff class.
 *
 * Calculates the changes to the Style properties.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package Prado\Web\UI\ActiveControls
 * @since 3.1
 */
class TStyleDiff extends TViewStateDiff
{
	/**
	 * @param TStyle control style
	 * @return array all the style properties combined.
	 */
	protected function getCombinedStyle($obj)
	{
		if(!($obj instanceof TStyle))
			return array();
		$style = $obj->getStyleFields();
		$style = array_merge($style,$this->getStyleFromString($obj->getCustomStyle()));
		if($obj->hasFont())
			$style = array_merge($style, $this->getStyleFromString($obj->getFont()->toString()));
		return $style;
	}

	/**
	 * @param string CSS custom style string.
	 * @param array CSS style as name-value array.
	 */
	protected function getStyleFromString($string)
	{
		$style = array();
		if(!is_string($string)) return $style;

		foreach(explode(';',$string) as $sub)
		{
			$arr=explode(':',$sub);
			if(isset($arr[1]) && trim($arr[0])!=='')
				$style[trim($arr[0])] = trim($arr[1]);
		}
		return $style;
	}

	/**
	 * @return string changes to the CSS class name.
	 */
	protected function getCssClassDiff()
	{
		if($this->_old===null)
		{
			return ($this->_new!==null) && $this->_new->hasCssClass()
						? $this->_new->getCssClass() : null;
		}
		else
		{
			return $this->_old->getCssClass() !== $this->_new->getCssClass() ?
				$this->_new->getCssClass() : null;
		}
	}

	/**
	 * @return array list of changes to the control style.
	 */
	protected function getStyleDiff()
	{
		$diff = array_diff_assoc(
					$this->getCombinedStyle($this->_new),
					$this->getCombinedStyle($this->_old));
		return count($diff) > 0 ? $diff : null;
	}

	/**
	 * @return array list of changes to the control style and CSS class name.
	 */
	public function getDifference()
	{
		if($this->_new===null)
			return $this->_null;
		else
		{
			$css = $this->getCssClassDiff();
			$style = $this->getStyleDiff();
			if(($css!==null) || ($style!==null))
				return array('CssClass' => $css, 'Style' => $style);
			else
				$this->_null;
		}
	}
}