1
|
<?php
|
2
|
/* --------------------------------------------------------------
|
3
|
CategoriesIndex.inc.php 2014-10-18 tt
|
4
|
Gambio GmbH
|
5
|
http://www.gambio.de
|
6
|
Copyright (c) 2012 Gambio GmbH
|
7
|
Released under the GNU General Public License (Version 2)
|
8
|
[http://www.gnu.org/licenses/gpl-2.0.html]
|
9
|
--------------------------------------------------------------*/
|
10
|
|
11
|
class CategoriesIndex
|
12
|
{
|
13
|
|
14
|
/*
|
15
|
* constructor
|
16
|
*/
|
17
|
function CategoriesIndex()
|
18
|
{
|
19
|
|
20
|
}
|
21
|
|
22
|
|
23
|
|
24
|
function build_categories_index($p_products_id)
|
25
|
{
|
26
|
# get categories_ids the products_id is linked to
|
27
|
$t_sql = "SELECT categories_id FROM products_to_categories WHERE products_id='".(int)$p_products_id."'";
|
28
|
$t_query = xtc_db_query($t_sql);
|
29
|
|
30
|
$t_categories_id_array = array();
|
31
|
|
32
|
# collect category's parent_ids from parent tree
|
33
|
for($i=0; $i<xtc_db_num_rows($t_query); $i++)
|
34
|
{
|
35
|
$t_row = xtc_db_fetch_array($t_query);
|
36
|
$t_parent_id_array = $this->get_categories_parents_array($t_row['categories_id']);
|
37
|
|
38
|
if($t_parent_id_array !== false)
|
39
|
{
|
40
|
$t_categories_id_array[] = $t_row['categories_id'];
|
41
|
$t_categories_id_array = array_merge($t_categories_id_array, $t_parent_id_array);
|
42
|
}
|
43
|
}
|
44
|
|
45
|
sort($t_categories_id_array); # sort array for cleaning
|
46
|
$t_categories_id_array = array_unique($t_categories_id_array); # delete doubled categories_ids
|
47
|
$t_categories_id_array = array_values($t_categories_id_array); # close key gaps after deleting duplicates
|
48
|
|
49
|
|
50
|
# build index string
|
51
|
$t_index_field = '';
|
52
|
for($i=0; $i<sizeof($t_categories_id_array); $i++)
|
53
|
{
|
54
|
$t_index_field .= '-'.$t_categories_id_array[$i].'-';
|
55
|
}
|
56
|
|
57
|
# declare data_object for saving
|
58
|
$coo_index_data_object = false;
|
59
|
|
60
|
# check for existing index
|
61
|
//$coo_data_object_group = MainFactory::create_object('GMDataObjectGroup', array('categories_index', array('products_id' => $p_products_id)));
|
62
|
//$t_data_object_array = $coo_data_object_group->get_data_objects_array();
|
63
|
|
64
|
$t_sql = "SELECT products_id FROM categories_index WHERE products_id='".(int)$p_products_id."'";
|
65
|
$t_query = xtc_db_query($t_sql);
|
66
|
|
67
|
if(xtc_db_num_rows($t_query) > 0)
|
68
|
{
|
69
|
# existing index found
|
70
|
//$coo_index_data_object = $t_data_object_array[0];
|
71
|
|
72
|
$t_sql = "UPDATE categories_index SET categories_index='".$t_index_field."' WHERE products_id='".(int)$p_products_id."'";
|
73
|
$t_query = xtc_db_query($t_sql);
|
74
|
|
75
|
}
|
76
|
else
|
77
|
{
|
78
|
# no index found. create new data object
|
79
|
//$coo_index_data_object = MainFactory::create_object('GMDataObject', array('categories_index'));
|
80
|
//$coo_index_data_object->set_keys(array('products_id' => false));
|
81
|
//$coo_index_data_object->set_data_value('products_id', $p_products_id);
|
82
|
|
83
|
$t_sql = "INSERT INTO categories_index SET categories_index='".$t_index_field."', products_id='".(int)$p_products_id."'";
|
84
|
$t_query = xtc_db_query($t_sql);
|
85
|
|
86
|
}
|
87
|
|
88
|
# save built index
|
89
|
//$coo_index_data_object->set_data_value('categories_index', $t_index_field);
|
90
|
//$coo_index_data_object->save_body_data();
|
91
|
|
92
|
// Unset objects to prevent out of memory error
|
93
|
unset($coo_data_object_group, $coo_index_data_object);
|
94
|
}
|
95
|
|
96
|
|
97
|
|
98
|
function get_categories_parents_array($p_categories_id)
|
99
|
{
|
100
|
$t_output_array = array();
|
101
|
|
102
|
if($p_categories_id == 0)
|
103
|
{
|
104
|
# categories_id is root and has no parents. return empty array.
|
105
|
return $t_output_array;
|
106
|
}
|
107
|
|
108
|
# get category's status and parent_id
|
109
|
//$coo_data_object = MainFactory::create_object('GMDataObject', array('categories', array('categories_id' => $p_categories_id)));
|
110
|
|
111
|
$t_sql = "SELECT categories_status,parent_id FROM categories WHERE categories_id='".(int)$p_categories_id."'";
|
112
|
$t_query = xtc_db_query($t_sql);
|
113
|
|
114
|
$t_row = xtc_db_fetch_array($t_query);
|
115
|
|
116
|
if($t_row['categories_status'] == '0')
|
117
|
{
|
118
|
# cancel recursion with false on inactive category
|
119
|
return false;
|
120
|
}
|
121
|
|
122
|
$t_parent_id = $t_row['parent_id'];
|
123
|
$t_output_array[] = $t_parent_id;
|
124
|
|
125
|
if($t_parent_id != 0)
|
126
|
{
|
127
|
# get more parents, if category is not root
|
128
|
$t_parent_id_array = $this->get_categories_parents_array($t_parent_id);
|
129
|
if($t_parent_id_array === false)
|
130
|
{
|
131
|
# cancel recursion with false on inactive category
|
132
|
return false;
|
133
|
}
|
134
|
# merge category's parent tree to categories_id
|
135
|
$t_output_array = array_merge($t_output_array, $t_parent_id_array);
|
136
|
}
|
137
|
return $t_output_array;
|
138
|
}
|
139
|
}
|
140
|
?>
|