<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class template_tmodel extends RT_Model
{

    protected $data = [];
    protected $default_mailtype_id = 1;

    /**
     * Fetch template
     * @param int $id
     * @return array
     */
    public function fetch($id = 0)
    {
        $res = $this->db->select('t.*')
                        ->where('t.id_emailmng', (int) $id)
                        ->get('emailmng t')->row_array();

        return $res;
    }

    /**
     * Fetch template by code of template
     * @param string $code
     * @return array
     */
    public function fetch_by_code($code = '')
    {
        if (empty($this->data[$code])) {
            $this->data[$code] = $this->db->select('copy_to'
                                    . ', hidden_copy_to'
                                    . ', subject'
                                    . ', message'
                                    . ', mailtype'
                            )
                            ->where('code', $code)
                            ->get('emailmng')->row_array();
        }

        return $this->data[$code];
    }

    /**
     * Add template
     * @param array $data
     * @return boolean
     */
    public function add($data = [])
    {
        if (empty($data)) {

            return false;
        }

        $data['mailtype'] = $this->default_mailtype_id;

        return $this->db->insert('emailmng', $data);
    }

    /**
     * Delete template by code
     * @param string $code
     * @return bool
     */
    public function delete_by_code($code)
    {
        return $this->db->delete('emailmng', ['code' => $code]);
    }

}
