Maintenanceclients Documentation
Maintenance Clients Controller Documentation
File: /controllers/maintenanceclients.php
Purpose: Manages client data specifically for the maintenance service operations module
Last Updated: December 20, 2024
Total Functions: 3
Lines of Code: ~181
---
๐ Overview
The Maintenance Clients Controller is a specialized client management module designed specifically for the maintenance service system. It provides essential client CRUD operations but with a focus on the maintenance workflow context. Unlike the main client controller, this one integrates with YouTube tutorial links and maintains a clear separation for maintenance-specific client operations.
Primary Functions
- โ Add new maintenance clients
- โ Edit existing maintenance clients
- โ Display maintenance client listings
- โ AJAX data grid functionality
- โ Client search and filtering
- โ Integration with branch and user systems
- โ Soft delete functionality
- โ Date range filtering
Related Controllers
- โข clientController.php - Main customer management
- โข maintenancereceipts.php - Maintenance receipt handling
- โข maintenancedeliverys.php - Maintenance delivery operations
---
๐๏ธ Database Tables
Primary Tables (Direct Operations)
| Table Name | Purpose | Key Columns |
|---|---|---|
| **client** | Customer master data | clientid, clientname, clientaddress, clientphone, clientmobile, clientdate, userid, branchId, conditions |
| Table Name | Purpose | Key Columns | |
|---|---|---|---|
| **user** | System users | userid, employeename | |
| **branch** | Branch information | branchId, branchName | |
| **youtubelink** | Tutorial videos | youtubelinkid, title, url |
๐ Key Functions
1. Default Action (Empty $do) - Add Client Form
Location: Lines 18-23
Purpose: Display the client addition form for maintenance operations
Process Flow:
1. Display header template
2. Load maintenanceclientview/add.html template
3. Set maintenance flag to 1
4. Display footer template
Template: maintenanceclientview/add.html
---
2. show - Display Client Grid
Location: Lines 23-31
Purpose: Show maintenance clients in a data table format
Process Flow:
1. Include authentication check
2. Load all YouTube tutorial links
3. Assign YouTube data to template
4. Display maintenanceclientview/show.html template
5. Set maintenance flag to 1
Security: Includes authentication check
---
3. edit - Edit Client Form
Location: Lines 31-42
Purpose: Display client editing form with existing data
Function Signature:
$id = filter_input(INPUT_GET, 'id');
$del = filter_input(INPUT_GET, 'del');
Process Flow:
1. Include authentication check
2. Get client ID and deletion flag from GET parameters
3. Load client data from database
4. Assign client data and deletion flag to template
5. Display maintenanceclientview/edit.html template
SQL Query:
SELECT client.* FROM `client` WHERE clientid = ?
---
4. savedata() - Save/Update Client
Location: Lines 49-77
Purpose: Handle client creation and updates via AJAX
Function Signature:
function savedata()
Input Parameters:
- โข
clientname- Client name - โข
clientphone- Phone number - โข
clientmobile- Mobile number - โข
clientaddress- Address - โข
clientid- Client ID (for updates) - โข
selectitr- Iteration selector for forms
Process Flow:
1. Get current date and user/branch session data
2. Filter and sanitize input parameters
3. Check if clientid exists to determine INSERT vs UPDATE
4. INSERT (New Client):
INSERT INTO `client`(`clientname`, `clientaddress`, `clientphone`, `clientmobile`, `clientdate`, `userid`, `branchId`)
VALUES ('$clientname', '$clientaddress', '$clientphone', '$clientmobile','$date', $userid, $branchid)
```
5. **UPDATE** (Existing Client):
```sql
UPDATE `client` SET `clientname`='$clientname',`clientaddress`='$clientaddress',`clientphone`='$clientphone',`clientmobile`='$clientmobile',`branchId`= $branchid WHERE clientid = $clientid
```
6. Return JSON response with client data
**Return Format**:
json
{
"id": 123,
"selectname": "clientid",
"selectid": "clientid1",
"text": "Client Name",
"selectitr": 1
}
---
### 5. **showajax()** - AJAX Data Grid
**Location**: Lines 80-173
**Purpose**: Provide server-side DataTables processing for client grid
**Input Parameters**:
- `start_date` / `end_date` - Date range filter
- `del` - Deletion status filter
- `data1` - Client ID filter
- `data3` - Branch ID filter
- `search[value]` - Global search term
- `order` - Column sorting
- `start` / `length` - Pagination
**Process Flow**:
1. Define column mapping array
2. Build dynamic search query based on filters
3. Apply user input sanitization
4. Handle search across multiple columns
5. Apply sorting and pagination
6. Execute main query with JOINs
7. Format output for DataTables
**Main Query**:
sql
SELECT client.* ,employeename ,branchName FROM client
LEFT JOIN user ON client.userid = user.userid
LEFT JOIN branch ON client.branchId = branch.branchId
WHERE 1 [FILTERS] [ORDER BY] [LIMIT]
**Search Fields**:
- Client ID, name, phone, mobile
- Client date
- Branch name
- Employee name
**Output Actions**:
- Edit button: Links to edit form
- Delete button: AJAX soft delete functionality
- Conditional display based on deletion status
---
## ๐ Workflows
### Workflow 1: Adding New Client
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ START: Add New Client โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Display Add Form โ
โ - Load maintenanceclientview/add.html โ
โ - Include maintenance navigation โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. User Submits Form โ
โ - Client fills out name, address, phone, mobile โ
โ - Form posts to ?do=savedata โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Process savedata() โ
โ - Validate input parameters โ
โ - Insert into client table โ
โ - Set userid from session โ
โ - Set branchId from session โ
โ - Set current date โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. Return JSON Response โ
โ - Include new client ID โ
โ - Return client name for UI update โ
โ - Provide selector information โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
### Workflow 2: Client Data Grid Display
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ START: View Client List โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. Load Show Page โ
โ - Check authentication โ
โ - Load YouTube tutorial links โ
โ - Display maintenanceclientview/show.html โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 2. DataTables AJAX Request โ
โ - Browser initiates AJAX call to ?do=showajax โ
โ - Includes search, filter, and pagination params โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 3. Process showajax() โ
โ - Build dynamic WHERE clause โ
โ - Apply search across multiple fields โ
โ - Join with user and branch tables โ
โ - Apply pagination and sorting โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 4. Format and Return Data โ
โ - Create action buttons for each row โ
โ - Handle soft-deleted items differently โ
โ - Return JSON for DataTables rendering โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
---
## ๐ URL Routes & Actions
| URL Parameter | Function Called | Description |
|---------------|----------------|-------------|
| `do=` (empty) | Default display | Show add client form |
| `do=show` | show action | Display clients data grid |
| `do=edit` | edit action | Show client edit form |
| `do=savedata` | `savedata()` | Save/update client via AJAX |
| `do=showajax` | `showajax()` | DataTables AJAX endpoint |
### Required Parameters by Action
**Add Client** (empty `do`):
- No parameters required
**Show Clients** (`do=show`):
- Authentication required
- No additional parameters
**Edit Client** (`do=edit`):
- `id` - Client ID to edit
- `del` - Deletion status flag
**Save Data** (`do=savedata`):
- `clientname` - Client name (required)
- `clientaddress` - Client address
- `clientphone` - Phone number
- `clientmobile` - Mobile number
- `clientid` - For updates only
- `selectitr` - Form iteration selector
**AJAX Data** (`do=showajax`):
- DataTables standard parameters
- Optional filters: `start_date`, `end_date`, `data1`, `data3`
---
## ๐งฎ Calculation Methods
### No Complex Calculations
This controller primarily handles basic CRUD operations without financial calculations. The main processing involves:
1. **Data Filtering**:
php
if($data1 != ''){
$searchQuery .= " and client.clientid = ".$data1. " ";
}
2. **Date Range Processing**:
php
if($start_date != '' && $end_date != ''){
$searchQuery .=' and client.clientdate >= "' . $start_date . ' 00-00-00" and client.clientdate <= "' . $end_date . ' 23-59-55" ';
}
3. **Search Term Processing**:
php
if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
$searchQuery .= "and ( client.clientid LIKE \"%'.$_POST["search"]["value"].'%\"
OR client.clientname LIKE \"%'.$_POST["search"]["value"].'%\"
OR client.clientmobile LIKE \"%'.$_POST["search"]["value"].'%\"
OR client.clientphone LIKE \"%'.$_POST["search"]["value"].'%\"
OR client.clientdate LIKE \"%'.$_POST["search"]["value"].'%\"
OR branchName LIKE \"%'.$_POST["search"]["value"].'%\"
OR employeename LIKE \"%'.$_POST["search"]["value"].'%\"
)
";
}
---
## ๐ Security & Permissions
### Authentication Requirements
- **show** and **edit** actions require authentication via `../public/authentication.php`
- **savedata** and **showajax** rely on session data but don't explicitly check authentication
### Input Sanitization
php
$clientname = filter_input(INPUT_POST, 'clientname');
$clientphone = filter_input(INPUT_POST, 'clientphone');
$clientmobile = filter_input(INPUT_POST, 'clientmobile');
$clientaddress = filter_input(INPUT_POST, 'clientaddress');
$clientid = filter_input(INPUT_POST, 'clientid');
### SQL Injection Prevention
- Uses parameterized queries for SELECT operations
- **SECURITY ISSUE**: Direct string concatenation in INSERT/UPDATE queries
**Vulnerable Code**:
php
// โ ๏ธ POTENTIAL SQL INJECTION RISK
R::exec("INSERT INTO client(clientname, clientaddress, clientphone, clientmobile, clientdate, userid, branchId) VALUES
('$clientname', '$clientaddress', '$clientphone', '$clientmobile','$date', $userid, $branchid)");
**Recommended Fix**:
php
// โ SECURE VERSION
R::exec("INSERT INTO client(clientname, clientaddress, clientphone, clientmobile, clientdate, userid, branchId) VALUES
(?, ?, ?, ?, ?, ?, ?)", [$clientname, $clientaddress, $clientphone, $clientmobile, $date, $userid, $branchid]);
### Session Security
- Relies on `$_SESSION['userid']` and `$_SESSION['branchId']`
- No explicit session validation in savedata functions
---
## ๐ Performance Considerations
### Database Optimization
1. **Indexes Recommended**:
- `client(clientid)` - Primary key (likely exists)
- `client(conditions)` - For soft delete filtering
- `client(branchId)` - For branch filtering
- `client(clientdate)` - For date range queries
2. **Query Performance**:
- JOINs with user and branch tables are efficient for display
- Search across multiple text fields could be slow on large datasets
- Consider full-text indexing for client names and addresses
### Potential Issues
- **N+1 Query Problem**: Not present in this controller
- **Large Result Sets**: DataTables pagination helps but large date ranges could still be slow
- **Wildcard Searches**: `LIKE "%search%"` searches can be performance-intensive
---
## ๐ Common Issues & Troubleshooting
### 1. **AJAX Save Failures**
**Issue**: Client save returns 0 instead of success
**Cause**: Database connection issues or constraint violations
**Debug**:
php
// Add to savedata() function
try {
// existing code
} catch (Exception $e) {
error_log("Client save error: " . $e->getMessage());
echo json_encode(['error' => $e->getMessage()]);
}
### 2. **DataTable Not Loading**
**Issue**: Client grid shows "No data available"
**Cause**: AJAX endpoint issues or query problems
**Debug**:
sql
-- Test the main query manually
SELECT client.* ,employeename ,branchName FROM client
LEFT JOIN user ON client.userid = user.userid
LEFT JOIN branch ON client.branchId = branch.branchId
WHERE 1 AND client.conditions = 0
LIMIT 10;
### 3. **Permission Issues**
**Issue**: Users see unauthorized data
**Cause**: Missing authentication checks
**Fix**: Add authentication to all AJAX endpoints:
php
if ($do == 'savedata' || $do == 'showajax') {
include_once("../public/authentication.php");
// existing code
}
### 4. **YouTube Links Not Loading**
**Issue**: Tutorial videos don't appear
**Cause**: YouTube DAO initialization problems
**Debug**:
php
// Check if YouTube DAO is properly initialized
if (!$youtubeLinkDAO) {
error_log("YouTube DAO not initialized");
}
---
## ๐งช Testing Scenarios
### Test Case 1: Basic Client CRUD
1. Navigate to maintenanceclients.php
2. Verify add form loads properly
3. Submit new client with all fields filled
4. Check database for inserted record
5. Verify client appears in grid
6. Edit client and save changes
7. Confirm updates in database and grid
### Test Case 2: Data Grid Functionality
1. Load client show page
2. Test search functionality with various terms
3. Apply date range filters
4. Test column sorting
5. Navigate pagination if multiple pages
6. Verify action buttons work properly
### Test Case 3: Error Handling
1. Submit form with empty required fields
2. Test with special characters in names
3. Try to save very long client names
4. Test duplicate client handling
5. Verify error messages are appropriate
### Test Case 4: Security Testing
1. Test without proper authentication
2. Try SQL injection in search fields
3. Test XSS in client name fields
4. Verify session handling
5. Test unauthorized access to edit forms
```
---
๐ Related Documentation
- โข CLAUDE.md - PHP 8.2 migration guide
- โข clientController.md - Main client management system
- โข maintenancereceipts.md - Maintenance receipts handling
- โข Database Schema Documentation - Table relationships
---
Documented By: AI Assistant
Review Status: โ Complete
Security Issues Found: โ ๏ธ SQL Injection vulnerability in savedata()
Next Review: After security fixes applied