| Server IP : 13.126.101.145 / Your IP : 216.73.217.37 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/rental-contract/ |
Upload File : |
<?php
/**
* Plugin Name: Rental Contract
* Description: A plugin to manage rental contracts.
* Version: 1.0
* Author: itrosys
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Add menu items for listing and editing rental contracts
function rental_contract_admin_menu() {
add_menu_page(
'Rental Contracts', // Page title
'Rental Contracts', // Menu title
'manage_options', // Capability
'rental_contract_list', // Menu slug for listing
'rental_contract_list_callback', // Callback function for listing
'dashicons-list-view', // Icon
6 // Position
);
// Hidden menu page for editing (not visible in the menu)
add_submenu_page(
null, // Parent slug (null means it's not in the menu)
'Edit Rental Contract', // Page title for the edit page
'Edit Rental Contract', // Menu title for the edit page
'manage_options', // Capability
'rental_contract_edit', // Menu slug for editing
'rental_contract_edit_callback' // Callback function for editing
);
}
add_action('admin_menu', 'rental_contract_admin_menu');
// Function to list rental contracts
function rental_contract_list_callback() {
global $wpdb;
$table_name = $wpdb->prefix . 'rental_contract';
$results = $wpdb->get_results("SELECT * FROM $table_name");
// Display table
echo '<div class="wrap"><h1>Rental Contracts</h1>';
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th>ID</th><th>User Name</th><th>Email</th><th>Product Name</th><th>Start Date</th><th>End Date</th><th>Shift</th><th>Final Price</th><th>Actions</th>';
echo '</tr></thead>';
echo '<tbody>';
// Loop through the results and display each row
foreach ($results as $row) {
$edit_url = admin_url('admin.php?page=rental_contract_edit&id=' . $row->id);
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->user_name . '</td>';
echo '<td>' . $row->user_email . '</td>';
echo '<td>' . $row->product_name . '</td>';
echo '<td>' . $row->start_date . '</td>';
echo '<td>' . $row->end_date . '</td>';
echo '<td>' . $row->shift . '</td>';
echo '<td>' . $row->final_price . '</td>';
echo '<td><a href="' . $edit_url . '" class="button-primary">Edit</a></td>';
echo '</tr>';
}
echo '</tbody></table></div>';
}
// Function to edit a specific rental contract
function rental_contract_edit_callback() {
global $wpdb;
$table_name = $wpdb->prefix . 'rental_contract';
// Get the contract ID from the URL
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
// Fetch the data for this contract
if ($id) {
$contract = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $id));
}
// Fetch similar product names based on the current product name
$similar_products = $wpdb->get_results($wpdb->prepare("SELECT product_id, product_name FROM $table_name WHERE product_name LIKE %s", '%' . $wpdb->esc_like($contract->product_name) . '%'));
// If form submitted, update the contract data
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$product_id = intval($_POST['product_id']);
$product_name = sanitize_text_field($_POST['product_name']); // Get the product name from hidden input field
$wpdb->update(
$table_name,
[
'product_id' => $product_id,
'product_name' => $product_name
],
['id' => $id]
);
// Redirect to the listing page after saving
wp_redirect(admin_url('admin.php?page=rental_contract_list'));
exit;
}
// Display the form for editing the contract
echo '<div class="wrap"><h1>Edit Rental Contract</h1>';
echo '<form method="POST">';
echo '<table class="form-table">';
echo '<tr><th>User Name</th><td><input type="text" name="user_name" value="' . esc_attr($contract->user_name) . '" readonly></td></tr>';
echo '<tr><th>User Email</th><td><input type="email" name="user_email" value="' . esc_attr($contract->user_email) . '" readonly></td></tr>';
echo '<tr><th>Start Date</th><td><input type="date" name="start_date" value="' . esc_attr($contract->start_date) . '" readonly></td></tr>';
echo '<tr><th>End Date</th><td><input type="date" name="end_date" value="' . esc_attr($contract->end_date) . '" readonly></td></tr>';
echo '<tr><th>Shift</th><td><input type="text" name="shift" value="' . esc_attr($contract->shift) . '" readonly></td></tr>';
echo '<tr><th>Final Price</th><td><input type="number" step="0.01" name="final_price" value="' . esc_attr($contract->final_price) . '" readonly></td></tr>';
// Dropdown to Assign Product
echo '<tr><th>Assign Product</th>';
echo '<td><select id="product_dropdown" name="product_name">';
foreach ($similar_products as $product) {
$selected = ($product->product_name == $contract->product_name) ? 'selected' : '';
echo '<option value="' . esc_attr($product->product_name) . '" data-product-id="' . esc_attr($product->product_id) . '" ' . $selected . '>' . esc_html($product->product_name) . '</option>';
}
echo '</select>';
echo '<input type="hidden" id="product_id" name="product_id" value="' . esc_attr($contract->product_id) . '">';
echo '</td></tr>';
echo '</table>';
echo '<p class="submit"><input type="submit" class="button-primary" value="Save Changes"></p>';
echo '</form>';
echo '</div>';
// Add JavaScript to update the hidden product_id field when a product is selected
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#product_dropdown').change(function() {
var selectedProductId = $(this).find('option:selected').data('product-id');
$('#product_id').val(selectedProductId);
});
});
</script>
<?php
}