summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Scaffold/TScaffoldBase.php
blob: dc4642452910d9464f3ae29fa654634415e96674 (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
105
106
107
108
109
110
111
112
113
<?php

Prado::using('System.Data.ActiveRecord.TActiveRecord');

abstract class TScaffoldBase extends TTemplateControl
{
	private $_record;
	private $_meta;

	protected function getTableMetaData()
	{
		if($this->_meta===null)
		{
			$finder = $this->getRecordFinder();
			$gateway = $finder->getRecordManager()->getRecordGateWay();
			$this->_meta = $gateway->getMetaData($finder);
		}
		return $this->_meta;
	}

	protected function getRecordProperties($record)
	{
		$data = array();
		foreach($this->getTableMetaData()->getColumns() as $name=>$column)
			$data[] = $record->{$name};
		return $data;
	}

	public function getRecordObjectPk($record)
	{
		$pk = array();
		foreach($this->getTableMetaData()->getColumns() as $name=>$column)
		{
			if($column->getIsPrimaryKey())
				$data[] = $record->{$name};
		}
		return $data;
	}

	public function getRecordClass()
	{
		return $this->getViewState('RecordClass');
	}

	public function setRecordClass($value)
	{
		$this->setViewState('RecordClass', $value);
	}

	public function copyFrom(TScaffoldBase $obj)
	{
		$this->_record = $obj->_record;
		$this->_meta = $obj->_meta;
		$this->setRecordClass($obj->getRecordClass());
	}

	protected function clearRecordObject()
	{
		$this->_record=null;
		$this->_meta=null;
	}

	public function getRecordObject($pk=null)
	{
		if($this->_record===null)
		{
			if($pk!==null)
				$this->_record=$this->getRecordFinder()->findByPk($pk);
			else
			{
				$class = $this->getRecordClass();
				if($class!==null)
					$this->_record=Prado::createComponent($class);
				else
					throw new TConfigurationException('scaffold_invalid_record_class', $this->getID());
			}
		}
		return $this->_record;
	}

	public function getRecordFinder()
	{
		return TActiveRecord::getRecordFinder(get_class($this->getRecordObject()));
	}

	public function setRecordObject($value)
	{
		if($value instanceof TActiveRecord)
			$this->_record=$value;
		else
			throw new TConfigurationException('scaffold_object_must_be_tactiverecord', $this->getID());
	}

	public function getDefaultStyle()
	{
		return $this->getViewState('DefaultStyle', 'style');
	}

	public function setDefaultStyle($value)
	{
		$this->setViewState('DefaultStyle', TPropertyValue::ensureString($value), 'style');
	}

	public function onPreRender($param)
	{
		parent::onPreRender($param);
		$url = $this->publishAsset($this->getDefaultStyle().'.css');
		$cs = $this->getPage()->getClientScript();
		$cs->registerStyleSheetFile($url,$url);
	}
}

?>