summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Scaffold/TScaffoldBase.php
blob: b41a593e17b4448094dbe20ddd5f3a02d2426815 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
 * TScaffoldBase class file.
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2008 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id$
 * @package System.Data.ActiveRecord.Scaffold
 */

/**
 * Include the base Active Record class.
 */
Prado::using('System.Data.ActiveRecord.TActiveRecord');

/**
 * Base class for Active Record scaffold views.
 *
 * Provides common properties for all scaffold views (such as, TScaffoldListView,
 * TScaffoldEditView, TScaffoldListView and TScaffoldView).
 *
 * During the OnPrRender stage the default css style file (filename style.css)
 * is published and registered. To override the default style, provide your own stylesheet
 * file explicitly.
 *
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
 * @version $Id$
 * @package System.Data.ActiveRecord.Scaffold
 * @since 3.1
 */
abstract class TScaffoldBase extends TTemplateControl
{
	/**
	 * @var TActiveRecord record instance (may be new or retrieved from db)
	 */
	private $_record;

	/**
	 * @return TDbMetaData table/view information
	 */
	protected function getTableInfo()
	{
		$finder = $this->getRecordFinder();
		$gateway = $finder->getRecordManager()->getRecordGateWay();
		return $gateway->getRecordTableInfo($finder);
	}

	/**
	 * @param TActiveRecord record instance
	 * @return array record property values
	 */
	protected function getRecordPropertyValues($record)
	{
		$data = array();
		foreach($this->getTableInfo()->getColumns() as $name=>$column)
			$data[] = $record->getColumnValue($name);
		return $data;
	}

	/**
	 * @param TActiveRecord record instance
	 * @return array record primary key values.
	 */
	protected function getRecordPkValues($record)
	{
		$data=array();
		foreach($this->getTableInfo()->getColumns() as $name=>$column)
		{
			if($column->getIsPrimaryKey())
				$data[] = $record->getColumnValue($name);
		}
		return $data;
	}

	/**
	 * Name of the Active Record class to be viewed or scaffolded.
	 * @return string Active Record class name.
	 */
	public function getRecordClass()
	{
		return $this->getViewState('RecordClass');
	}

	/**
	 * Name of the Active Record class to be viewed or scaffolded.
	 * @param string Active Record class name.
	 */
	public function setRecordClass($value)
	{
		$this->setViewState('RecordClass', $value);
	}

	/**
	 * Copy the view details from another scaffold view instance.
	 * @param TScaffoldBase scaffold view.
	 */
	protected function copyFrom(TScaffoldBase $obj)
	{
		$this->_record = $obj->_record;
		$this->setRecordClass($obj->getRecordClass());
		$this->setEnableDefaultStyle($obj->getEnableDefaultStyle());
	}

	/**
	 * Unset the current record instance and table information.
	 */
	protected function clearRecordObject()
	{
		$this->_record=null;
	}

	/**
	 * Gets the current Active Record instance. Creates new instance if the
	 * primary key value is null otherwise the record is fetched from the db.
	 * @param array primary key value
	 * @return TActiveRecord record instance
	 */
	protected function getRecordObject($pk=null)
	{
		if($this->_record===null)
		{
			if($pk!==null)
			{
				$this->_record=$this->getRecordFinder()->findByPk($pk);
				if($this->_record===null)
					throw new TConfigurationException('scaffold_invalid_record_pk',
						$this->getRecordClass(), $pk);
			}
			else
			{
				$class = $this->getRecordClass();
				if($class!==null)
					$this->_record=Prado::createComponent($class);
				else
				{
					throw new TConfigurationException('scaffold_invalid_record_class',
						$this->getRecordClass(),$this->getID());
				}
			}
		}
		return $this->_record;
	}

	/**
	 * @param TActiveRecord Active Record instance.
	 */
	protected function setRecordObject(TActiveRecord $value)
	{
		$this->_record=$value;
	}

	/**
	 * @return TActiveRecord Active Record finder instance
	 */
	protected function getRecordFinder()
	{
		return TActiveRecord::finder($this->getRecordClass());
	}

	/**
	 * @return string default scaffold stylesheet name
	 */
	public function getDefaultStyle()
	{
		return $this->getViewState('DefaultStyle', 'style');
	}

	/**
	 * @param string default scaffold stylesheet name
	 */
	public function setDefaultStyle($value)
	{
		$this->setViewState('DefaultStyle', TPropertyValue::ensureString($value), 'style');
	}

	/**
	 * @return boolean enable default stylesheet, default is true.
	 */
	public function getEnableDefaultStyle()
	{
		return $this->getViewState('EnableDefaultStyle', true);
	}

	/**
	 * @param boolean enable default stylesheet, default is true.
	 */
	public function setEnableDefaultStyle($value)
	{
		return $this->setViewState('EnableDefaultStyle', TPropertyValue::ensureBoolean($value), true);
	}

	/**
	 * Publish the default stylesheet file.
	 */
	public function onPreRender($param)
	{
		parent::onPreRender($param);
		if($this->getEnableDefaultStyle())
		{
			$url = $this->publishAsset($this->getDefaultStyle().'.css');
			$this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
		}
	}
}