| |
| <?php
|
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (!defined('ABSPATH')) exit;
|
|
|
|
|
| function gct_create_table() {
|
| global $wpdb;
|
| $table_name = $wpdb->prefix . 'gct_tracking';
|
|
|
| $charset_collate = $wpdb->get_charset_collate();
|
|
|
| $sql = "CREATE TABLE $table_name (
|
| id mediumint(9) NOT NULL AUTO_INCREMENT,
|
| container_no varchar(20) NOT NULL,
|
| location varchar(100) NOT NULL,
|
| status varchar(200) NOT NULL,
|
| event_date date NOT NULL,
|
| PRIMARY KEY (id)
|
| ) $charset_collate;";
|
|
|
| require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
| dbDelta($sql);
|
| }
|
| register_activation_hook(__FILE__, 'gct_create_table');
|
|
|
|
|
|
|
| function gct_admin_menu() {
|
| add_menu_page(
|
| 'Container Tracking',
|
| 'Container Tracking',
|
| 'manage_options',
|
| 'gct_tracking',
|
| 'gct_admin_page',
|
| 'dashicons-location',
|
| 25
|
| );
|
| }
|
| add_action('admin_menu', 'gct_admin_menu');
|
|
|
|
|
|
|
| function gct_admin_page() {
|
| global $wpdb;
|
| $table_name = $wpdb->prefix . 'gct_tracking';
|
|
|
| if (isset($_POST['submit'])) {
|
| $wpdb->insert($table_name, array(
|
| 'container_no' => sanitize_text_field($_POST['container_no']),
|
| 'location' => sanitize_text_field($_POST['location']),
|
| 'status' => sanitize_text_field($_POST['status']),
|
| 'event_date' => sanitize_text_field($_POST['event_date']),
|
| ));
|
| echo "<div class='updated'><p>Tracking event added.</p></div>";
|
| }
|
| ?>
|
|
|
| <div class="wrap">
|
| <h1>Add Container Event</h1>
|
| <form method="post">
|
| <input type="text" name="container_no" placeholder="Container No" required><br><br>
|
| <input type="text" name="location" placeholder="Location" required><br><br>
|
| <input type="text" name="status" placeholder="Status" required><br><br>
|
| <input type="date" name="event_date" required><br><br>
|
| <input type="submit" name="submit" class="button button-primary" value="Add Event">
|
| </form>
|
| </div>
|
| <?php
|
| }
|
|
|
|
|
| // Frontend Shortcode
|
| function gct_tracking_shortcode() {
|
| global $wpdb;
|
| $table_name = $wpdb->prefix . 'gct_tracking';
|
|
|
| ob_start();
|
| ?>
|
|
|
| <form method="post" style="max-width:500px;margin:auto;">
|
| <input type="text" name="track_container" placeholder="Enter Container Number" required style="width:70%;padding:10px;">
|
| <button type="submit" style="padding:10px;background:#0056b3;color:white;border:none;">Track</button>
|
| </form>
|
|
|
| <?php
|
|
|
| if (isset($_POST['track_container'])) {
|
| $container = sanitize_text_field($_POST['track_container']);
|
|
|
| $results = $wpdb->get_results(
|
| $wpdb->prepare("SELECT * FROM $table_name WHERE container_no = %s ORDER BY event_date DESC", $container)
|
| );
|
|
|
| if ($results) {
|
| echo "<div style='margin-top:20px;'>";
|
| foreach ($results as $row) {
|
| echo "<div style='border-left:4px solid #0056b3;padding-left:15px;margin-bottom:15px;'>";
|
| echo "<strong>" . esc_html($row->location) . "</strong><br>";
|
| echo esc_html($row->event_date) . "<br>";
|
| echo esc_html($row->status);
|
| echo "</div>";
|
| }
|
| echo "</div>";
|
| } else {
|
| echo "<p style='color:red;'>No tracking found.</p>";
|
| }
|
| }
|
|
|
| return ob_get_clean();
|
| }
|
| add_shortcode('container_tracking', 'gct_tracking_shortcode');
|
| |
| |
Comments