<?php

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

class emailmng_installer
{

    private $subject;
    private $subject_tooltip;
    private $message;
    private $message_tooltip;

    /**
     * Set subject for template
     * @param string $subject
     * @param string $tooltip
     * @return $this
     */
    public function set_subject($subject = '', $tooltip = '')
    {
        $this->subject = $subject;
        $this->subject_tooltip = $tooltip;

        return $this;
    }

    /**
     * Set message for template
     * @param string $message
     * @param string $tooltip
     * @return $this
     */
    public function set_message($message = '', $tooltip = '')
    {
        $this->message = $message;
        $this->message_tooltip = $tooltip;

        return $this;
    }

    /**
     * Add template
     * @param string $code
     * @return boolean
     */
    public function add_template($code)
    {
        $CI = &get_instance();
        $CI->load->model('emailmng/template_tmodel');
        if (empty($CI->template_tmodel->fetch_by_code($code))) {
            $data = [
                'code'            => $code,
                'subject'         => $this->subject,
                'message'         => $this->message,
                'subject_tooltip' => $this->subject_tooltip,
                'message_tooltip' => $this->message_tooltip,
            ];
            return $CI->template_tmodel->add($data);
        } else {

            return false;
        }
    }

    /**
     * Delete template
     * @param string $code
     * @return bool
     */
    public function delete_template_by_code($code)
    {
        $CI = &get_instance();
        $CI->load->model('emailmng/template_tmodel');

        return $CI->template_tmodel->delete_by_code($code);
    }

    /**
     * Install emailmng if not installed
     * @return boolean
     */
    public function install_emailmng()
    {
        $CI = &get_instance();
        $res = $CI->db
                ->select('id_acl_resource')
                ->where('acl_resource_name', 'emailmng')
                ->get('acl_resource')
                ->row_array();
        if (empty($res)) {
            // Load InstallController class
            $CI->load->plugin('ciext/install_controller');
            $CI->load->module('ciext/mconfig');
            
            return $CI->mconfig->installForce('emailmng', 'install');
        }

        return true;
    }

}
