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