Quick Search:

View

Revision:

Diff

Diff from 1181 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/frank/osc-tickets/admin/includes/content/categories.php

Annotated File View

hpdl
1016
1 <?php
2 /*
3   $Id: $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2006 osCommerce
9
10   Released under the GNU General Public License
11 */
12
13   class osC_Content_Categories extends osC_Template {
14
15 /* Private variables */
16
17     var $_module = 'categories',
hpdl
1078
18         $_page_title = HEADING_TITLE,
hpdl
1016
19         $_page_contents = 'categories.php';
20
21 /* Class constructor */
22
23     function osC_Content_Categories() {
24       global $osC_MessageStack, $cPath, $cPath_array, $current_category_id, $osC_CategoryTree;
25
26       if (!isset($_GET['action'])) {
27         $_GET['action'] = '';
28       }
29
30       if (!isset($_GET['page']) || (isset($_GET['page']) && !is_numeric($_GET['page']))) {
31         $_GET['page'] = 1;
32       }
33
34 // check if the categories image directory exists
35       if (is_dir(realpath('../images/categories'))) {
36         if (!is_writeable(realpath('../images/categories'))) {
hpdl
1078
37           $osC_MessageStack->add('header', ERROR_CATEGORIES_IMAGE_DIRECTORY_NOT_WRITEABLE, 'error');
hpdl
1016
38         }
39       } else {
hpdl
1078
40         $osC_MessageStack->add('header', ERROR_CATEGORIES_IMAGE_DIRECTORY_DOES_NOT_EXIST, 'error');
hpdl
1016
41       }
42
43 // calculate category path
44       $cPath = (isset($_GET['cPath']) ? $_GET['cPath'] : '');
45
46       if (!empty($cPath)) {
47         $cPath_array = osc_parse_category_path($cPath);
48         $cPath = implode('_', $cPath_array);
49         $current_category_id = end($cPath_array);
50       } else {
51         $current_category_id = 0;
52       }
53
54       require('includes/classes/category_tree.php');
55       $osC_CategoryTree = new osC_CategoryTree_Admin();
56       $osC_CategoryTree->setSpacerString('&nbsp;', 2);
57
58       if (!isset($_GET['search'])) {
59         $_GET['search'] = '';
60       }
61
62       if (!empty($_GET['action'])) {
63         switch ($_GET['action']) {
64           case 'save_category':
65             $this->_save();
66             break;
67
68           case 'delete_category_confirm':
69             $this->_delete();
70             break;
71
72           case 'move_category_confirm':
73             $this->_move();
74             break;
75         }
76       }
77     }
78
79 /* Private methods */
80
81     function _save() {
82       global $osC_Database, $osC_Language, $osC_MessageStack, $cPath, $current_category_id;
83
84       $error = false;
frank
1181
85       
86       foreach ($osC_Language->getAll() as $l) {
87         if  (empty($_POST['categories_name'][$l['id']])) {
88           $osC_MessageStack->add_session($this->_module, sprintf(ERROR_EMPTY_CATEGORY, $l['name']), 'error');
89           $error = true;
90         }
91       }
hpdl
1016
92
frank
1181
93       if ($error === false) {
94         $osC_Database->startTransaction();
hpdl
1016
95
frank
1181
96         if (isset($_GET['cID']) && is_numeric($_GET['cID'])) {
97           $Qcat = $osC_Database->query('update :table_categories set sort_order = :sort_order, last_modified = now() where categories_id = :categories_id');
98           $Qcat->bindInt(':categories_id', $_GET['cID']);
99         } else {
100           $Qcat = $osC_Database->query('insert into :table_categories (parent_id, sort_order, date_added) values (:parent_id, :sort_order, now())');
101           $Qcat->bindInt(':parent_id', $current_category_id);
102         }
103         $Qcat->bindTable(':table_categories', TABLE_CATEGORIES);
104         $Qcat->bindInt(':sort_order', $_POST['sort_order']);
105         $Qcat->execute();
hpdl
1016
106
frank
1181
107         if ($osC_Database->isError() === false) {
108           $category_id = (isset($_GET['cID']) && is_numeric($_GET['cID'])) ? $_GET['cID'] : $osC_Database->nextID();
hpdl
1016
109
frank
1181
110           foreach ($osC_Language->getAll() as $l) {
111             if (isset($_GET['cID']) && is_numeric($_GET['cID'])) {
112               $Qcd = $osC_Database->query('update :table_categories_description set categories_name = :categories_name where categories_id = :categories_id and language_id = :language_id');
113             } else {
114               $Qcd = $osC_Database->query('insert into :table_categories_description (categories_id, language_id, categories_name) values (:categories_id, :language_id, :categories_name)');
115             }
116             $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
117             $Qcd->bindInt(':categories_id', $category_id);
118             $Qcd->bindInt(':language_id', $l['id']);
119             $Qcd->bindValue(':categories_name', $_POST['categories_name'][$l['id']]);
120             $Qcd->execute();
hpdl
1016
121
frank
1181
122             if ($osC_Database->isError()) {
123               $error = true;
124               break;
125             }
hpdl
1016
126           }
127
frank
1181
128           if ($error === false) {
129             $categories_image = new upload('categories_image', realpath('../' . DIR_WS_IMAGES . 'categories'));
hpdl
1016
130
frank
1181
131             if ($categories_image->exists()) {
132               if ($categories_image->parse() && $categories_image->save()) {
133                 $Qcf = $osC_Database->query('update :table_categories set categories_image = :categories_image where categories_id = :categories_id');
134                 $Qcf->bindTable(':table_categories', TABLE_CATEGORIES);
135                 $Qcf->bindValue(':categories_image', $categories_image->filename);
136                 $Qcf->bindInt(':categories_id', $category_id);
137                 $Qcf->execute();
hpdl
1016
138
frank
1181
139                 if ($osC_Database->isError()) {
140                   $error = true;
141                 }
hpdl
1016
142               }
143             }
144           }
145         }
146
frank
1181
147         if ($error === false) {
148           $osC_Database->commitTransaction();
hpdl
1016
149
frank
1181
150           osC_Cache::clear('categories');
151           osC_Cache::clear('category_tree');
152           osC_Cache::clear('also_purchased');
hpdl
1016
153
frank
1181
154           $osC_MessageStack->add_session($this->_module, SUCCESS_DB_ROWS_UPDATED, 'success');
155         } else {
156           $osC_Database->rollbackTransaction();
157
158           $osC_MessageStack->add_session($this->_module, ERROR_DB_ROWS_NOT_UPDATED, 'error');
159         }
160         
161         osc_redirect(osc_href_link_admin(FILENAME_DEFAULT, $this->_module . '&page=' . $_GET['page'] . '&cPath=' . $cPath . '&cID=' . $category_id));
hpdl
1016
162       } else {
frank
1181
163         osc_redirect(osc_href_link_admin(FILENAME_DEFAULT, $this->_module . '&page=' . $_GET['page'] . '&cPath=' . $cPath));
164       }  
hpdl
1016
165     }
166
167     function _delete() {
168       global $osC_Database, $osC_MessageStack, $osC_CategoryTree, $cPath;
169
170       if (isset($_GET['cID']) && is_numeric($_GET['cID'])) {
171         include('includes/classes/product.php');
172
173         $osC_CategoryTree->setBreadcrumbUsage(false);
174
175         $categories = array_merge(array(array('id' => $_GET['cID'], 'text' => '')), $osC_CategoryTree->getTree($_GET['cID']));
176         $products = array();
177         $products_delete = array();
178
179         foreach ($categories as $c_entry) {
180           $Qproducts = $osC_Database->query('select products_id from :table_products_to_categories where categories_id = :categories_id');
181           $Qproducts->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
182           $Qproducts->bindInt(':categories_id', $c_entry['id']);
183           $Qproducts->execute();
184
185           while ($Qproducts->next()) {
186             $products[$Qproducts->valueInt('products_id')]['categories'][] = $c_entry['id'];
187           }
188         }
189
190         foreach ($products as $key => $value) {
191           $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');
192           $Qcheck->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
193           $Qcheck->bindInt(':products_id', $key);
194           $Qcheck->bindRaw(':categories_id', '("' . implode('", "', $value['categories']) . '")');
195           $Qcheck->execute();
196
197           if ($Qcheck->valueInt('total') < 1) {
198             $products_delete[$key] = $key;
199           }
200         }
201
202         osc_set_time_limit(0);
203
204         foreach ($categories as $c_entry) {
205           osc_remove_category($c_entry['id']);
206         }
207
208         foreach ($products_delete as $id) {
209           osC_Product_Admin::remove($id);
210         }
211
212         osC_Cache::clear('categories');
213         osC_Cache::clear('category_tree');
214         osC_Cache::clear('also_purchased');
215
216         $osC_MessageStack->add_session($this->_module, SUCCESS_DB_ROWS_UPDATED, 'success');
217       }
218
219       osc_redirect(osc_href_link_admin(FILENAME_DEFAULT, $this->_module . '&page=' . $_GET['page'] . '&cPath=' . $cPath . '&search=' . $_GET['search']));
220     }
221
222     function _move() {
223       global $osC_Database, $osC_MessageStack, $cPath;
224
225       $category_array = explode('_', $_POST['move_to_category_id']);
226
227       if (isset($_GET['cID']) && ($_GET['cID'] != end($category_array))) {
228         $path = explode('_', $_POST['move_to_category_id']);
229
230         if (in_array($_GET['cID'], $path)) {
231           $osC_MessageStack->add_session($this->_module, ERROR_CANNOT_MOVE_CATEGORY_TO_PARENT, 'error');
232         } else {
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', $_GET['cID']);
237           $Qupdate->execute();
238
239           if ($Qupdate->affectedRows()) {
240             osC_Cache::clear('categories');
241             osC_Cache::clear('category_tree');
242             osC_Cache::clear('also_purchased');
243
244             $osC_MessageStack->add_session($this->_module, SUCCESS_DB_ROWS_UPDATED, 'success');
245
246             osc_redirect(osc_href_link_admin(FILENAME_DEFAULT, $this->_module . '&page=' . $_GET['page'] . '&cPath=' . $cPath . '&search=' . $_GET['search']));
247           }
248         }
249       }
250
251       osc_redirect(osc_href_link_admin(FILENAME_DEFAULT, $this->_module . '&page=' . $_GET['page'] . '&cPath=' . $cPath . '&search=' . $_GET['search'] . '&cID=' . $_GET['cID']));
252     }
253   }
254 ?>