project_class.php

45

Arun Kr.
01-Oct-25
<?php

class Project_Class extends MysqliDb
{
    // ************************* Others Modules ************************* //

    // Return the date 3 days from now
    public function upcomming_3_date()
    {
        return date('Y-m-d', strtotime('+3 days'));
    }

    // Get the current full URL
    public function getCurrentUrl()
    {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
        $host     = $_SERVER['HTTP_HOST'];
        $uri      = $_SERVER['REQUEST_URI'];

        return $protocol . $host . $uri;
    }

    // Admin session check
    public function admin_login_check()
    {
        if (empty($_SESSION['ADMIN_ID'])) {
            header('Location:' . BASE_URL);
            exit();
        }
    }

    // User login session check
    public function user_logincheck()
    {
        if (isset($_SESSION['USER_ID'])) {
            $this->where('user_id', $_SESSION['USER_ID']);
            $user_d = $this->getOne('registeruser');
            return $user_d;
        } else {
            header('Location: ' . BASE_URL);
            exit();
        }
    }

    // Generate or return session token
    public function token()
    {
        if (isset($_SESSION["token"])) {
            $token = $_SESSION["token"];
        } else {
            $token = md5(uniqid(rand(), true));
            $_SESSION["token"] = $token;
        }
        return $_SESSION["token"];
    }

    // Format a date string to 'M d, Y'
    public function date_format($date)
    {
        return date("M d, Y", strtotime($date));
    }

    // Convert string to SEO-friendly URL slug
    public function seoUrl($string)
    {
        $string = trim($string);                             // Trim string
        $string = strtolower($string);                       // Convert to lowercase
        $string = preg_replace("/[^a-z0-9_\s-]/", "", $string); // Remove unwanted characters
        $string = preg_replace("/[\s-]+/", " ", $string);    // Remove multiple spaces/dashes
        $string = trim($string);                             // Trim again
        $string = preg_replace("/[\s_]/", "-", $string);     // Replace space/underscore with dash

        return $string;
    }

    // ✅ Build HTML email body from form data
    public function buildEmailBody($data, $title = "Form Submission")
    {
        $body  = "<h3>{$title}</h3>";
        $body .= "<table border='1' cellspacing='0' cellpadding='6' style='border-collapse:collapse;width:100%;font-family:Arial, sans-serif;font-size:14px;'>";

        foreach ($data as $name => $value) {
            // Skip non-data fields
            if (in_array($name, ['submit', 'add', 'bus_book'])) continue;

            // Convert name to label
            $label = ucwords(preg_replace('/([a-z])([A-Z])/', '$1 $2', $name));
            $label = str_replace("_", " ", $label);

            // Clean value
            $val = nl2br(htmlspecialchars(trim($value)));

            $body .= "
                <tr>
                    <td style='background:#f8f9fa;width:30%;'><b>{$label}</b></td>
                    <td>{$val}</td>
                </tr>";
        }

        $body .= "</table>";
        return $body;
    }

    // ✅ Send email
    public function sendMail($to, $subject, $body, $from = "noreply@example.com")
    {
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=UTF-8\r\n";
        $headers .= "From: {$from}\r\n";

        return mail($to, $subject, $body, $headers);
    }
}

?>
@Since 2024 Arun'Log Powered by Arun Git