Sizecolor Documentation
Size Color Controller Documentation
File: /controllers/sizecolorController.php
Purpose: Manages product size and color attributes with image support
Last Updated: December 21, 2024
Total Functions: 4
Lines of Code: ~220
---
๐ Overview
The Size Color Controller is a product attribute management module that handles size and color variations for products. It provides functionality for:
- โข Creating size and color attributes
- โข Type-based categorization (size vs color)
- โข Image upload for visual representation
- โข Soft delete functionality (conditions-based hiding)
- โข User tracking and audit trails
- โข Integration with product variant systems
Primary Functions
- โ Create size/color attributes with images
- โ Edit existing attributes and update images
- โ Soft delete attributes (hide from active use)
- โ Display attribute listings with type filtering
- โ Image upload and management
- โ User audit tracking
- โ Type-based categorization system
Related Controllers
- โข productController.php - Product management
- โข productVariantController.php - Product variations
- โข storeController.php - Inventory management
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| **sizecolor** | Size/color attributes | sizecolorid, name, type, userid, conditions, image, sysdate |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **youtubelink** | Tutorial video links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action - Add Form Display
Location: Line 78
Purpose: Display size/color attribute creation form
Process Flow:
1. Load add form template
2. Initialize form validation
3. Set up image upload fields
Template: sizecolorview/add.html
---
2. add() - Create New Size/Color Attribute
Location: Line 168
Purpose: Process new size/color attribute creation with image upload
Function Signature:
function add() {
global $sizeColor;
global $sizeColorDAO;
$name = $_POST['name']; // Attribute name
$type = (int) $_POST['type']; // Type: 0=size, 1=color
$image = uploadnew('image', False, 0, 0, 'sizecolor');
}
Process Flow:
1. Extract POST data for attribute fields
2. Upload image using uploadnew() to 'sizecolor' folder
3. Create attribute object with all properties
4. Set system metadata (user, date, conditions)
5. Insert into database via DAO
Attribute Object Setup:
$sizeColor->name = $name;
$sizeColor->type = $type; // 0=size, 1=color
$sizeColor->userid = $_SESSION['userid'];
$sizeColor->conditions = 0; // Active state
$sizeColor->image = $image;
$sizeColor->sysdate = date("Y-m-d");
$sizeColorDAO->insert($sizeColor);
---
3. show - Display Attribute Listings
Location: Line 93
Purpose: Show all active size/color attributes with management options
Process Flow:
1. Query all attributes with conditions = 0 (active only)
2. Load YouTube tutorial links
3. Assign data to template for display
4. Enable custom check functionality
Query Logic:
$allSizeColor = $sizeColorDAO->queryByConditions(0); // Active only
$smarty->assign("allSizeColor", $allSizeColor);
Template: sizecolorview/show.html
---
4. update() - Edit Existing Attribute
Location: Line 201
Purpose: Update attribute properties and optionally replace image
Function Signature:
function update() {
global $sizeColor;
global $sizeColorDAO;
$name = $_POST['name'];
$type = (int) $_POST['type'];
$id = (int) $_POST['id'];
$image = uploadupdate('image', 'imageurl', False, 0, 0, 'sizecolor');
}
Process Flow:
1. Extract POST data including attribute ID
2. Handle image update using uploadupdate() - preserves existing if no new upload
3. Load existing attribute record
4. Update all modifiable fields
5. Save changes via DAO
Update Logic:
$sizeColor = $sizeColorDAO->load($id); // Load existing
$sizeColor->name = $name;
$sizeColor->type = $type;
$sizeColor->image = $image; // New or existing image
$sizeColorDAO->update($sizeColor);
---
5. delete - Soft Delete Attribute
Location: Line 104
Purpose: Soft delete attribute by setting conditions flag
Process Flow:
1. Get attribute ID from URL parameter
2. Load existing attribute record
3. Set conditions = 1 (hidden/deleted state)
4. Update record to hide from active use
Soft Delete Logic:
$id = $_GET['id'];
$sizeColor = $sizeColorDAO->load($id);
$sizeColor->conditions = 1; // Mark as deleted
$sizeColorDAO->update($sizeColor);
Note: This is soft delete - records remain in database but hidden from normal operations.
---
๐ Workflows
Workflow 1: Size/Color Attribute Creation
Workflow 2: Attribute Management Process
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default | Display attribute creation form | |
| `do=add` | `add()` | Process new attribute creation | |
| `do=show` | Display listing | Show all active attributes with management | |
| `do=edit` | Edit form | Display attribute edit form with existing data | |
| `do=update` | `update()` | Process attribute modifications | |
| `do=delete` | Soft delete | Mark attribute as deleted (conditions = 1) | |
| `do=sucess` | Success page | Display operation success message | |
| `do=error` | Error page | Display operation error message |
Add Attribute (do=add):
- โข
name- Attribute name (e.g., "Red", "Large", "XL") - โข
type- Type integer (0=size, 1=color) - โข
image- Image file upload (optional)
Edit Attribute (do=edit):
- โข
id- Attribute ID to edit
Update Attribute (do=update):
- โข
id- Attribute ID - โข
name- Updated attribute name - โข
type- Updated type - โข
imageurl- Current image URL (for update logic) - โข
image- New image file (optional)
Delete Attribute (do=delete):
- โข
id- Attribute ID to soft delete
---
๐งฎ Calculation Methods
Type Classification System
// Type codes for attribute classification
$type = (int) $_POST['type'];
// 0 = Size attribute (Small, Medium, Large, XL, etc.)
// 1 = Color attribute (Red, Blue, Green, etc.)
Image Upload Handling
// New image upload (add function)
$image = uploadnew('image', False, 0, 0, 'sizecolor');
// Parameters: field_name, resize_flag, width, height, target_folder
// Update image upload (preserves existing if no new file)
$image = uploadupdate('image', 'imageurl', False, 0, 0, 'sizecolor');
// Handles: new_field, current_field, resize_flag, width, height, folder
Soft Delete Logic
// Active attributes query
$allSizeColor = $sizeColorDAO->queryByConditions(0); // conditions = 0
// Soft delete implementation
$sizeColor->conditions = 1; // Mark as deleted
$sizeColorDAO->update($sizeColor);
// Record remains in database but hidden from normal operations
Audit Trail Implementation
// System metadata tracking
$sizeColor->userid = $_SESSION['userid']; // Who created/modified
$sizeColor->sysdate = date("Y-m-d"); // When created/modified
$sizeColor->conditions = 0; // Status flag
---
๐ Security & Permissions
Authentication Requirements
- โข All actions require authentication via
include_once("../public/authentication.php") - โข Session-based user validation
- โข User ID tracking for audit purposes
Input Sanitization
// Type casting for security
$type = (int) $_POST['type']; // Ensure integer type
$id = (int) $_POST['id']; // Ensure integer ID
// Direct POST access - relies on framework filtering
$name = $_POST['name']; // Should be sanitized at framework level
File Upload Security
- โข Upload functions provide built-in security
- โข File type validation in upload library
- โข Path traversal prevention
- โข Target folder restriction to 'sizecolor'
SQL Injection Prevention
- โข Uses DAO layer with prepared statements
- โข Object-based database operations
- โข No direct SQL concatenation
---
๐ Performance Considerations
Database Optimization Tips
1. Indexes Required:
- sizecolor(conditions) for active/deleted filtering
- sizecolor(type) for size/color type filtering
- sizecolor(userid) for user-based queries
2. Query Efficiency:
- Soft delete filtering at database level
- Type-based categorization
- Efficient DAO-based operations
3. File Storage:
- Images stored in organized /upload/sizecolor/ directory
- No automatic resizing - consider for large images
- File cleanup not implemented for soft deletes
Known Performance Issues
// No pagination in show() function
$allSizeColor = $sizeColorDAO->queryByConditions(0);
// May become slow with thousands of attributes
// Missing file cleanup for soft deletes
// Files remain on disk even when attributes are "deleted"
---
๐ Common Issues & Troubleshooting
1. Type Classification Confusion
Issue: Unclear distinction between size and color types
Cause: No validation of type field values
Debug:
// Check type distribution
SELECT type, COUNT(*) FROM sizecolor GROUP BY type;
// Validate type values
if (!in_array($type, [0, 1])) {
throw new Exception("Invalid type: must be 0 (size) or 1 (color)");
}
2. Image Upload Failures
Issue: Images not uploading or updating properly
Cause: Directory permissions or file size limits
Fix:
// Check upload directory
if (!is_writable('../upload/sizecolor/')) {
chmod('../upload/sizecolor/', 0755);
}
// Debug upload function
$image = uploadnew('image', False, 0, 0, 'sizecolor');
if (empty($image)) {
error_log("Image upload failed");
}
3. Soft Delete Recovery
Issue: No way to recover soft-deleted attributes
Cause: Missing "restore" functionality
Solution:
// Add restore function
function restore($id) {
global $sizeColorDAO;
$sizeColor = $sizeColorDAO->load($id);
$sizeColor->conditions = 0; // Restore to active
$sizeColorDAO->update($sizeColor);
}
4. Orphaned Files
Issue: Image files remain after soft delete
Cause: No file cleanup in delete function
Fix:
// Implement file cleanup in delete function
if (!empty($sizeColor->image) && file_exists('../upload/sizecolor/' . $sizeColor->image)) {
// Consider moving to archive instead of deleting
rename(
'../upload/sizecolor/' . $sizeColor->image,
'../upload/sizecolor/deleted/' . $sizeColor->image
);
}
---
๐งช Testing Scenarios
Test Case 1: Size Attribute Creation
1. Access attribute add form
2. Enter size name "Large"
3. Select type = 0 (size)
4. Upload representative image
5. Submit and verify database entry
6. Check file exists in /upload/sizecolor/
Test Case 2: Color Attribute Management
1. Create color attribute "Red" with type = 1
2. Edit to change name to "Crimson Red"
3. Upload new color swatch image
4. Verify old image handling
5. Test soft delete functionality
Test Case 3: Type Filtering
1. Create multiple size attributes (S, M, L, XL)
2. Create multiple color attributes (Red, Blue, Green)
3. Query by type and verify separation
4. Test in product variant association
Test Case 4: Soft Delete and Recovery
1. Create test attribute
2. Perform soft delete (conditions = 1)
3. Verify hidden from show listing
4. Check database record still exists
5. Test manual recovery if implemented
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข File Upload Library - Upload function documentation
- โข Product Variants - How sizes/colors link to products
- โข DAO Pattern Documentation - Database access pattern
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When product variant system changes occur