Sliders Documentation
Sliders Controller Documentation
File: /controllers/sliders.php
Purpose: Manages website slider/banner content with image uploads and multi-language support
Last Updated: December 21, 2024
Total Functions: 4
Lines of Code: ~218
---
๐ Overview
The Sliders Controller is a content management module that handles website sliders and banner management. It provides comprehensive functionality for creating, editing, and managing promotional banners with:
- โข Multi-language title and content support (Arabic/English)
- โข Image/media upload handling
- โข Position-based ordering
- โข Active/inactive status control
- โข URL linking capabilities
- โข Target window control
- โข AJAX and standard form support
- โข CURL post handling for external integrations
Primary Functions
- โ Create slider/banner entries with media upload
- โ Edit existing slider content and images
- โ Delete sliders with file cleanup
- โ Display slider listings with status management
- โ Multi-language content support
- โ Position-based ordering system
- โ Active/inactive toggling
- โ External URL linking with target control
Related Controllers
- โข mediaController.php - Media file management
- โข contentController.php - General content management
- โข websiteController.php - Website configuration
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| **sliders** | Slider/banner storage | id, title, titleEn, titleUrl, openTarget, mediaType, media, description, descriptionEn, position, isActive, sysDate, userid, isdel |
| Table Name | Purpose | Key Columns |
|---|---|---|
| **youtubelink** | Tutorial video links | youtubelinkid, title, url |
๐ Key Functions
1. Default Action - Add Slider Form
Location: Line 54
Purpose: Display slider creation form
Process Flow:
1. Check authentication (handled externally)
2. Display slider add form template
3. Load form validation scripts
Template: slidersview/add.html
---
2. add() - Create New Slider
Location: Line 151
Purpose: Process new slider creation with media upload
Function Signature:
function add() {
$title = $_POST['title']; // Arabic title
$titleEn = $_POST['titleEn']; // English title
$titleUrl = $_POST['titleUrl']; // Link URL
$openTarget = $_POST['openTarget']; // Link target (_blank, _self)
$mediaType = $_POST['mediaType']; // Media type (0=image)
$content = $_POST['content']; // Arabic description
$contentEn = $_POST['contentEn']; // English description
$position = $_POST['position']; // Display position
$isActive = $_POST['isActive']; // Active status
}
Process Flow:
1. Extract POST data for slider fields
2. Upload media file using uploadnew('media', False, 0, 0, 'sliders')
3. Create RedBeanPHP record with all fields
4. Set system date and user ID
5. Save to database and return ID
RedBean Implementation:
$rdispense = R::dispense('sliders');
$rdispense->title = $title;
$rdispense->titleEn = $titleEn;
$rdispense->titleUrl = $titleUrl;
$rdispense->openTarget = $openTarget;
$rdispense->mediaType = 0; // Fixed as image type
$rdispense->media = $media;
$rdispense->description = $content;
$rdispense->descriptionEn = $contentEn;
$rdispense->position = $position;
$rdispense->isActive = $isActive;
$rdispense->sysDate = date("Y-m-d H:i:s");
$rdispense->userid = $_SESSION['userid'];
$rdispense->isdel = 0;
$id = R::store($rdispense);
---
3. show - Display Slider Listings
Location: Line 75
Purpose: Show all active sliders with management options
Process Flow:
1. Query all sliders with isdel = 0
2. Load YouTube tutorial links
3. Assign data to template
4. Display slider listing with edit/delete options
Query:
$showData = R::findAll('sliders', 'isdel = 0');
$smarty->assign('showData', $showData);
Template: slidersview/show.html
---
4. update() - Edit Existing Slider
Location: Line 181
Purpose: Update slider content and optionally replace media
Function Signature:
function update() {
$id = $_POST['id']; // Slider ID to update
// ... same fields as add() function
$media = uploadupdate('media', 'mediaurl', False, 0, 0, 'sliders');
}
Process Flow:
1. Extract POST data including slider ID
2. Handle media update using uploadupdate() - keeps existing if no new upload
3. Load existing record via RedBean
4. Update all fields with new values
5. Save updated record
Media Update Logic:
// uploadupdate() handles:
// - New file upload if provided
// - Keeps existing file if no new upload
// - Handles file replacement and cleanup
$media = uploadupdate('media', 'mediaurl', False, 0, 0, 'sliders');
---
5. deleteFinaly() - Permanent Deletion
Location: Line 210
Purpose: Permanently delete slider with file cleanup
Function Signature:
function deleteFinaly($id) {
$rtrash = R::load('sliders', $id);
if (file_exists('../upload/sliders/' . $rtrash->media)) {
chmod('../upload/sliders/' . $rtrash->media, 0777);
unlink('../upload/sliders/' . $rtrash->media);
}
R::trash($rtrash);
}
Process Flow:
1. Load slider record by ID
2. Check if media file exists
3. Set file permissions to 0777
4. Delete physical media file
5. Remove database record completely
---
๐ Workflows
Workflow 1: Slider Creation Process
Workflow 2: Slider Update Process
---
๐ URL Routes & Actions
| URL Parameter | Function Called | Description | |
|---|---|---|---|
| `do=` (empty) | Default | Display slider creation form | |
| `do=add` | `add()` | Process new slider creation | |
| `do=show` | Display listing | Show all active sliders with management | |
| `do=edit` | Edit form | Display slider edit form with existing data | |
| `do=update` | `update()` | Process slider modifications | |
| `do=deleteFinaly` | `deleteFinaly()` | Permanently delete slider and files | |
| `do=sucess` | Success page | Display operation success message | |
| `do=error` | Error page | Display operation error message |
Add Slider (do=add):
- โข
title- Arabic title - โข
titleEn- English title - โข
titleUrl- Link URL (optional) - โข
openTarget- Link target (_blank/_self) - โข
mediaType- Media type (currently fixed at 0) - โข
media- Upload file - โข
content- Arabic description - โข
contentEn- English description - โข
position- Display position/order - โข
isActive- Active status (0/1)
Edit Slider (do=edit):
- โข
id- Slider ID to edit
Update Slider (do=update):
- โข
id- Slider ID - โข All fields from add action
- โข
mediaurl- Current media URL (for update logic)
Delete Slider (do=deleteFinaly):
- โข
id- Slider ID to delete
---
๐งฎ Calculation Methods
Media File Handling
// New upload (add function)
$media = uploadnew('media', False, 0, 0, 'sliders');
// Parameters: field_name, resize, width, height, folder
// Update upload (preserves existing if no new file)
$media = uploadupdate('media', 'mediaurl', False, 0, 0, 'sliders');
// Parameters: field_name, current_file_field, resize, width, height, folder
File Cleanup on Deletion
// Safe file deletion with permission handling
if (file_exists('../upload/sliders/' . $rtrash->media)) {
chmod('../upload/sliders/' . $rtrash->media, 0777); // Ensure deletable
unlink('../upload/sliders/' . $rtrash->media); // Remove file
}
AJAX Response Handling
// Success response
$data = array(
'status' => 1,
'message' => 'ุชู
ุช ุงูุนู
ููู ุจูุฌุงุญ', // Arabic message
'message_en' => 'Success' // English message
);
echo json_encode($data);
// Error response
$data = array(
'status' => 2,
'message' => 'ุญุฏุซ ุฎุทุฃ', // Arabic error
'message_en' => 'Error' // English error
);
echo json_encode($data);
---
๐ Security & Permissions
Authentication
- โข No explicit authentication checks in controller
- โข Relies on external authentication handling
- โข Session-based user tracking via
$_SESSION['userid']
File Upload Security
// Upload function provides security:
// - File type validation
// - Size restrictions
// - Path traversal prevention
// - Sanitized filenames
uploadnew('media', False, 0, 0, 'sliders');
Input Sanitization
- โข Direct POST field access without explicit sanitization
- โข Relies on framework-level input filtering
- โข RedBeanPHP provides SQL injection protection
CURL/External Integration Support
// Supports external system integration
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
// Handle AJAX/CURL requests differently
// Return JSON instead of redirects
}
---
๐ Performance Considerations
File Storage Optimization
1. Media Organization:
- Files stored in /upload/sliders/ directory
- Unique filenames prevent conflicts
- No image resizing (False parameter) - consider for large files
2. Database Efficiency:
- RedBeanPHP provides ORM layer
- Simple queries for small slider datasets
- Consider pagination for large slider collections
3. Memory Management:
- File uploads may require adequate memory limits
- Large image files impact performance
Known Performance Issues
// No pagination in show() function
$showData = R::findAll('sliders', 'isdel = 0');
// May become slow with hundreds of sliders
// Consider implementing:
$showData = R::findAll('sliders', 'isdel = 0 ORDER BY position LIMIT 50');
---
๐ Common Issues & Troubleshooting
1. File Upload Failures
Issue: Media files not uploading properly
Cause: Directory permissions or upload size limits
Debug:
// Check upload directory permissions
if (!is_writable('../upload/sliders/')) {
chmod('../upload/sliders/', 0755);
}
// Check upload size in php.ini
ini_get('upload_max_filesize');
ini_get('post_max_size');
2. Media Files Not Deleting
Issue: File cleanup fails during deletion
Cause: File permissions or path issues
Fix:
// Debug file deletion
$file_path = '../upload/sliders/' . $rtrash->media;
if (file_exists($file_path)) {
echo "File exists: " . $file_path;
if (!is_writable($file_path)) {
echo "File not writable";
chmod($file_path, 0777);
}
}
3. AJAX Response Issues
Issue: AJAX requests not returning proper JSON
Cause: Mixed output or header issues
Fix:
// Ensure clean JSON output
header('Content-Type: application/json');
// Avoid any echo/print before JSON output
4. Multi-language Display Problems
Issue: Arabic/English content not displaying correctly
Cause: Character encoding or template issues
Fix:
// Ensure UTF-8 encoding
header('Content-Type: text/html; charset=UTF-8');
// Check database charset is utf8mb4
---
๐งช Testing Scenarios
Test Case 1: Basic Slider Creation
1. Access slider add form
2. Fill all required fields (title, titleEn, content, etc.)
3. Upload image file
4. Set position and active status
5. Submit form and verify database entry
6. Check file exists in /upload/sliders/
Test Case 2: Slider Update with Media
1. Edit existing slider
2. Change title and description
3. Upload new media file
4. Verify old file is cleaned up
5. Check updated data in database
Test Case 3: AJAX Integration
1. Submit slider form with curlpost=1
2. Verify JSON response format
3. Test both success and error responses
4. Confirm no redirect occurs
Test Case 4: File Cleanup on Deletion
1. Create slider with media file
2. Note uploaded filename
3. Delete slider permanently
4. Verify database record removed
5. Confirm media file deleted from filesystem
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข File Upload Library - Upload function documentation
- โข RedBeanPHP Documentation - ORM usage patterns
- โข Smarty Templates - Template system guide
---
Documented By: AI Assistant
Review Status: โ Complete
Next Review: When media handling changes occur