PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/includes/applications/categories/classes/categories.php

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