zen cart 类 category_tree 位于 /includes/classes/category_tree.php 文件中,主要的作用是,产生商品分类的树状目录。
- <?php
- /**
- * category_tree Class.
- *
- * @package classes
- * @copyright Copyright 2003-2006 Zen Cart Development Team
- * @copyright Portions Copyright 2003 osCommerce
- * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
- * @version $Id: category_tree.php 3041 2006-02-15 21:56:45Z wilt $
- */
- if (!defined( 'IS_ADMIN_FLAG' )) { //防止非法访问
- die ( 'Illegal Access' );
- }
- /**
- * category_tree Class.
- * This class is used to generate the category tree used for the categories sidebox
- *
- * @package classes
- */
- // 继承 base 基本类
- class category_tree extends base {
- // zen_category_tree 方法,传递一个参数:$product_type,默认值是:all
- function zen_category_tree( $product_type = "all" ) {
- global $db , $cPath , $cPath_array ; // 定义3个 global 变量。$cPath 和 $cPath_array 可以在 /includes/init_includes/init_category_path.php 文件中找到
- // 如果 $product_type 不是 'all',则执行以下代码(因为默认值是 all,所以如果不传递任何参数,则以下代码不会被执行)
- if ( $product_type != 'all' ) {
- // 将一条 sql 语句赋值给 $sql 变量。该语句的作用是
- // 从 product_types 数据库表中选择 type_master_type 字段,必须是和 $product_type 相等的 type_master_type 字段
- $sql = "select type_master_type from " . TABLE_PRODUCT_TYPES . "
- where type_master_type = " . $product_type . " ";
- // 执行 $sql 语句
- $master_type_result = $db ->Execute( $sql );
- // 从 $master_type_result 对象中得到 type_master_type 字段值
- $master_type = $master_type_result ->fields[ 'type_master_type' ];
- }
- $this ->tree = array (); // 定义一个类成员 tree 为数组,$this->tree 是类变量的表示方法,如果不在类里面定义,其实就是 $tree
- if ( $product_type == 'all' ) { // 如果 $product_type 的值等于 all
- // 将一条 sql 语句赋值给 $categories_query
- // 该语句的作用是多表查询 categories 和 categories_description 数据库表
- // 返回 categories 表中的 categories_id,parent_id,categories_image 字段;categories_description 表中的 categories_name 字段
- // 返回结果必须符合:parent_id = 0,同一个 categories_id,language_id = $_SESSION['languages_id'] 以及 categories_status= 1
- // 返回结果按照 categories_name 排序
- $categories_query = "select c.categories_id, cd.categories_name, c.parent_id, c.categories_image
- from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
- where c.parent_id = 0
- and c.categories_id = cd.categories_id
- and cd.language_id= '" . (int)$_SESSION[' languages_id '] . "'
- and c.categories_status= 1
- order by sort_order, cd.categories_name";
- } else { // 如果 $product_type 不为 all,则执行以下代码
- // 将一条 sql 语句赋值给 $categories_query,代码分析如上
- $categories_query = "select ptc.category_id as categories_id, cd.categories_name, c.parent_id, c.categories_image
- from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd, " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc
- where c.parent_id = 0
- and ptc.category_id = cd.categories_id
- and ptc.product_type_id = " . $master_type . "
- and c.categories_id = ptc.category_id
- and cd.language_id= " . (int)$_SESSION['languages_id'] ."
- and c.categories_status= 1
- order by sort_order, cd.categories_name";
- }
- // 执行 $categories_query 语句
- $categories = $db ->Execute( $categories_query , '' , true, 150);
- while (! $categories ->EOF) { // 查询结果没有出尽,则循环执行以下语句
- // 把查询得到的 categories_name,parent_id,categories_id,categories_image 和其它几个值合成一个数组,赋值给 $this->tree(X)
- // 这个 X 就是:$categories->fields['categories_id'] 即商品分类的 id
- $this ->tree[ $categories ->fields[ 'categories_id' ]] = array (
- 'name' => $categories ->fields[ 'categories_name' ],
- 'parent' => $categories ->fields[ 'parent_id' ],
- 'level' => 0,
- 'path' => $categories ->fields[ 'categories_id' ],
- 'image' => $categories ->fields[ 'categories_image' ],
- 'next_id' => false
- );
- if (isset( $parent_id )) { // 如果存在 $parent_id 这个变量
- // 把查询得到的 categories_id 赋值给 $this->tree[X]['next_id'],在上面的代码中,next_id 这个值默认是 false
- $this ->tree[ $parent_id ][ 'next_id' ] = $categories ->fields[ 'categories_id' ];
- }
- // 把查询得到的 categories_id 赋值给 $parent_id;
- $parent_id = $categories ->fields[ 'categories_id' ];
- if (!isset( $first_element )) { // 如果不存在 $first_element 这个变量
- // 将查询得到的 categories_id 赋值给 $first_element
- $first_element = $categories ->fields[ 'categories_id' ];
- }
- $categories ->MoveNext(); // $categories 往后移动一个指针
- }
- if (zen_not_null( $cPath )) { // 如果 $cPath 不为空
- $new_path = '' ;
- reset($cPath_array ); // 把 $cPath_array 数组的内部指针指向第一个元素,并返回这个元素的值
- while (list( $key , $value ) = each( $cPath_array )) { // 把 $cPath_array 元素为 $key $value 变量赋值
- unset($parent_id ); // 销毁 $parent_id 变量
- unset($first_id ); // 销毁 $first_id 变量
- if ( $product_type == 'all' ) { // 如果 $product_type 的值等于 all
- // sql 查询语句
- $categories_query = "select c.categories_id, cd.categories_name, c.parent_id, c.categories_image
- from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
- where c.parent_id = " . (int)$value . "
- and c.categories_id = cd.categories_id
- and cd.language_id= " . (int)$_SESSION['languages_id'] . "
- and c.categories_status= 1
- order by sort_order, cd.categories_name";
- } else { // 如果 $product_type 的值不为 all
- /*
- $categories_query = "select ptc.category_id as categories, cd.categories_name, c.parent_id, c.categories_image
- from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd, " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc
- where c.parent_id = '" . (int)$value . "'
- and ptc.category_id = cd.categories_id
- and ptc.product_type_id = '" . $master_type . "'
- and cd.language_id='" . (int)$_SESSION['languages_id'] . "'
- and c.categories_status= '1'
- order by sort_order, cd.categories_name";
- */
- // sql 查询语句,可以和上一条比较,看有什么不同
- $categories_query = "select ptc.category_id as categories_id, cd.categories_name, c.parent_id, c.categories_image
- from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd, " . TABLE_PRODUCT_TYPES_TO_CATEGORY . " ptc
- where c.parent_id = " . (int)$value . "
- and ptc.category_id = cd.categories_id
- and ptc.product_type_id = " . $master_type . "
- and c.categories_id = ptc.category_id
- and cd.language_id= " . (int)$_SESSION['languages_id'] ."
- and c.categories_status= 1
- order by sort_order, cd.categories_name";
- }
- $rows = $db ->Execute( $categories_query ); // 执行 $categories_query 查询语句
- if ( $rows ->RecordCount()>0) { // 如果 $rows 对象中的 RecordCount() 方法返回的结果大于0
- $new_path .= $value ; // 将 $cPath_array 数组的值,累计赋值给 $new_path
- while (! $rows ->EOF) { // 如果 $rows 没出尽
- $this ->tree[ $rows ->fields[ 'categories_id' ]] = array (
- 'name' => $rows ->fields[ 'categories_name' ],
- 'parent' => $rows ->fields[ 'parent_id' ],
- 'level' => $key +1,
- 'path' => $new_path . '_' . $rows ->fields[ 'categories_id' ],
- 'image' => $categories ->fields[ 'categories_image' ],
- 'next_id' => false
- );
- if (isset( $parent_id )) {
- $this ->tree[ $parent_id ][ 'next_id' ] = $rows ->fields[ 'categories_id' ];
- }
- $parent_id = $rows ->fields[ 'categories_id' ];
- if (!isset( $first_id )) {
- $first_id = $rows ->fields[ 'categories_id' ];
- }
- $last_id = $rows ->fields[ 'categories_id' ];
- $rows ->MoveNext();
- }
- $this ->tree[ $last_id ][ 'next_id' ] = $this ->tree[ $value ][ 'next_id' ];
- $this ->tree[ $value ][ 'next_id' ] = $first_id ;
- $new_path .= '_' ;
- } else {
- break ;
- }
- }
- }
- $row = 0;
- return $this ->zen_show_category( $first_element , $row );
- }
- function zen_show_category( $counter , $ii ) {
- global $cPath_array ;
- $this ->categories_string = "" ;
- for ( $i =0; $i < $this ->tree[ $counter ][ 'level' ]; $i ++) {
- if ( $this ->tree[ $counter ][ 'parent' ] != 0) {
- $this ->categories_string .= CATEGORIES_SUBCATEGORIES_INDENT;
- }
- }
- if ( $this ->tree[ $counter ][ 'parent' ] == 0) {
- $cPath_new = 'cPath=' . $counter ;
- $this ->box_categories_array[ $ii ][ 'top' ] = 'true' ;
- } else {
- $this ->box_categories_array[ $ii ][ 'top' ] = 'false' ;
- $cPath_new = 'cPath=' . $this ->tree[ $counter ][ 'path' ];
- $this ->categories_string .= CATEGORIES_SEPARATOR_SUBS;
- }
- $this ->box_categories_array[ $ii ][ 'path' ] = $cPath_new ;
- if (isset( $cPath_array ) && in_array( $counter , $cPath_array )) {
- $this ->box_categories_array[ $ii ][ 'current' ] = true;
- } else {
- $this ->box_categories_array[ $ii ][ 'current' ] = false;
- }
- // display category name
- $this ->box_categories_array[ $ii ][ 'name' ] = $this ->categories_string . $this ->tree[ $counter ][ 'name' ];
- // make category image available in case needed
- $this ->box_categories_array[ $ii ][ 'image' ] = $this ->tree[ $counter ][ 'image' ];
- if (zen_has_category_subcategories( $counter )) {
- $this ->box_categories_array[ $ii ][ 'has_sub_cat' ] = true;
- } else {
- $this ->box_categories_array[ $ii ][ 'has_sub_cat' ] = false;
- }
- if (SHOW_COUNTS == 'true' ) {
- $products_in_category = zen_count_products_in_category( $counter );
- if ( $products_in_category > 0) {
- $this ->box_categories_array[ $ii ][ 'count' ] = $products_in_category ;
- } else {
- $this ->box_categories_array[ $ii ][ 'count' ] = 0;
- }
- }
- if ( $this ->tree[ $counter ][ 'next_id' ] != false) {
- $ii ++;
- $this ->zen_show_category( $this ->tree[ $counter ][ 'next_id' ], $ii );
- }
- return $this ->box_categories_array;
- }
- }
- ?>