<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Settings_model extends CI_Model
{
    private $table = 'kns1_settings';

    /**
     * Get a settings row by ID
     */
    public function get_by_id($id)
{
    return $this->db->where('id', $id)->get('kns1_settings')->row();
}

    /**
     * Insert new settings row
     */
    public function insert($data)
    {
        // timestamps
        if (!isset($data['created_at'])) {
            $data['created_at'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['updated_at'])) {
            $data['updated_at'] = date('Y-m-d H:i:s');
        }

        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * Update existing settings row
     */
    public function update($id, $data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s');

        return $this->db->where('id', $id)->update($this->table, $data);
    }

    /**
     * Save (insert or update) settings row in one method
     * Pass $data['id'] to update, otherwise it inserts
     */
    public function save($data)
    {
        if (!empty($data['id'])) {
            $id = $data['id'];
            unset($data['id']);
            return $this->update($id, $data);
        } else {
            return $this->insert($data);
        }
    }
public function get_all()
{
    return $this->db->get($this->table)->result();
}
    /**
     * Get first settings row (useful if you have only one row)
     */

public function get_first()
{
    return $this->db->get($this->table)->row();
}

}
