<?php
class EmailTemplateHandler
{
    public $db;

    public function __construct()
    {
        global $db;
        
        $this->db = $db;
    }
    /**
     * 
     */
    public function load(string $name)
    {
        $sql = "
            SELECT *
            FROM llx_c_email_templates
            WHERE label ILIKE '{$name}'
            LIMIT 1
        ";

        return pg_fetch_assoc($this->db->query($sql));
    }
    /**
     * 
     */
    public function prepareTemplate(stdClass $tpl, array $vars = [])
    {
        if (!empty($vars)) {
            $keys = array_keys($vars);
            $values = array_values($vars);

            $tpl->topic = str_replace($keys, $values, $tpl->topic);
            $tpl->content = str_replace($keys, $values, $tpl->content);
        }

        return $tpl;
    }
}