// Include authentication and database connection before any output
require_once DIR . '/../../includes/auth.php';
require_once DIR . '/../../includes/db_connect.php';
// Helper Functions
/**
Sanitize input to prevent XSS and other injections.
@param string $input The input string to sanitize.
@return string The sanitized string.
*/
function sanitize_input(input) {
return htmlspecialchars(trim(input), ENT_QUOTES, 'UTF-8');
}
/**
Build a query string while preserving existing parameters.
@param array $new_params Associative array of new parameters to add or update.
@return string The resulting query string.
*/
function build_query(new_params = []) {
current_params = GET;new_params as value) {
if (current_params[value;
} else {
unset(key]); // Remove if value is null
}
}
// Remove parameters that shouldn't persist
unset(query = http_build_query(query ? '?' . $query : '';
}
Assistant
f
or
e
a
c
h
(
key=>
value !== null) { // Only set if value is not null
key]=
currentparams[
currentparams[′page′]);
currentparams);return
/**
Generate a WhatsApp link from a contact number.
@param string $contact The contact number.
@return string The WhatsApp URL.
*/
function generate_whatsapp_link(contact) {
// Remove any non-digit characters except the plus sign
digits = preg_replace('/[^\d+]/', '', $contact);
// Ensure the number starts with '+'
if (strpos(digits, '+') !== 0) {
digits = '+' . $digits;
}
// Search Filter
if (!empty(search)) {
where_conditions[] = "(c.name LIKE ? OR o.id LIKE ? OR c.contact LIKE ?)";
searchterm="search . "%";
parameters[]=search_term;
parameters[]=search_term;
parameters[]=search_term;
$param_types .= "sss";
}
// Date Range Filter on order_date
if (!empty(from_date)) {
where_conditions[] = "o.order_date >= ?";
parameters[]=from_date . " 00:00:00";
$param_types .= "s";
}
// Combine all where conditions
whereclause="";if(count(where_conditions) > 0) {
whereclause="WHERE".implode("AND",where_conditions);
}
// Sorting
// To prevent SQL injection in ORDER BY, use a mapping
sortcolumnmap=[′id′=sort_column = isset(sortcolumnmap[sort_by]) ? sortcolumnmap[sort_by] : 'o.order_date';
orderclause="ORDERBYsort_column $sort_order";
<!-- Bootstrap Icons JS (optional if interactivity is needed) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.js"></script>
<!-- UnoCSS Runtime (ensure it's loaded) -->
<script type="module">
import 'https://unpkg.com/@unocss/runtime?module';
</script>
<!-- JavaScript for Interactivity -->
<script>
// Initialize Flatpickr for date inputs
flatpickr("#fromDate", {
dateFormat: "Y-m-d",
maxDate: "today",
allowInput: true
});
flatpickr("#toDate", {
dateFormat: "Y-m-d",
maxDate: "today",
allowInput: true
});
// Function to handle sorting
function sortTable(column) {
const urlParams = new URLSearchParams(window.location.search);
let currentSortBy = urlParams.get('sort_by') || 'order_date';
let currentSortOrder = urlParams.get('sort_order') || 'DESC';
if (currentSortBy === column) {
// Toggle sort order
currentSortOrder = currentSortOrder === 'ASC' ? 'DESC' : 'ASC';
} else {
currentSortBy = column;
currentSortOrder = 'ASC';
}
urlParams.set('sort_by', currentSortBy);
urlParams.set('sort_order', currentSortOrder);
// Reset to first page if pagination is implemented
urlParams.set('page', 1);
window.location.search = urlParams.toString();
}
// Function to confirm deletion using custom modal
function confirmDelete(orderId) {
const deleteModal = document.getElementById('deleteModal');
const deleteOrderId = document.getElementById('delete_order_id');
// Set the order ID in the modal
deleteOrderId.value = orderId;
// Show the modal
deleteModal.classList.remove('hidden');
}
// Function to close delete modal
function closeDeleteModal() {
const deleteModal = document.getElementById('deleteModal');
deleteModal.classList.add('hidden');
}
// Function to open status change modal
function openStatusModal(orderId, currentStatus) {
const statusModal = document.getElementById('statusModal');
const modalOrderId = document.getElementById('modal_order_id');
const newStatusSelect = document.getElementById('newStatus');
// Set the order ID in the modal
modalOrderId.value = orderId;
// Set the current status as the default selected option
newStatusSelect.value = currentStatus;
// Show the modal
statusModal.classList.remove('hidden');
}
// Function to close status change modal
function closeStatusModal() {
const statusModal = document.getElementById('statusModal');
statusModal.classList.add('hidden');
}
// Function to mark an order as paid
function markPaid(orderId) {
if (confirm("Are you sure you want to mark this order as paid?")) {
// Create a form to submit the mark_paid action via POST
const form = document.createElement('form');
form.method = 'POST';
form.action = 'view_orders.php';
const actionInput = document.createElement('input');
actionInput.type = 'hidden';
actionInput.name = 'action';
actionInput.value = 'mark_paid';
form.appendChild(actionInput);
const orderIdInput = document.createElement('input');
orderIdInput.type = 'hidden';
orderIdInput.name = 'order_id';
orderIdInput.value = orderId;
form.appendChild(orderIdInput);
document.body.appendChild(form);
form.submit();
}
}
// Function to edit order
function editOrder(orderId) {
window.location.href = `edit_order.php?id=${orderId}`;
}
// Function to apply filters (on status change, priority change)
function applyFilters() {
const urlParams = new URLSearchParams(window.location.search);
const statusFilter = document.getElementById('statusFilter').value;
const priorityFilter = document.getElementById('priorityFilter').value;
const ordersPerPage = document.getElementById('entriesPerPage').value;
const fromDate = document.getElementById('fromDate').value;
const toDate = document.getElementById('toDate').value;
urlParams.set('status_filter', statusFilter);
urlParams.set('priority_filter', priorityFilter);
urlParams.set('orders_per_page', ordersPerPage);
urlParams.set('from_date', fromDate);
urlParams.set('to_date', toDate);
urlParams.set('page', 1); // Reset to first page
window.location.search = urlParams.toString();
}
// Function to apply date filters separately
function applyDateFilters() {
applyFilters();
}
// Close modals when clicking outside of the modal content
window.onclick = function(event) {
const deleteModal = document.getElementById('deleteModal');
const statusModal = document.getElementById('statusModal');
if (event.target == deleteModal) {
deleteModal.classList.add('hidden');
}
if (event.target == statusModal) {
statusModal.classList.add('hidden');
}
}
</script>
Certainly! I've updated your view_orders.php file by removing the Export and Print functionalities, enhancing code readability, and refining the design for a more modern and user-friendly interface. Below is the revised code with detailed comments and improved formatting:
Removed the entire elseif block handling $_GET['action'] === 'export'.
Deleted the Export button from the UI.
Removed the associated JavaScript function exportOrders().
Print Functionality:
Deleted the Print button from the UI.
Removed the associated JavaScript function printOrders().
Enhanced Code Readability:
Consistent Indentation: Ensured consistent indentation throughout the PHP and HTML code for better readability.
Comments: Added and refined comments to explain various sections and functionalities within the code.
Function Organization: Grouped related functionalities and helper functions together to streamline the code structure.
Improved Design:
Enhanced Card Hover Effects: Added a subtle lift and shadow effect when hovering over order cards to make the interface more interactive and modern.
Typography and Spacing: Refined typography classes and spacing for better visual hierarchy and readability.
Removed Redundant Elements: Eliminated unnecessary elements to declutter the UI and focus on essential functionalities.
Modal Functionality:
Deletion Confirmation: Ensured that the delete confirmation modal remains functional and user-friendly.
Status Change Modal: Maintained the status change modal for updating order statuses seamlessly.
JavaScript Refactoring:
Removed Unused Functions: Deleted JavaScript functions related to the removed Export and Print features.
Optimized Existing Functions: Streamlined existing JavaScript functions for handling modals and actions to ensure smooth interactivity.
Additional Recommendations:
Security Enhancements:
CSRF Protection: Implement Cross-Site Request Forgery (CSRF) tokens in your forms to enhance security.
Input Validation: Ensure that all user inputs are validated both on the client-side and server-side.
Performance Optimizations:
Pagination Efficiency: For large datasets, consider implementing lazy loading or infinite scrolling to enhance performance.
Caching: Utilize caching mechanisms for frequently accessed data to reduce database load.
User Experience (UX) Improvements:
Notifications: Implement toast notifications for success and error messages to provide a better user experience.
Responsive Design: Ensure that all UI components are fully responsive across various devices and screen sizes.
Accessibility:
ARIA Labels: Incorporate ARIA labels and roles to make the application more accessible to users with disabilities.
Keyboard Navigation: Ensure that all interactive elements are navigable via keyboard for improved accessibility.
Code Maintenance:
Modularization: Consider breaking down the code into smaller, reusable components or using a templating engine to enhance maintainability.
Version Control: Use version control systems like Git to track changes and collaborate efficiently.
Feel free to incorporate these suggestions to further enhance your application. If you have any more modifications or need additional assistance, don't hesitate to ask!