<?php
defined('_VALID_CALL') or die('Direct Access is not allowed.');

require_once(_SRV_WEBROOT.'xtWizard/lib/class.xtMailer_wizard.php');
class mail_installer_wizard
{
    public function _installMailTemplates($mail_dir, $lng, ADOConnection $idb, $db_prefix)
    {
        $directories = glob($mail_dir . '/*', GLOB_ONLYDIR);

        foreach ($directories as $dir) {
            $mail_type = basename($dir);

            $tpl_id = $idb->GetOne("SELECT tpl_id FROM " . $db_prefix . "mail_templates WHERE tpl_type = ?", array($mail_type));
            if (!$tpl_id)
            {
                $data = array(
                    'tpl_type' => $mail_type,
                );
                $idb->AutoExecute(TABLE_MAIL_TEMPLATES ,$data);
                $tpl_id = $idb->Insert_ID($db_prefix . 'mail_templates');
            }

            $idb->Execute("DELETE FROM " . $db_prefix . 'mail_templates_content' . " WHERE language_code = ? and tpl_id = ?", [$lng, $tpl_id]);

            $html_content = $this->_getFileContent($mail_dir . '/' . $mail_type . '/' . $mail_type . '_html.tpl.html');
            $txt_content = $this->_getFileContent($mail_dir . '/' . $mail_type . '/' . $mail_type . '_text.tpl.txt');
            $subject = $this->_getFileContent($mail_dir . '/' . $mail_type . '/' . $mail_type . '_subject.tpl.txt');

            $insert_array = array();
            $insert_array['tpl_id'] = $tpl_id;
            $insert_array['language_code'] = $lng;
            $insert_array['mail_body_html'] = $html_content;
            $insert_array['mail_body_txt'] = $txt_content;
            $insert_array['mail_subject'] = $subject;
            try {
                $idb->AutoExecute($db_prefix . 'mail_templates_content', $insert_array);
            } catch (exception $e) {
                return $e->getCode();
            }
        }

        return -1;
    }

    public function copyMailSettings($source_tpl, $target_tpl, ADOConnection $idb, $db_prefix)
    {
        if(!defined('_SYSTEM_MAIL_TYPE'))
            define('_SYSTEM_MAIL_TYPE', 'foobar');

        require_once(_SRV_WEBROOT.'xtWizard/lib/class.xtMailer_wizard.php');
        $m = new xtMailer_wizard($source_tpl, -1, -1, -1, -1);

        $m->prepareMail();

        $data = array(
            'tpl_type' => $target_tpl,

            'tpl_special' => $m->tplData["tpl_special"],
            'email_from'        => $m->tplData["email_from"],
            'email_from_name'   => $m->tplData["email_from_name"],
            'email_reply'       => $m->tplData["email_reply"],
            'email_reply_name'  => $m->tplData["email_reply_name"],
            'email_forward'     => $m->tplData["email_forward"]
        );
        $idb->AutoExecute(TABLE_MAIL_TEMPLATES ,$data, 'UPDATE', "tpl_type = '".$target_tpl."'");

    }

    protected function _getFileContent($filename)
    {
        if (!file_exists($filename)) {
            return 'not found: ' . $filename;
        }
        $handle = fopen($filename, 'rb');
        $content = fread($handle, filesize($filename));
        fclose($handle);
        return $content;
    }

}

