forked from koala-framework/koala-framework
/
Component.php
241 lines (213 loc) · 9.31 KB
/
Component.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
<?php
abstract class Kwc_Menu_Abstract_Component extends Kwc_Abstract
{
public static function getSettings()
{
$ret = parent::getSettings();
$ret['componentName'] = trlKwfStatic('Menu');
$ret['componentIcon'] = 'layout';
$ret['rootElementClass'] = 'kwfUp-webStandard kwfUp-webMenu kwfUp-webListNone';
$ret['showParentPage'] = false;
$ret['showParentPageLink'] = false;
$ret['level'] = 'main';
$ret['flags']['hasAlternativeComponent'] = true;
$ret['plugins']['hideInvisibleDynamic'] = 'Kwc_Menu_Abstract_HideInvisibleDynamicPlugin';
return $ret;
}
public function getActiveViewPlugins()
{
$ret = parent::getActiveViewPlugins();
$found = false;
$pages = $this->_getMenuPages(null, array());
foreach ($pages as $p) {
if (Kwc_Abstract::getFlag($p->componentClass, 'hasIsVisibleDynamic')) {
$found = true;
break;
}
}
if (!$found) {
unset($ret['hideInvisibleDynamic']);
}
return $ret;
}
public static function validateSettings($settings, $componentClass)
{
if (isset($settings['showAsEditComponent'])) {
throw new Kwf_Exception("showAsEditComponent setting doesn't exist anymore");
}
if ($settings['showParentPage'] || $settings['showParentPageLink']) {
if (is_string($settings['level'])) {
throw new Kwf_Exception("You can't use showParentPage for MainMenus (what should that do?)");
}
}
}
public static function getAlternativeComponents($componentClass)
{
if (!$componentClass) throw new Kwf_Exception("componentClass required");
return array(
//content is fetch from parent and slightly modified (current added etc)
'parentMenu' => 'Kwc_Menu_ParentMenu_Component.'.$componentClass,
//a category is shown but the current page is in another one. gets templateVars from the right component
'otherCategory' => 'Kwc_Menu_OtherCategory_Component.'.$componentClass,
//a category is shown but the current page is above categories. displays using ParentContent from the right child component
'otherCategoryChild' => 'Kwc_Menu_OtherCategoryChild_Component.'.$componentClass,
//if deep enough so no difference in menu content (depth is defined by _requiredLevels)
'parentContent' => 'Kwc_Menu_ParentContent_Component.'.$componentClass,
//if above categories
'none' => 'Kwc_Basic_None_Component',
);
}
protected static function _requiredLevels($componentClass)
{
$shownLevel = Kwc_Abstract::getSetting($componentClass, 'level');
if (!is_numeric($shownLevel)) $shownLevel = 1;
return $shownLevel;
}
public static function useAlternativeComponent($componentClass, $parentData, $generator)
{
if ($generator instanceof Kwf_Component_Generator_Box_StaticSelect) {
throw new Kwf_Exception("Menus are not compatible with StaticSelect");
}
if (!$parentData) return false;
$foundPageOrCategory = false;
$data = $parentData;
do {
if ($data->isPage || Kwc_Abstract::getFlag($data->componentClass, 'menuCategory')) {
$foundPageOrCategory = true;
break;
}
} while ($data = $data->parent);
if (!$foundPageOrCategory) {
return 'otherCategoryChild';
}
$data = $parentData;
$menuLevel = 0;
while ($data && !Kwc_Abstract::getFlag($data->componentClass, 'menuCategory')) {
if ($data->isPage) $menuLevel++;
$data = $data->parent;
}
$shownLevel = Kwc_Abstract::getSetting($componentClass, 'level');
if (!is_numeric($shownLevel)) $shownLevel = 1;
$requiredLevels = call_user_func(array($componentClass, '_requiredLevels'), $componentClass);
$ret = false;
if ($menuLevel > $requiredLevels) {
$ret = 'parentContent';
} else if ($shownLevel <= $menuLevel) {
$ret = 'parentMenu';
if (!is_numeric(Kwc_Abstract::getSetting($componentClass, 'level'))) {
$data = $parentData;
do {
if (Kwc_Abstract::getFlag($data->componentClass, 'menuCategory')) break;
} while ($data = $data->parent);
if ($data) {
$cat = Kwc_Abstract::getFlag($data->componentClass, 'menuCategory');
if ($cat) {
if ($cat === true) $cat = $data->id;
if ($cat != Kwc_Abstract::getSetting($componentClass, 'level')) {
//there are categories and we are in a different category than the menu is
//(so none is active and we can just show the parentContent (=efficient))
$ret = 'parentContent';
}
}
} else {
//we are outside categories, show parentContent (until we reach otherCategoryChild)
$ret = 'parentContent';
}
}
} else if ($menuLevel < $shownLevel-1) {
$ret = 'none';
}
if ($ret == false) {
if (!is_numeric(Kwc_Abstract::getSetting($componentClass, 'level'))) {
$cat = Kwc_Abstract::getFlag($parentData->componentClass, 'menuCategory');
if ($cat) {
if ($cat === true) $cat = $parentData->id;
if ($cat != Kwc_Abstract::getSetting($componentClass, 'level')) {
$ret = 'otherCategory';
}
} else {
//this would just display all pages (there are no categories)
}
}
}
//echo "$ret:: $parentData->componentId: menuLevel=$menuLevel requiredLevels=$requiredLevels shownLevel=$shownLevel level=".Kwc_Abstract::getSetting($componentClass, 'level')."\n";
return $ret;
}
public function getTemplateVars()
{
$ret = parent::getTemplateVars();
$ret['parentPage'] = null;
$ret['parentPageLink'] = $this->_getSetting('showParentPageLink');
if ($this->_getSetting('showParentPage') || $ret['parentPageLink']) {
$ret['parentPage'] = $this->getData()->getPage();
}
return $ret;
}
/**
* Used by chained and Events
*/
public function getMenuData($parentData = null, $select = array(), $editableClass = 'Kwc_Menu_EditableItems_Component')
{
return $this->_getMenuData($parentData, $select, $editableClass);
}
protected function _getMenuPages($parentData, $select)
{
if (is_array($select)) $select = new Kwf_Component_Select($select);
$select->whereShowInMenu(true);
$ret = array();
if ($parentData) {
$pageComponent = $parentData;
} else {
$pageComponent = $this->getData();
while ($pageComponent = $pageComponent->parent) {
if ($pageComponent->isPage) break;
if (Kwc_Abstract::getFlag($pageComponent->componentClass, 'menuCategory')) break;
}
}
if ($pageComponent) $ret = $pageComponent->getChildPages($select);
return $ret;
}
protected function _getMenuData($parentData = null, $select = array(), $editableClass = 'Kwc_Menu_EditableItems_Component')
{
$i = 0;
$ret = array();
$pages = $this->_getMenuPages($parentData, $select);
foreach ($pages as $p) {
$r = array(
'data' => $p,
'text' => '{name}',
'preHtml' => '',
'postHtml' => '',
'last' => false
);
$class = array();
$class[] = $this->_getBemClass('item');
if ($i == 0) { $class[] = $this->_getBemClass('item--first', 'first'); }
if ($i == count($pages)-1) {
$class[] = $this->_getBemClass('item--last', 'last');
$r['last'] = true;
}
if ($r['data']->getDeviceVisible() != Kwf_Component_Data::DEVICE_VISIBLE_ALL) {
$class[] = $this->_getBemClass('item--'.$r['data']->getDeviceVisible(), $r['data']->getDeviceVisible());
}
$r['class'] = implode(' ', $class);
if (Kwc_Abstract::getFlag($p->componentClass, 'hasIsVisibleDynamic')) {
$r['preHtml'] = '<!-- start '.$p->componentId.' '.$p->componentClass.' -->';
$r['postHtml'] = '<!-- end '.$p->componentId.' '.$p->componentClass.' -->';
}
$ret[] = $r;
$i++;
}
$this->_attachEditableToMenuData($ret, $editableClass);
return $ret;
}
protected function _attachEditableToMenuData(&$menuData, $editableClass)
{
foreach ($this->_getSetting('generators') as $key => $generator) {
if (isset($generator['component'][$key]) && is_instance_of($generator['component'][$key], $editableClass)) {
$c = $this->getData()->getChildComponent('-'.$key);
$c->getComponent()->attachEditableToMenuData($menuData);
}
}
}
}