<?php

defined('BASEPATH') or exit('No direct script access allowed');

class PropertyOwnershipTypes_model extends App_Model
{

    public function __construct()
    {
        parent::__construct();
    }

    public function create($data)
    {
        $data = hooks()->apply_filters('propertybase_before_ownership_type_added', $data);

        $this->db->insert(db_prefix() . 'propertybase_ownership_types', $data);
        $insert_id = $this->db->insert_id();

        if ($insert_id) {
            hooks()->do_action('propertybase_after_ownership_type_added', $insert_id);
            return $insert_id;
        }

        return false;
    }

    public function get($id)
    {
        $this->db->where('id', $id);
        $ownership_type = $this->db->get(db_prefix() . 'propertybase_ownership_types')->row();

        return hooks()->apply_filters('propertybase_get_ownership_type', $ownership_type);
    }

    public function get_all()
    {
        $ownership_types = $this->db->get(db_prefix() . 'propertybase_ownership_types')->result_array();
        return hooks()->apply_filters('propertybase_get_ownership_types', $ownership_types);
    }

    public function update($id, $data)
    {
        $data = hooks()->apply_filters('propertybase_before_ownership_type_updated', $data, $id);

        $this->db->where('id', $id);
        $this->db->update(db_prefix() . 'propertybase_ownership_types', $data);

        if ($this->db->affected_rows() > 0) {
            hooks()->do_action('propertybase_after_ownership_type_updated', $id);
            return true;
        }

        return false;
    }

    public function delete($id)
    {
        hooks()->do_action('propertybase_before_ownership_type_deleted', $id);

        $this->db->where('id', $id);
        $this->db->delete(db_prefix() . 'propertybase_ownership_types');

        if ($this->db->affected_rows() > 0) {
            hooks()->do_action('propertybase_after_ownership_type_deleted', $id);
            return true;
        }

        return false;
    }

}