Browse Source

fleat: add prodct_category

igb 1 month ago
parent
commit
0a7c1bc609
1 changed files with 659 additions and 0 deletions
  1. 659 0
      system/product_category.php

+ 659 - 0
system/product_category.php

@@ -0,0 +1,659 @@
+<?php
+require_once('conn.php');
+
+// Check login status
+checkLogin("信息管理");
+
+
+
+// Initialize all variables
+$act = isset($_GET['act']) ? $_GET['act'] : '';
+$name = isset($_POST['name']) ? htmlspecialcharsFix($_POST['name']) : '';
+$slug = ''; // Removed from form but kept for database compatibility
+$description = isset($_POST['description']) ? htmlspecialcharsFix($_POST['description']) : '';
+$image = isset($_POST['image']) ? htmlspecialcharsFix($_POST['image']) : '';
+$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
+$sort_order = isset($_POST['sort_order']) ? intval($_POST['sort_order']) : 0;
+$status = 1; // Always enabled
+$keys = isset($_GET['Keys']) ? urlencode($_GET['Keys']) : '';
+$keyscode = isset($_GET['Keys']) ? htmlspecialcharsFix($_GET['Keys']) : '';
+
+// Handle form submissions
+if ($act == 'save') {
+    $id = isset($_POST['id']) ? $_POST['id'] : '';
+    $is_edit = (!empty($id) && is_numeric($id));
+    
+    // Always generate slug based on name (even though it's not shown in the interface)
+    $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $name)));
+    
+    // Check if slug exists
+    $slug_check_sql = "SELECT id FROM product_categories WHERE slug = '" . mysqli_real_escape_string($conn, $slug) . "'" . ($is_edit ? " AND id != " . (int)$id : "");
+    $slug_check_result = mysqli_query($conn, $slug_check_sql);
+    
+    if (mysqli_num_rows($slug_check_result) > 0) {
+        // Slug already exists, append a unique identifier
+        $slug .= '-' . time();
+    }
+    
+    if ($is_edit) {
+        // Update existing category
+        $sql = "UPDATE product_categories SET 
+                name = '" . mysqli_real_escape_string($conn, $name) . "',
+                slug = '" . mysqli_real_escape_string($conn, $slug) . "',
+                parent_id = " . $parent_id . ",
+                description = '" . mysqli_real_escape_string($conn, $description) . "',
+                image = '" . mysqli_real_escape_string($conn, $image) . "',
+                sort_order = " . $sort_order . ",
+                updated_at = NOW()
+                WHERE id = " . (int)$id;
+        mysqli_query($conn, $sql);
+    } else {
+        // Insert new category
+        $sql = "INSERT INTO product_categories (name, slug, parent_id, description, image, sort_order, status, created_at, updated_at) 
+                VALUES (
+                    '" . mysqli_real_escape_string($conn, $name) . "',
+                    '" . mysqli_real_escape_string($conn, $slug) . "',
+                    " . $parent_id . ",
+                    '" . mysqli_real_escape_string($conn, $description) . "',
+                    '" . mysqli_real_escape_string($conn, $image) . "',
+                    " . $sort_order . ",
+                    1,
+                    NOW(),
+                    NOW()
+                )";
+        mysqli_query($conn, $sql);
+    }
+    
+    // Redirect after save
+    header("Location: ?keys=" . $keys);
+    exit();
+}
+
+// Handle bulk actions
+if ($act == 'postchk') {
+    if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
+        $chkact = $_POST['chkact'];
+        $id_list = array();
+        
+        foreach ($_POST['chkbox'] as $id) {
+            if (is_numeric($id)) {
+                $id_list[] = (int)$id;
+            }
+        }
+        
+        if (!empty($id_list)) {
+            $ids = implode(',', $id_list);
+            
+            if ($chkact == '-1') {
+                // Delete categories
+                mysqli_query($conn, "DELETE FROM product_categories WHERE id IN($ids)");
+                
+                // Reset parent_id for child categories of deleted categories
+                mysqli_query($conn, "UPDATE product_categories SET parent_id = 0 WHERE parent_id IN($ids)");
+            }
+            // Status update removed - all categories are enabled by default
+        }
+    }
+    
+    // Redirect after bulk action
+    header("Location: ?Keys=" . $keys);
+    exit();
+}
+
+// Display edit form
+if ($act == 'edit' || $act == 'add') {
+    $id = isset($_GET['id']) ? $_GET['id'] : '';
+    $is_edit = (!empty($id) && is_numeric($id) && $act == 'edit');
+    
+    // Check for parent_id in URL for add mode
+    if ($act == 'add' && isset($_GET['parent_id']) && is_numeric($_GET['parent_id'])) {
+        $parent_id = intval($_GET['parent_id']);
+        
+        // Verify that the parent category exists
+        $parent_check = mysqli_query($conn, "SELECT id FROM product_categories WHERE id = " . $parent_id);
+        if (mysqli_num_rows($parent_check) == 0) {
+            $parent_id = 0; // Reset if parent doesn't exist
+        }
+    }
+    
+    if ($is_edit) {
+        $sql = "SELECT * FROM product_categories WHERE id = " . (int)$id;
+        $result = mysqli_query($conn, $sql);
+        
+        if (mysqli_num_rows($result) > 0) {
+            $row = mysqli_fetch_assoc($result);
+            $name = htmlspecialcharsFix($row['name']);
+            $slug = htmlspecialcharsFix($row['slug']);
+            $description = htmlspecialcharsFix($row['description']);
+            $image = htmlspecialcharsFix($row['image']);
+            $parent_id = $row['parent_id'];
+            $sort_order = $row['sort_order'];
+            $status = $row['status'];
+        } else {
+            // Category not found
+            header("Location: ?Keys=" . $keys);
+            exit();
+        }
+    }
+    ?>
+    <!DOCTYPE html>
+    <html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+        <title>管理区域</title>
+        <link rel="stylesheet" href="css/common.css" type="text/css" />
+        <script language="javascript" src="js/jquery-1.7.2.min.js"></script>
+        <script type="text/javascript" src="js/js.js"></script>
+        <script type="text/javascript" src="xheditor-1.1.9/xheditor-1.1.9-zh-cn.min.js"></script>
+        <style>
+        /* 美化搜索区域 */
+        .search_panel {
+            /*background-color: #f5f5f5;*/
+            padding: 15px;
+            border-radius: 4px;
+            border: 1px solid #ddd;
+            margin-bottom: 20px;
+        }
+        .search_panel .inputTxt {
+            width: 300px;
+            padding: 6px 10px;
+            border: 1px solid #ccc;
+            border-radius: 3px;
+        }
+        .search_panel .searchgo {
+            padding: 6px 15px;
+            margin-left: 10px;
+            background-color: #337ab7;
+            color: white;
+            border: none;
+            border-radius: 3px;
+            cursor: pointer;
+        }
+        .search_panel .searchgo:hover {
+            background-color: #286090;
+        }
+        /* 新增按钮样式 */
+        .add_btn {
+            background-color: #5cb85c;
+            color: white;
+            padding: 6px 15px;
+            border: none;
+            border-radius: 3px;
+            cursor: pointer;
+            font-weight: bold;
+        }
+        .add_btn:hover {
+            background-color: #449d44;
+        }
+        /* 表格标题样式 */
+        .category_title {
+            font-size: 18px;
+            font-weight: bold;
+            margin: 15px 0;
+            color: #333;
+        }
+        /* 表单输入样式增强 */
+        .txt1 {
+            width: 90%;
+            padding: 8px;
+            border: 1px solid #ccc;
+            border-radius: 3px;
+        }
+        .txt1:focus {
+            border-color: #66afe9;
+            outline: 0;
+            box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
+        }
+        .txt2 {
+            width: 90%;
+            height: 150px;
+            padding: 8px;
+            border: 1px solid #ccc;
+            border-radius: 3px;
+            font-size: 14px;
+            line-height: 1.42857143;
+        }
+        .txt2:focus {
+            border-color: #66afe9;
+            outline: 0;
+            box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
+        }
+        .txt3 {
+            width: 100px;
+            padding: 8px;
+            border: 1px solid #ccc;
+            border-radius: 3px;
+        }
+        .txt3:focus {
+            border-color: #66afe9;
+            outline: 0;
+            box-shadow: inset 0 1px 1px rgba(0,0,0,.075), 0 0 8px rgba(102,175,233,.6);
+        }
+        /* 添加子分类链接样式 */
+        .ico_add {
+            color: #5cb85c;
+            font-weight: bold;
+            text-decoration: none;
+        }
+        .ico_add:hover {
+            color: #449d44;
+            text-decoration: underline;
+        }
+        </style>
+        <script>
+        $(document).ready(function(){
+            $('.txt2').xheditor({
+                tools:'full',
+                upLinkUrl:"upload.asp",upLinkExt:"zip,rar,txt,pdf",
+                upImgUrl:"upload.asp",upImgExt:"jpg,jpeg,gif,png",
+                upFlashUrl:"upload.asp",upFlashExt:"swf",
+                upMediaUrl:"upload.asp",upMediaExt:"wmv,avi,wma,mp3,mid"
+            });
+        });
+        </script>
+    </head>
+    <body>
+    <div id="man_zone">
+        <form name="form1" method="post" action="?act=save&keys=<?php echo $keys; ?>">
+            <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
+                <tbody>
+                    <tr>
+                        <th width="15%">分类名称</th>
+                        <td>
+                            <input type="text" id="name" name="name" value="<?php echo $name; ?>" class="txt1" required />
+                            <input type="hidden" name="id" value="<?php echo $id; ?>" />
+                        </td>
+                    </tr>
+                    <tr>
+                        <th>父分类</th>
+                        <td>
+                            <select name="parent_id" class="select1">
+                                <option value="0">-- 作为一级分类 --</option>
+                                <?php
+                                // Get all categories except current one (if editing)
+                                $exclude_condition = $is_edit ? " WHERE id != " . (int)$id : "";
+                                $categories_sql = "SELECT * FROM product_categories" . $exclude_condition . " ORDER BY parent_id ASC, sort_order ASC, id ASC";
+                                $categories_result = mysqli_query($conn, $categories_sql);
+                                
+                                // Build category tree
+                                $all_categories = array();
+                                $cat_tree = array();
+                                
+                                // First pass: create an array of all categories
+                                while ($cat_row = mysqli_fetch_assoc($categories_result)) {
+                                    $all_categories[$cat_row['id']] = $cat_row;
+                                    $all_categories[$cat_row['id']]['children'] = array();
+                                }
+                                
+                                // Second pass: build the tree structure
+                                foreach ($all_categories as $cat_id => $category) {
+                                    if ($category['parent_id'] == 0) {
+                                        // Root category
+                                        $cat_tree[$cat_id] = &$all_categories[$cat_id];
+                                    } else {
+                                        // Child category
+                                        if (isset($all_categories[$category['parent_id']])) {
+                                            $all_categories[$category['parent_id']]['children'][$cat_id] = &$all_categories[$cat_id];
+                                        }
+                                    }
+                                }
+                                
+                                // Function to output dropdown options recursively
+                                function outputCategoryOptions($categories, $parent_id, $level = 0) {
+                                    foreach ($categories as $cat) {
+                                        // Skip if it's a child of the category being edited to prevent circular references
+                                        if ($GLOBALS['is_edit'] && isChildOf($cat['id'], $GLOBALS['id'], $GLOBALS['all_categories'])) {
+                                            continue;
+                                        }
+                                        
+                                        $indent = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level);
+                                        $prefix = $level > 0 ? $indent . '└─ ' : '';
+                                        $selected = ($parent_id == $cat['id']) ? ' selected="selected"' : '';
+                                        
+                                        echo '<option value="' . $cat['id'] . '"' . $selected . '>' . $prefix . htmlspecialcharsFix($cat['name']) . '</option>';
+                                        
+                                        // Output children
+                                        if (!empty($cat['children'])) {
+                                            outputCategoryOptions($cat['children'], $parent_id, $level + 1);
+                                        }
+                                    }
+                                }
+                                
+                                // Helper function to check if a category is a child of another
+                                function isChildOf($categoryId, $potentialParentId, $categories) {
+                                    if ($categoryId == $potentialParentId) {
+                                        return true;
+                                    }
+                                    
+                                    if (!isset($categories[$categoryId])) {
+                                        return false;
+                                    }
+                                    
+                                    $category = $categories[$categoryId];
+                                    if ($category['parent_id'] == 0) {
+                                        return false;
+                                    }
+                                    
+                                    return isChildOf($category['parent_id'], $potentialParentId, $categories);
+                                }
+                                
+                                // Output options
+                                outputCategoryOptions($cat_tree, $parent_id);
+                                ?>
+                            </select>
+                        </td>
+                    </tr>
+                    <tr>
+                        <th>排序</th>
+                        <td>
+                            <input type="number" name="sort_order" value="<?php echo $sort_order; ?>" class="txt3" />
+                            <span class="note">数字越小越靠前</span>
+                        </td>
+                    </tr>
+                    <tr>
+                        <th>图片</th>
+                        <td>
+                            <input type="text" name="image" value="<?php echo $image; ?>" class="txt1" />
+                            <span class="note">分类图片URL</span>
+                        </td>
+                    </tr>
+                    <tr>
+                        <th>描述</th>
+                        <td>
+                            <textarea name="description" class="txt2"><?php echo $description; ?></textarea>
+                        </td>
+                    </tr>
+                    <tr>
+                        <th></th>
+                        <td>
+                            <input type="submit" name="save" value="保存" class="btn1" />
+                            <input type="button" value="返回" class="btn1" onClick="location.href='?keys=<?php echo $keys; ?>'" />
+                        </td>
+                    </tr>
+                </tbody>
+            </table>
+        </form>
+    </div>
+    </body>
+    </html>
+    <?php
+} else {
+    // Display category list
+    ?>
+    <!DOCTYPE html>
+    <html>
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+        <title>管理区域</title>
+        <link rel="stylesheet" href="css/common.css" type="text/css" />
+        <script language="javascript" src="js/jquery-1.7.2.min.js"></script>
+        <script type="text/javascript" src="js/js.js"></script>
+        <style>
+        /* 美化搜索区域 */
+        .search_panel {
+            /*background-color: #f5f5f5;*/
+            padding: 15px;
+            border-radius: 4px;
+            border: 1px solid #ddd;
+            margin-bottom: 20px;
+        }
+        .search_panel .inputTxt {
+            width: 300px;
+            padding: 6px 10px;
+            border: 1px solid #ccc;
+            border-radius: 3px;
+        }
+        .search_panel .searchgo {
+            padding: 6px 15px;
+            margin-left: 10px;
+            background-color: #337ab7;
+            color: white;
+            border: none;
+            border-radius: 3px;
+            cursor: pointer;
+        }
+        .search_panel .searchgo:hover {
+            background-color: #286090;
+        }
+        /* 新增按钮样式 */
+        .add_btn {
+            background-color: #5cb85c;
+            color: white;
+            padding: 6px 15px;
+            border: none;
+            border-radius: 3px;
+            cursor: pointer;
+            font-weight: bold;
+        }
+        .add_btn:hover {
+            background-color: #449d44;
+        }
+        /* 表格标题样式 */
+        .category_title {
+            font-size: 18px;
+            font-weight: bold;
+            margin: 15px 0;
+            color: #333;
+        }
+        /* 添加子分类链接样式 */
+        .ico_add {
+            color: #5cb85c;
+            font-weight: bold;
+            text-decoration: none;
+        }
+        .ico_add:hover {
+            color: #449d44;
+            text-decoration: underline;
+        }
+        </style>
+    </head>
+    <body>
+    <div id="man_zone">
+        <form id="form1" method="post" action="?act=postchk&Keys=<?php echo $keys; ?>" onSubmit="return false">
+            <div class="search_panel">
+                <div style="display: flex; ">
+
+                    <div>
+                        <input type="button" value="+ 新增分类" onClick="location.href='?act=add'" class="add_btn" />
+                    </div>
+
+                    <div style="padding-left: 50px;">
+                        <input type="text" id="keys" class="inputTxt" value="<?php echo $keyscode; ?>" placeholder="请输入搜索关键词" />
+                        <input type="button" id="searchgo" class="searchgo" value="搜索" onClick="location.href='?Keys='+escape(document.getElementById('keys').value)" />
+                    </div>
+                </div>
+            </div>
+
+            <div class="category_title">产品分类列表</div>
+            
+            <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
+                <thead>
+                    <tr>
+                        <th width="4%"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox[]')" /></th>
+                        <th width="6%">ID</th>
+                        <th width="30%">分类名称</th>
+                        <th width="10%">排序</th>
+                        <th width="10%">类型</th>
+                        <th width="40%">操作</th>
+                    </tr>
+                </thead>
+                <tbody>
+                <?php
+                // Search condition
+                $search_condition = '';
+                if (!empty($keyscode)) {
+                    $search_condition = " WHERE name LIKE '%" . mysqli_real_escape_string($conn, $keyscode) . "%' OR 
+                                         description LIKE '%" . mysqli_real_escape_string($conn, $keyscode) . "%'";
+                }
+                
+                // Get all categories
+                $sql = "SELECT * FROM product_categories ORDER BY sort_order ASC, id ASC";
+                $result = mysqli_query($conn, $sql);
+                
+                // 添加错误检查
+                if (!$result) {
+                    echo "查询错误: " . mysqli_error($conn) . "<br>";
+                    echo "SQL: " . $sql;
+                    exit;
+                }
+                
+                // Build category tree
+                $categories = array();
+                $tree = array();
+                $search_matches = array();
+                
+                // First pass: create an array of all categories
+                while ($row = mysqli_fetch_assoc($result)) {
+                    $categories[$row['id']] = $row;
+                    $categories[$row['id']]['children'] = array();
+                    
+                    // Check if this category matches the search criteria
+                    if (!empty($keyscode) && (
+                        stripos($row['name'], $keyscode) !== false || 
+                        stripos($row['description'], $keyscode) !== false)) {
+                        $search_matches[$row['id']] = true;
+                    }
+                }
+                
+                // Second pass: build the tree structure
+                foreach ($categories as $id => $category) {
+                    if ($category['parent_id'] == 0) {
+                        // Root category
+                        $tree[$id] = &$categories[$id];
+                    } else {
+                        // Child category
+                        if (isset($categories[$category['parent_id']])) {
+                            $categories[$category['parent_id']]['children'][$id] = &$categories[$id];
+                        }
+                    }
+                }
+                
+                // If searching, mark parents of matching categories
+                if (!empty($search_matches)) {
+                    foreach ($search_matches as $id => $match) {
+                        markParents($id, $categories);
+                    }
+                }
+                
+                // Helper function to mark all parent categories
+                function markParents($categoryId, &$categories) {
+                    if (!isset($categories[$categoryId])) {
+                        return;
+                    }
+                    
+                    $categories[$categoryId]['search_match'] = true;
+                    
+                    if ($categories[$categoryId]['parent_id'] > 0 && isset($categories[$categories[$categoryId]['parent_id']])) {
+                        markParents($categories[$categoryId]['parent_id'], $categories);
+                    }
+                }
+                
+                // Function to display category tree recursively
+                function displayCategoryTree($categories, $level = 0) {
+                    $temp_num = 0;
+                    foreach ($categories as $category) {
+                        // Skip if searching and neither this category nor any of its children match
+                        if (!empty($GLOBALS['keyscode']) && 
+                            !isset($category['search_match']) && 
+                            !categoryHasMatchingChild($category)) {
+                            continue;
+                        }
+                        
+                        $temp_num++;
+                        $indent = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level);
+                        $prefix = $level > 0 ? $indent . '└─ ' : '';
+                        
+                        $highlight = !empty($GLOBALS['keyscode']) && 
+                                    (stripos($category['name'], $GLOBALS['keyscode']) !== false || 
+                                     stripos($category['description'], $GLOBALS['keyscode']) !== false);
+                        
+                        echo '<tr onMouseOver="this.style.background=\'#F7FCFF\'" onMouseOut="this.style.background=\'#FFFFFF\'">';
+                        echo '<td align="center"><input type="checkbox" name="chkbox[]" value="' . $category['id'] . '" /></td>';
+                        echo '<td align="center">' . $category['id'] . '</td>';
+                        echo '<td>' . $prefix;
+                        
+                        if ($highlight) {
+                            echo '<strong style="background-color: #fff3cd;">' . htmlspecialcharsFix($category['name']) . '</strong>';
+                        } else {
+                            echo htmlspecialcharsFix($category['name']);
+                        }
+                        
+                        // Show subcategory count
+                        if (!empty($category['children'])) {
+                            echo ' <span style="color:#999;">(' . count($category['children']) . '个子分类)</span>';
+                        }
+                        
+                        echo '</td>';
+                        echo '<td align="center">' . $category['sort_order'] . '</td>';
+                        echo '<td align="center">';
+                        if ($level == 0) {
+                            echo '一级分类';
+                        } else if ($level == 1) {
+                            echo '二级分类';
+                        } else {
+                            echo $level + 1 . '级分类';
+                        }
+                        echo '</td>';
+                        echo '<td align="center">';
+                        echo '<a href="?Keys=' . $GLOBALS['keys'] . '&act=edit&id=' . $category['id'] . '" class="ico_edit ico">修改</a>';
+                        echo '&nbsp;|&nbsp;';
+                        echo '<a href="?Keys=' . $GLOBALS['keys'] . '&act=add&parent_id=' . $category['id'] . '" class="ico_add ico" style="color:#5cb85c;">添加子分类</a>';
+                        echo '</td>';
+                        echo '</tr>';
+                        
+                        // Display children recursively
+                        if (!empty($category['children'])) {
+                            displayCategoryTree($category['children'], $level + 1);
+                        }
+                    }
+                    return $temp_num;
+                }
+                
+                // Helper function to check if a category has any child that matches search
+                function categoryHasMatchingChild($category) {
+                    if (empty($category['children'])) {
+                        return false;
+                    }
+                    
+                    foreach ($category['children'] as $child) {
+                        if (isset($child['search_match']) || categoryHasMatchingChild($child)) {
+                            return true;
+                        }
+                    }
+                    
+                    return false;
+                }
+                
+                if (count($tree) > 0) {
+                    displayCategoryTree($tree);
+                } else {
+                    ?>
+                    <tr>
+                        <td colspan="8" align="center">
+                            <?php echo empty($keyscode) ? 'Sorry,当前暂无分类信息' : '<a href="?">Sorry,没有找到"' . htmlspecialcharsFix($keyscode) . '"相关的分类,点击返回</a>'; ?>
+                        </td>
+                    </tr>
+                    <?php
+                }
+                ?>
+                </tbody>
+                <tfoot>
+                    <tr>
+                        <td colspan="8">
+                            <div class="postchkbox">
+                                <select id="chkact" name="chkact">
+                                    <option value="-1">删除</option>
+                                </select>
+                                <input type="button" value="执行" onClick="postchk(1)" class="btn1" />
+                            </div>
+                        </td>
+                    </tr>
+                </tfoot>
+            </table>
+        </form>
+    </div>
+    </body>
+    </html>
+    <?php
+}
+?>