Your IP : 216.73.216.86


Current Path : /home/emeraadmin/www/Service/
Upload File :
Current File : /home/emeraadmin/www/Service/ServiceService.php

<?php

require_once __DIR__ . '/../Classes/Database.php';
require_once __DIR__ . '/../Classes/Service.php';

class ServiceService
{
    private $db;

    public function __construct()
    {
        $this->db = new Database();
    }

    public function addService($service)
    {
        $this->db->query('INSERT INTO services (name, region, address_line1, address_line2, suburb, city, state, postal_code, country, latitude, longitude, created_at, updated_at) VALUES (:name, :region, :address_line1, :address_line2, :suburb, :city, :state, :postal_code, :country, :latitude, :longitude, :created_at, :updated_at)');
        $this->db->bind(':name', $service->name);
        $this->db->bind(':region', $service->region);
        $this->db->bind(':address_line1', $service->address_line1);
        $this->db->bind(':address_line2', $service->address_line2);
        $this->db->bind(':suburb', $service->suburb);
        $this->db->bind(':city', $service->city);
        $this->db->bind(':state', $service->state);
        $this->db->bind(':postal_code', $service->postal_code);
        $this->db->bind(':country', $service->country);
        $this->db->bind(':latitude', $service->latitude);
        $this->db->bind(':longitude', $service->longitude);
        $this->db->bind(':created_at', $service->created_at);
        $this->db->bind(':updated_at', $service->updated_at);

        if ($this->db->execute()) {
            return true;
        } else {
            return false;
        }
    }

    public function addServicereturnlastid($service)
    {
        $this->db->query('INSERT INTO services (name, region, address_line1, address_line2, suburb, city, state, postal_code, country, latitude, longitude, created_at, updated_at) VALUES (:name, :region, :address_line1, :address_line2, :suburb, :city, :state, :postal_code, :country, :latitude, :longitude, :created_at, :updated_at)');

        $this->db->bind(':name', $service->name);
        $this->db->bind(':region', $service->region);
        $this->db->bind(':address_line1', $service->address_line1);
        $this->db->bind(':address_line2', $service->address_line2);
        $this->db->bind(':suburb', $service->suburb);
        $this->db->bind(':city', $service->city);
        $this->db->bind(':state', $service->state);
        $this->db->bind(':postal_code', $service->postal_code);
        $this->db->bind(':country', $service->country);
        $this->db->bind(':latitude', $service->latitude);
        $this->db->bind(':longitude', $service->longitude);
        $this->db->bind(':created_at', $service->created_at);
        $this->db->bind(':updated_at', $service->updated_at);

        if ($this->db->execute()) {
            return $this->db->lastInsertId(); // Retrieve the last inserted ID
        } else {
            return false;
        }
    }


    public function getAllServices()
    {
        $this->db->query('SELECT * FROM services');
        return $this->db->resultSet();
    }

    public function getAllServicesForDropdown()
    {
        $this->db->query('SELECT id, name,region FROM services');
        return $this->db->resultSet();
    }

    public function getServiceById(int $serviceId)
    {
        $this->db->query('SELECT * FROM services WHERE id = :id');
        $this->db->bind(':id', $serviceId);
        return $this->db->single();
    }

    public function updateService(array $serviceData)
    {
        $this->db->query('UPDATE services SET name = :name, region = :region, address_line1 = :address_line1, address_line2 = :address_line2, suburb = :suburb, city = :city, state = :state, postal_code = :postal_code, country = :country, phone = :phone, on_site_location = :on_site_location, latitude = :latitude, longitude = :longitude, updated_at = CURRENT_TIMESTAMP WHERE id = :id');

        $this->db->bind(':id', $serviceData['id']);
        $this->db->bind(':name', $serviceData['name']);
        $this->db->bind(':region', $serviceData['region']);
        $this->db->bind(':address_line1', $serviceData['address_line1']);
        $this->db->bind(':address_line2', $serviceData['address_line2']);
        $this->db->bind(':suburb', $serviceData['suburb']);
        $this->db->bind(':city', $serviceData['city']);
        $this->db->bind(':state', $serviceData['state']);
        $this->db->bind(':postal_code', $serviceData['postal_code']);
        $this->db->bind(':country', $serviceData['country']);
        $this->db->bind(':phone', $serviceData['phone']);
        $this->db->bind(':on_site_location', $serviceData['on_site_location']);
        $this->db->bind(':latitude', $serviceData['latitude']);
        $this->db->bind(':longitude', $serviceData['longitude']);

        if ($this->db->execute()) {
            return true; // Update successful
        } else {
            return false; // Update failed
        }
    }



    public function getServiceByNameAndRegion(string $serviceName, string $region)
    {
        $this->db->query('SELECT * FROM services WHERE name = :name AND region = :region');
        $this->db->bind(':name', $serviceName);
        $this->db->bind(':region', $region);
        return $this->db->single();
    }
}