PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/osCommerce/OM/Core/Site/Admin/Application/categories/classes/categories.php

https://github.com/azouts/oscommerce
PHP | 270 lines | 195 code | 63 blank | 12 comment | 37 complexity | 83d189c80fa556efd944d5a5fe7f6690 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');
  43. if ( $id > 0 ) {
  44. $Qcategories->appendQuery('c.parent_id = :parent_id');
  45. $Qcategories->bindInt(':parent_id', $id);
  46. } else {
  47. $Qcategories->appendQuery('c.parent_id is null');
  48. }
  49. $Qcategories->appendQuery('order by c.sort_order, cd.categories_name');
  50. $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
  51. $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  52. $Qcategories->bindInt(':language_id', $osC_Language->getID());
  53. $Qcategories->execute();
  54. while ( $Qcategories->next() ) {
  55. $result['entries'][] = $Qcategories->toArray();
  56. }
  57. $result['total'] = $Qcategories->numberOfRows();
  58. $Qcategories->freeResult();
  59. return $result;
  60. }
  61. public static function find($search, $id = null) {
  62. global $osC_Database, $osC_Language, $current_category_id;
  63. if ( !is_numeric($id) ) {
  64. if ( isset($current_category_id) && is_numeric($current_category_id) ) {
  65. $id = $current_category_id;
  66. } else {
  67. $id = 0;
  68. }
  69. }
  70. $osC_CategoryTree = new osC_CategoryTree_Admin();
  71. $osC_CategoryTree->setRootCategoryID($id);
  72. $categories = array();
  73. $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)');
  74. $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
  75. $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  76. $Qcategories->bindInt(':language_id', $osC_Language->getID());
  77. $Qcategories->bindValue(':categories_name', '%' . $search . '%');
  78. $Qcategories->execute();
  79. while ( $Qcategories->next() ) {
  80. if ( $Qcategories->valueInt('categories_id') != $id ) {
  81. $category_path = $osC_CategoryTree->getPathArray($Qcategories->valueInt('categories_id'));
  82. $top_category_id = $category_path[0]['id'];
  83. if ( !in_array($top_category_id, $categories) ) {
  84. $categories[] = $top_category_id;
  85. }
  86. }
  87. }
  88. $result = array('entries' => array());
  89. $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');
  90. $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
  91. $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  92. $Qcategories->bindInt(':language_id', $osC_Language->getID());
  93. $Qcategories->bindRaw(':categories_id', '("' . implode('", "', $categories) . '")');
  94. $Qcategories->execute();
  95. while ( $Qcategories->next() ) {
  96. $result['entries'][] = $Qcategories->toArray();
  97. }
  98. $result['total'] = $Qcategories->numberOfRows();
  99. $Qcategories->freeResult();
  100. return $result;
  101. }
  102. public static function save($id = null, $data) {
  103. global $osC_Database, $osC_Language;
  104. $category_id = '';
  105. $error = false;
  106. $osC_Database->startTransaction();
  107. if ( is_numeric($id) ) {
  108. $Qcat = $osC_Database->query('update :table_categories set sort_order = :sort_order, last_modified = now() where categories_id = :categories_id');
  109. $Qcat->bindInt(':categories_id', $id);
  110. } else {
  111. $Qcat = $osC_Database->query('insert into :table_categories (parent_id, sort_order, date_added) values (:parent_id, :sort_order, now())');
  112. if ( $data['parent_id'] > 0 ) {
  113. $Qcat->bindInt(':parent_id', $data['parent_id']);
  114. } else {
  115. $Qcat->bindRaw(':parent_id', 'null');
  116. }
  117. }
  118. $Qcat->bindTable(':table_categories', TABLE_CATEGORIES);
  119. $Qcat->bindInt(':sort_order', $data['sort_order']);
  120. $Qcat->setLogging($_SESSION['module'], $id);
  121. $Qcat->execute();
  122. if ( !$osC_Database->isError() ) {
  123. $category_id = (is_numeric($id)) ? $id : $osC_Database->nextID();
  124. foreach ( $osC_Language->getAll() as $l ) {
  125. if ( is_numeric($id) ) {
  126. $Qcd = $osC_Database->query('update :table_categories_description set categories_name = :categories_name where categories_id = :categories_id and language_id = :language_id');
  127. } else {
  128. $Qcd = $osC_Database->query('insert into :table_categories_description (categories_id, language_id, categories_name) values (:categories_id, :language_id, :categories_name)');
  129. }
  130. $Qcd->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
  131. $Qcd->bindInt(':categories_id', $category_id);
  132. $Qcd->bindInt(':language_id', $l['id']);
  133. $Qcd->bindValue(':categories_name', $data['name'][$l['id']]);
  134. $Qcd->setLogging($_SESSION['module'], $category_id);
  135. $Qcd->execute();
  136. if ( $osC_Database->isError() ) {
  137. $error = true;
  138. break;
  139. }
  140. }
  141. if ( $error === false ) {
  142. $categories_image = new upload($data['image'], realpath('../' . DIR_WS_IMAGES . 'categories'));
  143. if ( $categories_image->exists() && $categories_image->parse() && $categories_image->save() ) {
  144. $Qcf = $osC_Database->query('update :table_categories set categories_image = :categories_image where categories_id = :categories_id');
  145. $Qcf->bindTable(':table_categories', TABLE_CATEGORIES);
  146. $Qcf->bindValue(':categories_image', $categories_image->filename);
  147. $Qcf->bindInt(':categories_id', $category_id);
  148. $Qcf->setLogging($_SESSION['module'], $category_id);
  149. $Qcf->execute();
  150. if ( $osC_Database->isError() ) {
  151. $error = true;
  152. }
  153. }
  154. }
  155. }
  156. if ( $error === false ) {
  157. $osC_Database->commitTransaction();
  158. osC_Cache::clear('categories');
  159. osC_Cache::clear('category_tree');
  160. osC_Cache::clear('also_purchased');
  161. return true;
  162. }
  163. $osC_Database->rollbackTransaction();
  164. return false;
  165. }
  166. public static function delete($id) {
  167. global $osC_Database;
  168. $Qc = $osC_Database->query('delete from :table_categories where categories_id = :categories_id');
  169. $Qc->bindTable(':table_categories', TABLE_CATEGORIES);
  170. $Qc->bindInt(':categories_id', $id);
  171. $Qc->setLogging($_SESSION['module'], $id);
  172. $Qc->execute();
  173. if ( !$osC_Database->isError() ) {
  174. osC_Cache::clear('categories');
  175. osC_Cache::clear('category_tree');
  176. osC_Cache::clear('also_purchased');
  177. return true;
  178. }
  179. return false;
  180. }
  181. public static function move($id, $new_id) {
  182. global $osC_Database;
  183. $category_array = explode('_', $new_id);
  184. if ( in_array($id, $category_array)) {
  185. return false;
  186. }
  187. $parent_id = end($category_array);
  188. $Qupdate = $osC_Database->query('update :table_categories set parent_id = :parent_id, last_modified = now() where categories_id = :categories_id');
  189. $Qupdate->bindTable(':table_categories', TABLE_CATEGORIES);
  190. if ( $parent_id > 0 ) {
  191. $Qupdate->bindInt(':parent_id', $parent_id);
  192. } else {
  193. $Qupdate->bindRaw(':parent_id', 'null');
  194. }
  195. $Qupdate->bindInt(':categories_id', $id);
  196. $Qupdate->setLogging($_SESSION['module'], $id);
  197. $Qupdate->execute();
  198. osC_Cache::clear('categories');
  199. osC_Cache::clear('category_tree');
  200. osC_Cache::clear('also_purchased');
  201. return true;
  202. }
  203. }
  204. ?>