PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/jongleur/oscommerce
PHP | 336 lines | 251 code | 73 blank | 12 comment | 42 complexity | b5173016ce051d9e57b537e65642a6e9 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 get($id, $language_id = null, $key = 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. if ( !empty($key) && isset($data[$key]) ) {
  28. $data = $data[$key];
  29. }
  30. return $data;
  31. }
  32. public static function getAll($id = null) {
  33. global $osC_Database, $osC_Language, $current_category_id;
  34. if ( !is_numeric($id) ) {
  35. if ( isset($current_category_id) && is_numeric($current_category_id) ) {
  36. $id = $current_category_id;
  37. } else {
  38. $id = 0;
  39. }
  40. }
  41. $result = array('entries' => array());
  42. $Qcategories = $osC_Database->query('select c.*, cd.categories_name from :table_categories c, :table_categories_description cd where c.categories_id = cd.categories_id and cd.language_id = :language_id and c.parent_id = :parent_id order by c.sort_order, cd.categories_name');
  43. $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
  44. $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  45. $Qcategories->bindInt(':language_id', $osC_Language->getID());
  46. $Qcategories->bindInt(':parent_id', $id);
  47. $Qcategories->execute();
  48. while ( $Qcategories->next() ) {
  49. $result['entries'][] = $Qcategories->toArray();
  50. }
  51. $result['total'] = $Qcategories->numberOfRows();
  52. $Qcategories->freeResult();
  53. return $result;
  54. }
  55. public static function find($search, $id = null) {
  56. global $osC_Database, $osC_Language, $current_category_id;
  57. if ( !is_numeric($id) ) {
  58. if ( isset($current_category_id) && is_numeric($current_category_id) ) {
  59. $id = $current_category_id;
  60. } else {
  61. $id = 0;
  62. }
  63. }
  64. $osC_CategoryTree = new osC_CategoryTree_Admin();
  65. $osC_CategoryTree->setRootCategoryID($id);
  66. $categories = array();
  67. $Qcategories = $osC_Database->query('select c.categories_id from :table_categories c, :table_categories_description cd where c.categories_id = cd.categories_id and cd.language_id = :language_id and (cd.categories_name like :categories_name)');
  68. $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
  69. $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  70. $Qcategories->bindInt(':language_id', $osC_Language->getID());
  71. $Qcategories->bindValue(':categories_name', '%' . $search . '%');
  72. $Qcategories->execute();
  73. while ( $Qcategories->next() ) {
  74. if ( $Qcategories->valueInt('categories_id') != $id ) {
  75. $category_path = $osC_CategoryTree->getPathArray($Qcategories->valueInt('categories_id'));
  76. $top_category_id = $category_path[0]['id'];
  77. if ( !in_array($top_category_id, $categories) ) {
  78. $categories[] = $top_category_id;
  79. }
  80. }
  81. }
  82. $result = array('entries' => array());
  83. $Qcategories = $osC_Database->query('select c.*, cd.categories_name from :table_categories c, :table_categories_description cd where c.categories_id = cd.categories_id and cd.language_id = :language_id and c.categories_id in :categories_id order by c.sort_order, cd.categories_name');
  84. $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
  85. $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  86. $Qcategories->bindInt(':language_id', $osC_Language->getID());
  87. $Qcategories->bindRaw(':categories_id', '("' . implode('", "', $categories) . '")');
  88. $Qcategories->execute();
  89. while ( $Qcategories->next() ) {
  90. $result['entries'][] = $Qcategories->toArray();
  91. }
  92. $result['total'] = $Qcategories->numberOfRows();
  93. $Qcategories->freeResult();
  94. return $result;
  95. }
  96. public static function save($id = null, $data) {
  97. global $osC_Database, $osC_Language;
  98. $category_id = '';
  99. $error = false;
  100. $osC_Database->startTransaction();
  101. if ( is_numeric($id) ) {
  102. $Qcat = $osC_Database->query('update :table_categories set sort_order = :sort_order, last_modified = now() where categories_id = :categories_id');
  103. $Qcat->bindInt(':categories_id', $id);
  104. } else {
  105. $Qcat = $osC_Database->query('insert into :table_categories (parent_id, sort_order, date_added) values (:parent_id, :sort_order, now())');
  106. $Qcat->bindInt(':parent_id', $data['parent_id']);
  107. }
  108. $Qcat->bindTable(':table_categories', TABLE_CATEGORIES);
  109. $Qcat->bindInt(':sort_order', $data['sort_order']);
  110. $Qcat->setLogging($_SESSION['module'], $id);
  111. $Qcat->execute();
  112. if ( !$osC_Database->isError() ) {
  113. $category_id = (is_numeric($id)) ? $id : $osC_Database->nextID();
  114. foreach ( $osC_Language->getAll() as $l ) {
  115. if ( is_numeric($id) ) {
  116. $Qcd = $osC_Database->query('update :table_categories_description set categories_name = :categories_name where categories_id = :categories_id and language_id = :language_id');
  117. } else {
  118. $Qcd = $osC_Database->query('insert into :table_categories_description (categories_id, language_id, categories_name) values (:categories_id, :language_id, :categories_name)');
  119. }
  120. $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  121. $Qcd->bindInt(':categories_id', $category_id);
  122. $Qcd->bindInt(':language_id', $l['id']);
  123. $Qcd->bindValue(':categories_name', $data['name'][$l['id']]);
  124. $Qcd->setLogging($_SESSION['module'], $category_id);
  125. $Qcd->execute();
  126. if ( $osC_Database->isError() ) {
  127. $error = true;
  128. break;
  129. }
  130. }
  131. if ( $error === false ) {
  132. $categories_image = new upload($data['image'], realpath('../' . DIR_WS_IMAGES . 'categories'));
  133. if ( $categories_image->exists() && $categories_image->parse() && $categories_image->save() ) {
  134. $Qcf = $osC_Database->query('update :table_categories set categories_image = :categories_image where categories_id = :categories_id');
  135. $Qcf->bindTable(':table_categories', TABLE_CATEGORIES);
  136. $Qcf->bindValue(':categories_image', $categories_image->filename);
  137. $Qcf->bindInt(':categories_id', $category_id);
  138. $Qcf->setLogging($_SESSION['module'], $category_id);
  139. $Qcf->execute();
  140. if ( $osC_Database->isError() ) {
  141. $error = true;
  142. }
  143. }
  144. }
  145. }
  146. if ( $error === false ) {
  147. $osC_Database->commitTransaction();
  148. osC_Cache::clear('categories');
  149. osC_Cache::clear('category_tree');
  150. osC_Cache::clear('also_purchased');
  151. return true;
  152. }
  153. $osC_Database->rollbackTransaction();
  154. return false;
  155. }
  156. public static function delete($id) {
  157. global $osC_Database, $osC_CategoryTree;
  158. if ( is_numeric($id) ) {
  159. $osC_CategoryTree->setBreadcrumbUsage(false);
  160. $categories = array_merge(array(array('id' => $id, 'text' => '')), $osC_CategoryTree->getArray($id));
  161. $products = array();
  162. $products_delete = array();
  163. foreach ( $categories as $category ) {
  164. $Qproducts = $osC_Database->query('select products_id from :table_products_to_categories where categories_id = :categories_id');
  165. $Qproducts->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
  166. $Qproducts->bindInt(':categories_id', $category['id']);
  167. $Qproducts->execute();
  168. while ( $Qproducts->next() ) {
  169. $products[$Qproducts->valueInt('products_id')]['categories'][] = $category['id'];
  170. }
  171. }
  172. foreach ( $products as $key => $value ) {
  173. $Qcheck = $osC_Database->query('select categories_id from :table_products_to_categories where products_id = :products_id and categories_id not in :categories_id limit 1');
  174. $Qcheck->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
  175. $Qcheck->bindInt(':products_id', $key);
  176. $Qcheck->bindRaw(':categories_id', '("' . implode('", "', $value['categories']) . '")');
  177. $Qcheck->execute();
  178. if ( $Qcheck->numberOfRows() === 0 ) {
  179. $products_delete[$key] = $key;
  180. }
  181. }
  182. osc_set_time_limit(0);
  183. foreach ( $categories as $category) {
  184. $osC_Database->startTransaction();
  185. $Qimage = $osC_Database->query('select categories_image from :table_categories where categories_id = :categories_id');
  186. $Qimage->bindTable(':table_categories', TABLE_CATEGORIES);
  187. $Qimage->bindInt(':categories_id', $category['id']);
  188. $Qimage->execute();
  189. $Qc = $osC_Database->query('delete from :table_categories where categories_id = :categories_id');
  190. $Qc->bindTable(':table_categories', TABLE_CATEGORIES);
  191. $Qc->bindInt(':categories_id', $category['id']);
  192. $Qc->setLogging($_SESSION['module'], $id);
  193. $Qc->execute();
  194. if ( !$osC_Database->isError() ) {
  195. $Qcd = $osC_Database->query('delete from :table_categories_description where categories_id = :categories_id');
  196. $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  197. $Qcd->bindInt(':categories_id', $category['id']);
  198. $Qcd->setLogging($_SESSION['module'], $id);
  199. $Qcd->execute();
  200. if ( !$osC_Database->isError() ) {
  201. $Qp2c = $osC_Database->query('delete from :table_products_to_categories where categories_id = :categories_id');
  202. $Qp2c->bindTable(':table_products_to_categories', TABLE_PRODUCTS_TO_CATEGORIES);
  203. $Qp2c->bindInt(':categories_id', $category['id']);
  204. $Qp2c->setLogging($_SESSION['module'], $id);
  205. $Qp2c->execute();
  206. if ( !$osC_Database->isError() ) {
  207. $osC_Database->commitTransaction();
  208. osC_Cache::clear('categories');
  209. osC_Cache::clear('category_tree');
  210. osC_Cache::clear('also_purchased');
  211. if ( !osc_empty($Qimage->value('categories_image')) ) {
  212. $Qcheck = $osC_Database->query('select count(*) as total from :table_categories where categories_image = :categories_image');
  213. $Qcheck->bindTable(':table_categories', TABLE_CATEGORIES);
  214. $Qcheck->bindValue(':categories_image', $Qimage->value('categories_image'));
  215. $Qcheck->execute();
  216. if ( $Qcheck->numberOfRows() === 0 ) {
  217. if (file_exists(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')))) {
  218. @unlink(realpath('../' . DIR_WS_IMAGES . 'categories/' . $Qimage->value('categories_image')));
  219. }
  220. }
  221. }
  222. } else {
  223. $osC_Database->rollbackTransaction();
  224. }
  225. } else {
  226. $osC_Database->rollbackTransaction();
  227. }
  228. } else {
  229. $osC_Database->rollbackTransaction();
  230. }
  231. }
  232. foreach ( $products_delete as $id ) {
  233. osC_Products_Admin::remove($id);
  234. }
  235. osC_Cache::clear('categories');
  236. osC_Cache::clear('category_tree');
  237. osC_Cache::clear('also_purchased');
  238. return true;
  239. }
  240. return false;
  241. }
  242. public static function move($id, $new_id) {
  243. global $osC_Database;
  244. $category_array = explode('_', $new_id);
  245. if ( in_array($id, $category_array)) {
  246. return false;
  247. }
  248. $Qupdate = $osC_Database->query('update :table_categories set parent_id = :parent_id, last_modified = now() where categories_id = :categories_id');
  249. $Qupdate->bindTable(':table_categories', TABLE_CATEGORIES);
  250. $Qupdate->bindInt(':parent_id', end($category_array));
  251. $Qupdate->bindInt(':categories_id', $id);
  252. $Qupdate->setLogging($_SESSION['module'], $id);
  253. $Qupdate->execute();
  254. osC_Cache::clear('categories');
  255. osC_Cache::clear('category_tree');
  256. osC_Cache::clear('also_purchased');
  257. return true;
  258. }
  259. }
  260. ?>