@@ -48,16 +48,18 @@ public function __construct(
48
48
public function execute (\Iterator $ iterator ): array
49
49
{
50
50
$ tree = [];
51
+ /** @var CategoryInterface $rootCategory */
52
+ $ rootCategory = $ iterator ->current ();
51
53
while ($ iterator ->valid ()) {
52
- /** @var CategoryInterface $category */
53
- $ category = $ iterator ->current ();
54
+ /** @var CategoryInterface $currentCategory */
55
+ $ currentCategory = $ iterator ->current ();
54
56
$ iterator ->next ();
55
- if ($ category -> getIsActive ( )) {
56
- $ pathElements = explode ("/ " , $ category ->getPath ());
57
+ if ($ this -> areParentsActive ( $ currentCategory , $ rootCategory , ( array ) $ iterator )) {
58
+ $ pathElements = explode ("/ " , $ currentCategory ->getPath ());
57
59
if (empty ($ tree )) {
58
60
$ this ->startCategoryFetchLevel = count ($ pathElements ) - 1 ;
59
61
}
60
- $ this ->iteratingCategory = $ category ;
62
+ $ this ->iteratingCategory = $ currentCategory ;
61
63
$ currentLevelTree = $ this ->explodePathToArray ($ pathElements , $ this ->startCategoryFetchLevel );
62
64
if (empty ($ tree )) {
63
65
$ tree = $ currentLevelTree ;
@@ -68,6 +70,34 @@ public function execute(\Iterator $iterator): array
68
70
return $ tree ;
69
71
}
70
72
73
+
74
+ /**
75
+ * Test that all parents of the current category are active
76
+ *
77
+ * Assumes that $categoriesArray are key-pair values and key is the ID of the category
78
+ *
79
+ * @param CategoryInterface $currentCategory
80
+ * @param CategoryInterface $rootCategory
81
+ * @param $categoriesArray
82
+ */
83
+ private function areParentsActive (
84
+ CategoryInterface $ currentCategory ,
85
+ CategoryInterface $ rootCategory ,
86
+ array $ categoriesArray
87
+ ): bool {
88
+ if ($ currentCategory === $ rootCategory ) {
89
+ return true ;
90
+ } elseif (array_key_exists ($ currentCategory ->getParentId (), $ categoriesArray )) {
91
+ return $ this ->areParentsActive (
92
+ $ categoriesArray [$ currentCategory ->getParentId ()],
93
+ $ rootCategory ,
94
+ $ categoriesArray
95
+ );
96
+ } else {
97
+ return false ;
98
+ }
99
+ }
100
+
71
101
/**
72
102
* Merge together complex categories trees
73
103
*
0 commit comments