forked from koala-framework/koala-framework
/
Events.php
113 lines (108 loc) · 4.67 KB
/
Events.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
<?php
class Kwc_Abstract_List_Events extends Kwc_Abstract_Events
{
public function getListeners()
{
$ret = parent::getListeners();
$cls = strpos($this->_class, '.') ? substr($this->_class, 0, strpos($this->_class, '.')) : $this->_class;
$m = call_user_func(array($cls, 'createChildModel'), $this->_class);
$ret[] = array(
'class' => $m,
'event' => 'Kwf_Events_Event_Row_Inserted',
'callback' => 'onRowInsertOrDelete'
);
$ret[] = array(
'class' => $m,
'event' => 'Kwf_Events_Event_Row_Deleted',
'callback' => 'onRowInsertOrDelete'
);
$ret[] = array(
'class' => $m,
'event' => 'Kwf_Events_Event_Row_Updated',
'callback' => 'onRowUpdate'
);
$ret[] = array(
'class' => $m,
'event' => 'Kwf_Events_Event_Model_Updated',
'callback' => 'onModelUpdate'
);
$generators = Kwc_Abstract::getSetting($this->_class, 'generators');
$ret[] = array(
'class' => $generators['child']['component'],
'event' => 'Kwf_Component_Event_Component_HasContentChanged',
'callback' => 'onChildHasContentChange'
);
return $ret;
}
public function onRowInsertOrDelete(Kwf_Events_Event_Row_Abstract $event)
{
foreach ($this->_getComponentsByRow($event->row) as $c) {
if ($c->componentClass == $this->_class) {
if (!Kwc_Abstract::getSetting($this->_class, 'hasVisible') || $event->row->visible) {
$this->fireEvent(new Kwf_Component_Event_Component_ContentChanged(
$this->_class, $c
));
$this->fireEvent(new Kwf_Component_Event_Component_HasContentChanged(
$this->_class, $c
));
}
$generator = Kwf_Component_Generator_Abstract::getInstance($this->_class, 'child');
$child = $c->getChildComponent(array(
'id' => $generator->getIdSeparator() . $event->row->id,
'ignoreVisible' => true
));
if ($event instanceof Kwf_Events_Event_Row_Inserted) {
$this->fireEvent(new Kwc_Abstract_List_EventItemInserted($this->_class, $child));
} else if ($event instanceof Kwf_Events_Event_Row_Deleted) {
$this->fireEvent(new Kwc_Abstract_List_EventItemDeleted($this->_class, $child));
}
}
}
}
public function onRowUpdate(Kwf_Events_Event_Row_Updated $event)
{
foreach ($this->_getComponentsByRow($event->row) as $c) {
if ($c->componentClass == $this->_class) {
$generator = Kwf_Component_Generator_Abstract::getInstance($this->_class, 'child');
$child = $c->getChildComponent(array(
'id' => $generator->getIdSeparator() . $event->row->id,
'ignoreVisible' => true
));
$this->fireEvent(new Kwf_Component_Event_Component_ContentChanged(
$this->_class, $c
));
if (Kwc_Abstract::getSetting($this->_class, 'hasVisible') && $event->isDirty('visible')) {
$this->fireEvent(new Kwf_Component_Event_Component_HasContentChanged(
$this->_class, $c
));
if ($event->row->visible) {
$this->fireEvent(new Kwc_Abstract_List_EventItemInserted($this->_class, $child));
} else {
$this->fireEvent(new Kwc_Abstract_List_EventItemDeleted($this->_class, $child));
}
} else {
$this->fireEvent(new Kwc_Abstract_List_EventItemUpdated($this->_class, $child));
}
}
}
}
protected function _getComponentsByRow($row)
{
return Kwf_Component_Data_Root::getInstance()->getComponentsByDbId(
$row->component_id, array('ignoreVisible'=>true)
);
}
public function onModelUpdate(Kwf_Events_Event_Model_Updated $event)
{
$this->fireEvent(new Kwf_Component_Event_ComponentClass_ContentChanged($this->_class));
}
public function onChildHasContentChange(Kwf_Component_Event_Component_HasContentChanged $event)
{
$c = $event->component->parent;
if ($c->componentClass == $this->_class) {
$this->fireEvent(new Kwf_Component_Event_Component_HasContentChanged(
$this->_class, $event->component->parent
));
}
}
}