Quick Search:

View

Revision:

Diff

Diff from 978 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/tags/oscommerce-3.0a3/admin/categories.php

Annotated File View

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