<?php

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

/**
 * Send confirmation email for registration
 *
 * @author Igor Ratsun
 */
class send_confirm_email implements SplObserver
{

    public function update(SplSubject $subject)
    {
        $event_method = $subject->get_event_name();

        if ($event_method == 'add_new') {
            $id_user = $subject->get_event_data();
            $ci = & get_instance();

            $ci->load->model('user/user_model_site');

            $ci->load->library('ciext/configurator');
            $config = new configurator('system');
            $ci->load->library('email', ['mailtype' => 'html']);
            $ci->email->from($config->item('email_from'), $config->item('email_from'));
            $ci->email->to($ci->input->post('email'));
            $ci->email->subject($ci->lang->line('Confirm account'));

            $link = base_url('ajax/user/action/registration_confirm/' . $ci->user_model_site->makeHash($id_user));
            $message = sprintf($ci->lang->line('To confirm your registration click on the %s, after which your account will be activated'), '<a href="' . $link . '">' . $link . '</a>');
            $ci->email->message($message);

            $ci->email->send();
        }
    }

}
