/
Table.php
406 lines (357 loc) · 14.8 KB
/
Table.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
class Kwf_Component_Generator_Table extends Kwf_Component_Generator_Abstract
{
protected $_loadTableFromComponent = true;
protected $_idSeparator = '-'; //um in StaticTable _ verwenden zu können
protected $_idColumn = 'id';
protected $_hasNumericIds = true;
protected $_eventsClass = 'Kwf_Component_Generator_Events_Table';
protected $_addUrlPart = true;
protected $_useComponentId;
protected function _getUseComponentId()
{
if (!isset($this->_useComponentId)) {
$cacheId = 'useCmpId-'.$this->_class.'-'.$this->_settings['generator']; //cache to delay model creation
$this->_useComponentId = Kwf_Cache_SimpleStatic::fetch($cacheId, $success);
if (!$success) {
$this->_useComponentId = $this->_getModel()->hasColumn('component_id');
Kwf_Cache_SimpleStatic::add($cacheId, $this->_useComponentId);
}
}
return $this->_useComponentId;
}
final public function getFormattedSelect($parentData, $select = array())
{
$ret = $this->select($parentData, $select);
$ret = $this->_formatSelect($parentData, $ret);
return $ret;
}
public final function getChildIds($parentData, $select = array())
{
$select = $this->_formatSelect($parentData, $select);
return $this->_fetchIds($parentData, $select);
}
protected function _fetchIds($parentData, $select)
{
return $this->_getModel()->getIds($select);
}
protected function _fetchRows($parentData, $select)
{
return $this->_getModel()->getRows($select);
}
protected function _fetchCountChildData($parentData, $select)
{
if ($select) {
return $this->_getModel()->countRows($select);
} else {
return 0;
}
}
public function getChildData($parentData, $select = array())
{
Kwf_Benchmark::count('GenTable::getChildData');
if (is_array($select)) $select = new Kwf_Component_Select($select);
$ret = array();
if (!$parentData && ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF))
&& !$this->_getUseComponentId()) {
$parentDatas = $p->getRecursiveChildComponents(array(
'componentClass' => $this->_class
));
} else {
$parentDatas = array($parentData /* kann auch null sein*/);
}
foreach ($parentDatas as $parentData) {
$s = $this->_formatSelect($parentData, clone $select);
$rows = array();
if ($s) {
$rows = $this->_fetchRows($parentData, $s);
}
foreach ($rows as $row) {
$currentPd = $parentData;
if (!$currentPd) {
$currentPd = $this->_getParentDataByRow($row, $s);
}
if (!is_array($currentPd)) {
if ($currentPd) {
$currentPds = array($currentPd);
} else {
$currentPds = array();
}
} else {
$currentPds = $currentPd;
}
foreach ($currentPds as $currentPd) {
if (!$currentPd) {
throw new Kwf_Exception("No parentData returned in '".get_class($this)."'");
}
$data = $this->_createData($currentPd, $row, $s);
if ($data) {
$ret[] = $data;
}
}
}
}
return $ret;
}
public final function countChildData($parentData, $select = array())
{
$select = $this->_formatSelect($parentData, $select);
return $this->_fetchCountChildData($parentData, $select);
}
protected function _getParentDataByRow($row, $select)
{
if ($this->_getUseComponentId()) {
$constraints = array('componentClass'=>$this->_class);
if ($select->hasPart(Kwf_Component_Select::WHERE_SUBROOT)) {
$constraints['subroot'] = $select->getPart(Kwf_Component_Select::WHERE_SUBROOT);
}
if ($select->hasPart(Kwf_Component_Select::IGNORE_VISIBLE)) {
$constraints['ignoreVisible'] = $select->getPart(Kwf_Component_Select::IGNORE_VISIBLE);
}
if ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF)) {
//avoid getComponentsByDbId call when possible (so operate on strings only)
if ($p->dbId != substr($row->component_id, 0, strlen($p->dbId))) {
//some child might have a dbIdShortcut
$found = false;
foreach ($this->_getPossibleIndirectDbIdShortcuts($p->componentClass) as $dbIdShortcut) {
if (substr($row->component_id, 0, strlen($dbIdShortcut)) == $dbIdShortcut) {
$found = true;
}
}
if (!$found) return null;
}
$childIdPart = substr($row->component_id, strlen($p->dbId));
if (strpos($childIdPart, '_') !== false) {
return null; //not a direct child component
}
}
$ret = Kwf_Component_Data_Root::getInstance()
->getComponentsByDbId($row->component_id, $constraints);
//streng genommen nicht on same page sondern children of und auf same page
//siehe Kwf_Component_Generator_RecursiveTable2_RecursiveTest
if ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF)) {
foreach ($ret as $k=>$i) {
$found = false;
while ($i) {
if ($p->componentId == $i->componentId) {
$found = true;
}
if ($i->isPage) break; //bei page aufhoeren
$i = $i->parent;
}
if (!$found) {
unset($ret[$k]); //kein gemeinsamer parent vorhanden
}
}
}
} else {
$components = Kwf_Component_Data_Root::getInstance()->getComponentsBySameClass($this->_class, array('ignoreVisible' => true));
if (count($components) == 1) {
return $components[0];
} else if (count($components) == 0) {
return null;
}
throw new Kwf_Exception("Can't find parentData for row, implement _getParentDataByRow for the '{$this->_class}' '{$this->getGeneratorKey()}' Generator");
}
return $ret;
}
protected function _formatSelectId(Kwf_Component_Select $select)
{
if ($select->hasPart(Kwf_Model_Select::WHERE_ID)) {
$id = $select->getPart(Kwf_Model_Select::WHERE_ID);
$separator = substr($id, 0, 1);
if (in_array($separator, array('_', '-'))) {
$id = substr($id, 1);
if ($separator != $this->_idSeparator || ($this->_hasNumericIds && !is_numeric($id))) {
return null;
}
$select->whereEquals($this->_idColumn, $id);
$select->unsetPart(Kwf_Model_Select::WHERE_ID);
}
}
return $select;
}
protected function _formatSelect($parentData, $select)
{
$select = parent::_formatSelect($parentData, $select);
if (is_null($select)) return null;
if ($this->_getUseComponentId()) {
if ($parentData) {
$select->whereEquals('component_id', $parentData->dbId);
} else if ($p = $select->getPart(Kwf_Component_Select::WHERE_CHILD_OF)) {
$ors = array(
new Kwf_Model_Select_Expr_StartsWith('component_id', $p->dbId.'-'),
new Kwf_Model_Select_Expr_Equal('component_id', $p->dbId),
);
foreach ($this->_getPossibleIndirectDbIdShortcuts($p->componentClass) as $dbIdShortcut) {
$ors[] = new Kwf_Model_Select_Expr_StartsWith('component_id', $dbIdShortcut);
}
$select->where(new Kwf_Model_Select_Expr_Or($ors));
}
}
$select = $this->_formatSelectId($select);
if (is_null($select)) return null;
if (in_array('pos', $this->_getModel()->getOwnColumns()) && !$select->hasPart(Kwf_Component_Select::ORDER)) {
$select->order("pos");
}
$showInvisible = Kwf_Component_Data_Root::getShowInvisible();
if (!$select->getPart(Kwf_Component_Select::IGNORE_VISIBLE)
&& $this->_getModel()->hasColumn('visible') && !$showInvisible) {
$select->whereEquals("visible", 1);
}
if ($select->hasPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES)) {
$selectClasses = $select->getPart(Kwf_Component_Select::WHERE_COMPONENT_CLASSES);
if (!$selectClasses) return null;
$childClasses = $this->_settings['component'];
$keys = array();
foreach ($selectClasses as $selectClass) {
$keys = array_merge($keys, array_keys($childClasses, $selectClass));
}
if (!$keys) return null;
if (count($childClasses)==1) {
if (!in_array(key($childClasses), $keys)) {
return null;
}
} else {
if (!$this->_getModel()->hasColumn('component')) {
throw new Kwf_Exception("Column component does not exist for a generator '{$this->getGeneratorKey()}' in '$this->_class'");
}
$select->whereEquals('component', $keys);
}
}
return $select;
}
protected function _getComponentIdFromRow($parentData, $row)
{
return $parentData->componentId . $this->_idSeparator . $this->_getIdFromRow($row);
}
protected function _formatConfig($parentData, $row)
{
$componentId = $this->_getComponentIdFromRow($parentData, $row);
$dbId = $this->_getIdFromRow($row);
if (isset($this->_settings['dbIdShortcut'])) {
$dbId = $this->_settings['dbIdShortcut'] . $dbId;
} else {
$dbId = $parentData->dbId . $this->_idSeparator . $dbId;
}
if (count($this->_settings['component']) > 1 && isset($row->component)) {
$componentKey = $row->component;
} else {
$componentKey = null;
}
$componentClass = $this->_getChildComponentClass($componentKey, $parentData);
$data = array(
'componentId' => $componentId,
'dbId' => $dbId,
'componentClass' => $componentClass,
'parent' => $parentData,
'row' => $row,
'isPage' => false,
'isPseudoPage' => false
);
if ($this->_getModel()->hasColumn('visible') && !$row->visible) {
$data['invisible'] = true;
}
return $data;
}
/**
* wird in Link-Generator überschrieben
**/
protected function _getIdFromRow($row)
{
return $row->{$this->_idColumn};
}
public function getIdColumn()
{
return $this->_idColumn;
}
public function hasMultipleComponents()
{
return (count($this->_settings['component']) > 1);
}
public function getDuplicateProgressSteps($source)
{
return 1; //fixed, as we don't go any deeper (would be too expensive)
}
public function duplicateChild($source, $parentTarget, Zend_ProgressBar $progressBar = null)
{
if ($progressBar) $progressBar->next();
$progressBar = null; //stop here, as getDuplicateProgressSteps doesn't go any deeper
if ($source->generator !== $this) {
throw new Kwf_Exception("you must call this only with the correct source");
}
$ev = new Kwf_Component_Event_Component_FilterAddComponentClass($source->componentClass, $parentTarget);
Kwf_Events_Dispatcher::fireEvent($ev);
if ($ev->deny) {
return null;
}
$newRow = $this->_duplicateRow($source, $parentTarget);
if (!$newRow) {
return null;
}
$id = $this->_idSeparator . $newRow->{$this->_getModel()->getPrimaryKey()};
$targetGen = Kwf_Component_Generator_Abstract::getInstance($parentTarget->componentClass, $this->getGeneratorKey());
$targetChildData = $targetGen->getChildData($parentTarget, array('id'=>$id, 'ignoreVisible'=>true, 'limit'=>1));
$target = array_pop($targetChildData);
if (!$target) {
return null;
}
Kwc_Admin::getInstance($source->componentClass)->duplicate($source, $target, $progressBar);
return $target;
}
protected function _duplicateRow($source, $parentTarget)
{
$ret = null;
if ($this->_getUseComponentId()) { //only duplicate rows that are scoped to source component (using component_id)
if (count($this->_settings['component']) > 1) {
$targetGen = Kwf_Component_Generator_Abstract::getInstance($parentTarget->componentClass, $this->getGeneratorKey());
if (!array_key_exists($source->row->component, $targetGen->_settings['component'])) {
//ignore, component doesn't exist in target generator (we have incompatible generators)
return null;
}
}
$ret = $source->row->duplicate(array('component_id' => $parentTarget->dbId));
} else {
$ret = $source->row;
}
return $ret;
}
public function makeChildrenVisible($source)
{
if ($source->generator !== $this) {
throw new Kwf_Exception("you must call this only with the correct source");
}
$data = array();
if ($this->_getModel()->hasColumn('visible')) {
if (!$source->row->visible) {
$source->row->visible = 1;
$source->row->save();
}
}
Kwc_Admin::getInstance($source->componentClass)->makeVisible($source);
}
public function getGeneratorFlags()
{
$ret = parent::getGeneratorFlags();
$ret['table'] = true;
return $ret;
}
// TODO Cache
public function getStaticCacheVarsForMenu()
{
$ret = array();
$ret[] = array(
'model' => $this->getModel()
);
return $ret;
}
public function setVisible(Kwf_Component_Data $data, $visible)
{
if ($this->_getModel()->hasColumn('visible')) {
$data->row->visible = $visible;
} else {
throw new Kwf_Exception("Visible column doesn't exist");
}
$data->invisible = !$visible;
}
}