hpdl
|
1209
|
1
|
<?php
|
|
2
|
/*
|
|
3
|
$Id: $
|
|
4
|
|
|
5
|
osCommerce, Open Source E-Commerce Solutions
|
|
6
|
http://www.oscommerce.com
|
|
7
|
|
hpdl
|
1374
|
8
|
Copyright (c) 2007 osCommerce
|
hpdl
|
1209
|
9
|
|
hpdl
|
1498
|
10
|
This program is free software; you can redistribute it and/or modify
|
|
11
|
it under the terms of the GNU General Public License v2 (1991)
|
|
12
|
as published by the Free Software Foundation.
|
hpdl
|
1209
|
13
|
*/
|
|
14
|
|
|
15
|
class osC_Categories_Admin {
|
|
16
|
function getData($id, $language_id = null) {
|
|
17
|
global $osC_Database, $osC_Language, $osC_CategoryTree;
|
|
18
|
|
|
19
|
if ( empty($language_id) ) {
|
|
20
|
$language_id = $osC_Language->getID();
|
|
21
|
}
|
|
22
|
|
|
23
|
$Qcategories = $osC_Database->query('select c.*, cd.* from :table_categories c, :table_categories_description cd where c.categories_id = :categories_id and c.categories_id = cd.categories_id and cd.language_id = :language_id');
|
|
24
|
$Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
25
|
$Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
|
|
26
|
$Qcategories->bindInt(':categories_id', $id);
|
|
27
|
$Qcategories->bindInt(':language_id', $language_id);
|
|
28
|
$Qcategories->execute();
|
|
29
|
|
|
30
|
$data = $Qcategories->toArray();
|
|
31
|
|
|
32
|
$data['childs_count'] = sizeof($osC_CategoryTree->getChildren($Qcategories->valueInt('categories_id'), $dummy = array()));
|
|
33
|
$data['products_count'] = $osC_CategoryTree->getNumberOfProducts($Qcategories->valueInt('categories_id'));
|
|
34
|
|
|
35
|
$Qcategories->freeResult();
|
|
36
|
|
|
37
|
return $data;
|
|
38
|
}
|
|
39
|
|
|
40
|
function save($id = null, $data) {
|
|
41
|
global $osC_Database, $osC_Language;
|
|
42
|
|
|
43
|
$category_id = '';
|
|
44
|
$error = false;
|
|
45
|
|
|
46
|
$osC_Database->startTransaction();
|
|
47
|
|
|
48
|
if ( is_numeric($id) ) {
|
|
49
|
$Qcat = $osC_Database->query('update :table_categories set sort_order = :sort_order, last_modified = now() where categories_id = :categories_id');
|
|
50
|
$Qcat->bindInt(':categories_id', $id);
|
|
51
|
} else {
|
|
52
|
$Qcat = $osC_Database->query('insert into :table_categories (parent_id, sort_order, date_added) values (:parent_id, :sort_order, now())');
|
|
53
|
$Qcat->bindInt(':parent_id', $data['parent_id']);
|
|
54
|
}
|
|
55
|
|
|
56
|
$Qcat->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
57
|
$Qcat->bindInt(':sort_order', $data['sort_order']);
|
hpdl
|
1374
|
58
|
$Qcat->setLogging($_SESSION['module'], $id);
|
hpdl
|
1209
|
59
|
$Qcat->execute();
|
|
60
|
|
|
61
|
if ( !$osC_Database->isError() ) {
|
|
62
|
$category_id = (is_numeric($id)) ? $id : $osC_Database->nextID();
|
|
63
|
|
|
64
|
foreach ($osC_Language->getAll() as $l) {
|
|
65
|
if ( is_numeric($id) ) {
|
|
66
|
$Qcd = $osC_Database->query('update :table_categories_description set categories_name = :categories_name where categories_id = :categories_id and language_id = :language_id');
|
|
67
|
} else {
|
|
68
|
$Qcd = $osC_Database->query('insert into :table_categories_description (categories_id, language_id, categories_name) values (:categories_id, :language_id, :categories_name)');
|
|
69
|
}
|
|
70
|
|
|
71
|
$Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
|
|
72
|
$Qcd->bindInt(':categories_id', $category_id);
|
|
73
|
$Qcd->bindInt(':language_id', $l['id']);
|
|
74
|
$Qcd->bindValue(':categories_name', $data['name'][$l['id']]);
|
hpdl
|
1374
|
75
|
$Qcd->setLogging($_SESSION['module'], $category_id);
|
hpdl
|
1209
|
76
|
$Qcd->execute();
|
|
77
|
|
|
78
|
if ( $osC_Database->isError() ) {
|
|
79
|
$error = true;
|
|
80
|
break;
|
|
81
|
}
|
|
82
|
}
|
|
83
|
|
|
84
|
if ( $error === false ) {
|
|
85
|
$categories_image = new upload($data['image'], realpath('../' . DIR_WS_IMAGES . 'categories'));
|
|
86
|
|
|
87
|
if ( $categories_image->exists() && $categories_image->parse() && $categories_image->save() ) {
|
|
88
|
$Qcf = $osC_Database->query('update :table_categories set categories_image = :categories_image where categories_id = :categories_id');
|
|
89
|
$Qcf->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
90
|
$Qcf->bindValue(':categories_image', $categories_image->filename);
|
|
91
|
$Qcf->bindInt(':categories_id', $category_id);
|
hpdl
|
1374
|
92
|
$Qcf->setLogging($_SESSION['module'], $category_id);
|
hpdl
|
1209
|
93
|
$Qcf->execute();
|
|
94
|
|
|
95
|
if ( $osC_Database->isError() ) {
|
|
96
|
$error = true;
|
|
97
|
}
|
|
98
|
}
|
|
99
|
}
|
|
100
|
}
|
|
101
|
|
|
102
|
if ( $error === false ) {
|
|
103
|
$osC_Database->commitTransaction();
|
|
104
|
|
|
105
|
osC_Cache::clear('categories');
|
|
106
|
osC_Cache::clear('category_tree');
|
|
107
|
osC_Cache::clear('also_purchased');
|
|
108
|
|
|
109
|
return true;
|
|
110
|
}
|
|
111
|
|
|
112
|
$osC_Database->rollbackTransaction();
|
|
113
|
|
|
114
|
return false;
|
|
115
|
}
|
|
116
|
|
|
117
|
function delete($id) {
|
|
118
|
global $osC_Database, $osC_CategoryTree;
|
|
119
|
|
|
120
|
if ( is_numeric($id) ) {
|
|
121
|
$osC_CategoryTree->setBreadcrumbUsage(false);
|
|
122
|
|
hpdl
|
1862
|
123
|
$categories = array_merge(array(array('id' => $id, 'text' => '')), $osC_CategoryTree->getArray($id));
|
hpdl
|
1209
|
124
|
$products = array();
|
|
125
|
$products_delete = array();
|
|
126
|
|
|
127
|
foreach ($categories as $c_entry) {
|
|
128
|
$Qproducts = $osC_Database->query('select products_id from :table_products_to_categories where categories_id = :categories_id');
|
|
129
|
$Qproducts->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
|
|
130
|
$Qproducts->bindInt(':categories_id', $c_entry['id']);
|
|
131
|
$Qproducts->execute();
|
|
132
|
|
|
133
|
while ($Qproducts->next()) {
|
|
134
|
$products[$Qproducts->valueInt('products_id')]['categories'][] = $c_entry['id'];
|
|
135
|
}
|
|
136
|
}
|
|
137
|
|
|
138
|
foreach ($products as $key => $value) {
|
|
139
|
$Qcheck = $osC_Database->query('select count(*) as total from :table_products_to_categories where products_id = :products_id and categories_id not in :categories_id');
|
|
140
|
$Qcheck->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
|
|
141
|
$Qcheck->bindInt(':products_id', $key);
|
|
142
|
$Qcheck->bindRaw(':categories_id', '("' . implode('", "', $value['categories']) . '")');
|
|
143
|
$Qcheck->execute();
|
|
144
|
|
|
145
|
if ($Qcheck->valueInt('total') < 1) {
|
|
146
|
$products_delete[$key] = $key;
|
|
147
|
}
|
|
148
|
}
|
|
149
|
|
|
150
|
osc_set_time_limit(0);
|
|
151
|
|
|
152
|
foreach ($categories as $c_entry) {
|
|
153
|
$osC_Database->startTransaction();
|
|
154
|
|
|
155
|
$Qimage = $osC_Database->query('select categories_image from :table_categories where categories_id = :categories_id');
|
|
156
|
$Qimage->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
157
|
$Qimage->bindInt(':categories_id', $c_entry['id']);
|
|
158
|
$Qimage->execute();
|
|
159
|
|
|
160
|
$Qc = $osC_Database->query('delete from :table_categories where categories_id = :categories_id');
|
|
161
|
$Qc->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
162
|
$Qc->bindInt(':categories_id', $c_entry['id']);
|
hpdl
|
1374
|
163
|
$Qc->setLogging($_SESSION['module'], $id);
|
hpdl
|
1209
|
164
|
$Qc->execute();
|
|
165
|
|
|
166
|
if ( !$osC_Database->isError() ) {
|
|
167
|
$Qcd = $osC_Database->query('delete from :table_categories_description where categories_id = :categories_id');
|
|
168
|
$Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
|
|
169
|
$Qcd->bindInt(':categories_id', $c_entry['id']);
|
hpdl
|
1374
|
170
|
$Qcd->setLogging($_SESSION['module'], $id);
|
hpdl
|
1209
|
171
|
$Qcd->execute();
|
|
172
|
|
|
173
|
if ( !$osC_Database->isError() ) {
|
|
174
|
$Qp2c = $osC_Database->query('delete from :table_products_to_categories where categories_id = :categories_id');
|
|
175
|
$Qp2c->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
|
|
176
|
$Qp2c->bindInt(':categories_id', $c_entry['id']);
|
hpdl
|
1374
|
177
|
$Qp2c->setLogging($_SESSION['module'], $id);
|
hpdl
|
1209
|
178
|
$Qp2c->execute();
|
|
179
|
|
|
180
|
if ( !$osC_Database->isError() ) {
|
|
181
|
$osC_Database->commitTransaction();
|
|
182
|
|
|
183
|
osC_Cache::clear('categories');
|
|
184
|
osC_Cache::clear('category_tree');
|
|
185
|
osC_Cache::clear('also_purchased');
|
|
186
|
|
|
187
|
if ( !osc_empty($Qimage->value('categories_image')) ) {
|
|
188
|
$Qcheck = $osC_Database->query('select count(*) as total from :table_categories where categories_image = :categories_image');
|
|
189
|
$Qcheck->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
190
|
$Qcheck->bindValue(':categories_image', $Qimage->value('categories_image'));
|
|
191
|
$Qcheck->execute();
|
|
192
|
|
|
193
|
if ( $Qcheck->numberOfRows() === 0 ) {
|
|
194
|
if (file_exists(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')))) {
|
|
195
|
@unlink(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')));
|
|
196
|
}
|
|
197
|
}
|
|
198
|
}
|
|
199
|
} else {
|
|
200
|
$osC_Database->rollbackTransaction();
|
|
201
|
}
|
|
202
|
} else {
|
|
203
|
$osC_Database->rollbackTransaction();
|
|
204
|
}
|
|
205
|
} else {
|
|
206
|
$osC_Database->rollbackTransaction();
|
|
207
|
}
|
|
208
|
}
|
|
209
|
|
|
210
|
foreach ($products_delete as $id) {
|
|
211
|
osC_Products_Admin::remove($id);
|
|
212
|
}
|
|
213
|
|
|
214
|
osC_Cache::clear('categories');
|
|
215
|
osC_Cache::clear('category_tree');
|
|
216
|
osC_Cache::clear('also_purchased');
|
|
217
|
|
|
218
|
return true;
|
|
219
|
}
|
|
220
|
|
|
221
|
return false;
|
|
222
|
}
|
|
223
|
|
|
224
|
function move($id, $new_id) {
|
|
225
|
global $osC_Database;
|
|
226
|
|
|
227
|
$category_array = explode('_', $new_id);
|
|
228
|
|
|
229
|
if ( in_array($id, $category_array)) {
|
|
230
|
return false;
|
|
231
|
}
|
|
232
|
|
|
233
|
$Qupdate = $osC_Database->query('update :table_categories set parent_id = :parent_id, last_modified = now() where categories_id = :categories_id');
|
|
234
|
$Qupdate->bindTable(':table_categories', TABLE_CATEGORIES);
|
|
235
|
$Qupdate->bindInt(':parent_id', end($category_array));
|
|
236
|
$Qupdate->bindInt(':categories_id', $id);
|
hpdl
|
1374
|
237
|
$Qupdate->setLogging($_SESSION['module'], $id);
|
hpdl
|
1209
|
238
|
$Qupdate->execute();
|
|
239
|
|
|
240
|
osC_Cache::clear('categories');
|
|
241
|
osC_Cache::clear('category_tree');
|
|
242
|
osC_Cache::clear('also_purchased');
|
|
243
|
|
|
244
|
return true;
|
|
245
|
}
|
|
246
|
}
|
|
247
|
?>
|