-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategoryManager.class.php
More file actions
169 lines (91 loc) · 3.58 KB
/
categoryManager.class.php
File metadata and controls
169 lines (91 loc) · 3.58 KB
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
<?php
class categoryManager {
private $categoryArray;
private $categoryArrayByCategoryId;
private $idKey;
private $parentIdKey;
private $nameKey;
function __construct($categoryArray, $idKey, $parentKey, $nameKey) {
$this->categoryArray = $categoryArray;
$this->idKey = $idKey;
$this->parentIdKey = $parentKey;
$this->nameKey = $nameKey;
$categoryArrayByCategoryId = array();
foreach ($this->categoryArray as $category) {
$categoryArrayByCategoryId[$category[$this->idKey]][$this->nameKey] = $category[$this->nameKey];
$categoryArrayByCategoryId[$category[$this->idKey]][$this->parentIdKey] = $category[$this->parentIdKey];
}
$this->categoryArrayByCategoryId = $categoryArrayByCategoryId;
}
public function getChildCategories($id) {
$childCategoryArray = array();
foreach($this->categoryArray as $category) {
if ($category[$this->parentIdKey] == $id) {
array_push($childCategoryArray,$category);
}
}
return $childCategoryArray;
}
public function getChildCategoriesRecurring($id, $childCategoryArray = array()) {
foreach($this->categoryArray as $category) {
if ($category[$this->parentIdKey] == $id) {
array_push($childCategoryArray, $category[$this->idKey]);
$childCategoryArray = $this->getChildCategoriesRecurring($category[$this->idKey], $childCategoryArray);
}
}
return $childCategoryArray;
}
public function getCategoryParentID($id) {
if (isset($this->categoryArrayByCategoryId[$id][$this->parentIdKey])) {
return $this->categoryArrayByCategoryId[$id][$this->parentIdKey];
} else {
return 0;
}
}
public function getCategoryName($id) {
if (isset($this->categoryArrayByCategoryId[$id][$this->nameKey])) {
return $this->categoryArrayByCategoryId[$id][$this->nameKey];
} else {
return 0;
}
}
public function makeCategoryTreeArray($sortedArray = array(), $currentCategory = 0, $level = 0) {
$tmpArray = array();
foreach($this->categoryArray as $category) {
if ($category[$this->parentIdKey] == $currentCategory) {
array_push($tmpArray,$category);
}
}
if (count($tmpArray) > 0) {
foreach($tmpArray as $category) {
$arraySize = count($sortedArray);
$sortedArray[$arraySize][$this->nameKey] = $category[$this->nameKey];
$sortedArray[$arraySize][$this->idKey] = $category[$this->idKey];
$sortedArray[$arraySize]['level'] = $level;
$sortedArray = $this->makeCategoryTreeArray($sortedArray,$category[$this->idKey],($level+1));
}
}
return $sortedArray;
}
public function getCategoryPath($categoryID) {
$treeArray = array();
$i = 0;
$treeArray[$i]['name'] = $this->getCategoryName($categoryID);
$treeArray[$i]['id'] = $categoryID;
while ($parentID = $this->getCategoryParentID($categoryID)) {
$i++;
$treeArray[$i]['name'] = $this->getCategoryName($parentID);
$treeArray[$i]['id'] = $parentID;
$categoryID = $parentID;
}
$z = 0;
for ($i = (count($treeArray)-1); $i>=0; $i--) {
$sortedTreeArray[$z]['name'] = $treeArray[$i]['name'];
$sortedTreeArray[$z]['id'] = $treeArray[$i]['id'];
$sortedTreeArray[$z]['level'] = (count($treeArray)-$i);
$z++;
}
return $sortedTreeArray;
}
}
?>