| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TScaffoldBase class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TScaffoldBase.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Data.ActiveRecord.Scaffold
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Include the base Active Record class.
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Data.ActiveRecord.TActiveRecord');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Base class for Active Record scaffold views.
|
|
|
20 |
*
|
|
|
21 |
* Provides common properties for all scaffold views (such as, TScaffoldListView,
|
|
|
22 |
* TScaffoldEditView, TScaffoldListView and TScaffoldView).
|
|
|
23 |
*
|
|
|
24 |
* During the OnPrRender stage the default css style file (filename style.css)
|
|
|
25 |
* is published and registered. To override the default style, provide your own stylesheet
|
|
|
26 |
* file explicitly.
|
|
|
27 |
*
|
|
|
28 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
29 |
* @version $Id: TScaffoldBase.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
30 |
* @package System.Data.ActiveRecord.Scaffold
|
|
|
31 |
* @since 3.1
|
|
|
32 |
*/
|
|
|
33 |
abstract class TScaffoldBase extends TTemplateControl
|
|
|
34 |
{
|
|
|
35 |
/**
|
|
|
36 |
* @var TActiveRecord record instance (may be new or retrieved from db)
|
|
|
37 |
*/
|
|
|
38 |
private $_record;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* @return TDbMetaData table/view information
|
|
|
42 |
*/
|
|
|
43 |
protected function getTableInfo()
|
|
|
44 |
{
|
|
|
45 |
$finder = $this->getRecordFinder();
|
|
|
46 |
$gateway = $finder->getRecordManager()->getRecordGateWay();
|
|
|
47 |
return $gateway->getRecordTableInfo($finder);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @param TActiveRecord record instance
|
|
|
52 |
* @return array record property values
|
|
|
53 |
*/
|
|
|
54 |
protected function getRecordPropertyValues($record)
|
|
|
55 |
{
|
|
|
56 |
$data = array();
|
|
|
57 |
foreach($this->getTableInfo()->getColumns() as $name=>$column)
|
|
|
58 |
$data[] = $record->getColumnValue($name);
|
|
|
59 |
return $data;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* @param TActiveRecord record instance
|
|
|
64 |
* @return array record primary key values.
|
|
|
65 |
*/
|
|
|
66 |
protected function getRecordPkValues($record)
|
|
|
67 |
{
|
|
|
68 |
foreach($this->getTableInfo()->getColumns() as $name=>$column)
|
|
|
69 |
{
|
|
|
70 |
if($column->getIsPrimaryKey())
|
|
|
71 |
$data[] = $record->getColumnValue($name);
|
|
|
72 |
}
|
|
|
73 |
return $data;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Name of the Active Record class to be viewed or scaffolded.
|
|
|
78 |
* @return string Active Record class name.
|
|
|
79 |
*/
|
|
|
80 |
public function getRecordClass()
|
|
|
81 |
{
|
|
|
82 |
return $this->getViewState('RecordClass');
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Name of the Active Record class to be viewed or scaffolded.
|
|
|
87 |
* @param string Active Record class name.
|
|
|
88 |
*/
|
|
|
89 |
public function setRecordClass($value)
|
|
|
90 |
{
|
|
|
91 |
$this->setViewState('RecordClass', $value);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Copy the view details from another scaffold view instance.
|
|
|
96 |
* @param TScaffoldBase scaffold view.
|
|
|
97 |
*/
|
|
|
98 |
protected function copyFrom(TScaffoldBase $obj)
|
|
|
99 |
{
|
|
|
100 |
$this->_record = $obj->_record;
|
|
|
101 |
$this->setRecordClass($obj->getRecordClass());
|
|
|
102 |
$this->setEnableDefaultStyle($obj->getEnableDefaultStyle());
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* Unset the current record instance and table information.
|
|
|
107 |
*/
|
|
|
108 |
protected function clearRecordObject()
|
|
|
109 |
{
|
|
|
110 |
$this->_record=null;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Gets the current Active Record instance. Creates new instance if the
|
|
|
115 |
* primary key value is null otherwise the record is fetched from the db.
|
|
|
116 |
* @param array primary key value
|
|
|
117 |
* @return TActiveRecord record instance
|
|
|
118 |
*/
|
|
|
119 |
protected function getRecordObject($pk=null)
|
|
|
120 |
{
|
|
|
121 |
if($this->_record===null)
|
|
|
122 |
{
|
|
|
123 |
if($pk!==null)
|
|
|
124 |
{
|
|
|
125 |
$this->_record=$this->getRecordFinder()->findByPk($pk);
|
|
|
126 |
if($this->_record===null)
|
|
|
127 |
throw new TConfigurationException('scaffold_invalid_record_pk',
|
|
|
128 |
$this->getRecordClass(), $pk);
|
|
|
129 |
}
|
|
|
130 |
else
|
|
|
131 |
{
|
|
|
132 |
$class = $this->getRecordClass();
|
|
|
133 |
if($class!==null)
|
|
|
134 |
$this->_record=Prado::createComponent($class);
|
|
|
135 |
else
|
|
|
136 |
{
|
|
|
137 |
throw new TConfigurationException('scaffold_invalid_record_class',
|
|
|
138 |
$this->getRecordClass(),$this->getID());
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
return $this->_record;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* @param TActiveRecord Active Record instance.
|
|
|
147 |
*/
|
|
|
148 |
protected function setRecordObject(TActiveRecord $value)
|
|
|
149 |
{
|
|
|
150 |
$this->_record=$value;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* @return TActiveRecord Active Record finder instance
|
|
|
155 |
*/
|
|
|
156 |
protected function getRecordFinder()
|
|
|
157 |
{
|
|
|
158 |
return TActiveRecord::finder($this->getRecordClass());
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* @return string default scaffold stylesheet name
|
|
|
163 |
*/
|
|
|
164 |
public function getDefaultStyle()
|
|
|
165 |
{
|
|
|
166 |
return $this->getViewState('DefaultStyle', 'style');
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* @param string default scaffold stylesheet name
|
|
|
171 |
*/
|
|
|
172 |
public function setDefaultStyle($value)
|
|
|
173 |
{
|
|
|
174 |
$this->setViewState('DefaultStyle', TPropertyValue::ensureString($value), 'style');
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* @return boolean enable default stylesheet, default is true.
|
|
|
179 |
*/
|
|
|
180 |
public function getEnableDefaultStyle()
|
|
|
181 |
{
|
|
|
182 |
return $this->getViewState('EnableDefaultStyle', true);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* @param boolean enable default stylesheet, default is true.
|
|
|
187 |
*/
|
|
|
188 |
public function setEnableDefaultStyle($value)
|
|
|
189 |
{
|
|
|
190 |
return $this->setViewState('EnableDefaultStyle', TPropertyValue::ensureBoolean($value), true);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Publish the default stylesheet file.
|
|
|
195 |
*/
|
|
|
196 |
public function onPreRender($param)
|
|
|
197 |
{
|
|
|
198 |
parent::onPreRender($param);
|
|
|
199 |
if($this->getEnableDefaultStyle())
|
|
|
200 |
{
|
|
|
201 |
$url = $this->publishAsset($this->getDefaultStyle().'.css');
|
|
|
202 |
$this->getPage()->getClientScript()->registerStyleSheetFile($url,$url);
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|